ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

Revert Error response processing

Open cpecorari opened this issue 3 years ago • 5 comments

Hi, Trying to handle revert errors on gas estimate on-chain, but documentation does not seem to have anything clear on this. Made a few tests but i don't have much consistency, maybe we could enhance documentation and/or error handling, or even adding code example ? I even tried to understand ethers error logger from https://github.com/ethers-io/ethers.js/blob/eb432aa1f44ad2cc268d000b266eae9b03db1d17/packages/web/src.ts/index.ts

For example, a Uniswap Router Swap revert with "Too little Received" can be parsed to understand issue. I ended up handling pretty ugly like this :

const potentialError =
    typeof error === 'string'
      ? error.match(regexExtract)
      : typeof error.message === 'string'
      ? error.message.match(regexExtract)
      : typeof error.error?.body === 'string'
      ? JSON.parse(error.error?.body)
      : error.error?.message || "No revert reason found";
      

But a revert for whatever reason (token error ?) looks different in error type :

processing response error (body="{\"jsonrpc\":\"2.0\",\"id\":2052,\"error\":{\"code\":-32000,\"message\":\"execution reverted\"}}\n", error={"code":-32000}, requestBody="{\"method\":\"eth_estimateGas\",\"params\":[{\"type\":\"0x2\",\"from\":\"0xf0000000\",\"to\":\"0x12000000\",\"data\":\"0x0000\"}],\"id\":2052,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="http://192.168.1.3:8545", code=SERVER_ERROR, version=web/5.5.0)

So, am i missing something on ethers error on gas estimate ? Or should we enhance documentation ? Also a simple Typescript type Error would be great to parse.

Thanks !

PS : addresses are fine, just changed them for privacy matters

cpecorari avatar Jan 02 '22 10:01 cpecorari

Ok i think i got some point, each node client seems to be rejecting type on its own...

With hardhat and geth i got 2 different object types.

With hardhat 2.6.5 revert error catch :

{
  "reason": "processing response error",
  "code": "SERVER_ERROR",
  "body": "{\"jsonrpc\":\"2.0\",\"id\":117,\"error\":{\"code\":-32603,\"message\":\"Error: VM Exception while processing transaction: reverted with reason string 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT'\"}}",
  "error": {
    "code": -32603
  },
  "requestBody": "{\"method\":\"eth_estimateGas\",\"params\":[{\"from\":\"0xf00000000000000000\",\"to\":\"0x100000000000000\",\"data\":\"0x90[...]00000\"}],\"id\":117,\"jsonrpc\":\"2.0\"}",
  "requestMethod": "POST",
  "url": "http://127.0.0.1:8545"
}

Geth 1.10.14 Mainnet :

"reason": "cannot estimate gas; transaction may fail or may require manual gas limit",
  "code": "UNPREDICTABLE_GAS_LIMIT",
  "error": {
    "reason": "processing response error",
    "code": "SERVER_ERROR",
    "body": "{\"jsonrpc\":\"2.0\",\"id\":68,\"error\":{\"code\":3,\"message\":\"execution reverted: UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT\",\"data\":\"0x2222000000[...]0000\"}}\n",
    "error": {
      "code": 3,
      "data": "0x0[...]000"
    },
    "requestBody": "{\"method\":\"eth_estimateGas\",\"params\":[{\"from\":\"0xf00000\",\"to\":\"0x12000000\",\"data\":\"0x9[...]000\"}],\"id\":68,\"jsonrpc\":\"2.0\"}",
    "requestMethod": "POST",
    "url": "http://192.168.1.3:8545"
  },
  "method": "estimateGas",
  "transaction": {
    "from": "0xf000000000000000000",
    "to": "0x1000000000000000000000",
    "data": "0x222222000000[...]000000",
    "accessList": null
  }
}

So does anyone has an idea about how to handle those different types / possibilities ? I mean, better than my tests and regex on types ? Can Ethers do anything ? At least point this on documentation could save people some time i guess... Thanks !

cpecorari avatar Jan 03 '22 18:01 cpecorari

I'm working on a fix to help consolidate these. The Hardhat error will be hard to process in its current state, but I'll make a request for them to format it so it is machine readable (in addition to human-readable).

ricmoo avatar Mar 25 '22 22:03 ricmoo

I'm working on a fix to help consolidate these. The Hardhat error will be hard to process in its current state, but I'll make a request for them to format it so it is machine readable (in addition to human-readable).

Was just looking for this very issue and glad to see you're on it! Thanks for your great work 🙏

cinnabarhorse avatar Mar 26 '22 12:03 cinnabarhorse

I'm working on a fix to help consolidate these. The Hardhat error will be hard to process in its current state, but I'll make a request for them to format it so it is machine readable (in addition to human-readable).

If this issue is resolved, it will be very helpful for devs.

wanglonghong avatar Apr 27 '22 02:04 wanglonghong

Was this error fixed or is it still open?

RohitAudit avatar Aug 19 '22 11:08 RohitAudit