DEV Community

Cover image for Fetching an Ethereum Contract's ABI
Emmanuel Akanji
Emmanuel Akanji

Posted on

Fetching an Ethereum Contract's ABI

Introduction

With me being relatively new to the blockchain space and all.

I encountered an issue while trying to work on a bot recently as I was to get the smart contract ABI(Application Binary Interface) to interact with the contract itself and what I saw online required me to have the source code, paste it on remix, and recompile and all. And all that was a whole lot of stress since I'm still relatively new to solidity, armed with python skills and my background as a data person, I just had to find a way out.

Upon further research and with my experience working with Algorand Indexer on algorand blockchain. I decided to look up etherscan for their open API endpoints and found something helpful.

And since every information about the blockchain including all the smart contract information can be accessed by every one, I used etherscans API endpoint to interact with it.

I wrote a simple script that allows a person to fetch the contract ABI from the contract address and append it to a json file.

What is an ABI?

ABI stands for Application Binary Interface, which is an interface between two program modules, of which one of the modules is at the level of machine code. The ABI serves as a medium for which data is encoded or decoded into/out of the machine code.

It's relation to EVM(Ethereum Virtual Machine) and Smart contracts.

The major component of the Ethereum network is the EVM, which allows for smart contracts stored on the Ethereum blockchain to be executed.
This smart contracts are often written in high-level languages and need to be compiled to EVM executable bytecode, such that when a smartcontract is deployed, the high-level code is compiled into EVM executable bytecode which is them stored on the blockchain with an associated address.
The ABI helps to specify which function in the binary smart contract deployed on the EVM to call, and guarantees that the function returns data in the expected format, which is very much similar to how an API(Application Program Interface) works but on a much lower-level.

You can check out the repository on github.

  1. clone the project from github using:
git clone https://github.com/manny-uncharted/fetch_contract_abi.git
Enter fullscreen mode Exit fullscreen mode

Enter into the project directory

2. cd fetch_contract_abi
Enter fullscreen mode Exit fullscreen mode
  1. Run the file
python abi_fetch.py
Enter fullscreen mode Exit fullscreen mode

It would require you to enter the smart contract address (You can get the contract address from etherscan.io) and then saves the contract ABI in json, with the name abi.json in the project directory.

Image description

Note: This program was written to only work for ERC-721 smart contracts, I would further update the code to work for other networks and other token types.

Top comments (0)