From 131316de08166d9181cec1ddf11294e09fa3f69a Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 14 May 2024 17:43:53 +0200 Subject: [PATCH] move error conversion from TransactionResponse to Confirmable Ensures that provider no longer needs to know about error conversion, it now localized in contract.nim. Co-Authored-By: Eric Mastro --- ethers/contract.nim | 33 ++++++++++++++++++++------------- ethers/errors/conversion.nim | 3 +++ ethers/provider.nim | 9 +-------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/ethers/contract.nim b/ethers/contract.nim index f204d85..41de35c 100644 --- a/ethers/contract.nim +++ b/ethers/contract.nim @@ -35,10 +35,10 @@ type gasLimit*: ?UInt256 CallOverrides* = ref object of TransactionOverrides blockTag*: ?BlockTag - ContractError* = object of EthersError Confirmable* = object response*: ?TransactionResponse + convert*: ConvertCustomErrors EventHandler*[E: Event] = proc(event: E) {.gcsafe, raises:[].} func new*(ContractType: type Contract, @@ -124,14 +124,12 @@ proc call(contract: Contract, proc send(contract: Contract, function: string, parameters: tuple, - overrides = TransactionOverrides(), - convertCustomErrors: ConvertCustomErrors = nil): + overrides = TransactionOverrides()): Future[?TransactionResponse] {.async.} = if signer =? contract.signer: let transaction = createTransaction(contract, function, parameters, overrides) let populated = await signer.populateTransaction(transaction) var txResp = await signer.sendTransaction(populated) - txResp.convertCustomErrors = convertCustomErrors return txResp.some else: await call(contract, function, parameters, overrides) @@ -229,9 +227,9 @@ func addContractCall(procedure: var NimNode) = "unexpected return type, " & "missing {.view.}, {.pure.} or {.getter.} ?" .} + let response = await send(`contract`, `function`, `parameters`, overrides) let convert = customErrorConversion(`errors`) - let response = await send(`contract`, `function`, `parameters`, overrides, convert) - Confirmable(response: response) + Confirmable(response: response, convert: convert) procedure[6] = if procedure.isConstant: @@ -296,6 +294,21 @@ proc subscribe*[E: Event](contract: Contract, contract.provider.subscribe(filter, logHandler) +proc confirm(tx: Confirmable, confirmations, timeout: int): + Future[TransactionReceipt] {.async.} = + + without response =? tx.response: + raise newException( + EthersError, + "Transaction hash required. Possibly was a call instead of a send?" + ) + + try: + return await response.confirm(confirmations, timeout) + except ProviderError as error: + let convert = tx.convert + raise convert(error) + proc confirm*(tx: Future[Confirmable], confirmations: int = EthersDefaultConfirmations, timeout: int = EthersReceiptTimeoutBlks): @@ -305,13 +318,7 @@ proc confirm*(tx: Future[Confirmable], ## `await token.connect(signer0) ## .mint(accounts[1], 100.u256) ## .confirm(3)` - without response =? (await tx).response: - raise newException( - EthersError, - "Transaction hash required. Possibly was a call instead of a send?" - ) - - return await response.confirm(confirmations, timeout) + return await (await tx).confirm(confirmations, timeout) proc queryFilter[E: Event](contract: Contract, _: type E, diff --git a/ethers/errors/conversion.nim b/ethers/errors/conversion.nim index 22a43aa..9cb3d90 100644 --- a/ethers/errors/conversion.nim +++ b/ethers/errors/conversion.nim @@ -2,6 +2,9 @@ import ../basics import ../provider import ./encoding +type ConvertCustomErrors* = + proc(error: ref ProviderError): ref EthersError {.gcsafe, raises:[].} + func customErrorConversion*(ErrorTypes: type tuple): ConvertCustomErrors = func convert(error: ref ProviderError): ref EthersError = if data =? error.data: diff --git a/ethers/provider.nim b/ethers/provider.nim index efce84c..db7f0a4 100644 --- a/ethers/provider.nim +++ b/ethers/provider.nim @@ -41,9 +41,6 @@ type TransactionResponse* = object provider*: Provider hash* {.serialize.}: TransactionHash - convertCustomErrors*: ConvertCustomErrors - ConvertCustomErrors* = - proc(error: ref ProviderError): ref EthersError {.gcsafe, raises:[].} TransactionReceipt* {.serialize.} = object sender* {.serialize("from"), deserialize("from").}: ?Address to*: ?Address @@ -270,11 +267,7 @@ proc confirm*( if txBlockNumber + confirmations.u256 <= blockNumber + 1: await subscription.unsubscribe() - try: - await tx.provider.ensureSuccess(receipt) - except ProviderError as error: - if convert =? tx.convertCustomErrors: - raise convert(error) + await tx.provider.ensureSuccess(receipt) return receipt proc confirm*(