What is a Smart Contract ABI? Full Guide

Table of Contents

Do you want to create your first decentralized application (dapp)? Or perhaps a Web3 game? If so, knowing what a contract ABI is and how to use it is rather important. Of course, there are some dapps that may not require this aspect. However, once you start incorporating either fungible or non-fungible tokens (NFTs), you can’t work around it. Moreover, perhaps you’ve already taken on some of our example projects and copy-pasted a particular contract ABI without really knowing what exactly it did. Well, today we will clear things up and ensure that you all know what this smart contracts’ property is all about. 

First of all, we will first explain what a smart contract ABI is. Then, we will tell you why ABIs are important, and how they fit into blockchain programming. In addition, we will also look at how to use ABIs. As such, we will show you how to obtain any smart contract ABI. Luckily, you have several options to do this. Hence, you can go with the one that best suits you and your project. Nonetheless, we will also give you a chance to put the knowledge obtained herein into practice. We will share with you some example projects, where you need to use a smart contract ABI to implement the full functionality of example dapps. If that’s something you are interested in, make sure to create your free Moralis account and follow our lead.    

What is a Smart Contract ABI?

First, let’s look at what ABI stands for – specifically, Application Binary Interface. Basically, a smart contract ABI serves as an agent that enables a Web3 contract to communicate and interact with external apps and other smart contracts. And, we all know that receiving data from external sources can often be critical to cater to the users and completing the applications’ goals. 

Contract ABI Background

ABIs do not exist on their own. As such, it’s important that you have a basic understanding of the tech that brings them to life. Without smart contracts, there’d be no contract ABIs. Moreover, smart contracts are essentially pieces of software that live on blockchains. In the case of Ethereum and other EVM-compatible chains, Web3 contracts are the core applications. They are programmed to ensure that things run according to certain rules. As such, they execute predefined actions when specific conditions are met. Furthermore, it’s important to note that these conditions can be on-chain or off-chain events. 

When it comes to EVM (Ethereum Virtual Machine), devs create smart contracts using the Solidity programming language. And, while Solidity is a high-level language, the contracts get stored on the EVM as executable bytecode. The latter is in binary format, which is not human-readable. Thus, the bytecode requires a proper interpretation in order to make sense of it – enters contract ABI. As its full name suggests, the application binary interface serves those writing smart contracts. ABIs enable them to communicate between the bytecode that the EVM understands and web apps written in high-level languages (e.g.: JavaScript).  

API vs. ABI

Since you are probably more familiar with Web2, it makes sense to draw a comparison between APIs and ABIs. As you may know, in Web2, communication about data between apps and servers happens through APIs. That’s possible because servers act as centralized sources of information. Hence, they easily feed data to applications by request. However, the core property of blockchains is decentralization. Thus, there’s no centralized source of data. Instead, there are nodes that act as servers. Moreover, there are also smart contracts, which are on-chain programs. You can also view them as on-chain “hosted” functions. So, a contract ABI enables off-chain apps and other smart contracts to communicate with that contract. You’ll likely be familiar with Moralis’ most popular APIs, like the NFT API and our Auth API.

In a way, ABIs are APIs’ cousins. All-in-all, a contract ABI acts as a function selector. It defines the specific method that an app or a dapp can call. Then, the smart contract in question responds to that call by executing a specific function. Furthermore, a generated JSON RPC file lists these specific methods and the data types connected to them. The image below clearly displays how the details of a smart contract are converted into a contract ABI:

Contract ABI – a Bytecode “Translator”

As explained above, smart contracts only communicate in bytecode. Hence, unlike with an API, we can’t send a request directly in JSON format. The latter must be translated (encoded) into the language that the EVM understands. And, this is done via ABI encoding. These “translations” include all the details, including function signatures and variable declarations. Thus, the EVM knows exactly which function to execute within the given Web3 contract.   

It goes without saying that the responses are also in the bytecode. As such, interpretation needs to take place before a response can be processed by a web app. Moreover, since the response is in bytecode, we can always expect a certain structure when calling a contract’s function. This is quite an advantage. 

Now that you know what a contract ABI is, let us explain how to use it.

Using a Contract ABI

When it comes to using a smart contract ABI, there are three aspects that you need to understand:

  • Generating smart contract ABIs
  • Executing smart contract ABIs
  • Encoding smart contract ABIs

When it comes to generating contract ABIs, you have several reputable options, depending on which tools you use. If you use Hardhat or Remix to deploy a smart contract, these tools will automatically generate that contract’s ABI. However, you can go about generating contract ABIs manually. In that case, you need to install the Solidity Compiler NPM package. With the package in place, you can use your terminal and enter the “solcjscontractname.sol –abi” command. If things go smoothly, the latter will generate an ABI file.

By now you know that a contract ABI serves as the interpreter between the bytecode and high-level languages. Moreover, you also know that by executing an ABI you get to execute the functions of the corresponding smart contract. However, in order to execute a smart contract ABI, you also need that smart contract’s address. Luckily, the above-mentioned tools provide you with that piece of information as well. Moreover, once you have a smart contract’s address, you can easily use blockchain explorers to find other details. That includes a contract ABI.

Encoding Smart Contract ABIs

Encoding is another aspect where popular compilers like Remix can help you out. Moreover, these sorts of encodings follow a certain pattern. If you are interested in diving deeper into encoding, we recommend exploring the ABI specification. The latter is available in the Solidity documentation. For instance, the first four bytes are the function signature. This indicates the type of function that is being executed in the smart contract. A popular example of a function identifier is “a9059cbb” (an ERC20 transfer). For more details, you can explore a database directory of function signatures. 

Following the initial four bytes, the encoded bytecode stores the arguments of the function. Furthermore, the smart contract responses for a similar structure. However, in that case, there is no function signature.

Getting Smart Contrat ABIs

We’ve mentioned above that you can get an ABI at the time of deploying a smart contract when using Remix or Hardhat. On the other hand, you can also use the smart contract’s address or the name of the project and search based on these details using blockchain explorers. In this section, we want to show you where exactly you can locate a contract ABI for each of these methods.

Using Remix

When using Remix, you must first type or paste your smart contract into a new “.sol” file. Next, you will need to compile your contract:

Once you successfully compile your smart contract, you can deploy. Moreover, when deploying you have several options; the image below shows the most common settings:

After successfully deploying your contract, you get to copy its ABI. To do this, you need to go back to the “Solidity Compiler” tab and scroll down. There, you’ll see the copy icon and “ABI”:

So, just click the above-marked area and you will have your smart contract’s ABI in your clipboard.

Using Hardhat

Unlike Remix, which is available online, you need to install Hardhat locally. As such, there are some initial setups that you need to go through first. However, once you create a Hardhat project, you will get the template files you need. Next, you need to tweak those files to match your project and your smart contract. Essentially, you want to have a smart contract file, a config file, and a deployment file:

With these files ready, you deploy the contract using the terminal. And, as soon as the deployment is completed, your terminal will provide you with your contract’s address and ABI:

Note: As you can see in the screenshot above, the terminal provides you with a contract ABI in the “string” format. So, in instances where the “object” format is required, you will need to convert it either annually or using online tools. 

Using Blockchain Explorers

Using blockchain explorers to obtain contract ABIs is particularly practical in cases when you are using existing smart contracts. Luckily, every popular programmable blockchain has its explorer for its mainnet and testnet (e.g.: Etherscan, Polygonscan, Bscscan, Snowtrace, etc.).

So, let’s look at a popular NFT project CryptoPunks. Since the project is on Ethereum, we need to use Etherscan. We can use the search bar and type in the name of the project. However, if we had the project’s contract address, we could paste it into the search bar. Next, we need to click on the contract address of CryptoPunks:

On the next page, we have to scroll down a bit, until we see a horizontal option bar starting with “Transactions”. The “Contract” tab is the one we need to focus on:


Once on the “Contract” tab, we need to ensure that we are in the “Code” section:

If so, we can scroll down until we see “Contract ABI”. On the right-hand side we can use the copy icon to copy the contract ABI:

Learn to Use Contract ABIs – Example Projects 

As promised in the introduction, we encourage you to take action and learn how to use contract ABIs. As such, you will put the above-presented methods of obtaining this detail to use. Moreover, this is also the best way to learn how to use Moralis to create killer dapps and Web3 games. So, here are some example projects that we recommend tackling:

  • Convert an NFT to an in-game item – This is a Unity Web3 project, where you will learn how to use the Moralis’ Unity Web3 SDK. Moreover, you’ll also learn how to use NFTs as in-game power-ups. Along the way, you will also obtain a smart contract ABI to paste it into Unity: 
  • Sync smart contract Web3 events – In this project, you will learn how to use Moralis to sync and index smart contract events. Along the way, you will also learn how to work with Etherscan to obtain an ABI and other smart contract details: 

Nonetheless, below you can find a video tutorial about calling smart contracts in Unity. This is the best place to learn how to use Hardhat to get your contracts’ ABIs. Moreover, in this video, you can also see an example of ABI in the “string” format as well as in the “object” format.

What is a Smart Contract ABI? Full Guide – Summary

We’ve covered quite a distance in today’s article. First, you learned about the basics of a smart contract ABI. This is also where we covered the gist of smart contracts. Then, knowing that ABIs are essentially interpreters that enable communication between the bytecode and the high-level languages, you had a chance to find out how to use ABIs. Moreover, we also showed you where and how to get smart contract details using different Web3 development tools. Last but not least, you had an opportunity to take on some example projects to put the theory into practice. This is also where you were able to experience the power of Moralis first-hand. 

Furthermore, if you are interested in exploring other blockchain development topics or taking on additional example projects, we encourage you to visit Moralis’ Web3 blog and Moralis’ Web3 YouTube channel. These two outlets can help you become a Web3 developer for free. However, if you want to go full-time crypto sooner rather than later, then Moralis Academy might be the place for you. Also, if you’re looking to learn NFT secrets, be sure to learn from seasoned NFT coders – e.g. learning about practical NFT use cases or how to stake NFTs!

July 24, 2022
Join the Moralis Community
Get the latest Moralis Web3 news and updates via email.
Search
Generic filters
Related Articles
Become a Web3 Expert
Subscribe to our newsletter and get our E-book Web3 Speed run for FREE