|
| 1 | +import pytest |
| 2 | + |
| 3 | +from hexbytes import ( |
| 4 | + HexBytes, |
| 5 | +) |
| 6 | + |
| 7 | +from web3.utils.transaction import ( |
| 8 | + Web3Transaction, |
| 9 | +) |
| 10 | + |
| 11 | +ACCESS_LIST_TRANSACTION_TEST_CASE = { |
| 12 | + "expected_raw_transaction": "0x01f8e782076c22843b9aca00830186a09409616c3d61b3331fc4109a9e41a8bdb7d9776609865af3107a400086616263646566f872f85994de0b295669a9fd93d5f28d9ec85e40f4cb697baef842a00000000000000000000000000000000000000000000000000000000000000003a00000000000000000000000000000000000000000000000000000000000000007d694bb9bc244d798123fde783fcc1c72d3bb8c189413c001a08289e85fa00f8f7f78a53cf147a87b2a7f0d27e64d7571f9d06a802e365c3430a017dc77eae36c88937db4a5179f57edc6119701652f3f1c6f194d1210d638a061", # noqa: 501 |
| 13 | + "transaction": { |
| 14 | + "gas": "0x186a0", |
| 15 | + "gasPrice": "0x3b9aca00", |
| 16 | + "data": "0x616263646566", |
| 17 | + "nonce": "0x22", |
| 18 | + "to": "0x09616C3d61b3331fc4109a9E41a8BDB7d9776609", |
| 19 | + "value": "0x5af3107a4000", |
| 20 | + "accessList": ( # test case from EIP-2930 |
| 21 | + { |
| 22 | + "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae", |
| 23 | + "storageKeys": ( |
| 24 | + "0x0000000000000000000000000000000000000000000000000000000000000003", # noqa: E501 |
| 25 | + "0x0000000000000000000000000000000000000000000000000000000000000007", # noqa: E501 |
| 26 | + ), |
| 27 | + }, |
| 28 | + { |
| 29 | + "address": "0xbb9bc244d798123fde783fcc1c72d3bb8c189413", |
| 30 | + "storageKeys": (), |
| 31 | + }, |
| 32 | + ), |
| 33 | + "chainId": "0x76c", |
| 34 | + "v": "0x1", |
| 35 | + "r": "0x8289e85fa00f8f7f78a53cf147a87b2a7f0d27e64d7571f9d06a802e365c3430", |
| 36 | + "s": "0x17dc77eae36c88937db4a5179f57edc6119701652f3f1c6f194d1210d638a061", |
| 37 | + }, |
| 38 | +} |
| 39 | +DYNAMIC_FEE_TRANSACTION_TEST_CASE = { |
| 40 | + "expected_raw_transaction": "0x02f8758205390284773594008477359400830186a09496216849c49358b10257cb55b28ea603c874b05e865af3107a4000825544c001a0c3000cd391f991169ebfd5d3b9e93c89d31a61c998a21b07a11dc6b9d66f8a8ea022cfe8424b2fbd78b16c9911da1be2349027b0a3c40adf4b6459222323773f74", # noqa: 501 |
| 41 | + "transaction": { |
| 42 | + "gas": "0x186a0", |
| 43 | + "maxFeePerGas": "0x77359400", |
| 44 | + "maxPriorityFeePerGas": "0x77359400", |
| 45 | + "data": "0x5544", |
| 46 | + "nonce": "0x2", |
| 47 | + "to": "0x96216849c49358B10257cb55b28eA603c874b05E", |
| 48 | + "value": "0x5af3107a4000", |
| 49 | + "type": "0x2", |
| 50 | + "chainId": "0x539", |
| 51 | + "accessList": (), |
| 52 | + "v": "0x1", |
| 53 | + "r": "0xc3000cd391f991169ebfd5d3b9e93c89d31a61c998a21b07a11dc6b9d66f8a8e", |
| 54 | + "s": "0x22cfe8424b2fbd78b16c9911da1be2349027b0a3c40adf4b6459222323773f74", |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize( |
| 60 | + "txn", |
| 61 | + [ |
| 62 | + Web3Transaction.from_dict(ACCESS_LIST_TRANSACTION_TEST_CASE["transaction"]), |
| 63 | + Web3Transaction.from_bytes( |
| 64 | + HexBytes(ACCESS_LIST_TRANSACTION_TEST_CASE["expected_raw_transaction"]) |
| 65 | + ), |
| 66 | + ], |
| 67 | +) |
| 68 | +def test_access_list_transaction(txn): |
| 69 | + assert txn.typed_transaction.transaction_type == 1 |
| 70 | + assert txn.chain_id == 1_900 |
| 71 | + assert txn.nonce == 34 |
| 72 | + assert txn.gas == 100_000 |
| 73 | + assert txn.to == b"\tal=a\xb33\x1f\xc4\x10\x9a\x9eA\xa8\xbd\xb7\xd9wf\t" |
| 74 | + assert txn.value == 100_000_000_000_000 |
| 75 | + assert txn.data == b"abcdef" |
| 76 | + assert txn.gas_price == 1_000_000_000 |
| 77 | + |
| 78 | + |
| 79 | +def test_encode_access_list_transaction(): |
| 80 | + txn = Web3Transaction.from_dict(ACCESS_LIST_TRANSACTION_TEST_CASE["transaction"]) |
| 81 | + assert txn.encode() == HexBytes( |
| 82 | + ACCESS_LIST_TRANSACTION_TEST_CASE["expected_raw_transaction"] |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "txn", |
| 88 | + [ |
| 89 | + Web3Transaction.from_dict(DYNAMIC_FEE_TRANSACTION_TEST_CASE["transaction"]), |
| 90 | + Web3Transaction.from_bytes( |
| 91 | + HexBytes(DYNAMIC_FEE_TRANSACTION_TEST_CASE["expected_raw_transaction"]) |
| 92 | + ), |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_dynamic_fee_transaction(txn): |
| 96 | + assert txn.typed_transaction.transaction_type == 2 |
| 97 | + assert txn.chain_id == 1_337 |
| 98 | + assert txn.nonce == 2 |
| 99 | + assert txn.gas == 100_000 |
| 100 | + assert txn.to == b"\x96!hI\xc4\x93X\xb1\x02W\xcbU\xb2\x8e\xa6\x03\xc8t\xb0^" |
| 101 | + assert txn.value == 100_000_000_000_000 |
| 102 | + assert txn.data == b"UD" |
| 103 | + assert txn.max_priority_fee_per_gas == 2_000_000_000 |
| 104 | + assert txn.max_fee_per_gas == 2_000_000_000 |
| 105 | + |
| 106 | + |
| 107 | +def test_encode_dynamic_fee_transaction(): |
| 108 | + txn = Web3Transaction.from_dict(DYNAMIC_FEE_TRANSACTION_TEST_CASE["transaction"]) |
| 109 | + assert txn.encode() == HexBytes( |
| 110 | + DYNAMIC_FEE_TRANSACTION_TEST_CASE["expected_raw_transaction"] |
| 111 | + ) |
0 commit comments