大部分內(nèi)容都參考官方文檔灯变,但是官方資料更新不及時(shí)殴玛,坑比較多
官方文檔:
https://github.com/EOSIO/eos/wiki/Tutorial-Getting-Started-With-Contracts
賬號(hào)配置
創(chuàng)建完之后默認(rèn)錢包default已經(jīng)是unlock,如果是鎖定狀態(tài)使用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默認(rèn) eosio的私鑰,否則創(chuàng)建用戶以及導(dǎo)入合約時(shí)會(huì)報(bào)以下錯(cuò)誤(只是本地測(cè)試環(huán)境)
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.
附測(cè)試網(wǎng)絡(luò)的eosio的默認(rèn)key, eosio賬號(hào)是這個(gè)節(jié)點(diǎn)的Producer ID, 公私鑰都配置在/opt/eosio/data/config.ini中
eosio public key: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
eosio private key: 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
配置賬號(hào)
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:~$
通過(guò)cleos get accounts即可看到剛配置的賬號(hào)列表刃泌。
發(fā)幣
環(huán)境以及賬號(hào)配置妥當(dāng)之后凡壤,就開(kāi)始部署合約了
首先創(chuàng)建個(gè)賬號(hào)用于部署合約
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的方式的話是沒(méi)有合約編譯的環(huán)境的亚侠,如果想嘗試自己編譯的可以使用eos-dev的鏡像曹体。這里我有編譯好的合約復(fù)制到當(dāng)前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
創(chuàng)建代幣
創(chuàng)建一個(gè)名為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發(fā)行100代幣
可以使用命令
cleos push action eosio.token issue '[ "user", "100.0000 SYS", "this is a test" ]' -p eosio
這里使用如下命令給用戶user發(fā)行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用戶轉(zhuǎn)賬給tester用戶25個(gè)代幣
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:~$
查詢賬戶下的資產(chǎn)
tester賬戶下有25個(gè)代幣
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:~$
至此通過(guò)智能合約創(chuàng)建/發(fā)行Token/轉(zhuǎn)賬已經(jīng)完成硝烂。