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 gasLimit*: ?UInt256
CallOverrides* = ref object of TransactionOverrides CallOverrides* = ref object of TransactionOverrides
blockTag*: ?BlockTag blockTag*: ?BlockTag
ContractError* = object of EthersError ContractError* = object of EthersError
Confirmable* = object Confirmable* = object
response*: ?TransactionResponse response*: ?TransactionResponse
convert*: ConvertCustomErrors
EventHandler*[E: Event] = proc(event: E) {.gcsafe, raises:[].} EventHandler*[E: Event] = proc(event: E) {.gcsafe, raises:[].}
func new*(ContractType: type Contract, func new*(ContractType: type Contract,
@ -124,14 +124,12 @@ proc call(contract: Contract,
proc send(contract: Contract, proc send(contract: Contract,
function: string, function: string,
parameters: tuple, parameters: tuple,
overrides = TransactionOverrides(), overrides = TransactionOverrides()):
convertCustomErrors: ConvertCustomErrors = nil):
Future[?TransactionResponse] {.async.} = Future[?TransactionResponse] {.async.} =
if signer =? contract.signer: if signer =? contract.signer:
let transaction = createTransaction(contract, function, parameters, overrides) let transaction = createTransaction(contract, function, parameters, overrides)
let populated = await signer.populateTransaction(transaction) let populated = await signer.populateTransaction(transaction)
var txResp = await signer.sendTransaction(populated) var txResp = await signer.sendTransaction(populated)
txResp.convertCustomErrors = convertCustomErrors
return txResp.some return txResp.some
else: else:
await call(contract, function, parameters, overrides) await call(contract, function, parameters, overrides)
@ -229,9 +227,9 @@ func addContractCall(procedure: var NimNode) =
"unexpected return type, " & "unexpected return type, " &
"missing {.view.}, {.pure.} or {.getter.} ?" "missing {.view.}, {.pure.} or {.getter.} ?"
.} .}
let response = await send(`contract`, `function`, `parameters`, overrides)
let convert = customErrorConversion(`errors`) let convert = customErrorConversion(`errors`)
let response = await send(`contract`, `function`, `parameters`, overrides, convert) Confirmable(response: response, convert: convert)
Confirmable(response: response)
procedure[6] = procedure[6] =
if procedure.isConstant: if procedure.isConstant:
@ -296,6 +294,21 @@ proc subscribe*[E: Event](contract: Contract,
contract.provider.subscribe(filter, logHandler) 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], proc confirm*(tx: Future[Confirmable],
confirmations: int = EthersDefaultConfirmations, confirmations: int = EthersDefaultConfirmations,
timeout: int = EthersReceiptTimeoutBlks): timeout: int = EthersReceiptTimeoutBlks):
@ -305,13 +318,7 @@ proc confirm*(tx: Future[Confirmable],
## `await token.connect(signer0) ## `await token.connect(signer0)
## .mint(accounts[1], 100.u256) ## .mint(accounts[1], 100.u256)
## .confirm(3)` ## .confirm(3)`
without response =? (await tx).response: return await (await tx).confirm(confirmations, timeout)
raise newException(
EthersError,
"Transaction hash required. Possibly was a call instead of a send?"
)
return await response.confirm(confirmations, timeout)
proc queryFilter[E: Event](contract: Contract, proc queryFilter[E: Event](contract: Contract,
_: type E, _: type E,

View File

@ -2,6 +2,9 @@ import ../basics
import ../provider import ../provider
import ./encoding import ./encoding
type ConvertCustomErrors* =
proc(error: ref ProviderError): ref EthersError {.gcsafe, raises:[].}
func customErrorConversion*(ErrorTypes: type tuple): ConvertCustomErrors = func customErrorConversion*(ErrorTypes: type tuple): ConvertCustomErrors =
func convert(error: ref ProviderError): ref EthersError = func convert(error: ref ProviderError): ref EthersError =
if data =? error.data: if data =? error.data:

View File

@ -41,9 +41,6 @@ type
TransactionResponse* = object TransactionResponse* = object
provider*: Provider provider*: Provider
hash* {.serialize.}: TransactionHash hash* {.serialize.}: TransactionHash
convertCustomErrors*: ConvertCustomErrors
ConvertCustomErrors* =
proc(error: ref ProviderError): ref EthersError {.gcsafe, raises:[].}
TransactionReceipt* {.serialize.} = object TransactionReceipt* {.serialize.} = object
sender* {.serialize("from"), deserialize("from").}: ?Address sender* {.serialize("from"), deserialize("from").}: ?Address
to*: ?Address to*: ?Address
@ -270,11 +267,7 @@ proc confirm*(
if txBlockNumber + confirmations.u256 <= blockNumber + 1: if txBlockNumber + confirmations.u256 <= blockNumber + 1:
await subscription.unsubscribe() await subscription.unsubscribe()
try:
await tx.provider.ensureSuccess(receipt) await tx.provider.ensureSuccess(receipt)
except ProviderError as error:
if convert =? tx.convertCustomErrors:
raise convert(error)
return receipt return receipt
proc confirm*( proc confirm*(