转载于:黎跃春老师
不管是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 WAST/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$