EOSピットノート(3)スマート契約の例

7964 ワード

目次
 
プリセットの基本
一、Hello Eos
1、ディレクトリ保存の作成
2、実現した契約コードを作成してコンパイルする
3、知能契約を設定してテストする
4、契約その他の操作
二、貨幣を作る
1、契約ソースの取得
2、契約口座の作成(作成しなくてもよい)
3、契約書のコンパイル
4、配置契約
5、トークンの作成
6、発行コイン(issue)
7、トランスミッションコイン(transfer)
8、残高の表示
プリセットの基本
プリセットディレクトリ:CONTROACTS_DIR =/eos/contracts
言語選択:公式のDemo,C++による
使用する財布:ロック解除を先に行います.そうしないと、次の操作はできません.
一、Hello Eos
1、ディレクトリ保存の作成
(あってもなくてもよいとはいえ、管理を容易にするためにディレクトリを作成して保存します)
以前に設定されたディレクトリCONTROACTS_DIR、次はディレクトリ'hello'を作成し、ディレクトリに入ります.
cd CONTRACTS_DIR
mkdir hello
cd hello

CONTRACTS_DIR          
        ,  

2、実現した契約コードを作成してコンパイルする
1、コードの作成
やはり直接コピーしましょう.
#include 
#include 

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 
#include 

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タイプであり、2つのデータから構成され、最大供給浮動小数点数と大文字文字のみの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