What is a contract ABI?

overview

ABI is one of the essential components when interacting with smart contracts. In this article, let us understand what the ABI of a smart contract is.

solidity33333

What are ABIs?

ABI (Application Binary Interface) in the context of computer science is an interface between two program modules, usually between an operating system and a user program.

EVM (Ethereum Virtual Machine) is the core component of the Ethereum network. Smart contracts are code fragments stored on the Ethereum blockchain and executed on the EVM. Smart contracts written in high-level languages ​​like Solidity or Vyper need to be compiled into EVM executable bytecode; when a smart contract is deployed, this bytecode is stored on the blockchain and associated with an address. For Ethereum and the EVM, a smart contract is this series of bytecodes. To access a function defined in a high-level language, the user needs to convert the name and arguments to a byte representation so that the bytecode can use it. In order to interpret the bytes sent in the response, the user needs to convert back to a return value tuple defined in a high-level language. Languages ​​compiled for the EVM maintain strict conventions for these conversions, but in order to perform them, the precise names and types associated with the operations must be known. The ABI documents these names and types in an accurate, easy-to-parse format, translating between contrived method calls and discoverable and reliable smart contract operations.

It is very similar to an API (Application Programming Interface), which is a human-readable representation of a code interface. An ABI defines methods and structures for interacting with binary contracts, just like an API, but at a lower level. The ABI instructs the caller of a function to encode the required information, such as function signatures and variable declarations, in a format that the EVM can understand to call the function in bytecode; this is called ABI encoding. ABI encoding is mostly automated, handled by compilers like REMIX or wallets that interact with the blockchain. Contract ABIs are represented in JSON format. There are clear specifications on how to encode and decode contract ABIs.

Understanding the elements of the ABI

The JSON format of a contract's ABI is given by various function and/or event descriptions.

The following elements are present in the ABI description of a function:

type : Defines the type of the function. It can be one of the following, "function", "constructor", "receive" (for the receive ether function ), or "fallback" (for the default function ).

name : Define the name of the function.

inputs : it is an array of objects defining parameters; each object has:

name : Defines the name of the parameter.

type : defines the canonical type of the parameter. For example, uint256.

components : Used to define the tuple type, if a tuple type is reached, it is expressed as type = tuple [other attributes of the tuple element, such as name, type].

outputs : It is an array of output objects similar to the input.

stateMutability : Defines the mutability of the function. It can be one of the following values: ' pure ' (specify that the blockchain state is not read or written), ' view ' (specify when the blockchain state is read, but cannot be modified), 'nonpayable' (which is The default mutability, which doesn't need to be mentioned when writing functions in code, means the function doesn't accept Ether; using it we can read and write the blockchain state), 'payable' (mentioned means the function accepts Ether Ether and can read/write blockchain state).

Note : The name and output fields of the ABI for constructors and fallback functions are empty; even the input field for fallback functions is empty.
The following elements are present in the ABI description of an event:

type : Here, it is always "event".

name : Defines the name of the event.

inputs : it is an array of objects defining parameters; each object has:

name : Defines the name of the parameter.

type : defines the canonical type of the parameter. For example, uint256.

components : Used to define the tuple type, if a tuple type is reached, it is expressed as type = tuple [other attributes of the tuple element, such as name, type].

indexed : "true" if the field is part of the log's topic, "false" if the field is one of the log's data segments.

anonymous : This field is true if the event is declared anonymous in the contract code.

How to get/generate ABI?

One of the most common ways is to copy the ABI using the ABI button under the compile tab of the Ethereum REMIX IDE after the smart contract has been compiled .

Ki55ul0CgbS51yrAfvOd7xIcX45CWFoRNcI-I1LA3Hg59kLorwgZXY-QYml1rQsSoUZDvFPSi_0ykA6kuQYf3n9WY3E5bNSAbP21rvt2c6WkNJ

Another approach is to compile and generate the ABI using solc, which provides JavaScript bindings for the Solidity Compiler . To install solc we need to use npm which comes with node.js. Check if node.js is installed on your system.

$ node -v

If not installed, you can download the LTS version of NodeJS from the official website .

Now let's install solc

$ npm install solc

We will compile and generate an ABI for the following contract test.sol, a contract that increments variable values:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;

contract test {
    
    uint256 private count = 0;

    function increment() public {
        count += 1;
    }
    
    function getCount() public view returns (uint256) {
        return count;
    }

}

Explanation of the above code:
Line 1: Specifies the SPDX license type, which was added after Solidity ^0.6.8; whenever the source code of the smart contract is open to the public, these licenses can help solve/avoid copyright issues. If you don't want to specify any license type, you can use the special value UNLICENSED or simply skip the entire comment (doesn't cause an error, just a warning).

Line 2: Declare the Solidity version.

Line 4: Start our contract name test.

Line 6: Declare a private variable called count of type unsigned integer and assign it the value zero.

Lines 8-10: Declare a public function increment that increments the value of count by one when called.

Lines 12-14: Declare a public function getCount that returns the value of count as an unsigned integer.

Now, let's get the ABI of the above contract.

$ solcjs test.sol --abi

A file called test_sol_test.abi will be created in the same directory; it will have the ABI in JSON format as follows:

[
	{
    
    
		"inputs": [],
		"name": "getCount",
		"outputs": [
			{
    
    
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
    
    
		"inputs": [],
		"name": "increment",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
]

in conclusion

Now that you know what an ABI is, learn more about Solidity , Vyper smart contracts, and create your smart contracts. Learn more about the ABI specification from the Solidity documentation .

Original link: https://www.quicknode.com/guides/solidity/what-is-an-abi

Guess you like

Origin blog.csdn.net/hanru723/article/details/125736510