大部分内容都参考官方文档,但是官方资料更新不及时,坑比较多
官方文档:
https://github.com/EOSIO/eos/wiki/Tutorial-Getting-Started-With-Contracts
账号配置
创建完之后默认钱包default已经是unlock,如果是锁定状态使用unlock命令解锁。
配置钱包
eos@mybc:~$ cleos wallet create
"/opt/eosio/bin/keosd" launched
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5KHNypEkswDYuTkir28fbfTeFjmLHpgFEww3NWx5CVUVmazwJUL"
eos@mybc:~$ echo PW5KHNypEkswDYuTkir28fbfTeFjmLHpgFEww3NWx5CVUVmazwJUL >p.txt
eos@mybc:~$ cleos wallet unlock --password PW5KHNypEkswDYuTkir28fbfTeFjmLHpgFEww3NWx5CVUVmazwJUL
Error 3120007: Already unlocked
eos@mybc:~$ cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
eos@mybc:~$
非常重要,需要import默认 eosio的私钥,否则创建用户以及导入合约时会报以下错误(只是本地测试环境)
Error 3090003: provided keys, permissions, and delays do not satisfy declared authorizations
Ensure that you have the related private keys inside your wallet and your wallet is unlocked.
附测试网络的eosio的默认key, eosio账号是这个节点的Producer ID, 公私钥都配置在/opt/eosio/data/config.ini中
eosio public key: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
eosio private key: 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
配置账号
eos@mybc:~$ cleos create key
Private key: 5J3iFb6j1uJBw643jEvzqN2Mmcday7ctTtVpWwiEFoMYSxhWqCv
Public key: EOS6CnvYoPQVHwxFhbfjQrJxqzH4pdbCcgKidQdn3xZa4DEwsR4Tq
eos@mybc:~$ cleos create key
Private key: 5KcWzrq7VufNm6RjzeaeN3y8FaqhwYKT1ZhmNxUHsjFsk6pC69U
Public key: EOS6ArbyaJMU4DbrkUjkpgqDiVeWFPWCyL18YRXAJv9jZV6xzGA2Z
eos@mybc:~$ cleos wallet import 5J3iFb6j1uJBw643jEvzqN2Mmcday7ctTtVpWwiEFoMYSxhWqCv
imported private key for: EOS6CnvYoPQVHwxFhbfjQrJxqzH4pdbCcgKidQdn3xZa4DEwsR4Tq
eos@mybc:~$ cleos wallet import 5KcWzrq7VufNm6RjzeaeN3y8FaqhwYKT1ZhmNxUHsjFsk6pC69U
imported private key for: EOS6ArbyaJMU4DbrkUjkpgqDiVeWFPWCyL18YRXAJv9jZV6xzGA2Z
eos@mybc:~$
eos@mybc:~$ cleos create account eosio user EOS6CnvYoPQVHwxFhbfjQrJxqzH4pdbCcgKidQdn3xZa4DEwsR4Tq EOS6ArbyaJMU4DbrkUjkpgqDiVeWFPWCyL18YRXAJv9jZV6xzGA2Z
executed transaction: 51d8256e103ad4b38d55e54dc477cbf6935e41ff7d54dd9bf6f3116a69ce8ac1 200 bytes 243 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"user","owner":{"threshold":1,"keys":[{"key":"EOS6CnvYoPQVHwxFhbfjQrJxqzH4...
warning: transaction executed locally, but may not be confirmed by the network yet
eos@mybc:~$
eos@mybc:~$ cleos create account eosio tester EOS6CnvYoPQVHwxFhbfjQrJxqzH4pdbCcgKidQdn3xZa4DEwsR4Tq EOS6ArbyaJMU4DbrkUjkpgqDiVeWFPWCyL18YRXAJv9jZV6xzGA2Z
executed transaction: e739b3331adbbaeeb6ff07accb7cebf67f96eca5917e89a38674bf328fcc1b30 200 bytes 275 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"tester","owner":{"threshold":1,"keys":[{"key":"EOS6CnvYoPQVHwxFhbfjQrJxqz...
warning: transaction executed locally, but may not be confirmed by the network yet
eos@mybc:~$ cleos get accounts EOS6CnvYoPQVHwxFhbfjQrJxqzH4pdbCcgKidQdn3xZa4DEwsR4Tq
{
"account_names": [
"tester",
"user"
]
}
eos@mybc:~$
通过cleos get accounts即可看到刚配置的账号列表。
发币
环境以及账号配置妥当之后,就开始部署合约了
首先创建个账号用于部署合约
eos@mybc:~$ cleos create account eosio eosio.token EOS6CnvYoPQVHwxFhbfjQrJxqzH4pdbCcgKidQdn3xZa4DEwsR4Tq EOS6ArbyaJMU4DbrkUjkpgqDiVeWFPWCyL18YRXAJv9jZV6xzGA2Z
executed transaction: 3305b604d2edf05a7f0090f874cb8fd5fc0abadab3d6ca5c9fd4a6bcccad265c 200 bytes 248 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"eosio.token","owner":{"threshold":1,"keys":[{"key":"EOS6CnvYoPQVHwxFhbfjQ...
warning: transaction executed locally, but may not be confirmed by the network yet
部署合约
智能合约源代码在eos/contracts/eosio.token,是需要编译之后才能部署的,如果使用Docker hub的方式的话是没有合约编译的环境的,如果想尝试自己编译的可以使用eos-dev的镜像。这里我有编译好的合约复制到当前contracts目录, 也可直接使用前一章提到的/opt/eosio/data/contracts目录
eos@mybc:~$ cleos set contract eosio.token contracts/eosio.token -p eosio.token
Reading WAST/WASM from contracts/eosio.token/eosio.token.wasm...
Using already assembled WASM...
Publishing contract...
executed transaction: 3436ea1df1e2a9620b9697c3b2d9cd5e90acd3a4cdd54ff89cfc8c1fb9b33556 8104 bytes 1075 us
# eosio <= eosio::setcode {"account":"eosio.token","vmtype":0,"vmversion":0,"code":"0061736d01000000017e1560037f7e7f0060057f7e...
# eosio <= eosio::setabi {"account":"eosio.token","abi":"0e656f73696f3a3a6162692f312e30010c6163636f756e745f6e616d65046e616d65...
warning: transaction executed locally, but may not be confirmed by the network yet
创建代币
创建一个名为SYS,总量1000000000的代币
eos@mybc:~$ cleos push action eosio.token create '[ "eosio", "1000000000.0000 SYS"]' -p eosio.token
executed transaction: 962c1cf2f54c7d35be8b0e5f6d8f4fc8c8105c333ea6382db14bae335209e9a7 120 bytes 359 us
# eosio.token <= eosio.token::create {"issuer":"eosio","maximum_supply":"1000000000.0000 SYS"}
warning: transaction executed locally, but may not be confirmed by the network yet
给user发行100代币
可以使用命令
cleos push action eosio.token issue '[ "user", "100.0000 SYS", "this is a test" ]' -p eosio
这里使用如下命令给用户user发行Token
eos@mybc:~$ cleos push action eosio.token issue '["user", "100.0000 SYS", "memo"]' -p eosio -d -j
{
"expiration": "2018-06-19T09:08:58",
"ref_block_num": 17016,
"ref_block_prefix": 1044514165,
"max_net_usage_words": 0,
"max_cpu_usage_ms": 0,
"delay_sec": 0,
"context_free_actions": [],
"actions": [{
"account": "eosio.token",
"name": "issue",
"authorization": [{
"actor": "eosio",
"permission": "active"
}
],
"data": "00000000007015d640420f00000000000453595300000000046d656d6f"
}
],
"transaction_extensions": [],
"signatures": [
"SIG_K1_KaZbC8cL9s15Vx7LBD4TJy7T1fuHExcbFE8N5EZnxY1x8YqVpJbkgBSm1E7KEEmHQP6nuS6awGVPCr8Kt5uXPee8u7cX4B"
],
"context_free_data": []
}
user用户转账给tester用户25个代币
eos@mybc:~$ cleos push action eosio.token transfer '[ "user", "tester", "25.0000 SYS", "m" ]' -p user
executed transaction: ef457476720be62ad5b326ac597ec0a1c819f3930e6c278b28900c4c7aaa46b3 128 bytes 483 us
# eosio.token <= eosio.token::transfer {"from":"user","to":"tester","quantity":"25.0000 SYS","memo":"m"}
# user <= eosio.token::transfer {"from":"user","to":"tester","quantity":"25.0000 SYS","memo":"m"}
# tester <= eosio.token::transfer {"from":"user","to":"tester","quantity":"25.0000 SYS","memo":"m"}
warning: transaction executed locally, but may not be confirmed by the network yet
eos@mybc:~$
查询账户下的资产
tester账户下有25个代币
eos@mybc:~$ cleos get account user
permissions:
owner 1: 1 EOS6CnvYoPQVHwxFhbfjQrJxqzH4pdbCcgKidQdn3xZa4DEwsR4Tq
active 1: 1 EOS6ArbyaJMU4DbrkUjkpgqDiVeWFPWCyL18YRXAJv9jZV6xzGA2Z
memory:
quota: unlimited used: 3.02 KiB
net bandwidth:
used: unlimited
available: unlimited
limit: unlimited
cpu bandwidth:
used: unlimited
available: unlimited
limit: unlimited
SYS balances:
liquid: 75.0000 SYS
staked: 0.0000 SYS
unstaking: 0.0000 SYS
total: 75.0000 SYS
eos@mybc:~$ cleos get account tester
permissions:
owner 1: 1 EOS6CnvYoPQVHwxFhbfjQrJxqzH4pdbCcgKidQdn3xZa4DEwsR4Tq
active 1: 1 EOS6ArbyaJMU4DbrkUjkpgqDiVeWFPWCyL18YRXAJv9jZV6xzGA2Z
memory:
quota: unlimited used: 2.66 KiB
net bandwidth:
used: unlimited
available: unlimited
limit: unlimited
cpu bandwidth:
used: unlimited
available: unlimited
limit: unlimited
SYS balances:
liquid: 25.0000 SYS
staked: 0.0000 SYS
unstaking: 0.0000 SYS
total: 25.0000 SYS
eos@mybc:~$
至此通过智能合约创建/发行Token/转账已经完成。