Skip to content

Commit 5db56d5

Browse files
committed
Add formatter for data field for transaction results
- Nethermind returns both ``data`` and ``input`` with the same value for each. Add a formatter for ``data`` that will format the field if it is present. - Add a test for transaction result formatters via ``eth.get_transaction()`` test.
1 parent 9c88416 commit 5db56d5

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

newsfragments/3129.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add transaction result formatter for ``data`` field. Nethermind, for example, returns both ``data`` and ``input`` for transaction results.

tests/core/eth-module/test_transactions.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
import pytest
22

3+
from eth_utils import (
4+
to_checksum_address,
5+
to_int,
6+
)
7+
from hexbytes import (
8+
HexBytes,
9+
)
10+
311
from web3._utils.ens import (
412
ens_addresses,
513
)
14+
from web3._utils.rpc_abi import (
15+
RPC,
16+
)
17+
from web3.datastructures import (
18+
AttributeDict,
19+
)
620
from web3.exceptions import (
721
NameNotFound,
822
TimeExhausted,
923
TransactionNotFound,
1024
Web3ValidationError,
1125
)
26+
from web3.middleware import (
27+
construct_result_generator_middleware,
28+
)
1229
from web3.middleware.simulate_unmined_transaction import (
1330
unmined_receipt_simulator_middleware,
1431
)
@@ -174,3 +191,102 @@ def test_unmined_transaction_wait_for_receipt(w3):
174191
txn_receipt = w3.eth.wait_for_transaction_receipt(txn_hash)
175192
assert txn_receipt["transactionHash"] == txn_hash
176193
assert txn_receipt["blockHash"] is not None
194+
195+
196+
def test_get_transaction_formatters(w3):
197+
non_checksummed_addr = "0xB2930B35844A230F00E51431ACAE96FE543A0347" # all uppercase
198+
unformatted_transaction = {
199+
"blockHash": (
200+
"0x849044202a39ae36888481f90d62c3826bca8269c2716d7a38696b4f45e61d83"
201+
),
202+
"blockNumber": "0x1b4",
203+
"transactionIndex": "0x0",
204+
"nonce": "0x0",
205+
"gas": "0x4c4b40",
206+
"gasPrice": "0x1",
207+
"maxFeePerGas": "0x1",
208+
"maxPriorityFeePerGas": "0x1",
209+
"value": "0x1",
210+
"from": non_checksummed_addr,
211+
"publicKey": "0x",
212+
"r": "0xd148ae70c8cbef3a038e70e6d1639f0951e60a2965820f33bad19d0a6c2b8116",
213+
"raw": "0x142ab034696c09dcfb2a8b086b494f3f4c419e67b6c04d95882f87156a3b6f35",
214+
"s": "0x6f5216fc207221a11efe2e4c3e3a881a0b5ca286ede538fc9dbc403b2009ea76",
215+
"to": non_checksummed_addr,
216+
"hash": "0x142ab034696c09dcfb2a8b086b494f3f4c419e67b6c04d95882f87156a3b6f35",
217+
"v": "0x1",
218+
"yParity": "0x1",
219+
"standardV": "0x1",
220+
"type": "0x2",
221+
"chainId": "0x539",
222+
"accessList": [
223+
{
224+
"address": non_checksummed_addr,
225+
"storageKeys": [
226+
"0x0000000000000000000000000000000000000000000000000000000000000032", # noqa: E501
227+
"0x0000000000000000000000000000000000000000000000000000000000000036", # noqa: E501
228+
],
229+
},
230+
{
231+
"address": non_checksummed_addr,
232+
"storageKeys": [],
233+
},
234+
],
235+
"input": "0x5b34b966",
236+
"data": "0x5b34b966",
237+
}
238+
239+
result_middleware = construct_result_generator_middleware(
240+
{
241+
RPC.eth_getTransactionByHash: lambda *_: unformatted_transaction,
242+
}
243+
)
244+
w3.middleware_onion.inject(result_middleware, "result_middleware", layer=0)
245+
received_block = w3.eth.get_transaction("")
246+
247+
expected = AttributeDict(
248+
{
249+
"blockHash": HexBytes(unformatted_transaction["blockHash"]),
250+
"blockNumber": to_int(hexstr=unformatted_transaction["blockNumber"]),
251+
"transactionIndex": 0,
252+
"nonce": 0,
253+
"gas": to_int(hexstr=unformatted_transaction["gas"]),
254+
"gasPrice": 1,
255+
"maxFeePerGas": 1,
256+
"maxPriorityFeePerGas": 1,
257+
"value": 1,
258+
"from": to_checksum_address(non_checksummed_addr),
259+
"publicKey": HexBytes(unformatted_transaction["publicKey"]),
260+
"r": HexBytes(unformatted_transaction["r"]),
261+
"raw": HexBytes(unformatted_transaction["raw"]),
262+
"s": HexBytes(unformatted_transaction["s"]),
263+
"to": to_checksum_address(non_checksummed_addr),
264+
"hash": HexBytes(unformatted_transaction["hash"]),
265+
"v": 1,
266+
"yParity": 1,
267+
"standardV": 1,
268+
"type": 2,
269+
"chainId": 1337,
270+
"accessList": [
271+
AttributeDict(
272+
{
273+
"address": to_checksum_address(non_checksummed_addr),
274+
"storageKeys": [
275+
"0x0000000000000000000000000000000000000000000000000000000000000032", # noqa: E501
276+
"0x0000000000000000000000000000000000000000000000000000000000000036", # noqa: E501
277+
],
278+
}
279+
),
280+
AttributeDict(
281+
{
282+
"address": to_checksum_address(non_checksummed_addr),
283+
"storageKeys": [],
284+
}
285+
),
286+
],
287+
"input": HexBytes(unformatted_transaction["input"]),
288+
"data": HexBytes(unformatted_transaction["data"]),
289+
}
290+
)
291+
assert received_block == expected
292+
w3.middleware_onion.remove("result_middleware")

web3/_utils/method_formatters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def type_aware_apply_formatters_to_dict_keys_and_values(
219219
),
220220
),
221221
"input": HexBytes,
222+
"data": HexBytes, # Nethermind, for example, returns both `input` and `data`
222223
}
223224

224225

0 commit comments

Comments
 (0)