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 <eric.mastro@gmail.com>
This commit is contained in:
Mark Spanbroek 2024-05-14 17:43:53 +02:00 committed by markspanbroek
parent ab10354910
commit 131316de08
3 changed files with 24 additions and 21 deletions

View File

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

View File

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

View File

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