From 7113492fb0eea489a77333cae14fe651331d96ea Mon Sep 17 00:00:00 2001 From: ChefMist <133624774+ChefMist@users.noreply.github.com> Date: Fri, 21 Jun 2024 16:11:26 +0800 Subject: [PATCH] clean up unused imports etc.. --- src/pool-cl/VeCakeMembershipHook.sol | 38 ++++++++++++------------- test/pool-cl/VeCakeMembershipHook.t.sol | 14 ++++----- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/pool-cl/VeCakeMembershipHook.sol b/src/pool-cl/VeCakeMembershipHook.sol index 474bddb..c88791c 100644 --- a/src/pool-cl/VeCakeMembershipHook.sol +++ b/src/pool-cl/VeCakeMembershipHook.sol @@ -1,16 +1,17 @@ -// SPDX-License-Identifier: UNLICENSED +// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol"; -import {LPFeeLibrary} from "@pancakeswap/v4-core/src/libraries/LPFeeLibrary.sol"; -import {BalanceDelta} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol"; +import {BalanceDelta, BalanceDeltaLibrary} from "@pancakeswap/v4-core/src/types/BalanceDelta.sol"; import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@pancakeswap/v4-core/src/types/BeforeSwapDelta.sol"; import {PoolId, PoolIdLibrary} from "@pancakeswap/v4-core/src/types/PoolId.sol"; -import {Currency} from "@pancakeswap/v4-core/src/types/Currency.sol"; -import {CurrencySettlement} from "@pancakeswap/v4-core/test/helpers/CurrencySettlement.sol"; import {ICLPoolManager} from "@pancakeswap/v4-core/src/pool-cl/interfaces/ICLPoolManager.sol"; import {CLBaseHook} from "./CLBaseHook.sol"; +import {LPFeeLibrary} from "@pancakeswap/v4-core/src/libraries/LPFeeLibrary.sol"; +import {Currency} from "@pancakeswap/v4-core/src/types/Currency.sol"; +import {CurrencySettlement} from "@pancakeswap/v4-core/test/helpers/CurrencySettlement.sol"; + interface IVeCake { function balanceOf(address account) external view returns (uint256 balance); } @@ -21,11 +22,10 @@ interface IVeCake { contract VeCakeMembershipHook is CLBaseHook { using CurrencySettlement for Currency; using PoolIdLibrary for PoolKey; - using LPFeeLibrary for uint24; IVeCake public veCake; - uint256 public promoEndDate; mapping(PoolId => uint24) public poolIdToLpFee; + uint256 public promoEndDate; constructor(ICLPoolManager _poolManager, address _veCake) CLBaseHook(_poolManager) { veCake = IVeCake(_veCake); @@ -52,14 +52,13 @@ contract VeCakeMembershipHook is CLBaseHook { ); } - /// @dev Get the intended lpFee for this pool and store in mapping function afterInitialize(address, PoolKey calldata key, uint160, int24, bytes calldata hookData) external override returns (bytes4) { - (uint24 lpFee) = abi.decode(hookData, (uint24)); - poolIdToLpFee[key.toId()] = lpFee; + uint24 swapFee = abi.decode(hookData, (uint24)); + poolIdToLpFee[key.toId()] = swapFee; promoEndDate = block.timestamp + 1 hours; return this.afterInitialize.selector; @@ -71,10 +70,16 @@ contract VeCakeMembershipHook is CLBaseHook { poolManagerOnly returns (bytes4, BeforeSwapDelta, uint24) { - /// If within promo endDate and veCake holder, lpFee is 0 - uint24 lpFee = - block.timestamp < promoEndDate && veCake.balanceOf(tx.origin) >= 1 ether ? 0 : poolIdToLpFee[key.toId()]; + // return early if promo has ended + if (block.timestamp > promoEndDate) { + return ( + this.beforeSwap.selector, + BeforeSwapDeltaLibrary.ZERO_DELTA, + poolIdToLpFee[key.toId()] | LPFeeLibrary.OVERRIDE_FEE_FLAG + ); + } + uint24 lpFee = veCake.balanceOf(tx.origin) >= 1 ether ? 0 : poolIdToLpFee[key.toId()]; return (this.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, lpFee | LPFeeLibrary.OVERRIDE_FEE_FLAG); } @@ -84,17 +89,12 @@ contract VeCakeMembershipHook is CLBaseHook { ICLPoolManager.SwapParams calldata param, BalanceDelta delta, bytes calldata - ) external override returns (bytes4, int128) { + ) external override poolManagerOnly returns (bytes4, int128) { // return early if promo has ended if (block.timestamp > promoEndDate) { return (this.afterSwap.selector, 0); } - /// @dev this is POC code, do not use for production, this is only for POC. - /// Assumption: currency1 is subsidised currency and if veCake user swap token0 for token1, give 5% more token1. - /// zeroForOne: swap token0 for token1 - /// amountSpecified < 0: indicate exactIn token0 for token1. so unspecified token is token1 - /// veCake.balanceOf(tx.origin) >= 1 ether: only veCake holder if (param.zeroForOne && param.amountSpecified < 0 && veCake.balanceOf(tx.origin) >= 1 ether) { // delta.amount1 is positive as zeroForOne int128 extraToken = delta.amount1() * 5 / 100; diff --git a/test/pool-cl/VeCakeMembershipHook.t.sol b/test/pool-cl/VeCakeMembershipHook.t.sol index 2f5ace7..5ae71f8 100644 --- a/test/pool-cl/VeCakeMembershipHook.t.sol +++ b/test/pool-cl/VeCakeMembershipHook.t.sol @@ -6,14 +6,14 @@ import {Test} from "forge-std/Test.sol"; import {Constants} from "@pancakeswap/v4-core/test/pool-cl/helpers/Constants.sol"; import {Currency} from "@pancakeswap/v4-core/src/types/Currency.sol"; import {PoolKey} from "@pancakeswap/v4-core/src/types/PoolKey.sol"; -import {LPFeeLibrary} from "@pancakeswap/v4-core/src/libraries/LPFeeLibrary.sol"; import {CLPoolParametersHelper} from "@pancakeswap/v4-core/src/pool-cl/libraries/CLPoolParametersHelper.sol"; -import {VeCakeMembershipHook} from "../../src/pool-cl/VeCakeMembershipHook.sol"; import {CLTestUtils} from "./utils/CLTestUtils.sol"; +import {CLPoolParametersHelper} from "@pancakeswap/v4-core/src/pool-cl/libraries/CLPoolParametersHelper.sol"; import {PoolIdLibrary} from "@pancakeswap/v4-core/src/types/PoolId.sol"; -import {ICLSwapRouterBase} from "pancake-v4-periphery/src/pool-cl/interfaces/ICLSwapRouterBase.sol"; +import {ICLSwapRouterBase} from "@pancakeswap/v4-periphery/src/pool-cl/interfaces/ICLSwapRouterBase.sol"; -import {console2} from "forge-std/console2.sol"; +import {VeCakeMembershipHook} from "../../src/pool-cl/VeCakeMembershipHook.sol"; +import {LPFeeLibrary} from "@pancakeswap/v4-core/src/libraries/LPFeeLibrary.sol"; contract VeCakeMembershipHookTest is Test, CLTestUtils { using PoolIdLibrary for PoolKey; @@ -80,14 +80,14 @@ contract VeCakeMembershipHookTest is Test, CLTestUtils { assertLe(amtOut, 0.997 ether); } - function testVeCakeHolderX() public { + function testVeCakeHolder() public { // mint alice veCake veCake.mint(address(alice), 1 ether); uint256 amtOut = _swap(); - // amount out is more than amtIn to indicate hook has given some extra tokenOut - assertGt(amtOut, 1 ether); + // amount out is almost 1.05 due to the 5% subsidy from hook and 0% swap fee + assertGt(amtOut, 1.04 ether); } function _swap() internal returns (uint256 amtOut) {