1. 程式人生 > >EOS 智能合約案例解析(3)

EOS 智能合約案例解析(3)

EOS 智能合約

詳解 EOS 智能合約的 abi 文件

這次向大家介紹 eosio.token 智能合約的最後一個文件 —— abi文件。ABI 全稱 Application Binary Interface,中文名“應用程序二進制接口”,顧名思義是一個接口文件,描述了智能合約與上層應用之間的數據交換格式。abi 文件格式類似 JSON,具備很好的可讀性,有利於智能合約工程師與上層應用工程師之間的工作銜接。eosio.token.abi 文件地址: https://github.com/EOSIO/eos/blob/master/contracts/eosio.token/eosio.token.abi

EOS 智能合約 abi 文件由 5 部分組成:

{    "types":[...],              //定義類型的別名    "structs":[...],            //各個類型的數據結構    "actions":[...],            //智能合約的 action    "tables":[...],             //數據結構體    "ricardian_clauses":[...]   //李嘉圖條款
}

註:JSON 格式不支持註釋,上面的雙斜線大家理解就好。

我們將按照 actions -> structs

-> tables -> structs -> types -> ricardian_clauses 的順序了解 EOS 智能合約 abi 的開發方法。

actions

action 部分的作用是聲明智能合約有哪些可以調用的 action。如下所示。

  "actions": [{      "name": "transfer",      "type": "transfer",      "ricardian_contract": ""
    },{      "name": "issue",      "type": "issue",      "ricardian_contract": ""
    }, {      "name": "create",      "type": "create",      "ricardian_contract": ""
    }

  ]

其中每一項的 name 就是 action 的名字,type 用來在 structs 中查找數據結構。ricardian_contract 是李嘉圖合約,剛剛被加入到 EOS 智能合約中,官方還沒有進一步說明。

structs

剛才的只聲明了三個 action 的名稱,我們還要在 structs 裏聲明各個 action 需要傳入的參數,如下所示。

  "structs": [{      "name": "transfer",      "base": "",      "fields": [
        {"name":"from", "type":"account_name"},
        {"name":"to", "type":"account_name"},
        {"name":"quantity", "type":"asset"},
        {"name":"memo", "type":"string"}
      ]
    },{     "name": "create",     "base": "",     "fields": [
        {"name":"issuer", "type":"account_name"},
        {"name":"maximum_supply", "type":"asset"},
        {"name":"can_freeze", "type":"uint8"},
        {"name":"can_recall", "type":"uint8"},
        {"name":"can_whitelist", "type":"uint8"}
     ]
  },{     "name": "issue",     "base": "",     "fields": [
        {"name":"to", "type":"account_name"},
        {"name":"quantity", "type":"asset"},
        {"name":"memo", "type":"string"}
     ]
  }
  ]

EOS 系統會根據 actions 部分中聲明的 type ,在 structs 部分尋找對應的數據結構,每個數據結構的 fields 中,會列出每個參數的名稱和類型。

tables

tables 列出了 智能合約中需要建立的數據表名稱,以及數據表中所儲存的結構體名稱。

  "tables": [{      "name": "accounts",      "type": "account",      "index_type": "i64",      "key_names" : ["currency"],      "key_types" : ["uint64"]
    },{      "name": "stat",      "type": "currency_stats",      "index_type": "i64",      "key_names" : ["currency"],      "key_types" : ["uint64"]
    }
  ]

其中的 type 就是數據表中所儲存的結構體名稱。

structs

為什麽又回到 structs 了呢,因為不光是 action 裏的項目需要在 structs 裏列出詳細的數據結構,tables 中的項目也需要。

  "structs": [{      "name": "account",      "base": "",      "fields": [
        {"name":"balance", "type":"asset"},
        {"name":"frozen", "type":"uint8"},
        {"name":"whitelist", "type":"uint8"}
      ]
    },{      "name": "currency_stats",      "base": "",      "fields": [
        {"name":"supply", "type":"asset"},
        {"name":"max_supply", "type":"asset"},
        {"name":"issuer", "type":"account_name"},
        {"name":"can_freeze", "type":"uint8"},
        {"name":"can_recall", "type":"uint8"},
        {"name":"can_whitelist", "type":"uint8"},
        {"name":"is_frozen", "type":"uint8"},
        {"name":"enforce_whitelist", "type":"uint8"}
      ]
    }
  ]

types

types 部分用來建立類型的別名,比如你想給 account_name 類型建立一個別名:

"types": [{      "new_type_name": "account_name",      "type": "name"
    }
  ]

這樣在這個 abi 文件裏就可以用 name 來代替 account_name了。

ricardian_clauses

有關李嘉圖條款的部分 EOS 官方還在開發中。


EOS 智能合約案例解析(3)