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/

相关文章
相关标签/搜索