1. 程式人生 > >第7課 技術小白如何在EOS上發行通證(TOKEN)?

第7課 技術小白如何在EOS上發行通證(TOKEN)?

1. 文章摘要

【本文目標】

在EOS私鏈環境發行TOKEN代幣,並進行轉賬交易。

【環境前置條件】

1)已搭建好EOS私鏈環境(可參考第4課 如何在UBUNTU虛擬機器上編譯EOS完成環境搭建?) 2)會進行相關錢包賬號的操作( 參考第5課 EOS環境搭建入門(私鏈節點-錢包-金鑰-賬號)) 3) 本文操作時已存在以下資源資訊 1] 錢包名稱 duncanwang,對應的解鎖密碼為"PW5JYSZRvSropLhguyxYY1zXgeQidSZnRKPKUXr2JUVTYay2zhuxT" 2] 系統私鑰/公鑰對已匯入錢包: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV / 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

【技術收穫】

1) 建立自有代幣 2) 發行自有代幣 3) 代幣轉賬操作

2. 操作內容

本文的操作內容的流程圖如下所示:

2.1 建立賬戶

建立3個賬號,eosio.token賬號用於建立ColorBayToken代幣,wangdenghui賬號用於發行代幣,建立賬戶ouyanggege用於接收代幣。 建立eosio.token賬號

cleos create account eosio eosio.token EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

輸出內容:

