|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {EulerSwapTestBase, EulerSwap, EulerSwapPeriphery, IEulerSwap} from "./EulerSwapTestBase.t.sol"; |
| 5 | +import {TestERC20} from "evk-test/unit/evault/EVaultTestBase.t.sol"; |
| 6 | +import {EulerSwapHook} from "../src/EulerSwapHook.sol"; |
| 7 | + |
| 8 | +import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; |
| 9 | +import {IPoolManager, PoolManagerDeployer} from "./utils/PoolManagerDeployer.sol"; |
| 10 | +import {PoolSwapTest} from "@uniswap/v4-core/src/test/PoolSwapTest.sol"; |
| 11 | +import {MinimalRouter} from "./utils/MinimalRouter.sol"; |
| 12 | +import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol"; |
| 13 | +import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; |
| 14 | +import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; |
| 15 | +import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; |
| 16 | + |
| 17 | +contract EulerSwapHookTest is EulerSwapTestBase { |
| 18 | + using StateLibrary for IPoolManager; |
| 19 | + |
| 20 | + EulerSwapHook public eulerSwap; |
| 21 | + |
| 22 | + IPoolManager public poolManager; |
| 23 | + PoolSwapTest public swapRouter; |
| 24 | + MinimalRouter public minimalRouter; |
| 25 | + |
| 26 | + PoolSwapTest.TestSettings public settings = PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false}); |
| 27 | + |
| 28 | + function setUp() public virtual override { |
| 29 | + super.setUp(); |
| 30 | + |
| 31 | + poolManager = PoolManagerDeployer.deploy(address(this)); |
| 32 | + swapRouter = new PoolSwapTest(poolManager); |
| 33 | + minimalRouter = new MinimalRouter(poolManager); |
| 34 | + |
| 35 | + // set swap fee to 10 bips |
| 36 | + eulerSwap = createEulerSwapHook(poolManager, 60e18, 60e18, 0.001e18, 1e18, 1e18, 0.4e18, 0.85e18); |
| 37 | + eulerSwap.activate(); |
| 38 | + |
| 39 | + // confirm pool was created |
| 40 | + assertFalse(eulerSwap.poolKey().currency1 == CurrencyLibrary.ADDRESS_ZERO); |
| 41 | + (uint160 sqrtPriceX96,,,) = poolManager.getSlot0(eulerSwap.poolKey().toId()); |
| 42 | + assertNotEq(sqrtPriceX96, 0); |
| 43 | + } |
| 44 | + |
| 45 | + function test_SwapExactIn_withLpFee() public { |
| 46 | + int256 origNav = getHolderNAV(); |
| 47 | + (uint112 r0, uint112 r1,) = eulerSwap.getReserves(); |
| 48 | + |
| 49 | + uint256 amountIn = 1e18; |
| 50 | + uint256 amountInWithoutFee = amountIn * eulerSwap.feeMultiplier() / 1e18; |
| 51 | + uint256 amountOut = |
| 52 | + periphery.quoteExactInput(address(eulerSwap), address(assetTST), address(assetTST2), amountIn); |
| 53 | + |
| 54 | + assetTST.mint(anyone, amountIn); |
| 55 | + |
| 56 | + vm.startPrank(anyone); |
| 57 | + assetTST.approve(address(minimalRouter), amountIn); |
| 58 | + |
| 59 | + bool zeroForOne = address(assetTST) < address(assetTST2); |
| 60 | + BalanceDelta result = minimalRouter.swap(eulerSwap.poolKey(), zeroForOne, amountIn, 0, ""); |
| 61 | + vm.stopPrank(); |
| 62 | + |
| 63 | + assertEq(assetTST.balanceOf(anyone), 0); |
| 64 | + assertEq(assetTST2.balanceOf(anyone), amountOut); |
| 65 | + |
| 66 | + assertEq(zeroForOne ? uint256(-int256(result.amount0())) : uint256(-int256(result.amount1())), amountIn); |
| 67 | + assertEq(zeroForOne ? uint256(int256(result.amount1())) : uint256(int256(result.amount0())), amountOut); |
| 68 | + |
| 69 | + // assert fees were not added to the reserves |
| 70 | + (uint112 r0New, uint112 r1New,) = eulerSwap.getReserves(); |
| 71 | + if (zeroForOne) { |
| 72 | + assertEq(r0New, r0 + amountInWithoutFee); |
| 73 | + assertEq(r1New, r1 - amountOut); |
| 74 | + } else { |
| 75 | + // oneForZero, so the curve received asset1 |
| 76 | + assertEq(r0New, r0 - amountOut); |
| 77 | + assertEq(r1New, r1 + amountInWithoutFee); |
| 78 | + } |
| 79 | + |
| 80 | + assertGt(getHolderNAV(), origNav + int256(amountIn - amountInWithoutFee)); |
| 81 | + } |
| 82 | + |
| 83 | + function test_SwapExactOut_withLpFee() public { |
| 84 | + int256 origNav = getHolderNAV(); |
| 85 | + (uint112 r0, uint112 r1,) = eulerSwap.getReserves(); |
| 86 | + |
| 87 | + uint256 amountOut = 1e18; |
| 88 | + uint256 amountIn = |
| 89 | + periphery.quoteExactOutput(address(eulerSwap), address(assetTST), address(assetTST2), amountOut); |
| 90 | + |
| 91 | + // inverse of the fee math in Periphery |
| 92 | + uint256 amountInWithoutFee = (amountIn * eulerSwap.feeMultiplier() - eulerSwap.feeMultiplier()) / 1e18; |
| 93 | + |
| 94 | + assetTST.mint(anyone, amountIn); |
| 95 | + |
| 96 | + vm.startPrank(anyone); |
| 97 | + assetTST.approve(address(minimalRouter), amountIn); |
| 98 | + |
| 99 | + bool zeroForOne = address(assetTST) < address(assetTST2); |
| 100 | + BalanceDelta result = minimalRouter.swap(eulerSwap.poolKey(), zeroForOne, amountIn, amountOut, ""); |
| 101 | + vm.stopPrank(); |
| 102 | + |
| 103 | + assertEq(assetTST.balanceOf(anyone), 0); |
| 104 | + assertEq(assetTST2.balanceOf(anyone), amountOut); |
| 105 | + |
| 106 | + assertEq(zeroForOne ? uint256(-int256(result.amount0())) : uint256(-int256(result.amount1())), amountIn); |
| 107 | + assertEq(zeroForOne ? uint256(int256(result.amount1())) : uint256(int256(result.amount0())), amountOut); |
| 108 | + |
| 109 | + // assert fees were not added to the reserves |
| 110 | + (uint112 r0New, uint112 r1New,) = eulerSwap.getReserves(); |
| 111 | + if (zeroForOne) { |
| 112 | + assertEq(r0New, r0 + amountInWithoutFee + 1); // 1 wei of imprecision |
| 113 | + assertEq(r1New, r1 - amountOut); |
| 114 | + } else { |
| 115 | + // oneForZero, so the curve received asset1 |
| 116 | + assertEq(r0New, r0 - amountOut); |
| 117 | + assertEq(r1New, r1 + amountInWithoutFee); |
| 118 | + } |
| 119 | + |
| 120 | + assertGt(getHolderNAV(), origNav + int256(amountIn - amountInWithoutFee)); |
| 121 | + } |
| 122 | +} |
0 commit comments