EOS DAWN-V3.0.0 智能合约开发之Hello World

  • 时间:
  • 浏览:44
  • 来源:汇达财经

不管是C、C++、Java还是任何其他语言,一般刚开始学习的时候,我们都会从HelloWorld开始,这篇文章主要讲解EOS DAWN-V3.0.0 智能合约开发之Hello World。 在看这篇文章之前,请先阅读下面两篇入门文章。 第一篇 – 手把手教你理清EOS各种开发环境搭建来龙去脉 第二篇 – EOS Currency 合约案例  1. 编写合约代码

在桌面创建一个文件夹,比如:0418,用Atom打开0418文件夹。新建文件Hello.cpp文件,并将下面的源码拷贝到Hello.cpp文件中。

#include #include // 视频网站:http://kongyixueyuan.com // 个人博客:http://liyuechun.org // 公众号:区块链部落 // 进技术群,请加微信(kongyixueyuan) //用eosio命名空间 using namespace eosio; //所有的智能合约都继承自contract类 class Hello : public eosio::contract { public: using contract::contract; /// @abi action void hi( account_name user ) { print( "Hello, ", name{user} ); } }; EOSIO_ABI( Hello, (hi) )

2. 生成.wast文件

liyuechun:Project yuechunli$ eosiocpp -o Hello.wast Hello.cpp liyuechun:Project yuechunli$ ls Hello.cpp Hello.wast liyuechun:Project yuechunli$

3. 生成.abi文件

liyuechun:Project yuechunli$ eosiocpp -g Hello.abi Hello.cpp Generated Hello.abi ... liyuechun:Project yuechunli$ ls Hello.abi Hello.cpp Hello.wast liyuechun:Project yuechunli$

Hello.abi文件内容如下:

{ "____comment": "This file was generated by eosio-abigen. DO NOT EDIT - 2018-04-18T08:15:50", "types": [], "structs": [{ "name": "hi", "base": "", "fields": [{ "name": "user", "type": "account_name" } ] } ], "actions": [{ "name": "hi", "type": "hi", "ricardian_contract": "" } ], "tables": [], "clauses": [] }

4. 创建钱包账号 4.1 创建钱包

liyuechun:Hello yuechunli$ cleos wallet create Creating wallet: default Save password to use in the future to unlock this wallet. Without password imported keys will not be retrievable. "PW5J3rx7Bfg9zb8Kf2owTytccFyJqtDTrqnUX8iBRRUvbwM8RyzRL"

PW5J3rx7Bfg9zb8Kf2owTytccFyJqtDTrqnUX8iBRRUvbwM8RyzRL必须保存好,解锁钱包时需要使用到这个密码。

4.2 创建两组key

liyuechun:Hello yuechunli$ ./cleos create key -bash: ./cleos: No such file or directory liyuechun:Hello yuechunli$ cleos create key Private key: 5K7QdknUZsF9apdBhD8TDMZGJjw8zJ8esYwS173YyFRv2453Z9t Public key: EOS5RU8VsYBLnN5snGeUKmt1sDDzpvQbGyW7LPP6qEryaFctYieCK liyuechun:Hello yuechunli$ cleos create key Private key: 5J8kComGiQHZyNmH6VvkHgtFggeQemazLpihKR4QW75DNkWTVdA Public key: EOS5fqiC3VFAJ1riMrKf8vzD28nqd4EpXvZGpXt6YewEBnH8DYinG

4.3 向钱包导入私钥

liyuechun:Hello yuechunli$ cleos wallet import 5K7QdknUZsF9apdBhD8TDMZGJjw8zJ8esYwS173YyFRv2453Z9t imported private key for: EOS5RU8VsYBLnN5snGeUKmt1sDDzpvQbGyW7LPP6qEryaFctYieCK liyuechun:Hello yuechunli$ cleos wallet import 5J8kComGiQHZyNmH6VvkHgtFggeQemazLpihKR4QW75DNkWTVdA imported private key for: EOS5fqiC3VFAJ1riMrKf8vzD28nqd4EpXvZGpXt6YewEBnH8DYinG

