EOS開發入門

EOS開發入門



  在上一篇文章《EOS開發環境搭建》中,咱們已經完成了EOS開發環境的搭建,本次爲你們帶來的是EOS開發入門的相關內容。html

1. EOS的合約開發基礎

  智能合約是一種旨在以信息化方式傳播、驗證或執行合同的計算機協議。智能合約容許在沒有第三方的狀況下進行可信交易,這些交易可追蹤且不可逆轉。c++

1.1 所需知識

  • 熟悉或瞭解C++14的基本語法,這部分技能須要掌握,短期內沒法搞定。
  • 瞭解abi(The Application Binary Interface)文件,基於json格式的,描述合約的接口

1.2 c++與智能合約部分語法

  • require_auth——
    require_auth(name user): 驗證用戶是否有簽名
  • [[eosio::action]] action聲明的特殊標記
  • asset 是eos官方提供的一個結構體,內部主要有2個元素:amountsymbol
  • eosio_assert( expr, "errmsg" ) 驗證表達式是否位置,相似於以太坊的assert
  • [[eosio::table]] table聲明的特殊標記,在結構體聲明中使用
  • multi_index 是eos提供的一個數據庫存儲結構,能夠支持多個索引,一樣有增刪改查接口
struct [[eosio::table]] task {
         uint64_t    taskID;
         name        creator; 
         name        worker;
         asset       bonus;
         uint8_t     status = 0;
         string      remark;
         string      comment;

         uint64_t primary_key()const { return taskID; }
      };

      typedef eosio::multi_index< "tasks"_n, task > tasks;
      //tasks 是最終的表名字,task是結構體名字
  • multi_index可使用 findemplacemodifyearse四個接口,使用時須要先用tasks定義變量
tasks tk( _code, _code.value );

    tk.emplace( creator, [&](auto &t){
        t.creator = creator;
        t.worker  = worker;
        t.taskID  = tk.available_primary_key();//主鍵自動增加
        t.bonus   = taskBonus;
        t.remark  = memo;
    });

1.3 理解abi文件

  這是一個空的abi文件數據庫

{
   "version": "eosio::abi/1.0",
   "types": [],
   "structs": [],
   "actions": [],
   "tables": [],
   "ricardian_clauses": [],
   "abi_extensions": [],
   "___comment" : ""
}
  • types 新類型定義
  • structs 合約內結構體定義
  • actions 合約內動做定義,注意在合約中須要使用特殊的標記才能在abi中獲得
  • tables 合約內表結構定義,注意在合約中須要使用特殊的標記才能在abi中獲得
  • ricardian_clauses 李嘉圖條款
  • abi_extensions 擴展

  其中structsactionstables能夠認爲是abi文件的三要素,每一個合約內能夠有多個action,每一個action會執行這樣那樣的邏輯,須要藉助結構體的結構將信息存儲或變動保存在區塊鏈中,這個保存的數據咱們類比爲傳統數據庫中的表,也就是tables。咱們學習智能合約編寫主要要寫什麼?其實就是寫一個一個action,相似於rpc中的微服務。與傳統數據庫編程同樣,咱們一樣也是圍繞着table作增刪改查操做。編程

2. EOS的hello智能合約

2.1 編寫hello合約

  • 合約代碼以下
#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))

上例中hello是咱們的類名,須要繼承eos爲咱們提供的基類contract,[[eosio::action]]是一個特殊用法,定義一個action必須在函數聲明前加上此標記。hi這個函數就是一個 action,本例實現的就是一個打招呼的action,根據輸入的不一樣用戶進行打招呼。json

EOSIO_DISPATCH( hello, (hi)) 是生成action的關鍵,EOSIO_DISPATCH是eos提供的宏,hello顯然就是類名,(hi)就是要生成的action,若是多個action,採用相同格式在後面添加。函數

2.2 hello合約的部署和調用

  • 使用eosio-cpp編譯hello合約,這個就來自於cdt工具包
eosio-cpp -o hello.wasm hello.cpp --abigen

image

  • 建立hello合約帳戶
cleos create account eosio hello YOUR_PUBLIC_KEY -p eosio@active
  • 部署hello合約
cleos set contract hello ./hello -p hello@active

image

  • 調用hello合約
//先建立一個普通帳戶bob
cleos create account eosio bob YOUR_PUBLIC_KEY -p eosio@active
//bob調用hello合約的hi動做
cleos push action hello hi '["bob"]' -p bob@active

image

  到這一步,一個簡單的hello智能合約就部署並調用完成了,快來試試吧。下次將爲你們分享關於EOS開發實戰的內容,敬請關注。微服務




image

相關文章
相關標籤/搜索