ethereum.forks.prague.transactions

Transactions are atomic units of work created externally to Ethereum and submitted to be executed. If Ethereum is viewed as a state machine, transactions are the events that move between states.

LegacyTransaction

Atomic operation performed on the block chain. This represents the original transaction format used before EIP-1559, EIP-2930, EIP-4844, and EIP-7702.

28
@slotted_freezable
29
@dataclass
class LegacyTransaction:

nonce

A scalar value equal to the number of transactions sent by the sender.

42
    nonce: U256

gas_price

The price of gas for this transaction, in wei.

47
    gas_price: Uint

gas

The maximum amount of gas that can be used by this transaction.

52
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

57
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

63
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

68
    data: Bytes

v

The recovery id of the signature.

74
    v: U256

r

The first part of the signature.

79
    r: U256

s

The second part of the signature.

84
    s: U256

Access

A mapping from account address to storage slots that are pre-warmed as part of a transaction.

90
@slotted_freezable
91
@dataclass
class Access:

account

The address of the account that is accessed.

98
    account: Address

slots

A tuple of storage slots that are accessed in the account.

103
    slots: Tuple[Bytes32, ...]

AccessListTransaction

The transaction type added in EIP-2930 to support access lists.

This transaction type extends the legacy transaction with an access list and chain ID. The access list specifies which addresses and storage slots the transaction will access.

109
@slotted_freezable
110
@dataclass
class AccessListTransaction:

chain_id

The ID of the chain on which this transaction is executed.

122
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

127
    nonce: U256

gas_price

The price of gas for this transaction.

132
    gas_price: Uint

gas

The maximum amount of gas that can be used by this transaction.

137
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

142
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

148
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

153
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

159
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

165
    y_parity: U256

r

The first part of the signature.

170
    r: U256

s

The second part of the signature.

175
    s: U256

FeeMarketTransaction

The transaction type added in EIP-1559.

This transaction type introduces a new fee market mechanism with two gas price parameters: max_priority_fee_per_gas and max_fee_per_gas.

181
@slotted_freezable
182
@dataclass
class FeeMarketTransaction:

chain_id

The ID of the chain on which this transaction is executed.

193
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

198
    nonce: U256

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

203
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

208
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

214
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

219
    to: Bytes0 | Address

value

The amount of ether (in wei) to send with this transaction.

225
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

230
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

236
    access_list: Tuple[Access, ...]

y_parity

The recovery id of the signature.

242
    y_parity: U256

r

The first part of the signature.

247
    r: U256

s

The second part of the signature.

252
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

This transaction type extends the fee market transaction to support blob-carrying transactions.

258
@slotted_freezable
259
@dataclass
class BlobTransaction:

chain_id

The ID of the chain on which this transaction is executed.

270
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

275
    nonce: U256

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

280
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

285
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

291
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

296
    to: Address

value

The amount of ether (in wei) to send with this transaction.

302
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

307
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

313
    access_list: Tuple[Access, ...]

max_fee_per_blob_gas

The maximum fee per blob gas that the sender is willing to pay.

319
    max_fee_per_blob_gas: U256

blob_versioned_hashes

A tuple of objects that represent the versioned hashes of the blobs included in the transaction.

324
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

The recovery id of the signature.

330
    y_parity: U256

r

The first part of the signature.

335
    r: U256

s

The second part of the signature.

340
    s: U256

SetCodeTransaction

The transaction type added in EIP-7702.

This transaction type allows Ethereum Externally Owned Accounts (EOAs) to set code on their account, enabling them to act as smart contracts.

346
@slotted_freezable
347
@dataclass
class SetCodeTransaction:

chain_id

The ID of the chain on which this transaction is executed.

358
    chain_id: U64

nonce

A scalar value equal to the number of transactions sent by the sender.

363
    nonce: U64

max_priority_fee_per_gas

The maximum priority fee per gas that the sender is willing to pay.

368
    max_priority_fee_per_gas: Uint

max_fee_per_gas

The maximum fee per gas that the sender is willing to pay, including the base fee and priority fee.

373
    max_fee_per_gas: Uint

gas

The maximum amount of gas that can be used by this transaction.

379
    gas: Uint

to

The address of the recipient. If empty, the transaction is a contract creation.

384
    to: Address

value

The amount of ether (in wei) to send with this transaction.

390
    value: U256

data

The data payload of the transaction, which can be used to call functions on contracts or to create new contracts.

395
    data: Bytes

access_list

A tuple of Access objects that specify which addresses and storage slots are accessed in the transaction.

401
    access_list: Tuple[Access, ...]

authorizations

A tuple of Authorization objects that specify what code the signer desires to execute in the context of their EOA.

407
    authorizations: Tuple[Authorization, ...]

y_parity

The recovery id of the signature.

413
    y_parity: U256

r

The first part of the signature.

418
    r: U256

s

The second part of the signature.

423
    s: U256

Transaction

Union type representing any valid transaction type.

429
Transaction = (
430
    LegacyTransaction
431
    | AccessListTransaction
432
    | FeeMarketTransaction
433
    | BlobTransaction
434
    | SetCodeTransaction
435
)

AccessListCapableTransaction

Transaction types that include an EIP-2930-style access list.

See has_access_list and Access for more details.

441
AccessListCapableTransaction = (
442
    AccessListTransaction
443
    | FeeMarketTransaction
444
    | BlobTransaction
445
    | SetCodeTransaction
446
)

encode_transaction

Encode a transaction into its RLP or typed transaction format. Needed because non-legacy transactions aren't RLP.

Legacy transactions are returned as-is, while other transaction types are prefixed with their type identifier and RLP encoded.

def encode_transaction(tx: Transaction) -> LegacyTransaction | Bytes:
459
    """
460
    Encode a transaction into its RLP or typed transaction format.
461
    Needed because non-legacy transactions aren't RLP.
462
463
    Legacy transactions are returned as-is, while other transaction types
464
    are prefixed with their type identifier and RLP encoded.
465
    """
466
    if isinstance(tx, LegacyTransaction):
467
        return tx
468
    elif isinstance(tx, AccessListTransaction):
469
        return b"\x01" + rlp.encode(tx)
470
    elif isinstance(tx, FeeMarketTransaction):
471
        return b"\x02" + rlp.encode(tx)
472
    elif isinstance(tx, BlobTransaction):
473
        return b"\x03" + rlp.encode(tx)
474
    elif isinstance(tx, SetCodeTransaction):
475
        return b"\x04" + rlp.encode(tx)
476
    else:
477
        raise Exception(f"Unable to encode transaction of type {type(tx)}")

decode_transaction

Decode a transaction from its RLP or typed transaction format. Needed because non-legacy transactions aren't RLP.

Legacy transactions are returned as-is, while other transaction types are decoded based on their type identifier prefix.

def decode_transaction(tx: LegacyTransaction | Bytes) -> Transaction:
481
    """
482
    Decode a transaction from its RLP or typed transaction format.
483
    Needed because non-legacy transactions aren't RLP.
484
485
    Legacy transactions are returned as-is, while other transaction types
486
    are decoded based on their type identifier prefix.
487
    """
488
    if isinstance(tx, Bytes):
489
        if tx[0] == 1:
490
            return rlp.decode_to(AccessListTransaction, tx[1:])
491
        elif tx[0] == 2:
492
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
493
        elif tx[0] == 3:
494
            return rlp.decode_to(BlobTransaction, tx[1:])
495
        elif tx[0] == 4:
496
            return rlp.decode_to(SetCodeTransaction, tx[1:])
497
        else:
498
            raise TransactionTypeError(tx[0])
499
    else:
500
        return tx

validate_transaction

Verifies a transaction.

The gas in a transaction gets used to pay for the intrinsic cost of operations, therefore if there is insufficient gas then it would not be possible to execute a transaction and it will be declared invalid.

Additionally, the nonce of a transaction must not equal or exceed the limit defined in EIP-2681. In practice, defining the limit as 2**64-1 has no impact because sending 2**64-1 transactions is improbable. It's not strictly impossible though, 2**64-1 transactions is the entire capacity of the Ethereum blockchain at 2022 gas limits for a little over 22 years.

Also, the code size of a contract creation transaction must be within limits of the protocol.

This function takes a transaction as a parameter and returns the intrinsic gas cost and the minimum calldata gas cost for the transaction after validation. It throws an InsufficientTransactionGasError exception if the transaction does not provide enough gas to cover the intrinsic cost, and a NonceOverflowError exception if the nonce is greater than 2**64 - 2. It also raises an InitCodeTooLargeError if the code size of a contract creation transaction exceeds the maximum allowed size.

def validate_transaction(tx: Transaction) -> Tuple[Uint, Uint]:
504
    """
505
    Verifies a transaction.
506
507
    The gas in a transaction gets used to pay for the intrinsic cost of
508
    operations, therefore if there is insufficient gas then it would not
509
    be possible to execute a transaction and it will be declared invalid.
510
511
    Additionally, the nonce of a transaction must not equal or exceed the
512
    limit defined in [EIP-2681].
513
    In practice, defining the limit as ``2**64-1`` has no impact because
514
    sending ``2**64-1`` transactions is improbable. It's not strictly
515
    impossible though, ``2**64-1`` transactions is the entire capacity of the
516
    Ethereum blockchain at 2022 gas limits for a little over 22 years.
517
518
    Also, the code size of a contract creation transaction must be within
519
    limits of the protocol.
520
521
    This function takes a transaction as a parameter and returns the intrinsic
522
    gas cost and the minimum calldata gas cost for the transaction after
523
    validation. It throws an `InsufficientTransactionGasError` exception if
524
    the transaction does not provide enough gas to cover the intrinsic cost,
525
    and a `NonceOverflowError` exception if the nonce is greater than
526
    `2**64 - 2`. It also raises an `InitCodeTooLargeError` if the code size of
527
    a contract creation transaction exceeds the maximum allowed size.
528
529
    [EIP-2681]: https://eips.ethereum.org/EIPS/eip-2681
530
    [EIP-7623]: https://eips.ethereum.org/EIPS/eip-7623
531
    """
532
    from .vm.interpreter import MAX_INIT_CODE_SIZE
533
534
    intrinsic_gas, calldata_floor_gas_cost = calculate_intrinsic_cost(tx)
535
    if max(intrinsic_gas, calldata_floor_gas_cost) > tx.gas:
536
        raise InsufficientTransactionGasError("Insufficient gas")
537
    if U256(tx.nonce) >= U256(U64.MAX_VALUE):
538
        raise NonceOverflowError("Nonce too high")
539
    if tx.to == Bytes0(b"") and len(tx.data) > MAX_INIT_CODE_SIZE:
540
        raise InitCodeTooLargeError("Code size too large")
541
542
    return intrinsic_gas, calldata_floor_gas_cost

calculate_intrinsic_cost

Calculates the gas that is charged before execution is started.

The intrinsic cost of the transaction is charged before execution has begun. Functions/operations in the EVM cost money to execute so this intrinsic cost is for the operations that need to be paid for as part of the transaction. Data transfer, for example, is part of this intrinsic cost. It costs ether to send data over the wire and that ether is accounted for in the intrinsic cost calculated in this function. This intrinsic cost must be calculated and paid for before execution in order for all operations to be implemented.

The intrinsic cost includes:

  1. Base cost (TX_BASE)

  2. Cost for data (zero and non-zero bytes)

  3. Cost for contract creation (if applicable)

  4. Cost for access list entries (if applicable)

  5. Cost for authorizations (if applicable)

This function takes a transaction as a parameter and returns the intrinsic gas cost of the transaction and the minimum gas cost used by the transaction based on the calldata size.

