foundry-rs / foundry-rs/foundry

feat: add way for files to import other sources to work with `vm.getCode`

Open
#7,025 5 comments 1 reaction 0 assignees View on GitHub
A-config T-feature T-to-discuss
Dominant language
Rust
Stars
10.6k
Forks
2.6k
Avg merge
18h 20m
Merged PRs (30d)
510

Description

### 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](https://github.com/foundry-rs/foundry/pull/4) (to match an onchain deployment for instance). If the contract is not used anywhere else, an additional [mock file](https://github.com/foundry-rs/foundry/issues/1163#issuecomment-1102741463) 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**

```solidity
// src/MockUniswapV3Factory.sol
pragma solidity 0.7.6;
import "v3-core/UniswapV3Factory.sol";
```

```solidity
// 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**

```solidity
// 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.