From 8cc700412bffafab585361107892fdd29f41035b Mon Sep 17 00:00:00 2001 From: David Murdoch Date: Fri, 16 Dec 2022 19:08:13 -0500 Subject: [PATCH 1/2] fix: fix failing rejected gasEstimate --- src/chains/ethereum/ethereum/src/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chains/ethereum/ethereum/src/api.ts b/src/chains/ethereum/ethereum/src/api.ts index e47a39f6de..879269f93a 100644 --- a/src/chains/ethereum/ethereum/src/api.ts +++ b/src/chains/ethereum/ethereum/src/api.ts @@ -960,7 +960,7 @@ export default class EthereumApi implements Api { generateVM, runArgs, (err: Error, result: EstimateGasResult) => { - if (err) reject(err); + if (err) return void reject(err); resolve(Quantity.from(result.gasEstimate)); } ); From 3207f1996033c4ff01a39533659f925a37069dca Mon Sep 17 00:00:00 2001 From: Jeff Smale <6363749+jeffsmale90@users.noreply.github.com> Date: Mon, 19 Dec 2022 12:21:15 +1300 Subject: [PATCH 2/2] Add test to ensure taht 'unhandledRejection' is not raised when estimateGas fails --- .../tests/api/eth/estimateGas.test.ts | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts diff --git a/src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts b/src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts new file mode 100644 index 0000000000..9c7f3f3d2a --- /dev/null +++ b/src/chains/ethereum/ethereum/tests/api/eth/estimateGas.test.ts @@ -0,0 +1,45 @@ +import assert from "assert"; +import getProvider from "../../helpers/getProvider"; + +describe("api", () => { + describe("eth", () => { + describe("estimateGas", () => { + it("shouldn't raise an unhandled rejection when the transaction fails", async () => { + // see https://github.com/trufflesuite/ganache/pull/4056 + const provider = await getProvider(); + const [from] = await provider.request({ + method: "eth_accounts", + params: [] + }); + + const transaction = { + from, + // invalid bytecode + input: "0x1234" + }; + + let didRaiseUnhandledRejection = false; + const unhandledRejectionHandler = () => + (didRaiseUnhandledRejection = true); + process.once("unhandledRejection", unhandledRejectionHandler); + + try { + const estimatingGas = provider.request({ + method: "eth_estimateGas", + params: [transaction] + }); + + await assert.rejects(estimatingGas); + await new Promise(resolve => setImmediate(resolve)); + + assert( + didRaiseUnhandledRejection === false, + "Shouldn't have raised an unhandledRejection" + ); + } finally { + process.off("unhandledRejection", unhandledRejectionHandler); + } + }); + }); + }); +});