@pytest.mark.inclusion_test
@pytest.mark.parametrize(
"balance_diff, expected_exception",
[
pytest.param(
-1,
TransactionException.INSUFFICIENT_ACCOUNT_FUNDS,
marks=pytest.mark.exception_test,
),
(0, None), # Valid case - no exception
(1, None),
],
)
@pytest.mark.eels_base_coverage
def test_sender_balance(
blockchain_test: BlockchainTestFiller,
pre: Alloc,
env: Environment,
fork: BaseFork,
balance_diff: int,
expected_exception: TransactionException | None,
) -> None:
"""
Tests that the sender has sufficient balance.
"""
to = pre.fund_eoa()
intrinsic_cost = fork.transaction_intrinsic_cost_calculator()
tx_gas_limit = intrinsic_cost()
tx_gas_price = TransactionDefaults.gas_price
tx_value = 0
# Calculate required balance from tx fields and fund sender
required_balance = tx_gas_limit * tx_gas_price + tx_value
sender = pre.fund_eoa(amount=required_balance + balance_diff)
# Create transaction first with defaults
tx = Transaction(
sender=sender,
gas_limit=tx_gas_limit,
gas_price=tx_gas_price,
value=tx_value,
to=to,
protected=False,
error=expected_exception,
)
block = Block(
txs=[tx],
exception=expected_exception,
)
blockchain_test(pre=pre, post={}, blocks=[block], genesis_environment=env)