refactor: avoid result and unnecesary branching

This commit is contained in:
Sergei Tikhomirov 2024-12-12 09:10:23 +01:00
parent 5167eb61a6
commit 829c1f6d9b
1 changed files with 19 additions and 21 deletions

View File

@ -10,28 +10,26 @@ proc checkTxIdIsEligible(txHash: TxHash, ethClient: string): Future[bool] {.asyn
let txReceipt = await web3.getMinedTransactionReceipt(txHash) let txReceipt = await web3.getMinedTransactionReceipt(txHash)
# check that it is not a contract creation tx # check that it is not a contract creation tx
let toAddressOption = txReceipt.to let toAddressOption = txReceipt.to
let isContractCreationTx = toAddressOption.isNone if toAddressOption.isNone:
if isContractCreationTx: # this is a contract creation tx
result = false return false
else:
# check that it is a simple transfer (not a contract call) # check that it is a simple transfer (not a contract call)
# a simple transfer uses 21000 gas # a simple transfer uses 21000 gas
let gasUsed = txReceipt.gasUsed let gasUsed = txReceipt.gasUsed
let isSimpleTransferTx = (gasUsed == Quantity(21000)) let isSimpleTransferTx = (gasUsed == Quantity(21000))
if not isSimpleTransferTx: if not isSimpleTransferTx:
result = false return false
else:
# check that the amount is "as expected" (hard-coded for now) # check that the amount is "as expected" (hard-coded for now)
let txValue = tx.value let txValue = tx.value
let hasExpectedValue = (txValue == 200500000000005063.u256) let hasExpectedValue = (txValue == 200500000000005063.u256)
# check that the to address is "as expected" (hard-coded for now) # check that the to address is "as expected" (hard-coded for now)
let toAddress = toAddressOption.get() let toAddress = toAddressOption.get()
let hasExpectedToAddress = (toAddress == Address.fromHex("0x5e809a85aa182a9921edd10a4163745bb3e36284")) let hasExpectedToAddress = (toAddress == Address.fromHex("0x5e809a85aa182a9921edd10a4163745bb3e36284"))
result = true defer:
except ValueError as e:
result = false
await web3.close() await web3.close()
result return (hasExpectedValue and hasExpectedToAddress)
except ValueError as e:
return false
proc txidEligiblityCriteriaMet*( proc txidEligiblityCriteriaMet*(
eligibilityProof: EligibilityProof, ethClient: string eligibilityProof: EligibilityProof, ethClient: string