2.2部署,發行以及發送代幣
Step 1: 獲取合約源
首先來到妳的合約文件夾。
• Shell
cd CONTRACTS_DIR
pull github源
• Shell
git clone https://github.com/EOSIO/eosio.contracts --branch v1.7.0 --single-branch
這個代碼倉庫包含了幾種合約,不過對於本部分教程來說,我們只關心eosio.token合約。跳轉到eosio.contracts/eosio.token文件夾。
• Shell
cd eosio.contracts/contracts/eosio.token
Step 2: 為合約創建帳號
在部署代幣合約之前,我們必須創建壹個帳號,才能進行部署,我們將會使用eosio開發key。
在執行下面的命令之前妳需要解鎖妳的錢包。
• Shell
cleos create account eosio eosio.token EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
Step 3: 編譯合約
• Shell
eosio-cpp -I include -o eosio.token.wasm src/eosio.token.cpp --abigen
Step 4: 部署代幣合約
• Shell
cleos set contract eosio.token CONTRACTS_DIR/eosio.contracts/contracts/eosio.token --abi eosio.token.abi -p eosio.token@active
• Result
Reading WASM from ... Publishing contract... executed transaction: 69c68b1bd5d61a0cc146b11e89e11f02527f24e4b240731c4003ad1dc0c87c2c 9696 bytes 6290 us # eosio <= eosio::setcode {"account":"eosio.token","vmtype":0,"vmversion":0,"code":"0061736d0100000001aa011c60037f7e7f0060047f... # eosio <= eosio::setabi {"account":"eosio.token","abi":"0e656f73696f3a3a6162692f312e30000605636c6f73650002056f776e6572046e61... warning: transaction executed locally, but may not be confirmed by the network yet }
Step 5: 創建代幣
要創建壹種新的代幣,就需要調用create這個action,同時傳遞適當的參數給它。這個action接收壹個參數,它包括:
• 壹個eosio帳號作為發行者,在本例中,就是alice。這個發行者需要具備權限,能調用發行並且/或者執行其他action,比如凍結,召回,以及加入白名單。
• 壹個資產類型,包含兩種數據,壹個浮點數,它代表了該代幣的最大供應量,壹個符號,只能大寫。比如,"1.0000 SYS"。
下面就是調用這個方法的壹種簡潔的方式。
• Shell
cleos push action eosio.token create '[ "alice", "1000000000.0000 SYS"]' -p eosio.token@active
• Result
executed transaction: 0e49a421f6e75f4c5e09dd738a02d3f51bd18a0cf31894f68d335cd70d9c0e12 120 bytes 1000 cycles # eosio.token <= eosio.token::create {"issuer":"eosio","maximum_supply":"1000000000.0000 SYS"}
另壹種方式就指定了參數名:
• Shell
cleos push action eosio.token create '{"issuer":"alice", "maximum_supply":"1000000000.0000 SYS"}' -p eosio.token@active
• Result
executed transaction: 0e49a421f6e75f4c5e09dd738a02d3f51bd18a0cf31894f68d335cd70d9c0e12 120 bytes 1000 cycles # eosio.token <= eosio.token::create {"issuer":"eosio","maximum_supply":"1000000000.0000 SYS"}
這條命令創建了壹個新的代幣SYS ,精確到小數點後四位,總量為1000000000.0000 SYS。同時,它指定了alice作為發行者。要創建這個代幣,合約需要 eosio.token帳號的授權。出於這個原因,我們發送-p eosio.token@active來對這個action進行授權。
Step 6: 發行代币
發行者alice現在可以發行新代幣了。正如之前說的那樣,只有發行者可以這麽做,因此,必須發送-p alice@active來對issue action進行授權。
• Text
cleos push action eosio.token issue '[ "alice", "100.0000 SYS", "memo" ]' -p alice@active
• Result
executed transaction: 822a607a9196112831ecc2dc14ffb1722634f1749f3ac18b73ffacd41160b019 268 bytes 1000 cycles # eosio.token <= eosio.token::issue {"to":"alice","quantity":"100.0000 SYS","memo":"memo"} >> issue # eosio.token <= eosio.token::transfer {"from":"eosio","to":"alice","quantity":"100.0000 SYS","memo":"memo"} >> transfer # eosio <= eosio.token::transfer {"from":"eosio","to":"alice","quantity":"100.0000 SYS","memo":"memo"} # user <= eosio.token::transfer {"from":"eosio","to":"alice","quantity":"100.0000 SYS","memo":"memo"}
這次,輸出包含了幾種不同的action:壹個issue action和三個transfer action。只有issue action是被簽署的,issue action執行了 inline transfer,然後 inline transfer通知發送者和接受者帳號。輸出顯示了所有action的處理者,他們被調用的順序,以及該action是否生成或不生成的輸出。
技術上來講,eosio.token合約可以跳過inline transfer,直接修改帳號余額。但是,本例中,eosio.token合約遵循了代幣慣例:要求所有帳戶余額都可以通過引用它們的轉賬操作的總和派生出來。它還要求通知資金的發送者和接受者,這樣,他們就能自動處理存款和取款操作。
要想檢查轉賬操作,可以使用-d –j選項,它們分別代表“不要廣播”,“把轉賬操作以json格式返回”,
• Shell
cleos push action eosio.token issue '["alice", "100.0000 SYS", "memo"]' -p alice@active -d -j
Step 7: 發送代币
現在帳號alice以及發行了代幣,那麽我們試試給帳號bob發送壹些代幣。
• Shell
cleos push action eosio.token transfer '[ "alice", "bob", "25.0000 SYS", "m" ]' -p alice@active
• Result
executed transaction: 06d0a99652c11637230d08a207520bf38066b8817ef7cafaab2f0344aafd7018 268 bytes 1000 cycles # eosio.token <= eosio.token::transfer {"from":"alice","to":"bob","quantity":"25.0000 SYS","memo":"Here you go bob!"} >> transfer # user <= eosio.token::transfer {"from":"alice","to":"bob","quantity":"25.0000 SYS","memo":"Here you go bob!"} # tester <= eosio.token::transfer {"from":"alice","to":"bob","quantity":"25.0000 SYS","memo":"Here you go bob!"}
現在使用cleos get currency balance命令,查看bob是否收到了代幣
• Shell
cleos get currency balance eosio.token bob SYS
• Text
25.00 SYS
檢查alice的余額,註意,代幣數量已經從帳號中扣除了。
• Shell
cleos get currency balance eosio.token alice SYS
• Text
75.00 SYS
不錯!壹切順利!