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
This commit is contained in:
Eric 2023-09-11 16:55:17 +10:00
parent 9edb3d2fa7
commit 7686ef4d1a
No known key found for this signature in database
2 changed files with 31 additions and 1 deletions

View File

@ -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:

View File

@ -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()