EOS區塊鏈平臺智能合約示例HelloWorld

咱們將介紹一個使用EOS智能合約構建hello World的例子。node

通常環境設置經過上一篇文章已經說明,這方面的問題你們能夠看本博客上一篇文章,本文引用了官方EOS在Git上的示例react

運行nodeos

要經過Hello World日誌進行驗證,請使用contracts-console選項運行它。git

nodeos --contracts-console

<!--more-->github

生成智能合約

讓咱們嘗試經過一個不一樣的新的終端上(除了nodeos的終端以外)訪問EOS目錄,建立一個名爲Hello的文件夾。命令以下:web

$ cd eos
$ cd mkdir hello
$ cd hello

而後,建立hello.cpp文件並複製下面的示例。Hello合約繼承了EOS.IO提供的合約,並具備打印出用戶名Hello的功能。app

編譯代碼

  • 對於第一個命令,讓咱們用WebAssembly編譯代碼。編譯時,可能會提示警告(但咱們能夠忽略它)。
  • 對於第二個命令,讓咱們建立ABI。
# Compile webaassembly
eosiocpp -o hello.wast hello.cpp

# Creating abi
eosiocpp -g hello.abi hello.cpp

賬戶建立和合約發佈

建立一個名爲Hello.Code的賬戶。咱們將進一步使用這個賬戶發佈Hello合約。函數

cleos set contract ${account} ${path} -p ${permmition}命令發佈合約。工具

$ cleos create account eosio hello.code EOS8QMGRoRPZ4uf3w8WACcrg3wKzLtXpCk5Gpia6pdFzSuftLigWT EOS8QMGRoRPZ4uf3w8WACcrg3wKzLtXpCk5Gpia6pdFzSuftLigWT

executed transaction: e6847fc85c7733dd70a9ff27c2cad98ea0b50fb6c80c2b0c7ea1bf64f9917916  200 bytes  225 us

#         eosio <= eosio::newaccount            {"creator":"eosio","name":"hello.code","owner":{"threshold":1,"keys":[{"key":"EOS8QMGRoRPZ4uf3w8WACc...

$ cleos set contract hello.code ../hello -p hello.code

Reading WAST/WASM from ../hello/hello.wasm...

Using already assembled WASM...

Publishing contract...

executed transaction: 7e1b070382188677e70cf4b87e8fbe02c072f10063983ffc1d8259b127d8fea7  1800 bytes  723 us

#         eosio <= eosio::setcode               {"account":"hello.code","vmtype":0,"vmversion":0,"code":"0061736d01000000013b0c60027f7e006000017e600...

#         eosio <= eosio::setabi                {"account":"hello.code","abi":"0e656f73696f3a3a6162692f312e30000102686900010475736572046e616d6501000...

調用函數

讓咱們調用hello類的hi函數。它能夠用下面的命令來完成:學習

cleos push action ${contract_name} ${function} ${[argument]} -p ${permission}區塊鏈

在下面的示例中,hello.code合約中的用戶賬戶調用hi函數。

$ cleos push action hello.code hi '["user"]' -p user

executed transaction: d7932d1ee61ab6b0fed1f9e20d4a2e2607b029763aeaf1daea4ed718d2885797  104 bytes  500 us

#    hello.code <= hello.code::hi               {"user":"user"}

結果

nodeos終端中執行交易的塊中添加了如下輸出。hello是名爲hi函數的用戶的名字。

2703777ms thread-0 apply_context.cpp:28 print_debug ]

[(hello.code,hi)->hello.code]: CONSOLE OUTPUT BEGIN =====================

Hello, user

[(hello.code,hi)->hello.code]: CONSOLE OUTPUT END =====================

受權運行請求

  • 替換爲包含受權請求調用hi函數。

結果

  • 當咱們試圖用一個未經受權的賬戶調用它時,會出現一個錯誤(以下)。
  • 使用受權賬戶,它正常輸出無錯誤。
# Authorized

$ cleos push action hello.code hi '["tester"]' -p user

Error 3090004: missing required authority

# Unauthorized

$ cleos push action hello.code hi '["tester"]' -p tester

executed transaction: 16a34c27c7d162dc3940358197306df619911fb930cbddd6d208125a770886f4  104 bytes  243 us

#    hello.code <= hello.code::hi               {"user":"tester"}

能夠在nodeos的塊中按以下方式打印日誌:

2525788ms thread-0   http_plugin.cpp:405           handle_exception     ] FC Exception encountered while processing chain.push_transaction

2525788ms thread-0   http_plugin.cpp:406           handle_exception     ] Exception Details: 3090004 missing_auth_exception: missing required authority

missing authority of tester {"account":"tester"}

thread-0  apply_context.cpp:132 require_authorization {"_pending_console_output.str()":""}

thread-0  apply_context.cpp:62 exec_one

另:《EOS智能合約與DApp開發入門》教程已經上線,愛學習等不及的能夠抓緊體驗一下:

EOS教程

本教程幫助你快速入門EOS區塊鏈去中心化應用的開發,內容涵蓋EOS工具鏈、帳戶與錢包、發行代幣、智能合約開發與部署、使用代碼與智能合約交互等核心知識點,最後綜合運用react和EOS的各知識點完成一個便籤DApp的開發。

原文:http://blog.hubwiz.com/2018/07/23/EOS-SmartContracts-hello/

相關文章
相關標籤/搜索