From 09810e73ff734db8f274e4f5dee1d0ac58cb77f2 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 29 Jun 2023 10:11:13 +0200 Subject: [PATCH] Move `confirm()` override into contract module And simplify its test --- ethers/contract.nim | 18 ++++++++++++++++++ ethers/provider.nim | 18 ------------------ testmodule/testContracts.nim | 23 +++++++---------------- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/ethers/contract.nim b/ethers/contract.nim index db7bfa5..f079aa5 100644 --- a/ethers/contract.nim +++ b/ethers/contract.nim @@ -234,3 +234,21 @@ proc subscribe*[E: Event](contract: Contract, handler(event) contract.provider.subscribe(filter, logHandler) + +proc confirm*(tx: Future[?TransactionResponse], + confirmations: int = EthersDefaultConfirmations, + timeout: int = EthersReceiptTimeoutBlks): + Future[TransactionReceipt] {.async.} = + ## Convenience method that allows confirm to be chained to a contract + ## transaction, eg: + ## `await token.connect(signer0) + ## .mint(accounts[1], 100.u256) + ## .confirm(3)` + + without response =? (await tx): + raise newException( + EthersError, + "Transaction hash required. Possibly was a call instead of a send?" + ) + + return await response.confirm(confirmations, timeout) diff --git a/ethers/provider.nim b/ethers/provider.nim index dfc7778..c09fa69 100644 --- a/ethers/provider.nim +++ b/ethers/provider.nim @@ -158,23 +158,5 @@ proc confirm*(tx: Future[TransactionResponse], let txResp = await tx return await txResp.confirm(confirmations, timeout) -proc confirm*(tx: Future[?TransactionResponse], - confirmations: int = EthersDefaultConfirmations, - timeout: int = EthersReceiptTimeoutBlks): - Future[TransactionReceipt] {.async.} = - ## Convenience method that allows wait to be chained to a contract - ## transaction, eg: - ## `await token.connect(signer0) - ## .mint(accounts[1], 100.u256) - ## .confirm(3)` - - without txResp =? (await tx): - raise newException( - EthersError, - "Transaction hash required. Possibly was a call instead of a send?" - ) - - return await txResp.confirm(confirmations, timeout) - method close*(provider: Provider) {.async, base.} = discard diff --git a/testmodule/testContracts.nim b/testmodule/testContracts.nim index ab6bb8a..1dab234 100644 --- a/testmodule/testContracts.nim +++ b/testmodule/testContracts.nim @@ -181,20 +181,11 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]: check transfers.len == 1 test "can wait for contract interaction tx to be mined": - # must not be awaited so we can get newHeads inside of .wait - let futMined = provider.mineBlocks(10) - let signer0 = provider.getSigner(accounts[0]) - let receipt = await token.connect(signer0) - .mint(accounts[1], 100.u256) - .confirm(3) # wait for 3 confirmations - let endBlock = await provider.getBlockNumber() - - check receipt.blockNumber.isSome # was eventually mined - - # >= 3 because more blocks may have been mined by the time the - # check in `.wait` was done. - # +1 for the block the tx was mined in - check (endBlock - !receipt.blockNumber) + 1 >= 3 - - await futMined + let confirming = token.connect(signer0) + .mint(accounts[1], 100.u256) + .confirm(3) + await sleepAsync(100.millis) # wait for tx to be mined + await provider.mineBlocks(2) # two additional blocks + let receipt = await confirming + check receipt.blockNumber.isSome