def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
546
    """
547
    Calculates the gas that is charged before execution is started.
548
549
    The intrinsic cost of the transaction is charged before execution has
550
    begun. Functions/operations in the EVM cost money to execute so this
551
    intrinsic cost is for the operations that need to be paid for as part of
552
    the transaction. Data transfer, for example, is part of this intrinsic
553
    cost. It costs ether to send data over the wire and that ether is
554
    accounted for in the intrinsic cost calculated in this function. This
555
    intrinsic cost must be calculated and paid for before execution in order
556
    for all operations to be implemented.
557
558
    The intrinsic cost includes:
559
    1. Base cost (`TX_BASE`)
560
    2. Cost for data (zero and non-zero bytes)
561
    3. Cost for contract creation (if applicable)
562
    4. Cost for access list entries (if applicable)
563
    5. Cost for authorizations (if applicable)
564
565
566
    This function takes a transaction as a parameter and returns the intrinsic
567
    gas cost of the transaction and the minimum gas cost used by the
568
    transaction based on the calldata size.
569
    """
570
    from .vm.gas import GasCosts, init_code_cost
571
572
    num_zeros = Uint(tx.data.count(0))
573
    num_non_zeros = ulen(tx.data) - num_zeros
574
575
    tokens_in_calldata = num_zeros + num_non_zeros * Uint(4)
576
    # EIP-7623 floor price (note: no EVM costs)
577
    calldata_floor_gas_cost = (
578
        tokens_in_calldata * GasCosts.TX_DATA_TOKEN_FLOOR + GasCosts.TX_BASE
579
    )
580
581
    data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD
582
583
    if tx.to == Bytes0(b""):
584
        create_cost = GasCosts.TX_CREATE + init_code_cost(ulen(tx.data))
585
    else:
586
        create_cost = Uint(0)
587
588
    access_list_cost = Uint(0)
589
    if has_access_list(tx):
590
        for access in tx.access_list:
591
            access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
592
            access_list_cost += (
593
                ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
594
            )
595
596
    auth_cost = Uint(0)
597
    if isinstance(tx, SetCodeTransaction):
598
        auth_cost += Uint(
599
            GasCosts.AUTH_PER_EMPTY_ACCOUNT * len(tx.authorizations)
600
        )
601
602
    return (
603
        Uint(
604
            GasCosts.TX_BASE
605
            + data_cost
606
            + create_cost
607
            + access_list_cost
608
            + auth_cost
609
        ),
610
        calldata_floor_gas_cost,
611
    )

recover_sender

Extracts the sender address from a transaction.

The v, r, and s values are the three parts that make up the signature of a transaction. In order to recover the sender of a transaction the two components needed are the signature (v, r, and s) and the signing hash of the transaction. The sender's public key can be obtained with these two values and therefore the sender address can be retrieved.

This function takes chain_id and a transaction as parameters and returns the address of the sender of the transaction. It raises an InvalidSignatureError if the signature values (r, s, v) are invalid.

def recover_sender(chain_id: U64, ​​tx: Transaction) -> Address:
615
    """
616
    Extracts the sender address from a transaction.
617
618
    The v, r, and s values are the three parts that make up the signature
619
    of a transaction. In order to recover the sender of a transaction the two
620
    components needed are the signature (``v``, ``r``, and ``s``) and the
621
    signing hash of the transaction. The sender's public key can be obtained
622
    with these two values and therefore the sender address can be retrieved.
623
624
    This function takes chain_id and a transaction as parameters and returns
625
    the address of the sender of the transaction. It raises an
626
    `InvalidSignatureError` if the signature values (r, s, v) are invalid.
627
    """
628
    r, s = tx.r, tx.s
629
    if U256(0) >= r or r >= SECP256K1N:
630
        raise InvalidSignatureError("bad r")
631
    if U256(0) >= s or s > SECP256K1N // U256(2):
632
        raise InvalidSignatureError("bad s")
633
634
    if isinstance(tx, LegacyTransaction):
635
        v = tx.v
636
        if v == 27 or v == 28:
637
            public_key = secp256k1_recover(
638
                r, s, v - U256(27), signing_hash_pre155(tx)
639
            )
640
        else:
641
            chain_id_x2 = U256(chain_id) * U256(2)
642
            if v != U256(35) + chain_id_x2 and v != U256(36) + chain_id_x2:
643
                raise InvalidSignatureError("bad v")
644
            public_key = secp256k1_recover(
645
                r,
646
                s,
647
                v - U256(35) - chain_id_x2,
648
                signing_hash_155(tx, chain_id),
649
            )
650
    elif isinstance(tx, AccessListTransaction):
651
        if tx.y_parity not in (U256(0), U256(1)):
652
            raise InvalidSignatureError("bad y_parity")
653
        public_key = secp256k1_recover(
654
            r, s, tx.y_parity, signing_hash_2930(tx)
655
        )
656
    elif isinstance(tx, FeeMarketTransaction):
657
        if tx.y_parity not in (U256(0), U256(1)):
658
            raise InvalidSignatureError("bad y_parity")
659
        public_key = secp256k1_recover(
660
            r, s, tx.y_parity, signing_hash_1559(tx)
661
        )
662
    elif isinstance(tx, BlobTransaction):
663
        if tx.y_parity not in (U256(0), U256(1)):
664
            raise InvalidSignatureError("bad y_parity")
665
        public_key = secp256k1_recover(
666
            r, s, tx.y_parity, signing_hash_4844(tx)
667
        )
668
    elif isinstance(tx, SetCodeTransaction):
669
        if tx.y_parity not in (U256(0), U256(1)):
670
            raise InvalidSignatureError("bad y_parity")
671
        public_key = secp256k1_recover(
672
            r, s, tx.y_parity, signing_hash_7702(tx)
673
        )
674
675
    return Address(keccak256(public_key)[12:32])

signing_hash_pre155

Compute the hash of a transaction used in a legacy (pre EIP-155) signature.

This function takes a legacy transaction as a parameter and returns the signing hash of the transaction.

def signing_hash_pre155(tx: LegacyTransaction) -> Hash32:
679
    """
680
    Compute the hash of a transaction used in a legacy (pre [EIP-155])
681
    signature.
682
683
    This function takes a legacy transaction as a parameter and returns the
684
    signing hash of the transaction.
685
686
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
687
    """
688
    return keccak256(
689
        rlp.encode(
690
            (
691
                tx.nonce,
692
                tx.gas_price,
693
                tx.gas,
694
                tx.to,
695
                tx.value,
696
                tx.data,
697
            )
698
        )
699
    )

signing_hash_155

Compute the hash of a transaction used in a EIP-155 signature.

This function takes a legacy transaction and a chain ID as parameters and returns the hash of the transaction used in an EIP-155 signature.

def signing_hash_155(tx: LegacyTransaction, ​​chain_id: U64) -> Hash32:
703
    """
704
    Compute the hash of a transaction used in a [EIP-155] signature.
705
706
    This function takes a legacy transaction and a chain ID as parameters
707
    and returns the hash of the transaction used in an [EIP-155] signature.
708
709
    [EIP-155]: https://eips.ethereum.org/EIPS/eip-155
710
    """
711
    return keccak256(
712
        rlp.encode(
713
            (
714
                tx.nonce,
715
                tx.gas_price,
716
                tx.gas,
717
                tx.to,
718
                tx.value,
719
                tx.data,
720
                chain_id,
721
                Uint(0),
722
                Uint(0),
723
            )
724
        )
725
    )

signing_hash_2930

Compute the hash of a transaction used in a EIP-2930 signature.

This function takes an access list transaction as a parameter and returns the hash of the transaction used in an EIP-2930 signature.

def signing_hash_2930(tx: AccessListTransaction) -> Hash32:
729
    """
730
    Compute the hash of a transaction used in a [EIP-2930] signature.
731
732
    This function takes an access list transaction as a parameter
733
    and returns the hash of the transaction used in an [EIP-2930] signature.
734
735
    [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
736
    """
737
    return keccak256(
738
        b"\x01"
739
        + rlp.encode(
740
            (
741
                tx.chain_id,
742
                tx.nonce,
743
                tx.gas_price,
744
                tx.gas,
745
                tx.to,
746
                tx.value,
747
                tx.data,
748
                tx.access_list,
749
            )
750
        )
751
    )

signing_hash_1559

Compute the hash of a transaction used in an EIP-1559 signature.

