@@ -183,9 +183,10 @@ def opcode_refund_calculator(cls) -> OpcodeGasCalculator:
183183
184184 def fn (opcode : OpcodeBase ) -> int :
185185 # Get the gas refund or calculator
186+ state_refund = opcode_state_refund_calculator (opcode )
186187 if opcode not in opcode_refund_map :
187188 # Most opcodes don't provide refunds
188- return 0
189+ return state_refund
189190 refund_or_calculator = opcode_refund_map [opcode ]
190191
191192 # If it's a callable, call it with the opcode
@@ -196,7 +197,7 @@ def fn(opcode: OpcodeBase) -> int:
196197 regular_refund = refund_or_calculator
197198
198199 # EIP-8037 adds the state refund on top of the regular refund.
199- return regular_refund + opcode_state_refund_calculator ( opcode )
200+ return regular_refund + state_refund
200201
201202 return fn
202203
@@ -217,6 +218,11 @@ def opcode_state_refund_map(
217218 Opcodes .SSTORE : lambda op : cls ._calculate_sstore_state_refund (
218219 op , gas_costs
219220 ),
221+ Opcodes .SELFDESTRUCT : (
222+ lambda op : cls ._calculate_selfdestruct_state_refund (
223+ op , gas_costs
224+ )
225+ ),
220226 }
221227
222228 @classmethod
@@ -386,6 +392,37 @@ def _calculate_sstore_state_refund(
386392 return 32 * cpsb
387393 return 0
388394
395+ @classmethod
396+ def _calculate_selfdestruct_state_refund (
397+ cls , opcode : OpcodeBase , gas_costs : GasCosts
398+ ) -> int :
399+ """
400+ Calculate SELFDESTRUCT state gas refund.
401+
402+ Account creation: 112 × cost_per_state_byte
403+ Created storage slots: 32 × cost_per_state_byte per non-zero slot
404+ Code deposit: len(code) × cost_per_state_byte
405+ """
406+ del gas_costs
407+ metadata = opcode .metadata
408+ cpsb = cls .cost_per_state_byte ()
409+
410+ self_destructed_account = metadata ["self_destructed_account" ]
411+ self_destructed_account_storage_slot_count = metadata [
412+ "self_destructed_account_storage_slot_count"
413+ ]
414+ self_destructed_account_code_deposit = metadata [
415+ "self_destructed_account_code_deposit"
416+ ]
417+ state_refund = 0
418+ if self_destructed_account :
419+ state_refund = 112 * cpsb
420+ state_refund += (
421+ 32 * cpsb * self_destructed_account_storage_slot_count
422+ )
423+ state_refund += cpsb * self_destructed_account_code_deposit
424+ return state_refund
425+
389426 @classmethod
390427 def _calculate_return_gas (
391428 cls , opcode : OpcodeBase , gas_costs : GasCosts
0 commit comments