DEV Community

Cover image for How to convert Solidity JSON ABI to Human Readable ABI in Hardhat
Anjana Jayaweera
Anjana Jayaweera

Posted on

4 1 1 1 1

How to convert Solidity JSON ABI to Human Readable ABI in Hardhat

When you develop dApps, one of the main requirements is to load the Application Binary Interface (ABI) into your frontend app. The methods and data structures of the contract are defined in the ABI, which is a JSON object. Although the JSON ABI may be used by programs to interact with the contract and is machine-readable, it can be challenging for people to understand. This post will cover the process of converting Solidity JSON ABI to a readable format using ethers.js

Step 1: Get the Solidity JSON ABI

The first step is to obtain the Solidity JSON ABI from the smart contract. By utilizing the Solidity compiler to compile the contract or a program like Remix IDE, you can obtain the ABI. In this case, we are going to use hardhat.

Step 2: Load the ABI

When hardhat compiling the smart contracts, the compiled file is stored in artifacts/contracts/<your-contract-name>.sol/<your-contract-name>.json path. If your contract is not compiled yet, simply run npx hardhat compilecommand to compile the contracts.

Let’s create a javascript file under the scripts folder called “convert-abi.js” and write the below code in your js file.

create convert-abi.js file under scrips folder



touch convert-abi.js


Enter fullscreen mode Exit fullscreen mode
async function main() {
const jsonAbi = require("../artifacts/contracts/<your-contract-name>.sol/<your-contract-name>.json").abi;
const iface = new ethers.utils.Interface(jsonAbi);
console.log(iface.format(ethers.utils.FormatTypes.full));
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
view raw convert-abi.js hosted with ❤ by GitHub

Ether.js is supporting three outputs mainly named “json”, “full” and “minimal”. Using the “full” format will ensure the result objects have named properties, which improves code readability. Although the “minimal” format will save some room, it is typically not worthwhile because named properties on results won’t be present. So, we will use “full” format type.

Step 3: Convert the JSON ABI

Simply run the below command in your terminal.



npx hardhat run scripts/convert-abi.js


Enter fullscreen mode Exit fullscreen mode

Output displays after running convert-abi.js

Copy the output and use it in your project as you see fit.

Conclusion

Converting Solidity JSON ABI to a human-readable format can be a useful tool for understanding the interface of a smart contract. This can be useful for smart contract writers as well as for anyone interested to gain a better understanding of how a contract works.

Happy Coding!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (3)

Collapse
 
lwsh123k profile image
lwsh123k

oh, this is great work. thank you for saving my time to write the hubman readable abi.🙂

Collapse
 
awsfanboy profile image
Arshad Zackeriya 🇳🇿 ☁️

nice one @anjana_j

Collapse
 
paxyifier profile image
Paxyifier

Does not work right now. some changes required.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay