Remove calls to Option.get

This commit is contained in:
Mark Spanbroek 2022-05-23 17:39:00 +02:00 committed by markspanbroek
parent 33df1e759d
commit 801bf19157
2 changed files with 17 additions and 24 deletions

View File

@ -96,8 +96,6 @@ method subscribe*(provider: Provider,
method unsubscribe*(subscription: Subscription) {.base, async.} = method unsubscribe*(subscription: Subscription) {.base, async.} =
doAssert false, "not implemented" doAssert false, "not implemented"
import std/options # TODO
# Removed from `confirm` closure and exported so it can be tested. # Removed from `confirm` closure and exported so it can be tested.
# Likely there is a better way # Likely there is a better way
func confirmations*(receiptBlk, atBlk: UInt256): UInt256 = 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. # Removed from `confirm` closure and exported so it can be tested.
# Likely there is a better way # Likely there is a better way
func hasBeenMined*(receipt: ?TransactionReceipt, func hasBeenMined*(receipt: TransactionReceipt,
atBlock: UInt256, atBlock: UInt256,
wantedConfirms: int): bool = wantedConfirms: int): bool =
## Returns true if the transaction receipt has been returned from the node ## 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 ## with a valid block number and block hash and the specified number of
## blocks have passed since the tx was mined (confirmations) ## blocks have passed since the tx was mined (confirmations)
if receipt.isSome and if number =? receipt.blockNumber and
receipt.get.blockNumber.isSome and number > 0 and
receipt.get.blockNumber.get > 0 and
# from ethers.js: "geth-etc" returns receipts before they are ready # 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 number.confirmations(atBlock) >= wantedConfirms.u256
return receiptBlock.confirmations(atBlock) >= wantedConfirms.u256
return false return false
@ -153,12 +149,12 @@ proc confirm*(tx: TransactionResponse,
without blkNum =? blk.number: without blkNum =? blk.number:
return return
let receipt = await provider.getTransactionReceipt(tx.hash) if receipt =? (await provider.getTransactionReceipt(tx.hash)) and
if receipt.hasBeenMined(blkNum, wantedConfirms): receipt.hasBeenMined(blkNum, wantedConfirms):
# fire and forget # fire and forget
discard subscription.unsubscribe() discard subscription.unsubscribe()
if not retFut.finished: if not retFut.finished:
retFut.complete(receipt.get) retFut.complete(receipt)
elif timeoutInBlocks > 0: elif timeoutInBlocks > 0:
let blocksPassed = (blkNum - startBlock) + 1 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 # If our tx is already mined, return the receipt. Otherwise, check each
# new block to see if the tx has been mined # new block to see if the tx has been mined
let receipt = await provider.getTransactionReceipt(tx.hash) if receipt =? (await provider.getTransactionReceipt(tx.hash)) and
if receipt.hasBeenMined(startBlock, wantedConfirms): receipt.hasBeenMined(startBlock, wantedConfirms):
return receipt.get return receipt
else: else:
subscription = await provider.subscribe(newBlock) subscription = await provider.subscribe(newBlock)
return (await retFut) return (await retFut)

View File

@ -201,45 +201,42 @@ suite "JsonRpcProvider":
test "checks if transation has been mined correctly": test "checks if transation has been mined correctly":
var receipt = TransactionReceipt.none var receipt: TransactionReceipt
var currentBlock = 1.u256 var currentBlock = 1.u256
var wantedConfirms = 1 var wantedConfirms = 1
let blockHash = hexToByteArray[32]( let blockHash = hexToByteArray[32](
"0x7b00154e06fe4f27a87208eba220efb4dbc52f7429549a39a17bba2e0d98b960" "0x7b00154e06fe4f27a87208eba220efb4dbc52f7429549a39a17bba2e0d98b960"
).some ).some
# if TransactionReceipt is none, should not be considered mined
check not receipt.hasBeenMined(currentBlock, wantedConfirms)
# missing blockHash # missing blockHash
receipt = TransactionReceipt( receipt = TransactionReceipt(
blockNumber: 1.u256.some blockNumber: 1.u256.some
).some )
check not receipt.hasBeenMined(currentBlock, wantedConfirms) check not receipt.hasBeenMined(currentBlock, wantedConfirms)
# missing block number # missing block number
receipt = TransactionReceipt( receipt = TransactionReceipt(
blockHash: blockHash blockHash: blockHash
).some )
check not receipt.hasBeenMined(currentBlock, wantedConfirms) check not receipt.hasBeenMined(currentBlock, wantedConfirms)
# block number is 0 # block number is 0
receipt = TransactionReceipt( receipt = TransactionReceipt(
blockNumber: 0.u256.some blockNumber: 0.u256.some
).some )
check not receipt.hasBeenMined(currentBlock, wantedConfirms) check not receipt.hasBeenMined(currentBlock, wantedConfirms)
# not enough confirms # not enough confirms
receipt = TransactionReceipt( receipt = TransactionReceipt(
blockNumber: 1.u256.some blockNumber: 1.u256.some
).some )
check not receipt.hasBeenMined(currentBlock, wantedConfirms) check not receipt.hasBeenMined(currentBlock, wantedConfirms)
# success # success
receipt = TransactionReceipt( receipt = TransactionReceipt(
blockNumber: 1.u256.some, blockNumber: 1.u256.some,
blockHash: blockHash blockHash: blockHash
).some )
currentBlock = int.high.u256 currentBlock = int.high.u256
wantedConfirms = int.high wantedConfirms = int.high
check receipt.hasBeenMined(currentBlock, wantedConfirms) check receipt.hasBeenMined(currentBlock, wantedConfirms)