mirror of https://github.com/waku-org/nwaku.git
make try-block smaller
This commit is contained in:
parent
a92ffc7a42
commit
4b60383f39
|
@ -18,33 +18,35 @@ proc isEligibleTxId*(
|
||||||
return err("Eligibility proof is empty")
|
return err("Eligibility proof is empty")
|
||||||
let txHash = TxHash.fromHex(byteutils.toHex(eligibilityProof.proofOfPayment.get()))
|
let txHash = TxHash.fromHex(byteutils.toHex(eligibilityProof.proofOfPayment.get()))
|
||||||
let web3 = await newWeb3(ethClient)
|
let web3 = await newWeb3(ethClient)
|
||||||
|
var tx: TransactionObject
|
||||||
|
var txReceipt: ReceiptObject
|
||||||
try:
|
try:
|
||||||
# TODO: make requests in parallel (?)
|
# TODO: make requests in parallel (?)
|
||||||
let tx = await web3.provider.eth_getTransactionByHash(txHash)
|
tx = await web3.provider.eth_getTransactionByHash(txHash)
|
||||||
let txReceipt = await web3.getMinedTransactionReceipt(txHash)
|
txReceipt = await web3.getMinedTransactionReceipt(txHash)
|
||||||
# check that it is not a contract creation tx
|
|
||||||
let toAddressOption = txReceipt.to
|
|
||||||
if toAddressOption.isNone:
|
|
||||||
# this is a contract creation tx
|
|
||||||
return err("A contract creation tx is not eligible")
|
|
||||||
# check that it is a simple transfer (not a contract call)
|
|
||||||
# a simple transfer uses 21000 gas
|
|
||||||
let gasUsed = txReceipt.gasUsed
|
|
||||||
let isSimpleTransferTx = (gasUsed == SimpleTransferGasUsed)
|
|
||||||
if not isSimpleTransferTx:
|
|
||||||
return err("A contract call tx is not eligible")
|
|
||||||
# check that the to address is "as expected"
|
|
||||||
let toAddress = toAddressOption.get()
|
|
||||||
if toAddress != expectedToAddress:
|
|
||||||
return err("Wrong destination address: " & $toAddress)
|
|
||||||
# check that the amount is "as expected"
|
|
||||||
let txValue = tx.value
|
|
||||||
if txValue != expectedValue:
|
|
||||||
return err("Wrong tx value: got " & $txValue & ", expected " & $expectedValue)
|
|
||||||
defer:
|
|
||||||
await web3.close()
|
|
||||||
return ok()
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
let errorMsg = "Failed to fetch tx or tx receipt: " & getCurrentExceptionMsg()
|
let errorMsg = "Failed to fetch tx or tx receipt: " & getCurrentExceptionMsg()
|
||||||
error "exception in isEligibleTxId", error = $errorMsg
|
error "exception in isEligibleTxId", error = $errorMsg
|
||||||
return err($errorMsg)
|
return err($errorMsg)
|
||||||
|
# check that it is not a contract creation tx
|
||||||
|
let toAddressOption = txReceipt.to
|
||||||
|
if toAddressOption.isNone:
|
||||||
|
# this is a contract creation tx
|
||||||
|
return err("A contract creation tx is not eligible")
|
||||||
|
# check that it is a simple transfer (not a contract call)
|
||||||
|
# a simple transfer uses 21000 gas
|
||||||
|
let gasUsed = txReceipt.gasUsed
|
||||||
|
let isSimpleTransferTx = (gasUsed == SimpleTransferGasUsed)
|
||||||
|
if not isSimpleTransferTx:
|
||||||
|
return err("A contract call tx is not eligible")
|
||||||
|
# check that the to address is "as expected"
|
||||||
|
let toAddress = toAddressOption.get()
|
||||||
|
if toAddress != expectedToAddress:
|
||||||
|
return err("Wrong destination address: " & $toAddress)
|
||||||
|
# check that the amount is "as expected"
|
||||||
|
let txValue = tx.value
|
||||||
|
if txValue != expectedValue:
|
||||||
|
return err("Wrong tx value: got " & $txValue & ", expected " & $expectedValue)
|
||||||
|
defer:
|
||||||
|
await web3.close()
|
||||||
|
return ok()
|
||||||
|
|
Loading…
Reference in New Issue