Skip to content

Commit

Permalink
Fix BNB fork issue
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shuhuiluo authored and gnarlycow committed Apr 3, 2024
1 parent 18b0ec1 commit 6e768eb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
36 changes: 15 additions & 21 deletions test/foundry/Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,38 @@ 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)
: INPM(0x7b8A01B39D58278b5DE7e48c8449c9f4F5170613);
} 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");
Expand Down
2 changes: 1 addition & 1 deletion test/foundry/PositionLens.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion test/foundry/TickLens.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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];
(
Expand Down

0 comments on commit 6e768eb

Please sign in to comment.