Understanding the ABI of Ethereum smart contracts.

Application Binary Interface (ABI) have become vital in the development of applications using smart contracts, on Blockchain systems like Ethereum.

Before web3, traditional software development used API’s (Application Program Interface) in passing data between applications and servers. Servers act as the central source of truth that feed data to application through request (GET,POST etc).

The blockchain, as we all know, is decentralized and thus, have no central source of information that the traditional software development have (APIs). The nodes in the blockchain functions as the server and applications outside and inside the blockchain have to find a way to communicate with one another. This is where ABIs come into play.

Application Binary Interface deep dive

To fully understand ABI’s, we have to shed light a bit into what smart contracts and the ethereum virtual machines (EVM) are.

EVM is a virtual machine and is a core component of the ethereum network. Smart contracts are simply programs or code executed and stored on a blockchain (EVM). Most smart contracts are developed in a high-level programming language with solidity currently the most popular.

The EVM doesn’t understand high level programming languages. To cope with this setback, the EVM stores the smart contract as executable bytecode. Communicating between bytecode is huge task given that the bytecode isn’t human readable. With EVM, we can't send a request in JSON format to a smart contract like APIs do and expect a response since a contract only interacts in bytecode.

To translate these requests into a language the EVM understands, the information is encoded through ABI encoding. These encodings include variable declarations and function signatures to make sure that the EVM knows exactly which function to execute within the smart contract.

Getting the ABI of a smart contract

The ABI is a JSON format file that describes the deployed contract and its functions. Generating the ABI is done automatically when you’re using an integrated development environment (IDE) like Remix.

Getting the ABI of a smart contract with remix can be done with the following steps:

  1. Open Remix.
  2. Remix comes with custom smart contracts. Open the contract folder and open the “1_Storage.sol” file.
  3. Click on the solidity compiler icon left of the screen. Click on the compile button.
  4. The smart contract should compile successfully and the ABI button appear below the compile button. Clicking on the ABI button automatically copies the ABI.

You can also manually create the ABI by using the Solidity Compiler package. After installing the package, you can run the solcjs ${contractname}.sol --abi command in a terminal. This will create an .abi file with it’s content in a JSON format.

NOTE: For manual generation of ABIs, make sure you have nodejs and visual studio code installed.

The ABI of the 1_Storage.sol, explained below will always be the same so far there is no new code or function added.

    {
        "inputs": [],
        "name": "retrieve",  // NAME OF THE FUNCTION
        "outputs": [         // THE OUTPUTS OF THE FUNCTION
            {
                "internalType": "uint256",
                "name": "",   // NAME OF THE PARAMETERS
                "type": "uint256"  // TYPE OF PARAMETER E.G UINT, INT, BOOL
            }
        ],
        "stateMutability": "view", // WHETHER A FUNCTION IS MUTABLE. THIS FUNCTION IS VIEW ONLY
        "type": "function"          // DEFINES THE TYPE OF FUNCTION E.G. FUNCTION, CONSTRUCTOR, RECEIVE
    },
    {
        "inputs": [            // THE INPUTS OF THE FUNCTION
            {
                "internalType": "uint256",
                "name": "num",  // NAME OF THE PARAMETERS
                "type": "uint256"  // TYPE OF PARAMETER E.G UINT, INT, BOOL
            }
        ],
        "name": "store",      // NAME OF THE FUNCTION
        "outputs": [],
        "stateMutability": "nonpayable",  // THE FUNCTION RECEIVES NO ETHER OR ISNT PAYABLE
        "type": "function"   // DEFINES THE TYPE OF FUNCTION E.G. FUNCTION, CONSTRUCTOR, RECEIVE
    }
]

The comment ("//") explains what the each key and value of the ABI means.

Conclusion

ABIs are vital in the usability of smart contracts. Now that you have learnt what an ABI is, the next step is to apply this knowledge and start creating smart contracts. WAGMI.