[email protected]:~$ cleos wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 -n duncanwang
imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
[email protected]:~$ cleos create account eosio eosio.token EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
executed transaction: 5679e2ad487fe7956ab287beb1c8642635e493d54ef4e948be06e0c24af48520  200 bytes  1133 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"eosio.token","owner":{"threshold":1,"keys":[{"key":"EOS6MRyAjQq8ud7hVNYcf...
warning: transaction executed locally, but may not be confirmed by the network yet

建立wangdenghui賬號

cleos create account eosio wangdenghui EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

輸出內容:

[email protected]:~/eos$ cleos create account eosio wangdenghui EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
executed transaction: 09688423e0f287cd0ae829fc358b58ecd0ec7f6e7e403684969bcaa3c7227a91  200 bytes  320 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"wangdenghui","owner":{"threshold":1,"keys":[{"key":"EOS6MRyAjQq8ud7hVNYcf...
warning: transaction executed locally, but may not be confirmed by the network yet

建立ouyanggege賬號

cleos create account eosio ouyanggege EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV

輸出內容:

[email protected]:~/eos$ cleos create account eosio ouyanggege EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
executed transaction: 316a20b2c20669d853b116d6126b9f09fdece00a62c888acc2d4ab4c845d875b  200 bytes  738 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"ouyanggege","owner":{"threshold":1,"keys":[{"key":"EOS6MRyAjQq8ud7hVNYcfn...
warning: transaction executed locally, but may not be confirmed by the network yet

2.2 部署代幣合約

cleos set contract eosio.token build/contracts/eosio.token -p eosio.token

引數說明: -p,–permission TEXT 表示授權賬號,該合約授權給eosio.token賬號。 代幣合約eosio.token能讓許多不同的代幣同時在一個智慧合約上發行,但是卻能被不同使用者管理。

[email protected]:~/eos$ cleos set contract eosio.token build/contracts/eosio.token -p eosio.token
Reading WAST/WASM from build/contracts/eosio.token/eosio.token.wasm...
Using already assembled WASM...
Publishing contract...
executed transaction: 74e746a1bcb58d69a47b8182a960f2657b56023ab30df959ff5e29405fec785b  8104 bytes  2088 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

2.3 建立一個代幣

cleos push action eosio.token create ‘[ “eosio”, “1000000000.0000 CBT”, 0, 0, 0]’ -p eosio.token

引數說明: 1) create 函式是用來新建一種代幣,並輸入代幣的各種屬性,同時 create 函式也是一個 action。action 是 eos 智慧合約的介面函式,定義外界可以對智慧合約做什麼動作。 結合操作命令,說明各個引數的定義: create( account_name issuer,/發行方賬號的名稱為"eosio"/ asset maximum_supply,/總供應量為10億和代幣名稱CBT (ColorBayToken的縮寫)/ uint8_t can_freeze,/資產是否可以凍結,0表示否/ uint8_t can_recall,/資產是否可以召回,0表示否/ uint8_t can_whitelist );/資產是否可以設定白名單,0表示無/ 2)-p,–permission Type: Text - 授權給賬號eosio.token

[email protected]:~/eos$ cleos push action eosio.token create '[ "eosio", "1000000000.0000 CBT", 0, 0, 0]' -p eosio.token
executed transaction: 791687224acb86a715639af9b3b4660ac06ac07908c8746851382be661218739  120 bytes  4703 us
#   eosio.token <= eosio.token::create          {"issuer":"eosio","maximum_supply":"1000000000.0000 CBT"}
warning: transaction executed locally, but may not be confirmed by the network yet

2.4 發行代幣

cleos push action eosio.token issue ‘[ “wangdenghui”, “10000.0000 CBT”, “first ColorBayToken issue” ]’ -p eosio

引數說明: 1) issue 函式是用來增發代幣,eosio.token 合約並不是新建了代幣就會得到代幣,新建的代幣只是儲存了資料,發幣者要想獲取代幣,需要呼叫 issue action 來獲得代幣。 void issue( account_name to, /代幣的發行方為賬戶為wangdenghui,之前已經建立/ asset quantity, /發行代幣的數量為10000個,代幣名稱為CBT/ string memo ); /表示備註說明,不能超過256個位元組/

執行輸入結果如下:

[email protected]:~/eos$ cleos push action eosio.token issue '[ "wangdenghui", "10000.0000 CBT", "first ColorBayToken issue" ]' -p eosio
executed transaction: 33b23322d6a413108350a21f23c77150d77de71a8aeeaefd5befd9e30ceec288  144 bytes  2602 us
#   eosio.token <= eosio.token::issue           {"to":"wangdenghui","quantity":"10000.0000 CBT","memo":"first ColorBayToken issue"}
#   eosio.token <= eosio.token::transfer        {"from":"eosio","to":"wangdenghui","quantity":"10000.0000 CBT","memo":"first ColorBayToken issue"}
#         eosio <= eosio.token::transfer        {"from":"eosio","to":"wangdenghui","quantity":"10000.0000 CBT","memo":"first ColorBayToken issue"}
#   wangdenghui <= eosio.token::transfer        {"from":"eosio","to":"wangdenghui","quantity":"10000.0000 CBT","memo":"first ColorBayToken issue"}
warning: transaction executed locally, but may not be confirmed by the network yet        

##2.5 代幣轉賬 輝哥賬號轉賬給歐陽哥哥合計8000個彩貝幣,用於支付午餐費用。

cleos push action eosio.token transfer ‘[ “wangdenghui”, “ouyanggege”, “8000.0000 CBT”, “pay for lunch!” ]’ -p wangdenghui

轉賬成功輸出結果:

[email protected]:~/eos$ cleos push action eosio.token transfer '[ "wangdenghui", "ouyanggege", "8000.0000 CBT", "pay for lunch!" ]' -p wangdenghui
executed transaction: 42c0f99566561977424df6417581114c3d9865c5194256f6b7a1311abf97802a  144 bytes  2011 us
#   eosio.token <= eosio.token::transfer        {"from":"wangdenghui","to":"ouyanggege","quantity":"8000.0000 CBT","memo":"pay for lunch!"}
#   wangdenghui <= eosio.token::transfer        {"from":"wangdenghui","to":"ouyanggege","quantity":"8000.0000 CBT","memo":"pay for lunch!"}
#    ouyanggege <= eosio.token::transfer        {"from":"wangdenghui","to":"ouyanggege","quantity":"8000.0000 CBT","memo":"pay for lunch!"}
warning: transaction executed locally, but may not be confirmed by the network yet

2.6 查詢餘額

查詢輝哥和歐陽哥哥的賬號,核實金額是否已到賬。

cleos get currency balance eosio.token wangdenghui cleos get currency balance eosio.token ouyanggege 正確輸出結果:

[email protected]:~/eos$ cleos get currency balance eosio.token wangdenghui
2000.0000 CBT
[email protected]:~/eos$ cleos get currency balance eosio.token ouyanggege
8000.0000 CBT

2.7 閱讀eosio.token智慧合約程式碼

EOS 官方提供了一個代幣(資產)智慧合約 Demo —— eosio.token,這個示例合約給出了 EOS 官方智慧合約開發的標準結構和開發方法,並且真正的 EOS 代幣也會借鑑這個示例合約的邏輯,是 EOS 智慧合約入門的經典案例。 預設情況下,該智慧合約存在路徑:~/eos/contracts/eosio.token 檔案解釋如下: eosio.token.hpp - 標頭檔案,定義類和函式 eosio.token.cpp - 實現 eosio.token.abi - Application Binary Interface,是一個介面檔案,描述了智慧合約與上層應用之間的資料交換格式 CMakeLists.txt - 編譯檔案

3. 參考