Skip to content

feat: add way for files to import other sources to work with vm.getCodeΒ #7025

@adhusson

Description

@adhusson

Component

Forge

Describe the feature you would like

tldr

The effect of // forge-import: Contract.sol is to add a contract to the list of contracts to be compiled (but it does not import it in the file containing the directive).

Rationale

vm.getCode("Contract.sol") can be used to import the bytecode of a contract that was compiled using a different compiler version (to match an onchain deployment for instance). If the contract is not used anywhere else, an additional mock file can be used to make sure the contract is compiled by forge build.

This stops working when the foundry project that uses getCode is itself used as a dependency. The mock file is usually not imported, so Contract.sol is not compiled at all.

The forge-import directive would statically add a contract to the compilation plan.

Example

Before

// src/MockUniswapV3Factory.sol
pragma solidity 0.7.6;
import "v3-core/UniswapV3Factory.sol";
// test/Fixture.sol
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "v3-core/interfaces/IUniswapV3Factory.sol";
contract MyTest is Test {
    IUniswapV3Factory internal uniswapV3Factory;

    constructor() {
        uniswapV3Factory = IUniswapV3Factory(deployCode("UniswapV3Factory.sol"));
    }
}

After

// test/Fixture.sol
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "v3-core/interfaces/IUniswapV3Factory.sol";
contract MyTest is Test {
    IUniswapV3Factory internal uniswapV3Factory;

    constructor() {
        // forge-import: UniswapV3Factory.sol
        uniswapV3Factory = IUniswapV3Factory(deployCode("UniswapV3Factory.sol"));
    }
}

Related: #6099

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Next Up

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions