1. 啟動(dòng)本地測(cè)試網(wǎng)絡(luò)
nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --contracts-console
2. 創(chuàng)建錢包
cleos wallet create --to-console
#調(diào)用以上命令之后會(huì)返回以下信息, 記住要保存密碼, 以后會(huì)用到
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5Kewn9L76X8Fpd....................t42S9XCw2"
#錢包創(chuàng)建之后默認(rèn)關(guān)閉, 需要調(diào)用以下命令打開(kāi)
cleos wallet open
#查看錢包列表
cleos wallet list
#返回
Wallets:
[
"default"
]
#錢包現(xiàn)在處于鎖住的狀態(tài),需要解鎖
cleos wallet unlock
#這個(gè)時(shí)候會(huì)要求你輸入密碼, 輸入我們?cè)趧?chuàng)建錢包時(shí)得到的密碼
#接著查看list, 會(huì)看到default后面有個(gè)*
cleos wallet list
Wallets:
[
"default *"
]
#創(chuàng)建一個(gè)私鑰并導(dǎo)入到錢包
cleos wallet create_key
#返回
Created new private key with a public key of: "EOS8PEJ5FM42xLpHK...X6PymQu97KrGDJQY5Y"
#導(dǎo)入
cleos wallet import
#輸入eosio默認(rèn)key
5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
3. 創(chuàng)建賬號(hào)
#后面的公鑰填你上面生成的
cleos create account eosio bob EOS8PEJ5FM42xLpHK...X6PymQu97KrGDJQY5Y
cleos create account eosio alice EOS8PEJ5FM42xLpHK...X6PymQu97KrGDJQY5Y
#這樣就創(chuàng)建了bob和alice兩個(gè)賬號(hào)
#合約部署時(shí)需要一個(gè)賬號(hào),每個(gè)合約對(duì)應(yīng)一個(gè)賬號(hào), 所以我們現(xiàn)在創(chuàng)建一個(gè)合約賬號(hào)
#先查看現(xiàn)有的key
cleos wallet keys
#從返回結(jié)果中選一個(gè)key來(lái)生成我們的合約hello賬號(hào), 記住你選的key, 后面會(huì)用到
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
#部署成功之后嘗試調(diào)用
cleos push action hello hi '["bob"]' -p bob@active
#調(diào)用成功
executed transaction: 4c10c1426c16b1656e802f3302677594731b380b18a44851d38e8b5275072857 244 bytes 1000 cycles
# hello.code <= hello.code::hi {"user":"bob"}
>> Hello, bob
注: 部署完我們自己合約后, 我發(fā)現(xiàn)調(diào)用transfer會(huì)失敗, 這里有兩個(gè)原因. 一是transfer這個(gè)action是push給eosio.token的,而不是我們的hello; 二是我們賬號(hào)里沒(méi)錢
#transfer 4個(gè)參數(shù), from to quantity memo
cleos push action eosio.token transfer '["bob","hello","0.5000 EOS", "test"]' -p bob@active
5. 給自己發(fā)幣
先創(chuàng)建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
#創(chuàng)建token
cleos push action eosio.token create '[ "eosio", "1000000000.0000 EOS"]' -p eosio.token@active
#給自己發(fā)幣
cleos push action eosio.token issue '[ "alice", "100.0000 EOS", "memo" ]' -p eosio@active
#轉(zhuǎn)賬
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. 調(diào)試
#代碼中用print打印日志輸出, 調(diào)用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