Ethers.js: Support custom errors from Solidity 0.8.4 (new fragment type)

Created on 21 Apr 2021  路  5Comments  路  Source: ethers-io/ethers.js

Today Solidity 0.8.4 was released and it includes a new feature called "custom errors" that currently breaks Ethers.js.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract TestToken {
    error InsufficientBalance(uint256 available, uint256 required);

    function transfer(address /*to*/, uint amount) public pure {
        revert InsufficientBalance(0, amount);
    }
}

The resulting ABI contains a new type of fragment for errors:

{
  "type": "error",
  "name": "InsufficientBalance",
  "inputs": [
    {
      "internalType": "uint256",
      "name": "available",
      "type": "uint256"
    },
    {
      "internalType": "uint256",
      "name": "required",
      "type": "uint256"
    }
  ],
},

Loading the contract ABI on Ethers results in:

Error: invalid fragment object (argument="value", value={"inputs":[{"internalType":"uint256","name":"available","type":"uint256"},{"internalType":"uint256","name":"required","type":"uint256"}],"name":"InsufficientBalance","type":"error"}, code=INVALID_ARGUMENT, version=abi/5.1.1)

Eventually Ethers should parse the revert data into one of these error objects but in the meantime it should still support loading these ABIs.

enhancement fixed

Most helpful comment

Not erroring should be fixed in [5.1.4])https://github.com/ethers-io/ethers.js/releases/tag/v5.1.4).

Try it out and let me know. :)

All 5 comments

Sorry, just saw this. I鈥檝e opened up enhancement issues to track this and will be adding support soon. :)

Being tracking in #1497 and #1498.

Not erroring should be fixed in [5.1.4])https://github.com/ethers-io/ethers.js/releases/tag/v5.1.4).

Try it out and let me know. :)

Seems to work!

Awesome. I'll close this issue, and use #1498 to track the v5.2 release with full Custom Error support.

Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhABC picture PhABC  路  3Comments

ricmoo picture ricmoo  路  3Comments

GFJHogue picture GFJHogue  路  3Comments

jochenonline picture jochenonline  路  3Comments

crazyrabbitLTC picture crazyrabbitLTC  路  3Comments