July 24, 2022

What is a Smart Contract ABI? Full Guide

Table of Contents
What is a Smart Contract ABI? Full Guide

Smart contracts play pivotal roles within Web3, and they have already revolutionized multiple sectors! Typically written in a high-level programming language like Solidity, smart contracts must be compiled into bytecode (binary data) before being deployed to the blockchain. Unfortunately, we aren’t very good at interpreting and understanding bytecode, making it challenging to interact with smart contracts. Thankfully, this is where smart contract ABIs (application binary interfaces) enter the equation. But what is a smart contract ABI? Also, how do ABIs work? For the answers to these questions, join us in this article as we cover the intricacies of smart contract ABIs! 

Overview 

We’ll kickstart today’s article by diving into the ins and outs of smart contract ABIs. In doing so, we’ll explain what they are and why we need them. From there, we’ll also explore an actual smart contract ABI to show you an example of what they might look like. After this, we’ll briefly compare ABIs vs. APIs. Next, we’re going to show you how to generate and get the ABI of any smart contract. Lastly, for those interested in building decentralized applications (dapps), we’ll introduce you to Moralis – the industry’s premier Web3 API provider! 

In Moralis’ suite of Web3 APIs, you’ll find multiple APIs for different use cases. Some prominent examples include our free NFT API, the Wallet API, and many others. With these industry-leading tools, you can effortlessly get and integrate any on-chain data and Web3 functionality into your projects without breaking a sweat.

Graphic art illustration - Moralis and Smart Contract ABIs

Also, did you know that you can sign up with Moralis for free? So, if you haven’t already, create your account immediately, and you’ll get instant access to all our Web3 APIs! 

Now, without further ado, let’s jump straight into the ins and outs of smart contract ABIs! 

What is a Smart Contract ABI? 

A smart contract ABI is essentially an interface defining a set of rules for interacting with a particular smart contract on blockchain networks like Ethereum. These interfaces document available methods, specify formatting requirements for data, and provide instructions on the proper procedures for executing function calls when engaging with smart contracts!

Art illustration depicting the Ethereum logo on top of a smart contract ABI

But what exactly does this mean, and why do we need smart contract ABIs? To answer these questions, let’s briefly return to basics and explore the intricacies of smart contracts and what’s known as Ethereum Virtual Machine (EVM):

  • Smart Contracts: Smart contracts are programs stored on blockchain networks that execute when predetermined conditions are met. Moreover, they are typically written in high-level languages like Solidity. 
  • EVM: EVM is a piece of software responsible for executing smart contracts and computing the state of the Ethereum network. Also, the EVM can only read and process bytecode (binary data). 

Since smart contracts are written in high-level programming languages and EVM exclusively handles bytecode, there’s an apparent discrepancy between these two. This is why smart contracts are compiled into bytecode before they are deployed to the blockchain. However, as mentioned at the outset of this article, we aren’t very good at reading and understanding binary data. Therefore, we need a standardized way to translate high-level code into bytecode and vice versa to engage with smart contracts. Thankfully, this is where smart contract ABIs enter the equation.

ABIs define how to encode and decode data when interacting with smart contracts on the blockchain, facilitating the translations between human-readable method calls and smart contract operations. Essentially, they specify the methods and data structures used to interact with a particular binary contract!

Exploring the Components of a Smart Contract ABI 

Smart contract ABIs typically adhere to JSON format, and they comprise an array of various descriptions. Two common types are function and event descriptions, which we’ll take a closer look at in the following subsections to better understand what ABIs might look like! 

Function Descriptions

Here are the core properties of a function description: 

  • type: Defines the function type, and it can be one of the following: function, receiver, constructor, or fallback.
  • name: Defines the name of the function.
  • inputs: An array of objects, each of which has a name, type, and components
  • outputs: An array of output objects following the same structure as the inputs.
  • stateMutability: Specifies the mutability of a function. It can be either pure, view, nonpayable, or payable.

Event Descriptions

Here are the central properties of an event description:

  • type: For event descriptions, the type is always event
  • name: Specifies the name of the event.
  • inputs: An array of objects. Each object can have the following properties: name, type, components, and indexed.
  • anonymous: This field is true if an event was specified as anonymous in the contract code.

Smart Contract ABI Example 

To better understand how ABIs are structured, let’s explore what they can look like in practice. To do so, let’s use the following ”HelloWorld.sol” smart contract as an example: 

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

contract HelloWorld {
    string private message;


    constructor() {
        message = "Hello, World!";
    }
   
   function setMessage(string memory _message) public {
        message = _message;
   }

   function getMessage() public view returns (string memory) {
        return message;
   }
}

Here’s what the smart contract ABI looks like:

[
 {
   "inputs": [],
   "stateMutability": "nonpayable",
   "type": "constructor"
 },
 {
   "inputs": [],
   "name": "getMessage",
   "outputs": [
     {
       "internalType": "string",
       "name": "",
       "type": "string"
     }
   ],
   "stateMutability": "view",
   "type": "function"
 },
 {
   "inputs": [
     {
       "internalType": "string",
       "name": "_message",
       "type": "string"
     }
   ],
   "name": "setMessage",
   "outputs": [],
   "stateMutability": "nonpayable",
   "type": "function"
 }
]

Let’s briefly compare the smart contract code and the ABI, covering each function, beginning from the top!

The first part of our ABI corresponds to the constructor of our contract, and this description contains three properties: inputs": [], "stateMutability": ”nonpayable", and type": ”constructor

three contract ABI properties- ”inputs"- [], "stateMutability"- ”nonpayable", and ”type"- ”constructor”

The second JSON object references the getMessage() function and comprises the following properties: "inputs": [], "name": ”getMessage”, ”outputs”: [//…]”, "stateMutability": ”view”, and "type": ”function”:

ABI properties- "inputs"- [], "name"- ”getMessage”, ”outputs”- [//…]”, "stateMutability"- ”view”, and "type"- ”function”

The third object corresponds to the setMessage() function, and it consists of the following properties: ”inputs”: [//…], ”name": ”setMessage”, "outputs": [], "stateMutability": ”nonpayable”, and "type": ”function”

the setMessage() function, and it consists of the following properties- ”inputs”- [//…], ”name"- ”setMessage”, "outputs"- [], "stateMutability"- ”nonpayable”, and "type"- ”function”-

Studying the properties of the various JSON objects reveals information about the functions and events. For instance, by looking at the third object, we know that the smart contract has a function called setMessage(), which the ”name” and ”type” properties reveal. We also know from ”inputs” that it takes a string as an argument. 

All in all, by examining the ABI, we get information about how the smart contract works, and we don’t even have to look at the actual code! It’s also worth noting that even though this is a simple contract, the principles and overall structure remain the same for more complex programs.

ABIs vs. APIs 

If you’re somewhat familiar with programming, odds are you’ve heard of APIs (application programming interfaces). APIs are a set of rules, methods, and functions for interacting with a library, network endpoint, or other software services. Essentially, they define how two pieces of software can communicate and interact.

But how are APIs different from ABIs? 

ABIs are quite similar to APIs but work at a ”lower level” as they are binary interfaces. Consequently, the core functionality and purpose are similar, as both ABIs and APIs enable two pieces of software to communicate with one another. 

Graphic art illustration - ABIs vs. APIs - battle illustrating with the two interfaces facing each other

That gives you an overview of smart contract ABIs. In the following section, we’ll show you how to generate and fetch the ABI of any smart contract! 

How to Generate and Get a Smart Contract ABI 

You can generate and fetch the ABI of a smart contract in multiple ways, and in the following subsections, we’ll cover two distinct examples: 

  1. How to Generate a Smart Contract ABI Using Remix
  2. How to Get a Smart Contract ABI Using a Block Explorer

So, let’s kick things off by generating a smart contract ABI using Remix!

How to Generate a Contract ABI Using Remix 

Remix is a popular integrated development environment (IDE) for writing, compiling, deploying, debugging, and testing EVM-compatible smart contracts. In this section, we’ll show you how to use this tool to generate a smart contract ABI! 

Launch Remix and set up a project folder. From there, create a new file and add your smart contract code. In our case, we’ll be reusing the ”HelloWorld.sol” example from earlier: 

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

contract HelloWorld {
    string private message;


    constructor() {
        message = "Hello, World!";
    }
   
   function setMessage(string memory _message) public {
        message = _message;
   }

   function getMessage() public view returns (string memory) {
        return message;
   }
}

Once you’re happy with your code, you then need to compile the smart contract using Remix’s ”Solidity compiler” feature: 

Showing Remix’s ”Solidity compiler” feature

After the contract successfully compiles, Remix will automatically generate an ABI, which you can copy by clicking on ”ABI” in the menu to the left: 

clicking on ”ABI” in the menu to the left

For our ”HelloWorld.sol” contract, the ABI looks like this: 

[
 {
   "inputs": [],
   "stateMutability": "nonpayable",
   "type": "constructor"
 },
 {
   "inputs": [],
   "name": "getMessage",
   "outputs": [
     {
       "internalType": "string",
       "name": "",
       "type": "string"
     }
   ],
   "stateMutability": "view",
   "type": "function"
 },
 {
   "inputs": [
     {
       "internalType": "string",
       "name": "_message",
       "type": "string"
     }
   ],
   "name": "setMessage",
   "outputs": [],
   "stateMutability": "nonpayable",
   "type": "function"
 }
]

Congratulations; you can now seamlessly generate ABIs for your Solidity smart contracts using Remix! 

How to Get a Smart Contract ABI Using a Block Explorer 

Once a smart contract has been deployed to a blockchain network, you can use block explorers like Etherscan to fetch its ABI. To show you how this works, we’ll use the USDC smart contract as an example! 

Start by navigating to USDC on Etherscan: 

USDC ABI contract on Etherscan

Next, click on the ”Contract” tab: 

Showing how to click on the ”Contract” tab

From there, simply scroll down, and you’ll find the ”Contract ABI” section: 

”Contract ABI” section

It might look a bit clunky at first glance, but with proper formatting, it will have a structure similar to our previous examples: 

[
  {
    constant: false,
    inputs: [
      {
        name: "newImplementation",
        type: "address"
      }
    ],
    name: "upgradeTo",
    outputs: [ ],
    payable: false,
    stateMutability: "nonpayable",
    type: "function"
  },
  //...
]

That’s it; it doesn’t have to be more challenging than this. From here, you can follow the same steps to get the ABI of any other smart contract!

Beyond Smart Contract ABIs: Exploring Moralis – The Industry’s Leading Web3 API Provider 

With a better understanding of smart contract ABIs, you might be interested in continuing your Web3 development journey. If this is the case, we highly recommend checking our Moralis.

Moralis is an industry-leading Web3 API provider. With our premier development tools, you can build decentralized applications (dapps) faster and more efficiently! 

Build Web3 project using Moralis - graphic art illustration

In our diverse toolset, you’ll find multiple interfaces for various use cases, including the NFT API, Token API, Wallet API, etc. These tools streamline your interactions with various blockchain networks, allowing you to effortlessly fetch and integrate on-chain data and Web3 functionality into your dapps. 

Consequently, when leveraging Moralis, you can seamlessly build everything from an NFT marketplace to a crypto portfolio tracker without breaking a sweat!

But what makes Moralis stand out in the crowd? 

To answer the question above, let’s explore three benefits of our industry-leading Web3 API suite: 

  • Top Performance: Moralis provides Web3 APIs offering top performance. It doesn’t matter if you want to measure by speed, reliability, data coverage, or any other metric; Moralis consistently outperforms the competition. 
  • Trusted by Industry Leaders: Moralis is trusted by industry leaders, including MetaMask, Opera, Delta, and many others. 
  • Cross-Chain Compatibility: Our Web3 APIs support the biggest blockchains, including Ethereum, BNB Smart Chain (BSC), Solana, and other ETH layer-2 solutions. As such, when using Moralis, it has never been easier to build cross-chain compatible dapps.
smart contract ABIs supported by various blockchain networks

To explore our premier development tools, check out all our interfaces on the Web3 API page! 

Also, did you know you can sign up with Moralis free of charge? As such, if you haven’t already, create your account immediately and start leveraging the full power of blockchain technology today!

Summary: What is a Smart Contract ABI?

In today’s article, we started things off by answering the question, ”What is a smart contract ABI?”. In doing so, we learned that they are interfaces defining a set of rules for engaging with a specific smart contract on blockchain networks like Ethereum. They essentially outline available methods, specify formatting requirements, and give instructions on proper procedures for executing function calls when interacting with smart contracts! 

After exploring the ins and outs of smart contract ABIs, we also showed you how to generate and fetch the ABI of any smart contract. To generate a smart contract ABI, we used the Remix IDE. Then, to fetch one, we used the Etherscan block explorer. 

All in all, if you have followed along this far, you have a more solid understanding and familiarized yourself with the basics of smart contract ABIs! 

If you liked this smart contract ABI guide, consider reading more content here on the Moralis Web3 blog. For instance, check out our Starknet faucet article or learn how to list all the coins in an ETH address

Moralis "M" logo

Also, if you want to build your own Web3 projects, don’t forget to sign up with Moralis. You can create your account for free, and you’ll get immediate access to all our premier Web3 APIs so you can build dapps faster and more efficiently!

Moralis Money
Stay ahead of the markets with real-time, on-chain data insights. Inform your trades with true market alpha!
Moralis Money
Related Articles
January 22, 2023

Complete Tutorial on How to Create an ERC721 Token

February 12, 2024

Build on OP Mainnet – Full 2024 Guide

December 1, 2022

Python and Web3 – A Web3 and Python Tutorial for Blockchain Development

January 19, 2023

Web3 Get Block Feature – Use a Web3 Get Block Timestamp Function

January 12, 2023

BNB Faucet – The Full Guide to BNB Testnet Faucets

December 9, 2022

ERC 1155 NFTs – What is the ERC-1155 Standard?

October 11, 2022

Multichain NFT API – How to Build Cross-Chain NFT Dapps

January 4, 2024

Starknet Faucet – How to Get Testnet Funds for Starknet

November 28, 2022

Goerli ETH – What is the Goerli Testnet?