From e856b57669b4b8f90e9ae19d18a41cd20a8f0004 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 13 Apr 2023 12:03:14 +0200 Subject: [PATCH] Use "pending" blocktag by default, instead of "latest" This allows better simulation of the effects of sending a transaction before actually sending it. Especially when the block timestamp is used in the transaction. --- ethers/contract.nim | 10 ++++------ ethers/provider.nim | 4 ++-- ethers/providers/jsonrpc.nim | 2 +- ethers/signer.nim | 4 ++-- testmodule/testJsonRpcSigner.nim | 4 ++-- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/ethers/contract.nim b/ethers/contract.nim index 100d316..dc2f55c 100644 --- a/ethers/contract.nim +++ b/ethers/contract.nim @@ -80,19 +80,17 @@ proc decodeResponse(T: type, multiple: static bool, bytes: seq[byte]): T = proc call(contract: Contract, function: string, - parameters: tuple, - blockTag = BlockTag.latest) {.async.} = + parameters: tuple) {.async.} = let transaction = createTransaction(contract, function, parameters) - discard await contract.provider.call(transaction, blockTag) + discard await contract.provider.call(transaction) proc call(contract: Contract, function: string, parameters: tuple, ReturnType: type, - returnMultiple: static bool, - blockTag = BlockTag.latest): Future[ReturnType] {.async.} = + returnMultiple: static bool): Future[ReturnType] {.async.} = let transaction = createTransaction(contract, function, parameters) - let response = await contract.provider.call(transaction, blockTag) + let response = await contract.provider.call(transaction) return decodeResponse(ReturnType, returnMultiple, response) proc send(contract: Contract, diff --git a/ethers/provider.nim b/ethers/provider.nim index 5e447de..79f0eae 100644 --- a/ethers/provider.nim +++ b/ethers/provider.nim @@ -59,7 +59,7 @@ method getBlock*(provider: Provider, tag: BlockTag): Future[?Block] {.base.} = method call*(provider: Provider, tx: Transaction, - blockTag = BlockTag.latest): Future[seq[byte]] {.base.} = + blockTag = BlockTag.pending): Future[seq[byte]] {.base.} = doAssert false, "not implemented" method getGasPrice*(provider: Provider): Future[UInt256] {.base.} = @@ -67,7 +67,7 @@ method getGasPrice*(provider: Provider): Future[UInt256] {.base.} = method getTransactionCount*(provider: Provider, address: Address, - blockTag = BlockTag.latest): + blockTag = BlockTag.pending): Future[UInt256] {.base.} = doAssert false, "not implemented" diff --git a/ethers/providers/jsonrpc.nim b/ethers/providers/jsonrpc.nim index 942ea2a..4968706 100644 --- a/ethers/providers/jsonrpc.nim +++ b/ethers/providers/jsonrpc.nim @@ -133,7 +133,7 @@ method getGasPrice*(provider: JsonRpcProvider): Future[UInt256] {.async.} = method getTransactionCount*(provider: JsonRpcProvider, address: Address, - blockTag = BlockTag.latest): + blockTag = BlockTag.pending): Future[UInt256] {.async.} = convertError: let client = await provider.client diff --git a/ethers/signer.nim b/ethers/signer.nim index 3405056..85dd367 100644 --- a/ethers/signer.nim +++ b/ethers/signer.nim @@ -27,7 +27,7 @@ method getGasPrice*(signer: Signer): Future[UInt256] {.base.} = signer.provider.getGasPrice() method getTransactionCount*(signer: Signer, - blockTag = BlockTag.latest): + blockTag = BlockTag.pending): Future[UInt256] {.base, async.} = let address = await signer.getAddress() return await signer.provider.getTransactionCount(address, blockTag) @@ -55,7 +55,7 @@ method populateTransaction*(signer: Signer, if transaction.sender.isNone: populated.sender = some(await signer.getAddress()) if transaction.nonce.isNone: - populated.nonce = some(await signer.getTransactionCount(BlockTag.pending)) + populated.nonce = some(await signer.getTransactionCount()) if transaction.chainId.isNone: populated.chainId = some(await signer.getChainId()) if transaction.gasPrice.isNone and (transaction.maxFee.isNone or transaction.maxPriorityFee.isNone): diff --git a/testmodule/testJsonRpcSigner.nim b/testmodule/testJsonRpcSigner.nim index 1c4415a..0503f41 100644 --- a/testmodule/testJsonRpcSigner.nim +++ b/testmodule/testJsonRpcSigner.nim @@ -27,7 +27,7 @@ suite "JsonRpcSigner": test "can retrieve transaction count": let signer = provider.getSigner(accounts[9]) - let count = await signer.getTransactionCount(BlockTag.pending) + let count = await signer.getTransactionCount() check count == 0.u256 test "can estimate gas cost of a transaction": @@ -51,7 +51,7 @@ suite "JsonRpcSigner": let populated = await signer.populateTransaction(transaction) check !populated.sender == await signer.getAddress() check !populated.gasPrice == await signer.getGasPrice() - check !populated.nonce == await signer.getTransactionCount(BlockTag.pending) + check !populated.nonce == await signer.getTransactionCount() check !populated.gasLimit == await signer.estimateGas(transaction) check !populated.chainId == await signer.getChainId()