From 7686ef4d1a7c736e7ec8c50043ed4696e94f2831 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Mon, 11 Sep 2023 16:55:17 +1000 Subject: [PATCH] include nonce in JsonRpcProviderError This would allow applications to use the nonce in case of an error, eg cancel the transaction to prevent stuck txs from occurring --- ethers/providers/jsonrpc.nim | 15 ++++++++++++++- .../providers/jsonrpc/testJsonRpcProvider.nim | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ethers/providers/jsonrpc.nim b/ethers/providers/jsonrpc.nim index 505944d..390625d 100644 --- a/ethers/providers/jsonrpc.nim +++ b/ethers/providers/jsonrpc.nim @@ -24,6 +24,7 @@ type provider: JsonRpcProvider address: ?Address JsonRpcProviderError* = object of ProviderError + nonce*: ?UInt256 JsonRpcSubscription* = ref object of Subscription subscriptions: JsonRpcSubscriptions id: JsonNode @@ -34,7 +35,19 @@ proc raiseJsonRpcProviderError(message: string) {.upraises: [JsonRpcProviderErro message = parseJson(message){"message"}.getStr except Exception: discard - raise newException(JsonRpcProviderError, message) + let ex = newException(JsonRpcProviderError, message) + ex[].nonce = nonce + raise ex + +template convertError(nonce = none UInt256, body) = + try: + body + except JsonRpcError as error: + raiseProviderError(error.msg, nonce) + # Catch all ValueErrors for now, at least until JsonRpcError is actually + # raised. PR created: https://github.com/status-im/nim-json-rpc/pull/151 + except ValueError as error: + raiseProviderError(error.msg, nonce) template convertError(body) = try: diff --git a/testmodule/providers/jsonrpc/testJsonRpcProvider.nim b/testmodule/providers/jsonrpc/testJsonRpcProvider.nim index ee00acc..1065e32 100644 --- a/testmodule/providers/jsonrpc/testJsonRpcProvider.nim +++ b/testmodule/providers/jsonrpc/testJsonRpcProvider.nim @@ -99,3 +99,20 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]: discard await provider.subscribe(proc(_: Block) = discard) expect JsonRpcProviderError: discard await provider.getSigner().sendTransaction(Transaction.example) + + test "JsonRpcProviderError contains nonce": + let signer = provider.getSigner() + var transaction = Transaction.example + var populated: Transaction + try: + populated = await signer.populateTransaction(transaction) + populated.chainId = some 0.u256 + let confirming = signer.sendTransaction(populated).confirm(1) + await sleepAsync(100.millis) # wait for tx to be mined + await provider.mineBlocks(1) + discard await confirming + except JsonRpcProviderError as e: + check e.nonce.isSome + check e.nonce == populated.nonce + return + fail()