Skip to content

Commit

Permalink
fix: add finally block
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDZhon committed Nov 14, 2024
1 parent 678e83c commit b9dec0b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,21 @@ def balance_check_middleware(make_request, web3):
@wraps(make_request)
def middleware(method, params):
from_address = None
result = None
balance_diff = 0

if method in ["eth_sendTransaction", "eth_sendRawTransaction"]:
transaction = params[0]
from_address = transaction.get("from")
if from_address:
balance_diff = ensure_balance(from_address)

result = make_request(method, params)
if balance_diff > 0:
new_balance = max(0, web3.eth.get_balance(from_address) - balance_diff)
set_balance_in_wei(from_address, new_balance)
try:
result = make_request(method, params)
finally:
if balance_diff > 0:
new_balance = max(0, web3.eth.get_balance(from_address) - balance_diff)
set_balance_in_wei(from_address, new_balance)

return result

Expand Down

0 comments on commit b9dec0b

Please sign in to comment.