From 4a8d579dcaf026d0c232e20176605d34cba4767d Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sun, 22 Nov 2020 17:43:32 -0500 Subject: [PATCH] Fix for new versions of Geth which return formatted data on revert rather than standard data (#949). --- packages/providers/src.ts/etherscan-provider.ts | 12 ++++++++++-- packages/providers/src.ts/json-rpc-provider.ts | 14 +++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/providers/src.ts/etherscan-provider.ts b/packages/providers/src.ts/etherscan-provider.ts index f94a5a694..870f5c88d 100644 --- a/packages/providers/src.ts/etherscan-provider.ts +++ b/packages/providers/src.ts/etherscan-provider.ts @@ -1,7 +1,7 @@ "use strict"; import { BlockTag, TransactionRequest, TransactionResponse } from "@ethersproject/abstract-provider"; -import { hexlify, hexValue } from "@ethersproject/bytes"; +import { hexlify, hexValue, isHexString } from "@ethersproject/bytes"; import { Network, Networkish } from "@ethersproject/networks"; import { deepCopy, defineReadOnly } from "@ethersproject/properties"; import { ConnectionInfo, fetchJson } from "@ethersproject/web"; @@ -86,7 +86,15 @@ function checkLogTag(blockTag: string): number | "latest" { const defaultApiKey = "9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB"; -function checkError(method: string, error: any, transaction: any): never { +function checkError(method: string, error: any, transaction: any): any { + // Undo the "convenience" some nodes are attempting to prevent backwards + // incompatibility; maybe for v6 consider forwarding reverts as errors + if (method === "call" && error.code === Logger.errors.SERVER_ERROR) { + const e = error.error; + if (e && e.message.match("reverted") && isHexString(e.data)) { + return e.data; + } + } // Get the message from any nested error structure let message = error.message; diff --git a/packages/providers/src.ts/json-rpc-provider.ts b/packages/providers/src.ts/json-rpc-provider.ts index 4c3948a4f..d606e8434 100644 --- a/packages/providers/src.ts/json-rpc-provider.ts +++ b/packages/providers/src.ts/json-rpc-provider.ts @@ -5,7 +5,7 @@ import { Provider, TransactionRequest, TransactionResponse } from "@ethersproject/abstract-provider"; import { Signer, TypedDataDomain, TypedDataField, TypedDataSigner } from "@ethersproject/abstract-signer"; import { BigNumber } from "@ethersproject/bignumber"; -import { Bytes, hexlify, hexValue } from "@ethersproject/bytes"; +import { Bytes, hexlify, hexValue, isHexString } from "@ethersproject/bytes"; import { _TypedDataEncoder } from "@ethersproject/hash"; import { Network, Networkish } from "@ethersproject/networks"; import { checkProperties, deepCopy, Deferrable, defineReadOnly, getStatic, resolveProperties, shallowCopy } from "@ethersproject/properties"; @@ -21,7 +21,16 @@ import { BaseProvider, Event } from "./base-provider"; const errorGas = [ "call", "estimateGas" ]; -function checkError(method: string, error: any, params: any): never { +function checkError(method: string, error: any, params: any): any { + // Undo the "convenience" some nodes are attempting to prevent backwards + // incompatibility; maybe for v6 consider forwarding reverts as errors + if (method === "call" && error.code === Logger.errors.SERVER_ERROR) { + const e = error.error; + if (e && e.message.match("reverted") && isHexString(e.data)) { + return e.data; + } + } + let message = error.message; if (error.code === Logger.errors.SERVER_ERROR && error.error && typeof(error.error.message) === "string") { message = error.error.message; @@ -448,7 +457,6 @@ export class JsonRpcProvider extends BaseProvider { if (args == null) { logger.throwError(method + " not implemented", Logger.errors.NOT_IMPLEMENTED, { operation: method }); } - try { return await this.send(args[0], args[1]) } catch (error) {