DEV Community

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

Posted on

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

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!

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.