-Ethereum cat contract ABI-


When developing a DApp to call the Ethereum smart contract on the blockchain, the ABI of the smart contract is required. In this article, I hope to know more about ABI, such as why ABI is needed? How to interpret Ethereum’s smart contract ABI? And how to obtain the ABI of a smart contract?

ABI(Application Binary Interface)


It's easy to understand the ABI if you understand the API. Simply put, API is an interface for interaction between programs. This interface contains functions, variables, etc. that the program provides external access to. ABI is also an interface for interaction between programs, but the program is compiled binary code. So the same interface, but the data is transferred in binary format. Therefore, ABI describes how to decode/encode binary data passed between programs. The following figure takes Linux as an example to describe the relationship between API, ABI and programs in Linux.




Compile and deploy smart contracts


It is said that before Ethereum smart contracts can be used by everyone, they must first be deployed on the blockchain. From the source code of a smart contract to using a smart contract, there are probably several steps:

  1. Write the source code of smart contracts (usually written in Solidity)

  2. Compile the source code of the smart contract into bytecode (binary code) that can be executed on the EVM. At the same time, the ABI of the smart contract can be obtained through compilation.

  3. Deploying a smart contract actually stores the bytecode on the chain (through a transaction) and obtains an address unique to this contract.

  4. If you want to write a program to call this smart contract, you need to send the data to the address of this contract (also through a transaction). The Ethereum node will select which function in the contract to execute and the parameters to be input based on the input data.

And how do we know what functions this smart contract provides and what parameters should be passed in? This information is recorded in the ABI of the smart contract.

Ethereum smart contract ABI


The Ethereum smart contract ABI is represented by an array, which contains several Functions or Events expressed in JSON format. According to the latest Solidity documentation:

Function


There are 7 fields:

  • name:a string, function name

  • type:a string,"function", "constructor", or "fallback"

  • inputs:an array, function input parameters, including:

    • name:a string, parameter name

    • type: a string, parameter data type (eg uint256)

    • components: an array, this field will only exist if the input parameter is tuple(struct) type. Describe the data type contained in the struct

  • outputs: an array, the return value of function, and inputs Use the same representation. If there is no return value, it can be ignored. The value is  []

  • payabletrue, if the function can receive Ether, the default is false

  • constanttrue, if the function will not rewrite the state of the blockchain, vice versa false

  • stateMutability: a string whose value may be one of the following: "pure" (cannot read and write the blockchain status), "view" (can read the blockchain status, but will not rewrite the blockchain status), "payable" and "nonpayable" (will rewrite the blockchain status, and if Ether can be received, it is "payable", otherwise it is "nonpayable")

If you look carefully you will find payable and constant The content described in these two fields seems to be included in stateMutability middle.

This is indeed the case, in Solidity v0.4.16 constant The key words of this modified function are divided into view(neither reads from nor writes to the state)和 pure(does not modify the state), and starting from v0.4.17, Type Checker will force checking.constant Instead, it is only used to modify variables that cannot be modified. and added in ABI  stateMutability This field indicates uniformly,payable and constant Currently retained for backward compatibility. For detailed content and discussion of this update, please refer to: Introduce a real constant keyword and rename the current behavior #992.

Event


There are 4 fields:

  • name: a string, the name of the event

  • type: a string,always "event"

  • inputs: an array, input parameters, including:

    • name: a string, parameter name

    • type: a string, parameter data type (eg uint256)

    • components: an array, this field will only exist if the input parameter is tuple(struct) type. Describe the data type contained in the struct

    • indexedtrue, if this parameter is declared indexed (stored in the topics of the log), otherwise false(Stored in the data of log)

  • anonymoustrue, if the event is declared anonymous

Updating the smart contract status requires sending a transaction, and the transaction needs to wait for verification, so updating the contract status is asynchronous and the return value cannot be obtained immediately. Using Event, you can record relevant information to Log after the status is successfully updated, and let the DApp or any user interface listening to this Event receive notification. Each transaction has a corresponding Log.

