From 801bf19157fbc32aa8b0efb60de511c99b9d5523 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 23 May 2022 17:39:00 +0200 Subject: [PATCH] Remove calls to Option.get --- ethers/provider.nim | 26 +++++++++++--------------- testmodule/testJsonRpcProvider.nim | 15 ++++++--------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/ethers/provider.nim b/ethers/provider.nim index ade5810..e659f9b 100644 --- a/ethers/provider.nim +++ b/ethers/provider.nim @@ -96,8 +96,6 @@ method subscribe*(provider: Provider, method unsubscribe*(subscription: Subscription) {.base, async.} = doAssert false, "not implemented" -import std/options # TODO - # Removed from `confirm` closure and exported so it can be tested. # Likely there is a better way func confirmations*(receiptBlk, atBlk: UInt256): UInt256 = @@ -109,21 +107,19 @@ func confirmations*(receiptBlk, atBlk: UInt256): UInt256 = # Removed from `confirm` closure and exported so it can be tested. # Likely there is a better way -func hasBeenMined*(receipt: ?TransactionReceipt, +func hasBeenMined*(receipt: TransactionReceipt, atBlock: UInt256, wantedConfirms: int): bool = ## Returns true if the transaction receipt has been returned from the node ## with a valid block number and block hash and the specified number of ## blocks have passed since the tx was mined (confirmations) - if receipt.isSome and - receipt.get.blockNumber.isSome and - receipt.get.blockNumber.get > 0 and + if number =? receipt.blockNumber and + number > 0 and # from ethers.js: "geth-etc" returns receipts before they are ready - receipt.get.blockHash.isSome: + receipt.blockHash.isSome: - let receiptBlock = receipt.get.blockNumber.get - return receiptBlock.confirmations(atBlock) >= wantedConfirms.u256 + return number.confirmations(atBlock) >= wantedConfirms.u256 return false @@ -153,12 +149,12 @@ proc confirm*(tx: TransactionResponse, without blkNum =? blk.number: return - let receipt = await provider.getTransactionReceipt(tx.hash) - if receipt.hasBeenMined(blkNum, wantedConfirms): + if receipt =? (await provider.getTransactionReceipt(tx.hash)) and + receipt.hasBeenMined(blkNum, wantedConfirms): # fire and forget discard subscription.unsubscribe() if not retFut.finished: - retFut.complete(receipt.get) + retFut.complete(receipt) elif timeoutInBlocks > 0: let blocksPassed = (blkNum - startBlock) + 1 @@ -171,9 +167,9 @@ proc confirm*(tx: TransactionResponse, # If our tx is already mined, return the receipt. Otherwise, check each # new block to see if the tx has been mined - let receipt = await provider.getTransactionReceipt(tx.hash) - if receipt.hasBeenMined(startBlock, wantedConfirms): - return receipt.get + if receipt =? (await provider.getTransactionReceipt(tx.hash)) and + receipt.hasBeenMined(startBlock, wantedConfirms): + return receipt else: subscription = await provider.subscribe(newBlock) return (await retFut) diff --git a/testmodule/testJsonRpcProvider.nim b/testmodule/testJsonRpcProvider.nim index e4d6e0b..f097241 100644 --- a/testmodule/testJsonRpcProvider.nim +++ b/testmodule/testJsonRpcProvider.nim @@ -201,45 +201,42 @@ suite "JsonRpcProvider": test "checks if transation has been mined correctly": - var receipt = TransactionReceipt.none + var receipt: TransactionReceipt var currentBlock = 1.u256 var wantedConfirms = 1 let blockHash = hexToByteArray[32]( "0x7b00154e06fe4f27a87208eba220efb4dbc52f7429549a39a17bba2e0d98b960" ).some - # if TransactionReceipt is none, should not be considered mined - check not receipt.hasBeenMined(currentBlock, wantedConfirms) - # missing blockHash receipt = TransactionReceipt( blockNumber: 1.u256.some - ).some + ) check not receipt.hasBeenMined(currentBlock, wantedConfirms) # missing block number receipt = TransactionReceipt( blockHash: blockHash - ).some + ) check not receipt.hasBeenMined(currentBlock, wantedConfirms) # block number is 0 receipt = TransactionReceipt( blockNumber: 0.u256.some - ).some + ) check not receipt.hasBeenMined(currentBlock, wantedConfirms) # not enough confirms receipt = TransactionReceipt( blockNumber: 1.u256.some - ).some + ) check not receipt.hasBeenMined(currentBlock, wantedConfirms) # success receipt = TransactionReceipt( blockNumber: 1.u256.some, blockHash: blockHash - ).some + ) currentBlock = int.high.u256 wantedConfirms = int.high check receipt.hasBeenMined(currentBlock, wantedConfirms)