This function takes a fee market transaction as a parameter and returns the hash of the transaction used in an EIP-1559 signature.

def signing_hash_1559(tx: FeeMarketTransaction) -> Hash32:
755
    """
756
    Compute the hash of a transaction used in an [EIP-1559] signature.
757
758
    This function takes a fee market transaction as a parameter
759
    and returns the hash of the transaction used in an [EIP-1559] signature.
760
761
    [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
762
    """
763
    return keccak256(
764
        b"\x02"
765
        + rlp.encode(
766
            (
767
                tx.chain_id,
768
                tx.nonce,
769
                tx.max_priority_fee_per_gas,
770
                tx.max_fee_per_gas,
771
                tx.gas,
772
                tx.to,
773
                tx.value,
774
                tx.data,
775
                tx.access_list,
776
            )
777
        )
778
    )

signing_hash_4844

Compute the hash of a transaction used in an EIP-4844 signature.

This function takes a transaction as a parameter and returns the signing hash of the transaction used in an EIP-4844 signature.

def signing_hash_4844(tx: BlobTransaction) -> Hash32:
782
    """
783
    Compute the hash of a transaction used in an [EIP-4844] signature.
784
785
    This function takes a transaction as a parameter and returns the
786
    signing hash of the transaction used in an [EIP-4844] signature.
787
788
    [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
789
    """
790
    return keccak256(
791
        b"\x03"
792
        + rlp.encode(
793
            (
794
                tx.chain_id,
795
                tx.nonce,
796
                tx.max_priority_fee_per_gas,
797
                tx.max_fee_per_gas,
798
                tx.gas,
799
                tx.to,
800
                tx.value,
801
                tx.data,
802
                tx.access_list,
803
                tx.max_fee_per_blob_gas,
804
                tx.blob_versioned_hashes,
805
            )
806
        )
807
    )

signing_hash_7702

Compute the hash of a transaction used in a EIP-7702 signature.

This function takes a transaction as a parameter and returns the signing hash of the transaction used in a EIP-7702 signature.

def signing_hash_7702(tx: SetCodeTransaction) -> Hash32:
811
    """
812
    Compute the hash of a transaction used in a [EIP-7702] signature.
813
814
    This function takes a transaction as a parameter and returns the
815
    signing hash of the transaction used in a [EIP-7702] signature.
816
817
    [EIP-7702]: https://eips.ethereum.org/EIPS/eip-7702
818
    """
819
    return keccak256(
820
        b"\x04"
821
        + rlp.encode(
822
            (
823
                tx.chain_id,
824
                tx.nonce,
825
                tx.max_priority_fee_per_gas,
826
                tx.max_fee_per_gas,
827
                tx.gas,
828
                tx.to,
829
                tx.value,
830
                tx.data,
831
                tx.access_list,
832
                tx.authorizations,
833
            )
834
        )
835
    )

get_transaction_hash

Compute the hash of a transaction.

This function takes a transaction as a parameter and returns the keccak256 hash of the transaction. It can handle both legacy transactions and typed transactions (AccessListTransaction, FeeMarketTransaction, etc.).

def get_transaction_hash(tx: Bytes | LegacyTransaction) -> Hash32:
839
    """
840
    Compute the hash of a transaction.
841
842
    This function takes a transaction as a parameter and returns the
843
    keccak256 hash of the transaction. It can handle both legacy transactions
844
    and typed transactions (`AccessListTransaction`, `FeeMarketTransaction`,
845
    etc.).
846
    """
847
    assert isinstance(tx, (LegacyTransaction, Bytes))
848
    if isinstance(tx, LegacyTransaction):
849
        return keccak256(rlp.encode(tx))
850
    else:
851
        return keccak256(tx)

has_access_list

Return whether the transaction has an EIP-2930-style access list.

def has_access_list(tx: Transaction) -> TypeGuard[AccessListCapableTransaction]:
857
    """
858
    Return whether the transaction has an [EIP-2930]-style access list.
859
860
    [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
861
    """
862
    return isinstance(
863
        tx,
864
        AccessListCapableTransaction,
865
    )