Here is an example of how to use contracts with different Solidity versions in a Foundry test.
Last active
January 14, 2025 07:20
-
-
Save zt-9/f367a998e00294595a9bd700200c8b5a to your computer and use it in GitHub Desktop.
Deploy contracts with different solidity versions in Foundry test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.25; | |
import "forge-std/Test.sol"; | |
import "solady/src/tokens/WETH.sol"; | |
import "solady/src/tokens/ERC20.sol"; | |
import "./TestERC20.sol"; | |
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol"; | |
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol"; | |
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol"; | |
abstract contract UniV2Deploy is Test { | |
WETH weth = WETH(payable(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2)); | |
IUniswapV2Factory uniV2Factory = IUniswapV2Factory(0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f); | |
IUniswapV2Router02 univ2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D); | |
ERC20 token0 = ERC20(address(0x1111)); | |
ERC20 token1 = ERC20(address(0x2222)); | |
IUniswapV2Pair uniV2Pair; | |
constructor() { | |
// weth | |
deployCodeTo("WETH.sol", address(weth)); | |
// factory | |
bytes memory runtimecode = vm.getDeployedCode("out/UniswapV2Factory.sol/UniswapV2Factory.json"); | |
// use etch because somehow the deployCodeTo failed with StdCheats deployCodeTo(string,bytes,uint256,address): Failed to create runtime bytecode. | |
// deployCodeTo("out/UniswapV2Factory.sol/UniswapV2Factory.json", address(uniV2Factory)); | |
vm.etch(address(uniV2Factory), runtimecode); | |
// router | |
bytes memory routerConstructorArgs = abi.encode(address(uniV2Factory), address(weth)); | |
deployCodeTo("out/UniswapV2Router02.sol/UniswapV2Router02.json", routerConstructorArgs, address(univ2Router)); | |
// create a pair | |
deployCodeTo("TestERC20.sol", abi.encode("token0", "token-0"), address(token0)); | |
deployCodeTo("TestERC20.sol", abi.encode("token1", "token-1"), address(token1)); | |
address uniV2PairAddr = uniV2Factory.createPair(address(token0), address(token1)); | |
uniV2Pair = IUniswapV2Pair(uniV2PairAddr); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity =0.5.16; | |
// import files to make foundry generates its artifacts | |
import "@uniswap/v2-core/contracts/UniswapV2Factory.sol"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity =0.6.6; | |
// import files to make foundry generates its artifacts | |
import "@uniswap/v2-periphery/contracts/UniswapV2Router02.sol"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment