溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

EOS區塊鏈平臺智能合約怎么實現HelloWorld

發布時間:2021-12-29 14:22:12 來源:億速云 閱讀:114 作者:iii 欄目:互聯網科技

本篇內容主要講解“EOS區塊鏈平臺智能合約怎么實現HelloWorld”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“EOS區塊鏈平臺智能合約怎么實現HelloWorld”吧!

運行nodeos

要通過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”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

eos
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女