2

I'm working on a project that is deployed behind a proxy, and looking to do invariant tests with it in foundry.

My setup in my invariant test looks as such:

function setUp() public {
        initializerData = abi.encodeWithSignature(
            "initialize(string,string,uint256,uint256,(address,uint256)[],uint256)",
            tokenName,
            tokenSymbol,
            compositionTolerance,
            compositionEnforcementThreshold,
            fees,
            withdrawlFee
        );
        implementation = new Logic();
        implProxy = new ERC1967Proxy(address(implementation), initializerData);
        myContract = MyContract(address(implProxy));
        targetContract(address(myContract));
        targetArtifact("MyContract");
        excludeArtifact("ERC1967Proxy");
    }

And an invariant test:

    function invariant_getDepositRoomDoesntHold() public {
        assert(myContract.accessed() == false);
    }

However, running RUST_LOG=trace forge test -vvvvv results in:

2023-02-14T16:58:39.271000Z ERROR contract{name=test/MyContractTest.t.MyContractTest}: forge::multi_runner: error=No contracts to fuzz.

I'm not sure how it's not finding any contracts to test, I clearly am adding one with:

targetContract(address(myContract));
targetArtifact("MyContract");

1 Answer 1

2

I had the same problem. It turns out they renamed InvariantTest to StdInvariant. So, you have to import StdInvariant contract and extend your test contract for property-based fuzzing. For example:

pragma solidity ^0.8.13;

import {Test} from "forge-std/Test.sol";
import {StdInvariant} from "forge-std/StdInvariant.sol";
import {WETH9} from "../src/WETH9.sol";

contract WETH9Invariants is StdInvariant, Test {
    WETH9 public weth;

    function setUp() public {
        weth = new WETH9();
    }

    function invariant_badInvariantThisShouldFail() public {
        assertEq(1, weth.totalSupply());
    }
}

And run

forge test -v

3
  • But how does that fix the issue with targetArtifact? Mar 23, 2023 at 14:24
  • AFAIK, the issue is not related to targetArtifact. It is about how forge imports and starts the invariant fuzzing. Without the proper import, the invariant feature is missing which results in failure to find contract to fuzz.
    – Emrah
    Mar 23, 2023 at 14:45
  • I am using StdInvariant. Did you have the same issue with targetArtifact? could you show an example that uses targetArtifact working? Mar 24, 2023 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.