Introduce separate type for transaction overrides

This commit is contained in:
Mark Spanbroek 2022-07-12 12:43:56 +02:00 committed by markspanbroek
parent a7d1944406
commit 083af80dcc
2 changed files with 16 additions and 8 deletions

View File

@ -16,6 +16,11 @@ type
provider: Provider
signer: ?Signer
address: Address
TransactionOverrides* = object
nonce*: ?UInt256
gasPrice*: ?UInt256
gasLimit*: ?UInt256
ContractError* = object of EthersError
Confirmable* = ?TransactionResponse
EventHandler*[E: Event] = proc(event: E) {.gcsafe, upraises:[].}
@ -48,13 +53,16 @@ template raiseContractError(message: string) =
proc createTransaction(contract: Contract,
function: string,
parameters: tuple,
overrides = Transaction.default): Transaction =
overrides = TransactionOverrides.default): Transaction =
let selector = selector(function, typeof parameters).toArray
let data = @selector & AbiEncoder.encode(parameters)
var transaction = overrides
transaction.to = contract.address
transaction.data = data
return transaction
Transaction(
to: contract.address,
data: data,
gasPrice: overrides.gasPrice,
gasLimit: overrides.gasLimit,
nonce: overrides.nonce
)
proc decodeResponse(T: type, multiple: static bool, bytes: seq[byte]): T =
when multiple:
@ -84,7 +92,7 @@ proc call(contract: Contract,
proc send(contract: Contract,
function: string,
parameters: tuple,
overrides = Transaction.default):
overrides = TransactionOverrides.default):
Future[?TransactionResponse] {.async.} =
if signer =? contract.signer:
let transaction = createTransaction(contract, function, parameters, overrides)
@ -122,7 +130,7 @@ func addOverrides(procedure: var NimNode) =
newIdentDefs(
ident("overrides"),
newEmptyNode(),
quote do: Transaction.default
quote do: TransactionOverrides.default
)
)

View File

@ -114,7 +114,7 @@ suite "Contracts":
check (await token.connect(provider).balanceOf(accounts[2])) == 25.u256
test "takes custom values for nonce, gasprice and gaslimit":
let overrides = Transaction(
let overrides = TransactionOverrides(
nonce: some 100.u256,
gasPrice: some 200.u256,
gasLimit: some 300.u256