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

22
TX_BASE_COST = 21000

TX_DATA_COST_PER_NON_ZERO

23
TX_DATA_COST_PER_NON_ZERO = 16

TX_DATA_COST_PER_ZERO

24
TX_DATA_COST_PER_ZERO = 4

TX_CREATE_COST

25
TX_CREATE_COST = 32000

TX_ACCESS_LIST_ADDRESS_COST

26
TX_ACCESS_LIST_ADDRESS_COST = 2400

TX_ACCESS_LIST_STORAGE_KEY_COST

27
TX_ACCESS_LIST_STORAGE_KEY_COST = 1900

LegacyTransaction

Atomic operation performed on the block chain.

30
@slotted_freezable
31
@dataclass
class LegacyTransaction:

nonce

37
    nonce: U256

gas_price

38
    gas_price: Uint

gas

39
    gas: Uint

to

40
    to: Union[Bytes0, Address]

value

41
    value: U256

data

42
    data: Bytes

v

43
    v: U256

r

44
    r: U256

s

45
    s: U256

AccessListTransaction

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

48
@slotted_freezable
49
@dataclass
class AccessListTransaction:

chain_id

55
    chain_id: U64

nonce

56
    nonce: U256

gas_price

57
    gas_price: Uint

gas

58
    gas: Uint

to

59
    to: Union[Bytes0, Address]

value

60
    value: U256

data

61
    data: Bytes

access_list

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

y_parity

63
    y_parity: U256

r

64
    r: U256

s

65
    s: U256

FeeMarketTransaction

The transaction type added in EIP-1559.

68
@slotted_freezable
69
@dataclass
class FeeMarketTransaction:

chain_id

75
    chain_id: U64

nonce

76
    nonce: U256

max_priority_fee_per_gas

77
    max_priority_fee_per_gas: Uint

max_fee_per_gas

78
    max_fee_per_gas: Uint

gas

79
    gas: Uint

to

80
    to: Union[Bytes0, Address]

value

81
    value: U256

data

82
    data: Bytes

access_list

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

y_parity

84
    y_parity: U256

r

85
    r: U256

s

86
    s: U256

Transaction

89
Transaction = Union[
90
    LegacyTransaction, AccessListTransaction, FeeMarketTransaction
91
]

encode_transaction

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

def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
95
    """
96
    Encode a transaction. Needed because non-legacy transactions aren't RLP.
97
    """
98
    if isinstance(tx, LegacyTransaction):
99
        return tx
100
    elif isinstance(tx, AccessListTransaction):
101
        return b"\x01" + rlp.encode(tx)
102
    elif isinstance(tx, FeeMarketTransaction):
103
        return b"\x02" + rlp.encode(tx)
104
    else:
105
        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:
109
    """
110
    Decode a transaction. Needed because non-legacy transactions aren't RLP.
111
    """
112
    if isinstance(tx, Bytes):
113
        if tx[0] == 1:
114
            return rlp.decode_to(AccessListTransaction, tx[1:])
115
        elif tx[0] == 2:
116
            return rlp.decode_to(FeeMarketTransaction, tx[1:])
117
        else:
118
            raise InvalidBlock
119
    else:
120
        return tx