nwaku/waku/incentivization/txid_proof.nim

44 lines
1.7 KiB
Nim
Raw Normal View History

import std/options, chronos, web3, stew/byteutils, stint, strutils
import waku/incentivization/rpc
const SimpleTransferGasUsed = Quantity(21000)
proc checkTxIdIsEligible(txHash: TxHash, ethClient: string): Future[bool] {.async.} =
let web3 = await newWeb3(ethClient)
try:
let tx = await web3.provider.eth_getTransactionByHash(txHash)
let txReceipt = await web3.getMinedTransactionReceipt(txHash)
2024-12-11 15:29:56 +00:00
# check that it is not a contract creation tx
let toAddressOption = txReceipt.to
if toAddressOption.isNone:
# this is a contract creation tx
return false
# 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 false
# check that the amount is "as expected" (hard-coded for now)
let txValue = tx.value
let hasExpectedValue = (txValue == 200500000000005063.u256)
# check that the to address is "as expected" (hard-coded for now)
let toAddress = toAddressOption.get()
2024-12-13 07:25:36 +00:00
let hasExpectedToAddress =
(toAddress == Address.fromHex("0x5e809a85aa182a9921edd10a4163745bb3e36284"))
defer:
await web3.close()
2024-12-12 09:47:00 +00:00
return hasExpectedValue and hasExpectedToAddress
except ValueError as e:
return false
proc txidEligiblityCriteriaMet*(
eligibilityProof: EligibilityProof, ethClient: string
): Future[bool] {.async.} =
if eligibilityProof.proofOfPayment.isNone():
return false
let txHash = TxHash.fromHex(byteutils.toHex(eligibilityProof.proofOfPayment.get()))
let txExists = await checkTxIdIsEligible(txHash, ethClient)
return txExists