1. 启动本地测试网络
nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --contracts-console
2. 创建钱包
cleos wallet create --to-console
#调用以上命令之后会返回以下信息, 记住要保存密码, 以后会用到
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5Kewn9L76X8Fpd....................t42S9XCw2"
#钱包创建之后默认关闭, 需要调用以下命令打开
cleos wallet open
#查看钱包列表
cleos wallet list
#返回
Wallets:
[
"default"
]
#钱包现在处于锁住的状态,需要解锁
cleos wallet unlock
#这个时候会要求你输入密码, 输入我们在创建钱包时得到的密码
#接着查看list, 会看到default后面有个*
cleos wallet list
Wallets:
[
"default *"
]
#创建一个私钥并导入到钱包
cleos wallet create_key
#返回
Created new private key with a public key of: "EOS8PEJ5FM42xLpHK...X6PymQu97KrGDJQY5Y"
#导入
cleos wallet import
#输入eosio默认key
5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
3. 创建账号
#后面的公钥填你上面生成的
cleos create account eosio bob EOS8PEJ5FM42xLpHK...X6PymQu97KrGDJQY5Y
cleos create account eosio alice EOS8PEJ5FM42xLpHK...X6PymQu97KrGDJQY5Y
#这样就创建了bob和alice两个账号
#合约部署时需要一个账号,每个合约对应一个账号, 所以我们现在创建一个合约账号
#先查看现有的key
cleos wallet keys
#从返回结果中选一个key来生成我们的合约hello账号, 记住你选的key, 后面会用到
cleos create account eosio hello EOS6omqMHR4QzMg2cVRNdJmyKoK9zhnkc4RHk5tzuUX7ByaM61LUF -p eosio@active
4. 部署合约
#直接用eosio.cdt里的examples/hello
#先编译
eosio-cpp hello.cpp -o hello.wasm --abigen
#再部署,注意CONTRACTS_DIR是你的hello文件夹所在的全路径
cleos set contract hello CONTRACTS_DIR/hello -p hello@active
#部署成功之后尝试调用
cleos push action hello hi '["bob"]' -p bob@active
#调用成功
executed transaction: 4c10c1426c16b1656e802f3302677594731b380b18a44851d38e8b5275072857 244 bytes 1000 cycles
# hello.code <= hello.code::hi {"user":"bob"}
>> Hello, bob
注: 部署完我们自己合约后, 我发现调用transfer会失败, 这里有两个原因. 一是transfer这个action是push给eosio.token的,而不是我们的hello; 二是我们账号里没钱
#transfer 4个参数, from to quantity memo
cleos push action eosio.token transfer '["bob","hello","0.5000 EOS", "test"]' -p bob@active
5. 给自己发币
先创建eosio.token
git clone https://github.com/EOSIO/eosio.contracts --branch v1.4.0 --single-branch
cd eosio.contracts/eosio.token
#注 后面的key填你自己的
cleos create account eosio eosio.token EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
#编译合约
eosio-cpp -I include -o eosio.token.wasm src/eosio.token.cpp --abigen
#部署合约 CONTRACTS_DIR填全路径
cleos set contract eosio.token CONTRACTS_DIR/eosio.contracts/eosio.token --abi eosio.token.abi -p eosio.token@active
#创建token
cleos push action eosio.token create '[ "eosio", "1000000000.0000 EOS"]' -p eosio.token@active
#给自己发币
cleos push action eosio.token issue '[ "alice", "100.0000 EOS", "memo" ]' -p eosio@active
#转账
cleos push action eosio.token transfer '[ "alice", "bob", "25.0000 EOS", "m" ]' -p alice@active
#查询余额
cleos get currency balance eosio.token bob EOS
25.00 EOS
6. 调试
#代码中用print打印日志输出, 调用transfer, 即可在nodeos的终端中看到日志打印
cleos push action eosio.token transfer '["bob","hello","0.5000 EOS", "test"]' -p bob@active
#修改了代码之后重新编译和部署即可
eosio-cpp hello.cpp -o hello.wasm --abigen
cleos set contract hello CONTRACTS_DIR/hello -p hello@active