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

Execution reverted (coult not decode custom error) #3785

Closed
dennohpeter opened this issue Feb 16, 2023 · 8 comments
Closed

Execution reverted (coult not decode custom error) #3785

dennohpeter opened this issue Feb 16, 2023 · 8 comments
Assignees
Labels
bug Verified to be an issue. v6 Issues regarding v6

Comments

@dennohpeter
Copy link

Ethers Version

6.0.3

Search Terms

decode error

Describe the Problem

Ethers V6 is unable to decode custom errors declared in solidity e.g error RevertWithCode(uint8 code); and errors msgs from function reverts in solidity e.g require(a > b, "a is not greater than b");

Reverts with Cannot assign to read only property 'invocation' of object 'Error: execution reverted (coult not decode custom error) or Error: execution reverted (unknown custom error)

Here is a link to verified contract on bsc testnet that you can use to reproduce the issue

https://testnet.bscscan.com/address/0xb8e82dd09afa2e5a16fb1f62e38b03edc20163e3#code

Code Snippet

import { Contract, JsonRpcProvider } from "ethers";

const Main = async () => {
  let provider = new JsonRpcProvider(
    "https://data-seed-prebsc-2-s1.binance.org:8545"
  );

  let abi = [
    {
      inputs: [{ internalType: "uint8", name: "code", type: "uint8" }],
      name: "RevertWithCode",
      type: "error",
    },
    {
      inputs: [],
      name: "number",
      outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
      stateMutability: "view",
      type: "function",
    },
    {
      inputs: [
        { internalType: "uint256", name: "a", type: "uint256" },
        { internalType: "uint256", name: "b", type: "uint256" },
      ],
      name: "requireRevert",
      outputs: [{ internalType: "bool", name: "", type: "bool" }],
      stateMutability: "pure",
      type: "function",
    },
    {
      inputs: [
        { internalType: "uint256", name: "a", type: "uint256" },
        { internalType: "uint256", name: "b", type: "uint256" },
      ],
      name: "revertWithCode",
      outputs: [{ internalType: "bool", name: "", type: "bool" }],
      stateMutability: "pure",
      type: "function",
    },
  ];

  let contract = new Contract(
    "0xB8e82DD09afa2E5A16Fb1f62e38B03EDC20163E3",
    abi,
    provider
  );

  let a = 6;
  let b = 4;
  // test revert with code
  //   console.log(await contract.revertWithCode.staticCall(a, b));

  // test require revert
  console.log(await contract.requireRevert.staticCall(b, a));
};
Main();

Contract ABI

[
    {
      inputs: [{ internalType: "uint8", name: "code", type: "uint8" }],
      name: "RevertWithCode",
      type: "error",
    },
    {
      inputs: [],
      name: "number",
      outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
      stateMutability: "view",
      type: "function",
    },
    {
      inputs: [
        { internalType: "uint256", name: "a", type: "uint256" },
        { internalType: "uint256", name: "b", type: "uint256" },
      ],
      name: "requireRevert",
      outputs: [{ internalType: "bool", name: "", type: "bool" }],
      stateMutability: "pure",
      type: "function",
    },
    {
      inputs: [
        { internalType: "uint256", name: "a", type: "uint256" },
        { internalType: "uint256", name: "b", type: "uint256" },
      ],
      name: "revertWithCode",
      outputs: [{ internalType: "bool", name: "", type: "bool" }],
      stateMutability: "pure",
      type: "function",
    },
  ]

Errors

TypeError: Cannot assign to read only property 'invocation' of object 'Error: execution reverted (coult not decode custom error)'
    at Interface.makeError (/<redacted>/client/node_modules/ethers/src.ts/abi/interface.ts:803:29)
    at Proxy.staticCallResult (/<redacted>/client/node_modules/ethers/src.ts/contract/contract.ts:345:48)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at Proxy.staticCall (/<redacted>/client/node_modules/ethers/src.ts/contract/contract.ts:308:24)

Environment

node.js (v12 or newer)

Environment (Other)

No response

@dennohpeter dennohpeter added the investigate Under investigation and may be a bug. label Feb 16, 2023
@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2023

Thanks for the concise repro steps. I’ll look into this asap. :)

@ricmoo ricmoo added on-deck This Enhancement or Bug is currently being worked on. v6 Issues regarding v6 bug Verified to be an issue. and removed investigate Under investigation and may be a bug. labels Feb 16, 2023
@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2023

Found the issue. I make the properties on an error immutable and then try to update the null invocation info later, along with myriad other possible properties.

I don't think it makes sense to make the error properties immutable (especially since that isn't included in return type).

Trying a few things out. Should be able to have a new version up soon. :)

@dennohpeter
Copy link
Author

Awesome @ricmoo 🚀

ricmoo added a commit that referenced this issue Feb 16, 2023
@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2023

This should be tied in v6.0.4. Try it out and let me know. :)

@dennohpeter
Copy link
Author

Working great for require() reverts. For custom errors the error is kinda generic RevertWithCode(uint8) though it works. I figured to get the actual revert code you have to check on
revert: { name: 'RevertWithCode', signature: 'RevertWithCode(uint8)', args: [Result]} args which is okay

@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2023

Exactly. There is no way to generically generate a string representation of a custom error. For custom errors, the .reason is the signature, so you need to dig into the .revert property which gives you everything parsed and nicely machine readable. :)

@dennohpeter
Copy link
Author

😄 true, noted, thanks @ricmoo

@ricmoo
Copy link
Member

ricmoo commented Feb 16, 2023

Thanks to you too! :)

@ricmoo ricmoo closed this as completed Feb 16, 2023
@ricmoo ricmoo removed the on-deck This Enhancement or Bug is currently being worked on. label Feb 16, 2023
Woodpile37 pushed a commit to Woodpile37/ethers.js that referenced this issue Jan 14, 2024
Woodpile37 pushed a commit to Woodpile37/ethers.js that referenced this issue Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

2 participants