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.
touch convert-abi.js
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
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)
oh, this is great work. thank you for saving my time to write the hubman readable abi.🙂
nice one @anjana_j
Does not work right now. some changes required.