22
33from itertools import count
44from random import randint
5- from typing import Iterator , List , Literal , Tuple
5+ from typing import Generator , Iterator , List , Literal , Tuple
66
77import pytest
88from pydantic import PrivateAttr
99
10- from ethereum_test_base_types import Number , StorageRootType , ZeroPaddedHexNumber
10+ from ethereum_test_base_types import Bytes , Number , StorageRootType , ZeroPaddedHexNumber
1111from ethereum_test_base_types .conversions import (
1212 BytesConvertible ,
1313 FixedSizeBytesConvertible ,
@@ -94,7 +94,7 @@ class Alloc(BaseAlloc):
9494 _sender : EOA = PrivateAttr ()
9595 _eth_rpc : EthRPC = PrivateAttr ()
9696 _txs : List [Transaction ] = PrivateAttr (default_factory = list )
97- _deployed_contracts : List [Tuple [Address , bytes ]] = PrivateAttr (default_factory = list )
97+ _deployed_contracts : List [Tuple [Address , Bytes ]] = PrivateAttr (default_factory = list )
9898 _funded_eoa : List [EOA ] = PrivateAttr (default_factory = list )
9999 _evm_code_type : EVMCodeType | None = PrivateAttr (None )
100100 _chain_id : int = PrivateAttr ()
@@ -206,7 +206,7 @@ def deploy_contract(
206206 self ._txs .append (deploy_tx )
207207
208208 contract_address = deploy_tx .created_contract
209- self ._deployed_contracts .append ((contract_address , bytes (code )))
209+ self ._deployed_contracts .append ((contract_address , Bytes (code )))
210210
211211 assert Number (nonce ) >= 1 , "impossible to deploy contract with nonce lower than one"
212212
@@ -373,9 +373,14 @@ def pre(
373373 evm_code_type : EVMCodeType ,
374374 chain_id : int ,
375375 eoa_fund_amount_default : int ,
376- ) -> Alloc :
376+ default_gas_price : int ,
377+ ) -> Generator [Alloc , None , None ]:
377378 """Return default pre allocation for all tests (Empty alloc)."""
378- return Alloc (
379+ # Record the starting balance of the sender
380+ sender_test_starting_balance = eth_rpc .get_balance (sender_key )
381+
382+ # Prepare the pre-alloc
383+ pre = Alloc (
379384 fork = fork ,
380385 sender = sender_key ,
381386 eth_rpc = eth_rpc ,
@@ -384,3 +389,31 @@ def pre(
384389 chain_id = chain_id ,
385390 eoa_fund_amount_default = eoa_fund_amount_default ,
386391 )
392+
393+ # Yield the pre-alloc for usage during the test
394+ yield pre
395+
396+ # Refund all EOAs (regardless of whether the test passed or failed)
397+ refund_txs = []
398+ for eoa in pre ._funded_eoa :
399+ remaining_balance = eth_rpc .get_balance (eoa )
400+ eoa .nonce = Number (eth_rpc .get_transaction_count (eoa ))
401+ refund_gas_limit = 21_000
402+ tx_cost = refund_gas_limit * default_gas_price
403+ if remaining_balance < tx_cost :
404+ continue
405+ refund_txs .append (
406+ Transaction (
407+ sender = eoa ,
408+ to = sender_key ,
409+ gas_limit = 21_000 ,
410+ gas_price = default_gas_price ,
411+ value = remaining_balance - tx_cost ,
412+ ).with_signature_and_sender ()
413+ )
414+ eth_rpc .send_wait_transactions (refund_txs )
415+
416+ # Record the ending balance of the sender
417+ sender_test_ending_balance = eth_rpc .get_balance (sender_key )
418+ used_balance = sender_test_starting_balance - sender_test_ending_balance
419+ print (f"Used balance={ used_balance / 10 ** 18 :.18f} " )
0 commit comments