Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse encoded revert reasons from ABI (ContractCustomError) #3523

Open
F4ever opened this issue Oct 31, 2024 · 1 comment
Open

Parse encoded revert reasons from ABI (ContractCustomError) #3523

F4ever opened this issue Oct 31, 2024 · 1 comment

Comments

@F4ever
Copy link

F4ever commented Oct 31, 2024

What feature should we add?

🌟 Hello everyone! 🌟

It would be easier to debug and understand what happened if contracts automatically parsed reverts into a human-readable format.

Current contract reverts exceptions example:
ContractCustomError('0xcd0883ea', '0xcd0883ea')

Parsed reverts with feature:

abi = '[{"inputs":[],"name":"InitialEpochIsYetToArrive","type":"error"}]'

contract = Contract(abi=abi, parse_reverts=True)

try:
    contract.functions.someMethod()
except ContractCustomError as error:
    print(error)
    # output: ContractCustomError('0xcd0883ea', 'InitialEpochIsYetToArrive()')

Also, parsing inputs could be implemented.

abi = '[{"inputs":[{"internalType":"uint256","name":"secondsPerSlot","type":"uint256"}],"name":"InitialEpochIsYetToArrive","type":"error"}]'

ContractCustomError('0xcd0883ea', 'InitialEpochIsYetToArrive(secondsPerSlot=12)')
# or
ContractCustomError('0xcd0883ea', 'InitialEpochIsYetToArrive', kwargs={'secondsPerSlot': 12})

Some python pseudocode how to parse exceptions could be implemented

# Parse encoded revert data by matching the error signature

error_signature = revert_reason.data

for item in abi:
    if item.get("type") == "error" and w3.keccak(text=item["name"])[:4].hex() == error_signature:
        error_name = item["name"]
        error_inputs = item["inputs"]
        decoded_data = contract.decode_function_input("0x" + revert_reason_data)
        
        print(f"Error Name: {error_name}")
        print("Decoded Data:", decoded_data)
        break
@kclowes
Copy link
Collaborator

kclowes commented Oct 31, 2024

This would be nice, thanks for the issue. We have a bunch of new abi helper methods that might help here that we've added in v7. Feel free to take a crack at it or we'll put it in our queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants