foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Allow replacing `constant` or `immutable` in Foundry, or allow `mockCall` to replace constructor

Open hcheng826 opened this issue 10 months ago • 2 comments

Component

Forge

Describe the feature you would like

is there a way to replace a constant or immutable variable? vm.store doesn't work as they are not in storage slot. Or is there a way to mockCall to override the constructor?

My use case is that I deploy a new contract in the constructor and save the contract address as immutable variable. While in my unit test, I want to override the implementation of that contract

pragma solidity ^0.8.0;

contract DataExtractor {
    function extractData(bytes32 dataId) public pure returns (uint256) {
        return uint256(dataId);
    }
}

contract DataFetcher {
    address public immutable dataExtractor;

    constructor() {
        dataExtractor = address(new DataExtractor());
    }

  // other functions that use dataExtractor
}

Additional context

No response

hcheng826 avatar Apr 06 '24 10:04 hcheng826

how would you implement this? constants/immutables are replaced in the bytecode itself before deployment

shafu0x avatar Apr 25 '24 11:04 shafu0x

Related to https://github.com/foundry-rs/foundry/pull/5718

mds1 avatar Apr 26 '24 21:04 mds1

Maybe use a proxy contract for testing? Then you can modify the immutables in the implementation contract

kbrav avatar Jun 11 '24 05:06 kbrav