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 …


This content originally appeared on DEV Community and was authored by Anjana Jayaweera

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

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

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!


This content originally appeared on DEV Community and was authored by Anjana Jayaweera


Print Share Comment Cite Upload Translate Updates
APA

Anjana Jayaweera | Sciencx (2023-03-16T00:27:42+00:00) How to convert Solidity JSON ABI to Human Readable ABI in Hardhat. Retrieved from https://www.scien.cx/2023/03/16/how-to-convert-solidity-json-abi-to-human-readable-abi-in-hardhat/

MLA
" » How to convert Solidity JSON ABI to Human Readable ABI in Hardhat." Anjana Jayaweera | Sciencx - Thursday March 16, 2023, https://www.scien.cx/2023/03/16/how-to-convert-solidity-json-abi-to-human-readable-abi-in-hardhat/
HARVARD
Anjana Jayaweera | Sciencx Thursday March 16, 2023 » How to convert Solidity JSON ABI to Human Readable ABI in Hardhat., viewed ,<https://www.scien.cx/2023/03/16/how-to-convert-solidity-json-abi-to-human-readable-abi-in-hardhat/>
VANCOUVER
Anjana Jayaweera | Sciencx - » How to convert Solidity JSON ABI to Human Readable ABI in Hardhat. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/16/how-to-convert-solidity-json-abi-to-human-readable-abi-in-hardhat/
CHICAGO
" » How to convert Solidity JSON ABI to Human Readable ABI in Hardhat." Anjana Jayaweera | Sciencx - Accessed . https://www.scien.cx/2023/03/16/how-to-convert-solidity-json-abi-to-human-readable-abi-in-hardhat/
IEEE
" » How to convert Solidity JSON ABI to Human Readable ABI in Hardhat." Anjana Jayaweera | Sciencx [Online]. Available: https://www.scien.cx/2023/03/16/how-to-convert-solidity-json-abi-to-human-readable-abi-in-hardhat/. [Accessed: ]
rf:citation
» How to convert Solidity JSON ABI to Human Readable ABI in Hardhat | Anjana Jayaweera | Sciencx | https://www.scien.cx/2023/03/16/how-to-convert-solidity-json-abi-to-human-readable-abi-in-hardhat/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.