本篇內容主要講解“EOS區塊鏈平臺智能合約怎么實現HelloWorld”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“EOS區塊鏈平臺智能合約怎么實現HelloWorld”吧!
要通過Hello World日志進行驗證,請使用contracts-console
選項運行它。
nodeos --contracts-console
<!--more-->
讓我們嘗試通過一個不同的新的終端上(除了nodeos
的終端之外)訪問EOS目錄,創建一個名為Hello的文件夾。命令如下:
$ cd eos $ cd mkdir hello $ cd hello
然后,創建hello.cpp
文件并復制下面的示例。Hello合約繼承了EOS.IO提供的合約,并具有打印出用戶名Hello
的功能。
對于第一個命令,讓我們用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區塊鏈平臺智能合約怎么實現HelloWorld”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。