Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3129.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add transaction result formatter for ``data`` field. Nethermind, for example, returns both ``data`` and ``input`` for transaction results.
27 changes: 15 additions & 12 deletions tests/core/contracts/test_contract_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from eth_utils import (
decode_hex,
)
from hexbytes import (
HexBytes,
)

from web3._utils.contract_sources.contract_data.constructor_contracts import (
CONSTRUCTOR_WITH_ADDRESS_ARGUMENT_CONTRACT_RUNTIME,
Expand Down Expand Up @@ -217,11 +220,11 @@ def test_contract_constructor_build_transaction_no_constructor(
txn = w3.eth.get_transaction(txn_hash)
nonce = w3.eth.get_transaction_count(w3.eth.coinbase)
unsent_txn = math_contract_factory.constructor().build_transaction({"nonce": nonce})
assert txn["data"] == unsent_txn["data"]
assert txn["data"] == HexBytes(unsent_txn["data"])

new_txn_hash = w3.eth.send_transaction(unsent_txn)
new_txn = w3.eth.get_transaction(new_txn_hash)
assert new_txn["data"] == unsent_txn["data"]
assert new_txn["data"] == HexBytes(unsent_txn["data"])
assert new_txn["nonce"] == nonce


Expand All @@ -234,11 +237,11 @@ def test_contract_constructor_build_transaction_without_arguments(
txn = w3.eth.get_transaction(txn_hash)
nonce = w3.eth.get_transaction_count(w3.eth.coinbase)
unsent_txn = math_contract_factory.constructor().build_transaction({"nonce": nonce})
assert txn["data"] == unsent_txn["data"]
assert txn["data"] == HexBytes(unsent_txn["data"])

new_txn_hash = w3.eth.send_transaction(unsent_txn)
new_txn = w3.eth.get_transaction(new_txn_hash)
assert new_txn["data"] == unsent_txn["data"]
assert new_txn["data"] == HexBytes(unsent_txn["data"])
assert new_txn["nonce"] == nonce


Expand Down Expand Up @@ -266,11 +269,11 @@ def test_contract_constructor_build_transaction_with_arguments(
unsent_txn = non_strict_contract_with_constructor_args_factory.constructor(
*constructor_args, **constructor_kwargs
).build_transaction({"nonce": nonce})
assert txn["data"] == unsent_txn["data"]
assert txn["data"] == HexBytes(unsent_txn["data"])

new_txn_hash = w3_non_strict_abi.eth.send_transaction(unsent_txn)
new_txn = w3_non_strict_abi.eth.get_transaction(new_txn_hash)
assert new_txn["data"] == unsent_txn["data"]
assert new_txn["data"] == HexBytes(unsent_txn["data"])
assert new_txn["nonce"] == nonce


Expand Down Expand Up @@ -525,11 +528,11 @@ async def test_async_contract_constructor_build_transaction_no_constructor(
unsent_txn = await async_math_contract_factory.constructor().build_transaction(
{"nonce": nonce}
)
assert txn["data"] == unsent_txn["data"]
assert txn["data"] == HexBytes(unsent_txn["data"])

new_txn_hash = await async_w3.eth.send_transaction(unsent_txn)
new_txn = await async_w3.eth.get_transaction(new_txn_hash)
assert new_txn["data"] == unsent_txn["data"]
assert new_txn["data"] == HexBytes(unsent_txn["data"])
assert new_txn["nonce"] == nonce


Expand All @@ -547,11 +550,11 @@ async def test_async_contract_constructor_build_transaction_without_arguments(
unsent_txn = await async_math_contract_factory.constructor().build_transaction(
{"nonce": nonce}
)
assert txn["data"] == unsent_txn["data"]
assert txn["data"] == HexBytes(unsent_txn["data"])

new_txn_hash = await async_w3.eth.send_transaction(unsent_txn)
new_txn = await async_w3.eth.get_transaction(new_txn_hash)
assert new_txn["data"] == unsent_txn["data"]
assert new_txn["data"] == HexBytes(unsent_txn["data"])
assert new_txn["nonce"] == nonce


Expand Down Expand Up @@ -586,9 +589,9 @@ async def test_async_contract_constructor_build_transaction_with_arguments(
*constructor_args, **constructor_kwargs
).build_transaction({"nonce": nonce})
)
assert txn["data"] == unsent_txn["data"]
assert txn["data"] == HexBytes(unsent_txn["data"])

new_txn_hash = await async_w3_non_strict_abi.eth.send_transaction(unsent_txn)
new_txn = await async_w3_non_strict_abi.eth.get_transaction(new_txn_hash)
assert new_txn["data"] == unsent_txn["data"]
assert new_txn["data"] == HexBytes(unsent_txn["data"])
assert new_txn["nonce"] == nonce
121 changes: 121 additions & 0 deletions tests/core/eth-module/test_transactions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import pytest

from eth_utils import (
to_checksum_address,
to_int,
)
from hexbytes import (
HexBytes,
)

from web3._utils.ens import (
ens_addresses,
)
from web3._utils.rpc_abi import (
RPC,
)
from web3.datastructures import (
AttributeDict,
)
from web3.exceptions import (
NameNotFound,
TimeExhausted,
TransactionNotFound,
Web3ValidationError,
)
from web3.middleware import (
construct_result_generator_middleware,
)
from web3.middleware.simulate_unmined_transaction import (
unmined_receipt_simulator_middleware,
)
Expand Down Expand Up @@ -174,3 +191,107 @@ def test_unmined_transaction_wait_for_receipt(w3):
txn_receipt = w3.eth.wait_for_transaction_receipt(txn_hash)
assert txn_receipt["transactionHash"] == txn_hash
assert txn_receipt["blockHash"] is not None


def test_get_transaction_formatters(w3):
non_checksummed_addr = "0xB2930B35844A230F00E51431ACAE96FE543A0347" # all uppercase
unformatted_transaction = {
"blockHash": (
"0x849044202a39ae36888481f90d62c3826bca8269c2716d7a38696b4f45e61d83"
),
"blockNumber": "0x1b4",
"transactionIndex": "0x0",
"nonce": "0x0",
"gas": "0x4c4b40",
"gasPrice": "0x1",
"maxFeePerGas": "0x1",
"maxPriorityFeePerGas": "0x1",
"value": "0x1",
"from": non_checksummed_addr,
"publicKey": "0x",
"r": "0xd148ae70c8cbef3a038e70e6d1639f0951e60a2965820f33bad19d0a6c2b8116",
"raw": "0x142ab034696c09dcfb2a8b086b494f3f4c419e67b6c04d95882f87156a3b6f35",
"s": "0x6f5216fc207221a11efe2e4c3e3a881a0b5ca286ede538fc9dbc403b2009ea76",
"to": non_checksummed_addr,
"hash": "0x142ab034696c09dcfb2a8b086b494f3f4c419e67b6c04d95882f87156a3b6f35",
"v": "0x1",
"yParity": "0x1",
"standardV": "0x1",
"type": "0x2",
"chainId": "0x539",
"accessList": [
{
"address": non_checksummed_addr,
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000032", # noqa: E501
"0x0000000000000000000000000000000000000000000000000000000000000036", # noqa: E501
],
},
{
"address": non_checksummed_addr,
"storageKeys": [],
},
],
"input": "0x5b34b966",
"data": "0x5b34b966",
}

result_middleware = construct_result_generator_middleware(
{
RPC.eth_getTransactionByHash: lambda *_: unformatted_transaction,
}
)
w3.middleware_onion.inject(result_middleware, "result_middleware", layer=0)

# test against eth_getTransactionByHash
received_tx = w3.eth.get_transaction("")

checksummed_addr = to_checksum_address(non_checksummed_addr)
assert non_checksummed_addr != checksummed_addr

expected = AttributeDict(
{
"blockHash": HexBytes(unformatted_transaction["blockHash"]),
"blockNumber": to_int(hexstr=unformatted_transaction["blockNumber"]),
"transactionIndex": 0,
"nonce": 0,
"gas": to_int(hexstr=unformatted_transaction["gas"]),
"gasPrice": 1,
"maxFeePerGas": 1,
"maxPriorityFeePerGas": 1,
"value": 1,
"from": checksummed_addr,
"publicKey": HexBytes(unformatted_transaction["publicKey"]),
"r": HexBytes(unformatted_transaction["r"]),
"raw": HexBytes(unformatted_transaction["raw"]),
"s": HexBytes(unformatted_transaction["s"]),
"to": to_checksum_address(non_checksummed_addr),
"hash": HexBytes(unformatted_transaction["hash"]),
"v": 1,
"yParity": 1,
"standardV": 1,
"type": 2,
"chainId": 1337,
"accessList": [
AttributeDict(
{
"address": checksummed_addr,
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000032", # noqa: E501
"0x0000000000000000000000000000000000000000000000000000000000000036", # noqa: E501
],
}
),
AttributeDict(
{
"address": checksummed_addr,
"storageKeys": [],
}
),
],
"input": HexBytes(unformatted_transaction["input"]),
"data": HexBytes(unformatted_transaction["data"]),
}
)
assert received_tx == expected
w3.middleware_onion.remove("result_middleware")
1 change: 1 addition & 0 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def type_aware_apply_formatters_to_dict_keys_and_values(
),
),
"input": HexBytes,
"data": HexBytes, # Nethermind, for example, returns both `input` and `data`
}


Expand Down