Web3.js: is it possible to get contract ABI through contract address?

Created on 2 Apr 2018  路  4Comments  路  Source: ChainSafe/web3.js

or at least ,can get ERC20 contract ABI?

Most helpful comment

In general it is not feasible to generate an ABI from just contract byte code. You can decompile byte code back to source to some degree but the level of fidelity required to reproduce the ABI is probably not attainable for most contracts.

When compiled to byte code method/event signatures are hashed and the last 4 bytes of the hashes are used as the method/event IDs. This is why you need the ABI- to know what the method/event signatures are so that you can hash out their ID and use it to call the method or filter the event. If it were possible to get the ABI from byte code reliably, there wouldn't really be a need for the ABI in the first place.

All 4 comments

Basic ERC20 contract ABI is always the same. Concerning the contract address, you can run eth.getCode(address). It gives you the byte code and you can, from there, format it to get ABI.

closing this since @Andromelus answered the question

@Andromelus could you please give me some more advice on how to transfer erc20 byte code to its ABI? Now I want to get the full ERC20 list, including contract address and abi on the chain, someone told me one solution is to traversal the whole blocks on the chain and check every transaction in the block whether it is a ERC20 transfer, then try to obtain abi from the ERC20 contract address, according to your post, first call eth.getCode(address) and then try to convert bytecode to abi, is this procedure right? or may be there is a better way to do this?

In general it is not feasible to generate an ABI from just contract byte code. You can decompile byte code back to source to some degree but the level of fidelity required to reproduce the ABI is probably not attainable for most contracts.

When compiled to byte code method/event signatures are hashed and the last 4 bytes of the hashes are used as the method/event IDs. This is why you need the ABI- to know what the method/event signatures are so that you can hash out their ID and use it to call the method or filter the event. If it were possible to get the ABI from byte code reliably, there wouldn't really be a need for the ABI in the first place.

Was this page helpful?
0 / 5 - 0 ratings