|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {Test, console} from "forge-std/Test.sol"; |
| 5 | +import {IEVault, IEulerSwap, EulerSwapTestBase, EulerSwap, TestERC20} from "./EulerSwapTestBase.t.sol"; |
| 6 | + |
| 7 | +contract FeesTest is EulerSwapTestBase { |
| 8 | + EulerSwap public eulerSwap; |
| 9 | + |
| 10 | + function setUp() public virtual override { |
| 11 | + super.setUp(); |
| 12 | + |
| 13 | + eulerSwap = createEulerSwap(60e18, 60e18, 0, 1e18, 1e18, 0.9e18, 0.9e18); |
| 14 | + } |
| 15 | + |
| 16 | + function test_fees_exactIn() public monotonicHolderNAV { |
| 17 | + int256 origNav = getHolderNAV(); |
| 18 | + uint256 fee = 0.05e18; |
| 19 | + |
| 20 | + // No fees |
| 21 | + |
| 22 | + uint256 amountInNoFees = 1e18; |
| 23 | + uint256 amountOutNoFees = periphery.quoteExactInput(address(eulerSwap), address(assetTST), address(assetTST2), amountInNoFees); |
| 24 | + assertApproxEqAbs(amountOutNoFees, 0.9983e18, 0.0001e18); |
| 25 | + |
| 26 | + // With fees: Increase input amount so that corresponding output amount matches |
| 27 | + |
| 28 | + eulerSwap = createEulerSwap(60e18, 60e18, fee, 1e18, 1e18, 0.9e18, 0.9e18); |
| 29 | + |
| 30 | + uint256 amountIn = amountInNoFees * 1e18 / (1e18 - fee); |
| 31 | + uint256 amountOut = periphery.quoteExactInput(address(eulerSwap), address(assetTST), address(assetTST2), amountIn); |
| 32 | + assertApproxEqAbs(amountOut, amountOutNoFees, 1); // Same except for possible rounding down by 1 |
| 33 | + |
| 34 | + // Actually execute swap |
| 35 | + |
| 36 | + assetTST.mint(address(this), amountIn); |
| 37 | + assetTST.transfer(address(eulerSwap), amountIn); |
| 38 | + |
| 39 | + // Pulling out one extra reverts... |
| 40 | + |
| 41 | + vm.expectRevert(EulerSwap.CurveViolation.selector); |
| 42 | + eulerSwap.swap(0, amountOut + 1, address(this), ""); |
| 43 | + |
| 44 | + // Just right: |
| 45 | + |
| 46 | + eulerSwap.swap(0, amountOut, address(this), ""); |
| 47 | + |
| 48 | + // Swapper received their quoted amount: |
| 49 | + |
| 50 | + assertEq(assetTST2.balanceOf(address(this)), amountOut); |
| 51 | + |
| 52 | + // eulerSwap instance is empty: |
| 53 | + |
| 54 | + assertEq(assetTST.balanceOf(address(eulerSwap)), 0); |
| 55 | + assertEq(assetTST2.balanceOf(address(eulerSwap)), 0); |
| 56 | + |
| 57 | + // Holder's NAV increased by fee amount, plus slightly extra because we are not at curve equilibrium point |
| 58 | + |
| 59 | + assertGt(getHolderNAV(), origNav + int256(amountIn - amountInNoFees)); |
| 60 | + assertEq(eTST.balanceOf(address(holder)), 10e18 + amountIn); |
| 61 | + assertEq(eTST2.balanceOf(address(holder)), 10e18 - amountOut); |
| 62 | + } |
| 63 | + |
| 64 | + function test_fees_exactOut() public monotonicHolderNAV { |
| 65 | + int256 origNav = getHolderNAV(); |
| 66 | + uint256 fee = 0.05e18; |
| 67 | + |
| 68 | + // No fees |
| 69 | + |
| 70 | + uint256 amountOut = 1e18; |
| 71 | + uint256 amountInNoFees = periphery.quoteExactOutput(address(eulerSwap), address(assetTST), address(assetTST2), amountOut); |
| 72 | + assertApproxEqAbs(amountInNoFees, 1.0017e18, 0.0001e18); |
| 73 | + |
| 74 | + // With fees: Increase input amount so output amount stays same |
| 75 | + |
| 76 | + eulerSwap = createEulerSwap(60e18, 60e18, fee, 1e18, 1e18, 0.9e18, 0.9e18); |
| 77 | + |
| 78 | + uint256 amountIn = periphery.quoteExactOutput(address(eulerSwap), address(assetTST), address(assetTST2), amountOut); |
| 79 | + assertApproxEqAbs(amountIn, amountInNoFees * 1e18 / (1e18 - fee), 1); // Same except for possible rounding up by 1 |
| 80 | + |
| 81 | + // Actually execute swap |
| 82 | + |
| 83 | + assetTST.mint(address(this), amountIn); |
| 84 | + assetTST.transfer(address(eulerSwap), amountIn); |
| 85 | + |
| 86 | + // Pulling out one extra reverts... |
| 87 | + |
| 88 | + vm.expectRevert(EulerSwap.CurveViolation.selector); |
| 89 | + eulerSwap.swap(0, amountOut + 1, address(this), ""); |
| 90 | + |
| 91 | + // Just right: |
| 92 | + |
| 93 | + eulerSwap.swap(0, amountOut, address(this), ""); |
| 94 | + |
| 95 | + // Swapper received their quoted amount: |
| 96 | + |
| 97 | + assertEq(assetTST2.balanceOf(address(this)), amountOut); |
| 98 | + |
| 99 | + // eulerSwap instance is empty: |
| 100 | + |
| 101 | + assertEq(assetTST.balanceOf(address(eulerSwap)), 0); |
| 102 | + assertEq(assetTST2.balanceOf(address(eulerSwap)), 0); |
| 103 | + |
| 104 | + // Holder's NAV increased by fee amount, plus slightly extra because we are not at curve equilibrium point |
| 105 | + |
| 106 | + assertGt(getHolderNAV(), origNav + int256(amountIn - amountInNoFees)); |
| 107 | + assertEq(eTST.balanceOf(address(holder)), 10e18 + amountIn); |
| 108 | + assertEq(eTST2.balanceOf(address(holder)), 10e18 - amountOut); |
| 109 | + } |
| 110 | +} |
0 commit comments