ethereum.shanghai.transactionsethereum.cancun.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.

TX_BASE_COST

17
TX_BASE_COST = 21000

TX_DATA_COST_PER_NON_ZERO

18
TX_DATA_COST_PER_NON_ZERO = 16

TX_DATA_COST_PER_ZERO

19
TX_DATA_COST_PER_ZERO = 4

TX_CREATE_COST

20
TX_CREATE_COST = 32000

TX_ACCESS_LIST_ADDRESS_COST

21
TX_ACCESS_LIST_ADDRESS_COST = 2400

TX_ACCESS_LIST_STORAGE_KEY_COST

22
TX_ACCESS_LIST_STORAGE_KEY_COST = 1900

LegacyTransaction

Atomic operation performed on the block chain.

25
@slotted_freezable
26
@dataclass
class LegacyTransaction:

nonce

32
    nonce: U256

gas_price

33
    gas_price: Uint

gas

34
    gas: Uint

to

35
    to: Union[Bytes0, Address]

value

36
    value: U256

data

37
    data: Bytes

v

38
    v: U256

r

39
    r: U256

s

40
    s: U256

AccessListTransaction

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

43
@slotted_freezable
44
@dataclass
class AccessListTransaction:

chain_id

50
    chain_id: U64

nonce

51
    nonce: U256

gas_price

52
    gas_price: Uint

gas

53
    gas: Uint

to

54
    to: Union[Bytes0, Address]

value

55
    value: U256

data

56
    data: Bytes

access_list

57
    access_list: Tuple[Tuple[Address, Tuple[Bytes32, ...]], ...]

y_parity

58
    y_parity: U256

r

59
    r: U256

s

60
    s: U256

FeeMarketTransaction

The transaction type added in EIP-1559.

63
@slotted_freezable
64
@dataclass
class FeeMarketTransaction:

chain_id

70
    chain_id: U64

nonce

71
    nonce: U256

max_priority_fee_per_gas

72
    max_priority_fee_per_gas: Uint

max_fee_per_gas

73
    max_fee_per_gas: Uint

gas

74
    gas: Uint

to

75
    to: Union[Bytes0, Address]

value

76
    value: U256

data

77
    data: Bytes

access_list

78
    access_list: Tuple[Tuple[Address, Tuple[Bytes32, ...]], ...]

y_parity

79
    y_parity: U256

r

80
    r: U256

s

81
    s: U256

BlobTransaction

The transaction type added in EIP-4844.

84
@slotted_freezable
85
@dataclass
class BlobTransaction:

chain_id

91
    chain_id: U64

nonce

92
    nonce: U256

max_priority_fee_per_gas

93
    max_priority_fee_per_gas: Uint

max_fee_per_gas

94
    max_fee_per_gas: Uint

gas

95
    gas: Uint

to

96
    to: Address

value

97
    value: U256

data

98
    data: Bytes

access_list

99
    access_list: Tuple[Tuple[Address, Tuple[Bytes32, ...]], ...]

max_fee_per_blob_gas

100
    max_fee_per_blob_gas: U256

blob_versioned_hashes

101
    blob_versioned_hashes: Tuple[VersionedHash, ...]

y_parity

102
    y_parity: U256

r

103
    r: U256

s

104
    s: U256

Transaction

107
Transaction = Union[
85
    LegacyTransaction, AccessListTransaction, FeeMarketTransaction
108
    LegacyTransaction,
109
    AccessListTransaction,
110
    FeeMarketTransaction,
111
    BlobTransaction,
112
]

encode_transaction

Encode a transaction. Needed because non-legacy transactions aren't RLP.

def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
116
    """
117
    Encode a transaction. Needed because non-legacy transactions aren't RLP.
118
    """
119
    if isinstance(tx, LegacyTransaction):
120
        return tx
121
    elif isinstance(tx, AccessListTransaction):
122
        return b"\x01" + rlp.encode(tx)
123
    elif isinstance(tx, FeeMarketTransaction):
124
        return b"\x02" + rlp.encode(tx)
99
    else:
100
        raise Exception(f"Unable to encode transaction of type {type(tx)}")
125
    elif isinstance(tx, BlobTransaction):
126
        return b"\x03" + rlp.encode(tx)
127
    else:
128
        raise Exception(f"Unable to encode transaction of type {type(tx)}")

decode_transaction

Decode a transaction. Needed because non-legacy transactions aren't RLP.

def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
132
    """
133
    Decode a transaction. Needed because non-legacy transactions aren't RLP.
134
    """
135
    if isinstance(tx, Bytes):
136
        if tx[0] == 1:
137
            return rlp.decode_to(AccessListTransaction, tx[1:])
138
        elif tx[0] == 2:
139
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
112
        else:
113
            raise TransactionTypeError(tx[0])
140
        elif tx[0] == 3:
141
            return rlp.decode_to(BlobTransaction, tx[1:])
142
        else:
143
            raise TransactionTypeError(tx[0])
144
    else:
145
        return tx