Solidity ABI – How to Get ABI from Source Code Programmatically?

abietherjsgo-ethereumsolidityweb3js

Is it possible to get an ABI from the source code without using solc compiler? Like I can get source code of verified contracts through etherscan API and I want to convert it into ABI.

Are there any ways to do this with ether.js or web3.js? Or any other way?

Thank you.

Best Answer

In theory, you could create your own code that creates the ABI of a contract given the source code of that contract, reading the function's name and parameters and producing a javascript/JSON output, but you would be reinventing the wheel since you would be implementing something similar to a compiler.

Web3js and Ethersjs don't do this.

You could put the contract source code in Remix and compile it and get the source code. But I guess this is not what you want since it will use solc.

You could create a tool in, like, nodejs to programmatically handle the abi generation given some contract code base, using 'solc' under the hood, but adding more functionality, like receiving source code of contracts dynamically and compiling it, taking the abi and returning it, etc. Like a backend, like Etherscan and Remix do.

Check out this project: https://github.com/smartcontractkit/solc-api