So to put it simply, Event can be used to: 1. Get the return value after the function updates the contract status 2. It can also be used as additional storage space for the contract.

The parameters of Event are divided into: yes indexed, and others without indexed of. have  indexed The parameters can use filters. For example, for the same Event, I can choose to only listen to transactions sent from a specific address. The data of each Log is also divided into two parts: Topics (array with a maximum length of 4) and Data. Indexed parameters will be stored in Log's Topics, and other parameters will be stored in Data. If declared as  anonymous, Topics[0] in the following example will not be generated, and its value is the hash of the Event signature, which is used as the ID of this Event.


-event Set(address indexed _from, uint value)-

To learn more about Solidity Event, please refer to Solidity's event event (21) | Getting Started Series, Solidity's event support guide (22) | Getting Started Series.

Take a simple smart contract as an example


This smart contract contains:

  • data: A rewriteable state variable will automatically generate a read-only state variable. data() function

  • set(): a rewrite data value function

  • Set(): one at each update data Record log events

Smart contract Source Code:
pragma solidity ^0.4.20;contract SimpleStorage {    uint public data;    event Set(address indexed _from, uint value);    function set(uint x) public {        data = x;        Set(msg.sender, x);    }}
Smart contract ABI:
[{        "constant": true,        "inputs": [],        "name": "data",        "outputs": [{"name": "","type": "uint256"}],        "payable": false,        "stateMutabㄒility": "view",        "type": "function"    },    {        "anonymous": false,        "inputs": [{"indexed": true,"name": "_from","type": "address"},{"indexed": false,"name": "value","type": "uint256"}],        "name": "Set",        "type": "event"    },    {        "constant": false,        "inputs": [{"name": "x","type": "uint256"}],        "name": "set",        "outputs": [],        "payable": false,        "stateMutability": "nonpayable",        "type": "function"}]

Obtain Ethereum smart contract ABI

Solidity Compiler


You can use Solidity Compiler to obtain the contract ABI. I use the JavaScript version of Compiler as an example.

Install:
npm install solc -g
Get the contract ABI:
solcjs simpleStorage.sol --abi
A simpleStorage_sol_SimpleStorage.abi file will be generated, which contains the contract ABI content.

You can also get the contract binary code:
solcjs your_contract.sol --bin

Remix


You can also use Solidity Compiler or Remix. The complete ABI can be seen in the details of the contract. The Compiler version can be specified in Settings.


-Remix-

Etherscan


Many well-known contracts will put the contract source code on Etherscan for verification, and you can see the contract ABI at the same time.


-Etherscan-

In addition, an API is provided to obtain the verified contract ABI.

References


  • Latest Solidity Document

  • Ethereum contract Application Binary Interface(Video)

  • What is an application binary interface (ABI)?

  • What is an ABI and why is it needed to interact with contracts?

  • Writing smart contracts with Solidity and precautions (2) — Introduction to how to use Events

Related kits


  • https://github.com/ethereum/solc-js

  • https://github.com/ethereumjs/ethereumjs-abi

  • https://github.com/ConsenSys/abi-decoder


Original link: https://www.jianshu.com/p/fb1bd44f7b9b
Author: Anderson_Anderson

This article was first published in the author's brief book, and the author authorized EthFans to reprint it.


Ethereum smart contract development notes series:

Tutorial | [Ethereum Smart Contract Development Notes] Using Infura and web3.js to call the contract

Tutorial | [Ethereum Smart Contract Development Notes] The first approach to compiling and deploying contracts: using Remix



You might also like:

Tutorial | A brief introduction to Ethereum programming (2)

Introduction | The life cycle of Ethereum transactions

Dry information | You might as well start with this article to learn the tools and technologies in the Ethereum ecosystem


The following is a questionnaire survey on "Current Status and Challenges of Smart Contract Development",

We always believe that

Every enthusiastic investment,

will make the world a little better,

Looking forward to your participation ~

Please directly click "Read the original text" in the lower left corner to participate in the survey