@@ -71,7 +71,10 @@ def serialize_transaction_hash(block, transaction, transaction_index, is_pending
71
71
72
72
73
73
def serialize_transaction (block , transaction , transaction_index , is_pending ):
74
+ txn_type = _extract_transaction_type (transaction )
75
+
74
76
common_transaction_params = {
77
+ "type" : txn_type ,
75
78
"hash" : transaction .hash ,
76
79
"nonce" : transaction .nonce ,
77
80
"block_hash" : None if is_pending else block .hash ,
@@ -91,7 +94,7 @@ def serialize_transaction(block, transaction, transaction_index, is_pending):
91
94
type_specific_params = {
92
95
'chain_id' : transaction .chain_id ,
93
96
'gas_price' : transaction .gas_price ,
94
- 'access_list' : transaction .access_list or [] ,
97
+ 'access_list' : transaction .access_list or () ,
95
98
'y_parity' : transaction .y_parity ,
96
99
}
97
100
else :
@@ -108,11 +111,17 @@ def serialize_transaction(block, transaction, transaction_index, is_pending):
108
111
'chain_id' : transaction .chain_id ,
109
112
'max_fee_per_gas' : transaction .max_fee_per_gas ,
110
113
'max_priority_fee_per_gas' : transaction .max_priority_fee_per_gas ,
111
- 'access_list' : transaction .access_list or [] ,
114
+ 'access_list' : transaction .access_list or () ,
112
115
'y_parity' : transaction .y_parity ,
116
+
117
+ # TODO: Sometime in early 2022 (the merge?), the inclusion of gas_price will be removed
118
+ # from dynamic fee transactions and we can get rid of this behavior.
119
+ # https://github.com/ethereum/execution-specs/pull/251
120
+ 'gas_price' : _calculate_effective_gas_price (transaction , block , txn_type ),
113
121
}
114
122
else :
115
123
raise ValidationError ('Transaction serialization error' )
124
+
116
125
return merge (common_transaction_params , type_specific_params )
117
126
118
127
@@ -128,7 +137,7 @@ def _field_in_transaction(transaction, field):
128
137
# all legacy transactions inherit from BaseTransaction
129
138
return field in transaction .as_dict ()
130
139
elif isinstance (transaction , TypedTransaction ):
131
- # all typed transaction inherit from TypedTransaction
140
+ # all typed transactions inherit from TypedTransaction
132
141
return hasattr (transaction , field )
133
142
134
143
@@ -140,6 +149,7 @@ def serialize_transaction_receipt(
140
149
is_pending
141
150
):
142
151
receipt = receipts [transaction_index ]
152
+ _txn_type = _extract_transaction_type (transaction )
143
153
144
154
if transaction .to == b'' :
145
155
contract_addr = generate_contract_address (
@@ -153,20 +163,21 @@ def serialize_transaction_receipt(
153
163
origin_gas = 0
154
164
else :
155
165
origin_gas = receipts [transaction_index - 1 ].gas_used
156
-
157
166
return {
158
167
"transaction_hash" : transaction .hash ,
159
168
"transaction_index" : None if is_pending else transaction_index ,
160
169
"block_number" : None if is_pending else block .number ,
161
170
"block_hash" : None if is_pending else block .hash ,
162
171
"cumulative_gas_used" : receipt .gas_used ,
163
172
"gas_used" : receipt .gas_used - origin_gas ,
173
+ "effective_gas_price" : _calculate_effective_gas_price (transaction , block , _txn_type ),
164
174
"contract_address" : contract_addr ,
165
175
"logs" : [
166
176
serialize_log (block , transaction , transaction_index , log , log_index , is_pending )
167
177
for log_index , log in enumerate (receipt .logs )
168
178
],
169
179
'state_root' : receipt .state_root ,
180
+ 'type' : _txn_type ,
170
181
}
171
182
172
183
@@ -182,3 +193,24 @@ def serialize_log(block, transaction, transaction_index, log, log_index, is_pend
182
193
"data" : log .data ,
183
194
"topics" : [int_to_32byte_big_endian (topic ) for topic in log .topics ],
184
195
}
196
+
197
+
198
+ def _extract_transaction_type (transaction ):
199
+ if isinstance (transaction , TypedTransaction ):
200
+ try :
201
+ transaction .gas_price # noqa: 201
202
+ return '0x1'
203
+ except AttributeError :
204
+ return '0x2'
205
+ return '0x0' # legacy transactions being '0x0' taken from current geth version v1.10.10
206
+
207
+
208
+ def _calculate_effective_gas_price (transaction , block , transaction_type ):
209
+ return (
210
+ min (
211
+ transaction .max_fee_per_gas ,
212
+ transaction .max_priority_fee_per_gas + block .header .base_fee_per_gas
213
+ )
214
+ if transaction_type == '0x2'
215
+ else transaction .gas_price
216
+ )
0 commit comments