目錄
預設基本信息
預設目錄:CONTRACTS_DIR = /eos/contracts
語言選用: 根據官方的Demo ,C++
所要使用的錢包:先進行解鎖,否則無法進行下一步操作
一、Hello Eos
1、創建目錄存放
(雖然說可有可無,但是爲了方便管理,還是創建一個目錄存放)
之前預設的目錄 CONTRACTS_DIR ,下創建一個目錄'hello',目錄,並進入目錄
cd CONTRACTS_DIR
mkdir hello
cd hello
CONTRACTS_DIR 預先創建的工作目錄
目錄名稱沒有要求,隨意
2、創建實現的合約代碼並編譯
1、創建代碼
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
class hello : public contract {
public:
using contract::contract;
[[eosio::action]]
void hi( name user ) {
print( "Hello, ", user);
}
};
EOSIO_DISPATCH( hello, (hi))
// 還記得C++ 的可以忽略以下內容
//using namespace eosio 使用頭文件中的整個命名空間
//class hello : public contrac 是繼承合約類
//[[eosio::action]] 添加C++ 11樣式屬性,這樣abi生成器可以產生更可靠的輸出。
EOSIO_DISPATCH( hello, (hi)),總體上就是發佈的智能合約名稱叫hello,被調用的方法叫hi。
2、編譯代碼
編譯爲Web assembly(.wasm)
eosio-cpp -o hello.wasm hello.cpp --abigen
3、設置智能合約並測試
1、設置智能合約
cleos set contract coffeeandice CONTRACTS_DIR/hello -p coffeeandice@active
//第一個coffeeandice是合約名稱
// CONTRACTS_DIR 參照自己合約的目錄
// hello@active 賬戶爲hello ,權限爲active
2、調用合約
cleos push action coffeeandice hi '["helloword"]' -p coffeeandice@active
// 調用coffeeandice合約的hi方法,參數爲helloword,授權賬號爲coffeeandice。
返回結果
executed transaction: 80c8d2e5bae59bcd1fe42ebbdcd8cb8913bda6b2f277b8f067e69a0b109c51ad 104 bytes 264 us
# coffeeandice <= coffeeandice::hi {"user":"helloword"}
>> Hello, helloword
這時候所有賬戶都可以打招呼,執行合約操作
4、合約其他操作
1、require_auth 方法
1、代碼更改
可以用來校驗傳入的參數是否是特定用戶,此處利用接受的name 來傳入參數
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
class hello : public contract {
public:
using contract::contract;
[[eosio::action]]
void hi( name user ) {
require_auth( user ); //傳入name接受的參數,檢查用戶是否與參數相同
print( "Hello, ", user);
}
};
EOSIO_DISPATCH( hello, (hi))
2、編譯並重新設置智能合約
eosio-cpp -o hello.wasm hello.cpp --abigen
cleos set contract coffeeandice CONTRACTS_DIR/hello -p coffeeandice@active
3、測試:
使用 名稱不同的賬戶觸發:
cleos push action hello hi '["alice"]' -p coffeeandice@active
響應:
Error 3090004: Missing required authority
Ensure that you have the related authority inside your transaction!;
If you are currently using 'cleos push action' command, try to add the relevant authority using -p option.
Error Details:
missing authority of helloword
pending console output:
使用 名稱相同的賬戶觸發:
cleos push action coffeeandice hi '["alice"]' -p alice@active
響應:
executed transaction: fec843e79be1a6f81899bbc97e21bb58218eed36377170f7701e67dbdbdf1122 104 bytes 275 us
# coffeeandice <= coffeeandice::hi {"user":"alice"}
>> Hello, alice
二、搞搞發幣
獲取合約源後可以查看一下相應代碼,就能知道存在什麼的方法
1、獲取合約源
先進入目錄
cd CONTRACTS_DIR # 工作目錄
拉取源數據:
獲取存儲庫中包含的eosio.token 合約
git clone https://github.com/EOSIO/eosio.contracts --branch v1.4.0 --single-branch
cd eosio.contracts/eosio.token
2、創建合約賬戶(可不創建)
需要創建一個賬戶 / 其實也可以使用已有的賬戶來部署這份合約
3、編譯合約
eosio-cpp -I include -o eosio.token.wasm src/eosio.token.cpp --abigen
4、部署合約
cleos set contract coffeeandice CONTRACTS_DIR/eosio.contracts/eosio.token --abi eosio.token.abi -p coffeeandice@active
第一個 coffeeandice 是部署合約的賬號
第二個 eosio.token 是要部署的合約
-p coffeeandice的意思是授權賬號爲coffeeandice
響應
Reading WASM from /eos/contracts/eosio.contracts/eosio.token/eosio.token.wasm...
Publishing contract...
executed transaction: b4dba1cededd3e8ec9f065c55c9ad6b27d8f0182fca3339b59035f088bd09335 9608 bytes 2614 us
# eosio <= eosio::setcode {"account":"coffeeandice","vmtype":0,"vmversion":0,"code":"0061736d0100000001bb011f60000060037f7e7f0...
# eosio <= eosio::setabi {"account":"coffeeandice","abi":"0e656f73696f3a3a6162692f312e310008076163636f756e7400010762616c616e6...
5、創建代幣
調用create(...)操作,根據官方和代碼解釋,此操作存在接受1個參數,它是一個symbolname類型,由兩個數據組成,最大供應的浮點數和僅大寫字母字符的symbolname,例如“1.0000 SYM”
發行人將是有權要求發行或執行其他操作,如凍結、召回和列入所有者的白名單。
示例:
cleos push action coffeeandice create '[ "coffeeandice", "1000000000.0000 LG"]' -p coffeeandice@active
1、coffeeandice 是合約名稱
2、create 參數:
①發行人賬戶名稱
②空格前 :創造代幣的數量, 小數點代表精度爲N位
空格後 :代幣的符號
3、-p eosio.token@active
結果:
executed transaction: fb1dddcc499bde5dcce11079b64c9c137d395841baa542c373d35015e5a2c0a9 120 bytes 827 us
# coffeeandice <= coffeeandice::create {"issuer":"coffeeandice","maximum_supply":"1000000000.0000 LG"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
6、發行代幣 (issue)
執行了“內聯轉移”,“內聯轉移”通知了發件人和收件人帳戶,輸出指示被調用的所有操作處理程序、調用它們的順序以及操作是否生成任何輸出。
cleos push action coffeeandice issue '[ "alice", "100.0000 LG", "hello" ]' -p coffeeandice@active
1、要檢查交易,請嘗試使用-d -j選項,它們表示“不要廣播”和“將交易返回爲json”,這在開發過程中可能會有用。
2、hello可以隨意填寫
結果:
executed transaction: ade4e7a44ca7ca223395a2ddb6b9328c1739df1bef9f5d4edac04bbd3b7bbf72 128 bytes 861 us
# coffeeandice <= coffeeandice::issue {"to":"alice","quantity":"100.0000 LG","memo":"hello"}
# coffeeandice <= coffeeandice::transfer {"from":"coffeeandice","to":"alice","quantity":"100.0000 LG","memo":"hello"}
# alice <= coffeeandice::transfer {"from":"coffeeandice","to":"alice","quantity":"100.0000 LG","memo":"hello"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
7、轉移代幣(transfer)
cleos push action coffeeandice transfer '[ "alice", "bob", "25.0000 LG", "demos" ]' -p alice@active
結果:
executed transaction: 80a7544ec9fe70c61b79fce6407d97f1e255814cc30be2e11d085ff0c51cae91 136 bytes 575 us # coffeeandice <= coffeeandice::transfer
{"from":"alice","to":"bob","quantity":"25.0000 LG","memo":"demos"}
# alice <= coffeeandice::transfer {"from":"alice","to":"bob","quantity":"25.0000 LG","memo":"demos"}
# bob <= coffeeandice::transfer {"from":"alice","to":"bob","quantity":"25.0000 LG","memo":"demos"}
8、查看餘額
cleos get currency balance coffeeandice bob LG
返回結果:
25.0000 LG