Author: Li Yuechun | Founder of Confucius Institute
Video Website: http://kongyixueyuan.com
WeChat search "kongyixueyuan" to add Lily WeChat to apply to join the group
Li Yuechun Blockchain Blog: http://liyuechun.org
Whether it is C、C++、Java
or any other language, when we first start learning, we will start from the HelloWorld
beginning. This article mainly explains EOS DAWN-V3.0.0
the development of smart contracts Hello World
.
Before reading this article, please read the following two introductory articles.
- Part 1 - Teach you to sort out the ins and outs of building various EOS development environments
- Part 2 - EOS Currency Contract Case
1. Write contract code
Create a folder on the desktop, such as: 0418
, Atom
open the 0418
folder with. Create a new file Hello.cpp
file and copy the following source code into the Hello.cpp
file.
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
// 视频网站: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. Generate .wast
files
liyuechun:Project yuechunli$ eosiocpp -o Hello.wast Hello.cpp
liyuechun:Project yuechunli$ ls
Hello.cpp Hello.wast
liyuechun:Project yuechunli$
3. Generate .abi
files
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
The contents of the file are as follows:
{
"____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. Create a wallet account
4.1 Create wallet
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
It must be saved, and this password is required to unlock the wallet.
4.2 Create two sets of keys
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 Import the private key to the wallet
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 Create an account
liyuechun:cleos yuechunli$ ./cleos create account eosio liyc111 EOS5RU8VsYBLnN5snGeUKmt1sDDzpvQbGyW7LPP6qEryaFctYieCK EOS5fqiC3VFAJ1riMrKf8vzD28nqd4EpXvZGpXt6YewEBnH8DYinG
5. Deploy the contract
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. Execute the contract
6.1 Unlock the wallet
PW5J3rx7Bfg9zb8Kf2owTytccFyJqtDTrqnUX8iBRRUvbwM8RyzRL
is the password to create the wallet yes.
liyuechun:build yuechunli$ cleos wallet unlock --password PW5J3rx7Bfg9zb8Kf2owTytccFyJqtDTrqnUX8iBRRUvbwM8RyzRL
Unlocked: default
6.2 Execute the contract
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$