4.4 创建账户

liyuechun:cleos yuechunli$ ./cleos create account eosio liyc111 EOS5RU8VsYBLnN5snGeUKmt1sDDzpvQbGyW7LPP6qEryaFctYieCK EOS5fqiC3VFAJ1riMrKf8vzD28nqd4EpXvZGpXt6YewEBnH8DYinG

5. 部署合约

liyuechun:build yuechunli$ cleos set contract liyc111 ./contracts/Hello Reading W/WASM from ./contracts/Hello/Hello.wast... Assembling WASM... Publishing contract... executed transaction: 21d891e425f3d65852432e2b6a78146e2e2992a267c9f28c8ce56cd5dbea98f2 1632 bytes 2200576 cycles # eosio <= eosio::setcode {"account":"liyc111","vmtype":0,"vmversion":0,"code":"0061736d0100000001370b60027f7e0060027e7e006001... # eosio <= eosio::setabi {"account":"liyc111","abi":{"types":[],"structs":[{"name":"hi","base":"","fields":[{"name":"user","t... liyuechun:build yuechunli$ cleos get code liyc111 code hash: e387951f9a18870f2c151fbceea5b279a3861bdabab58ea87a67296a8a6583d0 liyuechun:build yuechunli$

6. 执行合约 6.1 解锁钱包

PW5J3rx7Bfg9zb8Kf2owTytccFyJqtDTrqnUX8iBRRUvbwM8RyzRL是创建钱包是的密码。

liyuechun:build yuechunli$ cleos wallet unlock --password PW5J3rx7Bfg9zb8Kf2owTytccFyJqtDTrqnUX8iBRRUvbwM8RyzRL Unlocked: default

6.2 执行合约

liyuechun:build yuechunli$ cleos push action liyc111 hi '{"user":"liyc1215"}' -p liyc111 executed transaction: 9abcaec2711ce31c693e5124af507f34aa666702bd5bb230ec31ddd6903248a8 232 bytes 102400 cycles # liyc111 <= liyc111::hi {"user":"liyc1215"} >> Hello, liyc1215 liyuechun:build yuechunli$

猜你喜欢

EOS价格暴涨,节点之争白热化,蚁池、薛蛮子、老猫……多少大佬已入场?(图文)

   最近,EOS可谓赚足了眼球,除了主网还有两个月上线,EOS全球21个超级节点之争也正在如火如荼地进行。有人戏称“EOS21个节点之争”都有种

2018-07-14

EOS区块链周报(图文)

“许多年以后,面对行刑队,奥雷良诺.布恩地亚上校将会回想起,他父亲带他去见识冰块的那个遥远的下午。”     EOS当前价格为17.4

2018-07-14

温州帮炒EOS能不暴涨吗?组超级饭局,当场买进6亿多元EOS(图文)

摘要: 我把这个饭局叫做EOS饭局,其实也没那么复杂,就是一群朋友坐在一块聊投资聊区块链,没想到当场就有人买下了2个亿的EOS,之后的两个小时内,又有企业家通过我们看好了

2018-07-14

两周暴涨150%仍遭市场哄抢 EOS的“底气”由何而来?(图文)

4月11日以来,EOS在短短半个月时间里取得了超150%的惊人涨幅,同期其他主流币种虽然迎来了一波普涨,但BTC、LTC涨幅仅四成左右,表现相对强势的ETH也不过只取得了70%

2018-07-14

时戳资本: 以太坊、EOS等底层公链仍是未来热门投资领域(图文)

    在商业应用仍未广泛落地之前,区块链的“操作系统”仍然是现阶段区块链行业的攻关重点,与互联网逻辑不同的是,区块链的底层价值协议远超

2018-07-14