Skip to content

Commit

Permalink
fix conversion from string to Address in tx eligibility check
Browse files Browse the repository at this point in the history
  • Loading branch information
s-tikhomirov committed Dec 6, 2024
1 parent c377113 commit fe89f5d
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions waku/incentivization/txid_proof.nim
Original file line number Diff line number Diff line change
@@ -1,42 +1,51 @@
import std/options, chronos, web3, stew/byteutils, stint
import std/options, chronos, web3, stew/byteutils, stint, strutils

import waku/incentivization/rpc


# Function to convert a hex string to Address
proc toAddress*(hexStr: string): Address =
# Remove the "0x" prefix if it exists
let cleaned = if hexStr.startsWith("0x"): hexStr[2..^1] else: hexStr

# Ensure the length is exactly 40 characters (20 bytes)
if cleaned.len != 40:
raise newException(ValueError, "Invalid hexadecimal string length for Address")

var arr: array[20, byte]
for i in 0 ..< 20:
let byteValue = cleaned[i * 2 ..< i * 2 + 2] # Get two hex characters
arr[i] = byte(parseHexInt(byteValue))

result = Address(arr)


proc checkTxIdIsEligible(txHash: TxHash, ethClient: string): Future[bool] {.async.} =
let web3 = await newWeb3(ethClient)
try:
let tx = await web3.provider.eth_getTransactionByHash(txHash)
echo tx
let txReceipt = await web3.getMinedTransactionReceipt(txHash)
echo txReceipt
result = true
echo "got tx and tx receipt"
if result:
# check that it is not a contract creation tx
let toAddressOption = txReceipt.to
let isContractCreationTx = toAddressOption.isNone
if isContractCreationTx:
echo "not eligible: contract creation tx!"
result = false
else:
# check that it is a simple transfer (not a contract call)
# a simple transfer uses 21000 gas
let gasUsed = txReceipt.gasUsed
let isSimpleTransferTx = (gasUsed == Quantity(21000))
echo "isSimpleTransferTx:" & $isSimpleTransferTx
if not isSimpleTransferTx:
result = false
else:
# check that the amount is "as expected" (hard-coded for now)
let txValue = tx.value
let hasExpectedValue = (txValue == 200500000000005063.u256)
echo "hasExpectedValue:" & $hasExpectedValue

# check that the to address is "as expected" (hard-coded for now)
let toAddress = toAddressOption.get()
let hasExpectedToAddress = (toAddress == address"0x5e809a85aa182a9921edd10a4163745bb3e36284")
echo "hasExpectedToAddress:" & $hasExpectedToAddress

let hasExpectedToAddress = (toAddress == toAddress("0x5e809a85aa182a9921edd10a4163745bb3e36284"))
result = true
except ValueError as e:
result = false
Expand Down

0 comments on commit fe89f5d

Please sign in to comment.