ABI
What is ABI
ABI stands for Application Binary Interface. It is an interface file that describes the data exchange format between a smart contract and an upper-layer application. The ABI file format is similar to JSON and is easily readable, facilitating the collaboration between smart contract engineers and upper-layer application engineers.
The smart contract ABI file consists of five parts:
1 | { |
We will explore the development approach for DMC smart contract ABI in the following order: actions, tables, structs, and types. actions
-> tables
-> structs
-> types
ABI development
actions
The purpose of the action section is to declare which actions can be called in the smart contract. Here is an example:
1 | "actions": [{ |
In each item, the name
represents the name of the action, the type
is used to look up the data structure in structs
, and ricardian_contract
refers to the Ricardian contract.
A Ricardian contract is a special structured text used to explicitly define the intentions of both parties in a transaction. On DMC, every action you send can be accompanied by a contract. This contract has a specific format that can be read by programs and is also human-readable. This type of contract is called a Ricardian contract.
Note: Currently, the naming convention for names in actions is limited to the following constraints: only supports digits 1 to 5 and lowercase English letters, and the maximum character length is 12.
If there are any changes in the future, we will promptly update the documentation.
tables
The tables
section lists the names of the data tables that need to be created in the smart contract, along with the names of the structs stored in each data table.
1 | "tables": [{ |
The above code constructs a data table named “players” with a struct type “player”. The primary key of the table is named “id” and has a type of “int64”.
structs
The content in the structs
section corresponds to the content in the actions
section. In the previous actions
section, we declared the names of actions, and now we need to declare the parameters required for each action in the structs
section. It is shown below.
1 | "structs": [{ |
1 | "structs": [{ |
Inheriting through the base
field is equivalent to:1
2
3
4
5
6
7
8
9
10
11"structs": [{
"name": "colorshape",
"base": "",
"fields": [{
"name": "area",
"type": "flout64"
},{
"name": "color",
"type": "int64"
}]
}]
DMC system will search for the corresponding data structure in the structs
section based on the type
declared in the actions
section. Each data structure in the structs
section lists the names and types of each parameter in its fields
.
In addition to the items in the actions
section, detailed data structures also need to be listed in the structs
section for the items in the tables
section.
1 | "structs": [{ |
In this way, in the structs
section, we have defined a struct named player
that lists the two fields nickname
and age
for the player
table. The types of these fields are my_account_name
and int32
, respectively.
types
types
is used to define custom data types:
1 | { |
In this ABI file, we have defined a custom data type named my_account_name
of type name
using the new_type_name
and type
keywords. The type name
is a system-defined data type.
Conclusion
A complete ABI file has been written.
1 | { |
With this ABI file, we have defined a data table named player
with three fields: id
, nickname
, and age
, with types int64
, my_account_name
, and int32
respectively. The primary key is id
. We have also defined an action method named hi
that takes a parameter named nickname
of type my_account_name
.