|
| 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 {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol"; |
| 12 | +import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol"; |
| 13 | +import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; |
| 14 | + |
| 15 | +contract EulerSwapHookTest is EulerSwapTestBase { |
| 16 | + using StateLibrary for IPoolManager; |
| 17 | + |
| 18 | + EulerSwapHook public eulerSwap; |
| 19 | + |
| 20 | + IPoolManager public poolManager; |
| 21 | + PoolSwapTest public swapRouter; |
| 22 | + |
| 23 | + PoolSwapTest.TestSettings public settings = PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false}); |
| 24 | + |
| 25 | + function setUp() public virtual override { |
| 26 | + super.setUp(); |
| 27 | + |
| 28 | + poolManager = PoolManagerDeployer.deploy(address(this)); |
| 29 | + swapRouter = new PoolSwapTest(poolManager); |
| 30 | + |
| 31 | + eulerSwap = createEulerSwapHook(poolManager, 60e18, 60e18, 0, 1e18, 1e18, 0.4e18, 0.85e18); |
| 32 | + |
| 33 | + // confirm pool was created |
| 34 | + assertFalse(eulerSwap.poolKey().currency1 == CurrencyLibrary.ADDRESS_ZERO); |
| 35 | + (uint160 sqrtPriceX96,,,) = poolManager.getSlot0(eulerSwap.poolKey().toId()); |
| 36 | + assertNotEq(sqrtPriceX96, 0); |
| 37 | + } |
| 38 | + |
| 39 | + function test_SwapExactIn() public { |
| 40 | + uint256 amountIn = 1e18; |
| 41 | + uint256 amountOut = |
| 42 | + periphery.quoteExactInput(address(eulerSwap), address(assetTST), address(assetTST2), amountIn); |
| 43 | + |
| 44 | + assetTST.mint(anyone, amountIn); |
| 45 | + |
| 46 | + vm.startPrank(anyone); |
| 47 | + assetTST.approve(address(swapRouter), amountIn); |
| 48 | + |
| 49 | + bool zeroForOne = address(assetTST) < address(assetTST2); |
| 50 | + _swap(eulerSwap.poolKey(), zeroForOne, true, amountIn); |
| 51 | + vm.stopPrank(); |
| 52 | + |
| 53 | + assertEq(assetTST.balanceOf(anyone), 0); |
| 54 | + assertEq(assetTST2.balanceOf(anyone), amountOut); |
| 55 | + } |
| 56 | + |
| 57 | + function test_SwapExactOut() public { |
| 58 | + uint256 amountOut = 1e18; |
| 59 | + uint256 amountIn = |
| 60 | + periphery.quoteExactOutput(address(eulerSwap), address(assetTST), address(assetTST2), amountOut); |
| 61 | + |
| 62 | + assetTST.mint(anyone, amountIn); |
| 63 | + |
| 64 | + vm.startPrank(anyone); |
| 65 | + assetTST.approve(address(periphery), amountIn); |
| 66 | + bool zeroForOne = address(assetTST) < address(assetTST2); |
| 67 | + _swap(eulerSwap.poolKey(), zeroForOne, false, amountOut); |
| 68 | + vm.stopPrank(); |
| 69 | + |
| 70 | + assertEq(assetTST.balanceOf(anyone), 0); |
| 71 | + assertEq(assetTST2.balanceOf(anyone), amountOut); |
| 72 | + } |
| 73 | + |
| 74 | + function _swap(PoolKey memory key, bool zeroForOne, bool exactInput, uint256 amount) internal { |
| 75 | + IPoolManager.SwapParams memory swapParams = IPoolManager.SwapParams({ |
| 76 | + zeroForOne: zeroForOne, |
| 77 | + amountSpecified: exactInput ? -int256(amount) : int256(amount), |
| 78 | + sqrtPriceLimitX96: zeroForOne ? TickMath.MIN_SQRT_PRICE + 1 : TickMath.MAX_SQRT_PRICE - 1 |
| 79 | + }); |
| 80 | + swapRouter.swap(key, swapParams, settings, ""); |
| 81 | + } |
| 82 | +} |
0 commit comments