|
1 | 1 | import pytest |
2 | 2 |
|
| 3 | +from eth_utils import ( |
| 4 | + to_checksum_address, |
| 5 | + to_int, |
| 6 | +) |
| 7 | +from hexbytes import ( |
| 8 | + HexBytes, |
| 9 | +) |
| 10 | + |
3 | 11 | from web3._utils.ens import ( |
4 | 12 | ens_addresses, |
5 | 13 | ) |
| 14 | +from web3._utils.rpc_abi import ( |
| 15 | + RPC, |
| 16 | +) |
| 17 | +from web3.datastructures import ( |
| 18 | + AttributeDict, |
| 19 | +) |
6 | 20 | from web3.exceptions import ( |
7 | 21 | NameNotFound, |
8 | 22 | TimeExhausted, |
9 | 23 | TransactionNotFound, |
10 | 24 | Web3ValidationError, |
11 | 25 | ) |
| 26 | +from web3.middleware import ( |
| 27 | + construct_result_generator_middleware, |
| 28 | +) |
12 | 29 | from web3.middleware.simulate_unmined_transaction import ( |
13 | 30 | unmined_receipt_simulator_middleware, |
14 | 31 | ) |
@@ -174,3 +191,107 @@ def test_unmined_transaction_wait_for_receipt(w3): |
174 | 191 | txn_receipt = w3.eth.wait_for_transaction_receipt(txn_hash) |
175 | 192 | assert txn_receipt["transactionHash"] == txn_hash |
176 | 193 | 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 | + |
| 246 | + # test against eth_getTransactionByHash |
| 247 | + received_tx = w3.eth.get_transaction("") |
| 248 | + |
| 249 | + checksummed_addr = to_checksum_address(non_checksummed_addr) |
| 250 | + assert non_checksummed_addr != checksummed_addr |
| 251 | + |
| 252 | + expected = AttributeDict( |
| 253 | + { |
| 254 | + "blockHash": HexBytes(unformatted_transaction["blockHash"]), |
| 255 | + "blockNumber": to_int(hexstr=unformatted_transaction["blockNumber"]), |
| 256 | + "transactionIndex": 0, |
| 257 | + "nonce": 0, |
| 258 | + "gas": to_int(hexstr=unformatted_transaction["gas"]), |
| 259 | + "gasPrice": 1, |
| 260 | + "maxFeePerGas": 1, |
| 261 | + "maxPriorityFeePerGas": 1, |
| 262 | + "value": 1, |
| 263 | + "from": checksummed_addr, |
| 264 | + "publicKey": HexBytes(unformatted_transaction["publicKey"]), |
| 265 | + "r": HexBytes(unformatted_transaction["r"]), |
| 266 | + "raw": HexBytes(unformatted_transaction["raw"]), |
| 267 | + "s": HexBytes(unformatted_transaction["s"]), |
| 268 | + "to": to_checksum_address(non_checksummed_addr), |
| 269 | + "hash": HexBytes(unformatted_transaction["hash"]), |
| 270 | + "v": 1, |
| 271 | + "yParity": 1, |
| 272 | + "standardV": 1, |
| 273 | + "type": 2, |
| 274 | + "chainId": 1337, |
| 275 | + "accessList": [ |
| 276 | + AttributeDict( |
| 277 | + { |
| 278 | + "address": checksummed_addr, |
| 279 | + "storageKeys": [ |
| 280 | + "0x0000000000000000000000000000000000000000000000000000000000000032", # noqa: E501 |
| 281 | + "0x0000000000000000000000000000000000000000000000000000000000000036", # noqa: E501 |
| 282 | + ], |
| 283 | + } |
| 284 | + ), |
| 285 | + AttributeDict( |
| 286 | + { |
| 287 | + "address": checksummed_addr, |
| 288 | + "storageKeys": [], |
| 289 | + } |
| 290 | + ), |
| 291 | + ], |
| 292 | + "input": HexBytes(unformatted_transaction["input"]), |
| 293 | + "data": HexBytes(unformatted_transaction["data"]), |
| 294 | + } |
| 295 | + ) |
| 296 | + assert received_tx == expected |
| 297 | + w3.middleware_onion.remove("result_middleware") |
0 commit comments