From 458e9693976eb076abd7d458be0cf590ec8aa688 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Wed, 20 Sep 2023 13:09:23 +1000 Subject: [PATCH] Make Transaction.data optional eth_getTransactionByHash responses from geth don't include data, and there was an exception raised, "key not found: data" --- ethers/contract.nim | 2 +- ethers/providers/jsonrpc/signatures.nim | 2 +- ethers/transaction.nim | 4 +- ethers/wallet/signing.nim | 2 +- testmodule/examples.nim | 2 +- .../providers/jsonrpc/testConversions.nim | 47 +++++++++++++++++++ testmodule/testWallet.nim | 2 +- 7 files changed, 54 insertions(+), 7 deletions(-) diff --git a/ethers/contract.nim b/ethers/contract.nim index bdec859..3218aef 100644 --- a/ethers/contract.nim +++ b/ethers/contract.nim @@ -66,7 +66,7 @@ proc createTransaction(contract: Contract, let data = @selector & AbiEncoder.encode(parameters) Transaction( to: contract.address, - data: data, + data: some data, nonce: overrides.nonce, chainId: overrides.chainId, gasPrice: overrides.gasPrice, diff --git a/ethers/providers/jsonrpc/signatures.nim b/ethers/providers/jsonrpc/signatures.nim index 66abb4a..227c218 100644 --- a/ethers/providers/jsonrpc/signatures.nim +++ b/ethers/providers/jsonrpc/signatures.nim @@ -5,7 +5,7 @@ proc eth_call(transaction: Transaction, blockTag: BlockTag): seq[byte] proc eth_gasPrice(): UInt256 proc eth_getBlockByNumber(blockTag: BlockTag, includeTransactions: bool): ?Block proc eth_getLogs(filter: EventFilter | Filter | FilterByBlockHash): JsonNode -proc eth_getTransactionByHash(hash: BlockHash): ?Transaction +proc eth_getTransactionByHash(hash: TransactionHash): ?Transaction proc eth_getBlockByHash(hash: BlockHash, includeTransactions: bool): ?Block proc eth_getTransactionCount(address: Address, blockTag: BlockTag): UInt256 proc eth_estimateGas(transaction: Transaction): UInt256 diff --git a/ethers/transaction.nim b/ethers/transaction.nim index 92652af..1c1e7f9 100644 --- a/ethers/transaction.nim +++ b/ethers/transaction.nim @@ -4,7 +4,7 @@ import ./basics type Transaction* = object sender*: ?Address to*: Address - data*: seq[byte] + data*: ?seq[byte] value*: UInt256 nonce*: ?UInt256 chainId*: ?UInt256 @@ -19,7 +19,7 @@ func `$`*(transaction: Transaction): string = result &= "from: " & $sender & ", " result &= "to: " & $transaction.to & ", " result &= "value: " & $transaction.value & ", " - result &= "data: 0x" & $transaction.data.toHex + result &= "data: 0x" & $(transaction.data.?toHex) if nonce =? transaction.nonce: result &= ", nonce: " & $nonce if chainId =? transaction.chainId: diff --git a/ethers/wallet/signing.nim b/ethers/wallet/signing.nim index b56e406..734e8b2 100644 --- a/ethers/wallet/signing.nim +++ b/ethers/wallet/signing.nim @@ -26,7 +26,7 @@ func toSignableTransaction(transaction: Transaction): SignableTransaction = signable.gasLimit = GasInt(gasLimit.truncate(uint64)) signable.to = some EthAddress(transaction.to) signable.value = transaction.value - signable.payload = transaction.data + signable.payload = transaction.data |? newSeq[byte]() if maxFee =? transaction.maxFee and maxPriorityFee =? transaction.maxPriorityFee: diff --git a/testmodule/examples.nim b/testmodule/examples.nim index dfd7345..d2fc4f4 100644 --- a/testmodule/examples.nim +++ b/testmodule/examples.nim @@ -21,4 +21,4 @@ proc example*(_: type UInt256): UInt256 = UInt256.fromBytesBE(array[32, byte].example) proc example*(_: type Transaction): Transaction = - Transaction(to: Address.example, data: seq[byte].example) + Transaction(to: Address.example, data: some seq[byte].example) diff --git a/testmodule/providers/jsonrpc/testConversions.nim b/testmodule/providers/jsonrpc/testConversions.nim index 065729f..05edafb 100644 --- a/testmodule/providers/jsonrpc/testConversions.nim +++ b/testmodule/providers/jsonrpc/testConversions.nim @@ -94,3 +94,50 @@ suite "JSON Conversions": let receipt = TransactionReceipt.fromJson(json) check receipt.blockHash.isNone + + test "newHeads subcription raises exception when deserializing to Log": + let json = """{ + "parentHash":"0xd68d4d0f29307df51e1284fc8a13595ae700ef0f1128830a69e6854381363d42", + "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + "miner":"0x0000000000000000000000000000000000000000", + "stateRoot":"0x1f6f2d05de35bbfd50213be96ddf960d62b978b472c55d6ac223cd648cbbbbb0", + "transactionsRoot":"0xb9bb8a26abe091bb628ab2b6585c5af151aeb3984f4ba47a3c65d438283e069d", + "receiptsRoot":"0x33f229b7133e1ba3fb524b8af22d8184ca10b2da5bb170092a219c61ca023c1d", + "logsBloom":"0x00000000000000000000000000000000000000000020000000000002000000000000000000000000000000000000000000000000000008080000100200200000000000000000000000000008000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000010000040000000000100000000000800000000000000000000000000000000020000000000020000000000000000000000000000040000008000000000000000000020000000000002000000000000000000000000000000000000000000000000000001000010000000000000000020002000000020000000000000008002000000000000", + "difficulty":"0x2", + "number":"0x21d", + "gasLimit":"0x1c1b59a7", + "gasUsed":"0xda41b", + "timestamp":"0x6509410e", + "extraData":"0xd883010b05846765746888676f312e32302e32856c696e7578000000000000007102a27d75709b90ca9eb23cdaaccf4fc2d571d710f3bc5a7dc874f43af116a93ff832576a53c16f0d0aa1cd9e9a1dc0a60126c4d420f72b0866fc96ba6664f601", + "mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000", + "nonce":"0x0000000000000000", + "baseFeePerGas":"0x7", + "withdrawalsRoot":null, + "hash":"0x64066c7150c660e5357c4b6b02d836c10353dfa8edb32c805fca9367fd29c6e7" + }""" + expect ValueError: + discard Log.fromJson(parseJson(json)) + + test "missing data in Transaction isNone": + let json = %*{ + "blockHash":"0x595bffbe897e025ea2df3213c4cc52c3f3d69bc04b49011d558f1b0e70038922", + "blockNumber":"0x22e", + "from":"0xe00b677c29ff8d8fe6068530e2bc36158c54dd34", + "gas":"0x4d4bb", + "gasPrice":"0x3b9aca07", + "hash":"0xa31608907c338d6497b0c6ec81049d845c7d409490ebf78171f35143897ca790", + "input":"0x6368a471d26ff5c7f835c1a8203235e88846ce1a196d6e79df0eaedd1b8ed3deec2ae5c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000012a00000000000000000000000000000000000000000000000000000000000000", + "nonce":"0x3", + "to":"0x92f09aa59dccb892a9f5406ddd9c0b98f02ea57e", + "transactionIndex":"0x3", + "value":"0x0", + "type":"0x0", + "chainId":"0xc0de4", + "v":"0x181bec", + "r":"0x57ba18460934526333b80b0fea08737c363f3cd5fbec4a25a8a25e3e8acb362a", + "s":"0x33aa50bc8bd719b6b17ad0bf52006bf8943999198f2bf731eb33c118091000f2" + } + + let receipt = Transaction.fromJson(json) + check receipt.data.isNone diff --git a/testmodule/testWallet.nim b/testmodule/testWallet.nim index 703ccf5..4a8845b 100644 --- a/testmodule/testWallet.nim +++ b/testmodule/testWallet.nim @@ -66,7 +66,7 @@ suite "Wallet": gasPrice: some 20 * 10.u256.pow(9), gasLimit: some 21000.u256, value: 10.u256.pow(18), - data: @[] + data: none seq[byte] ) let signed = await wallet.signTransaction(transaction) check signed.toHex == "f86c098504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83"