How to get an ABI json file from contract code and address in Solidity?

import Image from β€˜@components/Image.astro’

This tutorial demonstrates how to generate an Application Binary Interface (ABI) JSON file for a given contract source code.

The Application Binary Interface is an interface that encapsulates the functions and arguments of a contract in JSON format. It serves the purpose of encoding and decoding data to and from blockchain transactions.

Generate ABI JSON file using local solc compiler

If the Solidity compiler is installed on your local machine and the solc command is operational, type the following command to obtain the ABI file. To install the solc compiler:

  • Ensure that NodeJS is installed on your machine.
  • Next, install the solcπŸ”— package, which is a JavaScript binding for Solidity.
  • Once the installation of the npm library is complete, the solc command will work.
solc contract.sol --abi

Use --abi option to the command line to get generated ABI code. This generates an ABI JSON file. Also, the --bin option is provided to deploy the contract.

How to get the ABI file in Remix IDE

The following are steps to get an ABI JSON file for a given smart contract.

  • Go to https://remix.ethereum.org/ and create a contract source code

  • Go to the Solidity Compiler tab and select ABI from the top-bottom tab as the given screenshot. How to get ABI json file in REMIX solidity ide editor

  • On click on ABI and copy it to the clipboard

[
  {
    "inputs": [],
    "name": "addEmployee",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  }
]

How to get an ABI json file for a given contract address?

Another way to obtain the ABI JSON file for a specific contract address is as follows:

  • Navigate to Ether ScanπŸ”—.
  • Enter the contract address to access the contract details.
  • Go to the Contract tab and select Code.
  • Within the Code section, locate Contract ABI and copy it.

Conclusion

In summary, You learned multiple ways to get ABI JSON for a given contract source code or address.