2

How can I construct the ABI code for a contract event?

I don't need the whole contract ABI.

Example:

  • transferFrom(address,address,uint256)
  • executeMetaTransaction(address,bytes,bytes32,bytes32,uint8)

I need to get the ABI code for events like this.

Should I just create a basic contract with the event in Remix and compile it?

2
  • I just used this to get encoded data. Using the event + parameters and inputting data from a txn from the contract. Is there any way to get the ABI json from the encoded data? abi.hashex.org
    – jinsley8
    Feb 10, 2023 at 5:24
  • just keccak the string transferFrom(address,address,uint256) and you will get the "abi code" which is just a hash. this is how it is done by Geth: github.com/ethereum/go-ethereum/blob/…
    – Nulik
    Feb 10, 2023 at 14:23

1 Answer 1

1

You can get the whole ABI via Etherscan API and then via for loop filter out only the event related part.

Here is a snippet from a Python code on how to do it:

# contract ABI extraction via Etherscan API
events = []
etherscan_url = f'https://api-goerli.etherscan.io/api?module=contract&action=getabi&address={contract_address}&apikey={etherscan_api}'
etherscan_response = requests.get(etherscan_url)
etherscan_content = etherscan_response.json()
contract_abi = etherscan_content.get("result")
contract_abi_dict = json.loads(contract_abi)
print(f"ABI: {contract_abi}")

# Event name extraction
for i, j in enumerate(contract_abi_dict):
    if contract_abi_dict[i]['type'] == "event":
        events.append(contract_abi_dict[i]['name'])
        print(contract_abi_dict[i]['name'])
2
  • I'm looking at a contract on Polygonscan that doesn't have their Contract verified so can't do this.
    – jinsley8
    Feb 10, 2023 at 8:15
  • You will need to get the code itself and once compiled in Remix for example, you can get the ABI manually.
    – f22daniel
    Feb 10, 2023 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.