From 6e768eb7cc0f758d6841a59ded36e6012b692802 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Wed, 3 Apr 2024 01:39:15 -0400 Subject: [PATCH] Fix BNB fork issue The code has been refactored to create forks after the initialization process rather than before, allowing proper configuration of state variables for each chain. Additional token and pool validations have been introduced for enhanced reliability. This update fixed an issue with initiating forks in BNB, and the 'verifyTicks' and 'verifyPosition' methods now have a 'view' visibility specifier for better encapsulation. --- lib/forge-std | 2 +- test/foundry/Base.t.sol | 36 ++++++++++++++------------------- test/foundry/PositionLens.t.sol | 2 +- test/foundry/TickLens.t.sol | 2 +- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/forge-std b/lib/forge-std index 2f11269..e4aef94 160000 --- a/lib/forge-std +++ b/lib/forge-std @@ -1 +1 @@ -Subproject commit 2f112697506eab12d433a65fdc31a639548fe365 +Subproject commit e4aef94c1768803a16fe19f7ce8b65defd027cfd diff --git a/test/foundry/Base.t.sol b/test/foundry/Base.t.sol index 1273ad8..12a7948 100644 --- a/test/foundry/Base.t.sol +++ b/test/foundry/Base.t.sol @@ -56,21 +56,31 @@ abstract contract BaseTest is uint256 internal token1Unit; int24 internal tickSpacing; - // Configure state variables for each chain before creating a fork - function initBeforeFork() internal returns (string memory chainAlias, uint256 blockNumber) { + // Configure state variables for each chain after creating a fork + function initAfterFork() internal { + factory = npm.factory(); + WETH = npm.WETH9(); + (token0, token1) = (WETH < USDC).switchIf(USDC, WETH); + pool = IUniswapV3Factory(factory).getPool(token0, token1, fee); + tickSpacing = V3PoolCallee.wrap(pool).tickSpacing(); + token0Unit = 10 ** IERC20Metadata(token0).decimals(); + token1Unit = 10 ** IERC20Metadata(token1).decimals(); + } + + function setUp() public virtual { if (chainId == 0) { chainId = vm.envOr("CHAIN_ID", uint256(1)); } - chainAlias = getChain(chainId).chainAlias; + string memory chainAlias = getChain(chainId).chainAlias; // Configuration for each chain if (chainId == 1) { - blockNumber = 17000000; + vm.createSelectFork(chainAlias, 17000000); USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; npm = dex == DEX.PancakeSwapV3 ? INPM(0x46A15B0b27311cedF172AB29E4f4766fbE7F4364) : INPM(0xC36442b4a4522E871399CD717aBDD847Ab11FE88); } else if (chainId == 56) { - blockNumber = 37460000; + vm.createSelectFork(chainAlias); USDC = 0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d; npm = dex == DEX.PancakeSwapV3 ? INPM(0x46A15B0b27311cedF172AB29E4f4766fbE7F4364) @@ -78,22 +88,6 @@ abstract contract BaseTest is } else { revert("Unsupported chain"); } - } - - // Configure state variables for each chain after creating a fork - function initAfterFork() internal { - factory = npm.factory(); - WETH = npm.WETH9(); - (token0, token1) = (WETH < USDC).switchIf(USDC, WETH); - pool = IUniswapV3Factory(factory).getPool(token0, token1, fee); - tickSpacing = V3PoolCallee.wrap(pool).tickSpacing(); - token0Unit = 10 ** IERC20Metadata(token0).decimals(); - token1Unit = 10 ** IERC20Metadata(token1).decimals(); - } - - function setUp() public virtual { - (string memory chainAlias, uint256 blockNumber) = initBeforeFork(); - vm.createSelectFork(chainAlias, blockNumber); initAfterFork(); vm.label(WETH, "WETH"); vm.label(USDC, "USDC"); diff --git a/test/foundry/PositionLens.t.sol b/test/foundry/PositionLens.t.sol index e71b695..b70ca3f 100644 --- a/test/foundry/PositionLens.t.sol +++ b/test/foundry/PositionLens.t.sol @@ -88,7 +88,7 @@ contract PositionLensTest is BaseTest { assertEq(collect1, amount1); } - function verifyPosition(PositionState memory pos) internal { + function verifyPosition(PositionState memory pos) internal view { { assertEq(pos.owner, npm.ownerOf(pos.tokenId), "owner"); (, , address token0, , uint24 fee, int24 tickLower, , uint128 liquidity, , , , ) = npm.positions( diff --git a/test/foundry/TickLens.t.sol b/test/foundry/TickLens.t.sol index e37782b..cfd6ae9 100644 --- a/test/foundry/TickLens.t.sol +++ b/test/foundry/TickLens.t.sol @@ -7,7 +7,7 @@ import "contracts/EphemeralGetPopulatedTicksInRange.sol"; import "./Base.t.sol"; contract TickLensTest is BaseTest, PoolUtils { - function verifyTicks(PopulatedTick[] memory populatedTicks) internal { + function verifyTicks(PopulatedTick[] memory populatedTicks) internal view { for (uint256 i; i < populatedTicks.length; ++i) { PopulatedTick memory populatedTick = populatedTicks[i]; (