ABI code

Interacting with smart contracts requires some additional components, one of them is an ABI code.

ABI code

Interacting with smart contracts requires some additional components, one of them is an ABI code.

It can looks like annoying, mysterious json file, which cause problems for every newbie.


Agenda:

  • intro,
  • What is it?
  • Detailed description,
  • Elements of ABI,
  • Conclusion

What is it?

ABI — application binary interface is an interface between two program modules.

It can be applied even between two operating systems.

In web3, abi provides names and types of high-level programming languages needed to be compiled by EVM.


Detailed description

Ethereum Virtual machine is the core of Ethereum network, it allows to interact with smart contracts, which are literally the piece of code, stored in blockchain. EVM compiles it and allows execution of included rules.

Smart contracts are written in high-level programming languages like Solidity, Rust, Vyper, GoLang. EVM needs to compile that, but it requires to change these piece of codes to byte code, so that compiler can understand that.

The problem is with interpretation of byte code returned in response. User has to convert it back to the tuple of return values defined in high level programming language.

Moreover, for performing above process, it is needed to define precise names and values associated with that operation…

That’s when ABI comes.


Elements of ABI

ABI code is formatted in JSON and includes elements listed below:

Description of function:

  • type:

Defines the type of function. It can be one of the following, function, constructor, receive, or fallback

  • name:

Defines the name of the function.

  • inputs:

It is an array of objects which defines parameters; each object has:

  • name:

Defines the name of the parameters.

  • type:

Defines the canonical types of the parameters. For example, uint256.

  • components:

Used to define tuple types, if a tuple type is reached, it is represented as type = tuple.

  • outputs:

It is an array of output objects similar to inputs.

  • stateMutability:

Defines the mutability of a function. It can be one of the following values: ‘pure’ (specified not to read or write blockchain state), ‘view’ (specified when blockchain state is to be read, but no modification can be done), ‘nonpayable’ (this is the default mutability and doesn’t need to be mentioned while writing a function in code, this means a function does not accept Ether; using this we can read and write blockchain state), ‘payable’ (mentioning this means a function accepts Ether and can read/write blockchain state).

Description of event:

  • type:

here, it’s always ‘event’.

  • name:

Defines the name of the event.

  • inputs:

It is an array of objects which defines parameters; each object has:

  • name:

Defines the name of the parameters.

  • type:

Defines the canonical types of the parameters. For example, uint256.

  • components:

Used to define tuple types, if a tuple type is reached, it is represented as type = tuple.

  • indexed:

This is ‘true’ if the field is part of the log’s topics and ‘false’ if it is one of the log’s data segments.

  • anonymous:

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


Conclusion

ABI code is crucial in smart contacts development, following above informations can speed up process of learning web3 and get developer to higher level. Create first smart contract and generate an ABI code to have the practical knowledge.

If you like my content, follow repo which contains all topics from the challenge:

GitHub - Kacper-Hernacki/100-days-of-web3-challenge-blockchain-free-materials: This is the repo…
This is the repo which consists 100 topics about blockchain/ decentralisation/ web3. There are links to my articles,…