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

Support for Generation of Custom Error type #2173

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

psychoplasma
Copy link

What does this PR do?

  • Adds CustomError class to abi package
  • Adds CustomErrorEncoder class to abi package
  • Adds code generation support to codegen package for Custom Error(error) type introduced in solidity >=0.8.4

With this PR, codegen will be able to generate variables for Custom Errors to corresponding Contract java class.

codegen will generate two things

  • variable for each error type defined in smart contract's abi with org.web3j.abi.datatypes.CustomError java class
  • a list with List.<org.web3j.abi.datatypes.CustomError> Java class which contains all org.web3j.abi.datatypes.CustomError variables defined in the contract class

The former is straight-forward, it's just the Java type corresponding to error type in Solidity.
The latter is for later use(on the client side) to iterate over contract's error types such as resolving revert reason returned by eth_call.

Example: let's say we have the following smart contract

pragma solidity ^0.8.4;

contract MyContract {
    error InvalidAccess(address, string reason);

    function unauthorized() public {
        revert InvalidAccess(msg.sender, "unauthorized address");
    }
}

Then will generate the following code

public class MyContract extends Contract {
    // previous code block

    public static final org.web3j.abi.datatypes.CustomError INVALIDACCESS_ERROR = new org.web3j.abi.datatypes.CustomError("InvalidAccess", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}, new TypeReference<Utf8String>() {}));
    ;

    public static final List CUSTOM_ERRORS = Arrays.<org.web3j.abi.datatypes.CustomError>asList(
            INVALIDACCESS_ERROR);
    ;

    // rest of the code
}

Some notes:

  • If there is no error type defined in contract's abi, no variables are generated for error except CUSTOM_ERRORS list which will effectively be an empty list. I thought, at first, not to generate the list if there is no error type defined in abi, but the client implementations depend on CUSTOM_ERRORS list, then the client implementation would not compile in case of no error type defined in abi.

  • This support will not break backward compatibility for previous solidity versions.

Where should the reviewer start?

abi and codegen packages

Why is it needed?

Currently there is no support for custom errors, and it requires some effort to resolve revert reasons on the client side.

Checklist

  • I've read the contribution guidelines.
  • I've added tests (if applicable).
  • I've added a changelog entry if necessary.

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

Successfully merging this pull request may close these issues.

1 participant