Skip to content

PriceIsRightTest fails due to fuzzing + insufficient balance #47

@jmakwana0x1

Description

@jmakwana0x1

Issue: Fuzz test for PriceIsRight isn’t working as intended

Right now the fuzz test in PriceIsRightTest looks like this:

function testBuy(uint256 amount) public {
    vm.deal(address(this), 1000 ether);

    if (amount == 1 ether) {
        priceIsRight.buy{value: amount}();
    } else {
        vm.expectRevert();
        priceIsRight.buy{value: amount}();
    }
}

The problem is that Foundry will generate really huge numbers for amount (like 3e48).
When we try to send those as msg.value, the transaction fails before it even hits the contract.
So the test doesn’t actually check our require(msg.value == 1 ether) — it just fails at the EVM level.


Why this matters

  • The fuzzing isn’t exploring useful cases.
  • We only care about two situations: exactly 1 ether (should succeed) and everything else (should revert).
  • Letting fuzz run wild across the whole uint256 space just adds noise and failing tests.

What we should do

Two simple fixes would make this more reliable:

  1. Add explicit unit tests for the clear cases:

    function testBuySuccess() public {
        vm.deal(address(this), 1 ether);
        priceIsRight.buy{value: 1 ether}();
    }
    
    function testBuyFailure() public {
        vm.deal(address(this), 2 ether);
        vm.expectRevert();
        priceIsRight.buy{value: 2 ether}();
    }
  2. Keep the fuzz test but constrain the input so it only explores reasonable ranges (say up to 10 ether):

    function testBuyFuzz(uint256 amount) public {
        vm.assume(amount <= 10 ether);
        vm.deal(address(this), amount);
    
        if (amount == 1 ether) {
            priceIsRight.buy{value: amount}();
        } else {
            vm.expectRevert();
            priceIsRight.buy{value: amount}();
        }
    }

Summary

Right now fuzzing is breaking because it generates values that don’t make sense for msg.value.
By adding explicit tests and constraining fuzz input, we get the best of both worlds: clear behavior tests and some fuzz exploration without the noise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions