Parse ABI – How to Parse ABI Out of Contract Code

abiweb3js

Using the web3 JS library, there's hooks to grab a transaction by its hash (web3.eth.getTransaction()), which includes an input property that is the data payload of the transaction. The first four bytes of which are the function selector which is a fragment of a hash.

The question is, how to determine what function was called? Hashes can't be worked backwards, so we need to derive the list of function signatures for that contract (a.k.a. the contract ABI).

Web3 has the web3.eth.getCode(txnHash) method that can get the hex code for a contract, but can that be parsed into an ABI of the contract?

Or is it impossible to derive the ABI without having the source code of the contract?

Best Answer

Here's a database of mappings from the functions to the function signatures - https://www.4byte.directory/. Note that the server has been returning 'Server Error (500)' intermittently.

Some further information about this service: Ethereum Function Signature Database - database of 4-Btye function signatures to their human readable counterparts.