|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | + |
| 3 | +pragma solidity ^0.8.24; |
| 4 | + |
| 5 | +import { console2 } from "forge-std/console2.sol"; |
| 6 | + |
| 7 | +import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol"; |
| 8 | + |
| 9 | +import { BaseAclAmmTest } from "./utils/BaseAclAmmTest.sol"; |
| 10 | +import { AclAmmPool } from "../../contracts/AclAmmPool.sol"; |
| 11 | +import { AclAmmMath } from "../../contracts/lib/AclAmmMath.sol"; |
| 12 | + |
| 13 | +contract AclAmmLiquidityTest is BaseAclAmmTest { |
| 14 | + using FixedPoint for uint256; |
| 15 | + |
| 16 | + uint256 constant _MAX_PRICE_ERROR_ABS = 5; |
| 17 | + uint256 constant _MAX_CENTEREDNESS_ERROR_ABS = 1e9; |
| 18 | + |
| 19 | + function testAddLiquidity_Fuzz( |
| 20 | + uint256 exactBptAmountOut, |
| 21 | + uint256 initialDaiBalance, |
| 22 | + uint256 initialUsdcBalance |
| 23 | + ) public { |
| 24 | + _setPoolBalances(initialDaiBalance, initialUsdcBalance); |
| 25 | + |
| 26 | + uint256 totalSupply = vault.totalSupply(pool); |
| 27 | + exactBptAmountOut = bound(exactBptAmountOut, 1e6, 100 * totalSupply); |
| 28 | + |
| 29 | + uint256[] memory maxAmountsIn = new uint256[](2); |
| 30 | + maxAmountsIn[daiIdx] = dai.balanceOf(alice); |
| 31 | + maxAmountsIn[usdcIdx] = usdc.balanceOf(alice); |
| 32 | + |
| 33 | + uint256[] memory virtualBalancesBefore = AclAmmPool(pool).getLastVirtualBalances(); |
| 34 | + (, , uint256[] memory balancesBefore, ) = vault.getPoolTokenInfo(pool); |
| 35 | + |
| 36 | + vm.prank(alice); |
| 37 | + router.addLiquidityProportional(pool, maxAmountsIn, exactBptAmountOut, false, ""); |
| 38 | + |
| 39 | + uint256[] memory virtualBalancesAfter = AclAmmPool(pool).getLastVirtualBalances(); |
| 40 | + (, , uint256[] memory balancesAfter, ) = vault.getPoolTokenInfo(pool); |
| 41 | + |
| 42 | + // Check if virtual balances were correctly updated. |
| 43 | + uint256 proportion = exactBptAmountOut.divUp(totalSupply); |
| 44 | + assertEq( |
| 45 | + virtualBalancesAfter[daiIdx], |
| 46 | + virtualBalancesBefore[daiIdx].mulUp(FixedPoint.ONE + proportion), |
| 47 | + "DAI virtual balance does not match" |
| 48 | + ); |
| 49 | + assertEq( |
| 50 | + virtualBalancesAfter[usdcIdx], |
| 51 | + virtualBalancesBefore[usdcIdx].mulUp(FixedPoint.ONE + proportion), |
| 52 | + "USDC virtual balance does not match" |
| 53 | + ); |
| 54 | + |
| 55 | + _checkPriceAndCenteredness(balancesBefore, balancesAfter, virtualBalancesBefore, virtualBalancesAfter); |
| 56 | + } |
| 57 | + |
| 58 | + function testRemoveLiquidity_Fuzz( |
| 59 | + uint256 exactBptAmountIn, |
| 60 | + uint256 initialDaiBalance, |
| 61 | + uint256 initialUsdcBalance |
| 62 | + ) public { |
| 63 | + _setPoolBalances(initialDaiBalance, initialUsdcBalance); |
| 64 | + |
| 65 | + uint256 totalSupply = vault.totalSupply(pool); |
| 66 | + exactBptAmountIn = bound(exactBptAmountIn, 1e6, (9 * totalSupply) / 10); |
| 67 | + |
| 68 | + uint256[] memory minAmountsOut = new uint256[](2); |
| 69 | + minAmountsOut[daiIdx] = 0; |
| 70 | + minAmountsOut[usdcIdx] = 0; |
| 71 | + |
| 72 | + uint256[] memory virtualBalancesBefore = AclAmmPool(pool).getLastVirtualBalances(); |
| 73 | + (, , uint256[] memory balancesBefore, ) = vault.getPoolTokenInfo(pool); |
| 74 | + |
| 75 | + vm.prank(lp); |
| 76 | + router.removeLiquidityProportional(pool, exactBptAmountIn, minAmountsOut, false, ""); |
| 77 | + |
| 78 | + uint256[] memory virtualBalancesAfter = AclAmmPool(pool).getLastVirtualBalances(); |
| 79 | + (, , uint256[] memory balancesAfter, ) = vault.getPoolTokenInfo(pool); |
| 80 | + |
| 81 | + // Check if virtual balances were correctly updated. |
| 82 | + uint256 proportion = exactBptAmountIn.divUp(totalSupply); |
| 83 | + assertEq( |
| 84 | + virtualBalancesAfter[daiIdx], |
| 85 | + virtualBalancesBefore[daiIdx].mulDown(FixedPoint.ONE - proportion), |
| 86 | + "DAI virtual balance does not match" |
| 87 | + ); |
| 88 | + assertEq( |
| 89 | + virtualBalancesAfter[usdcIdx], |
| 90 | + virtualBalancesBefore[usdcIdx].mulDown(FixedPoint.ONE - proportion), |
| 91 | + "USDC virtual balance does not match" |
| 92 | + ); |
| 93 | + |
| 94 | + _checkPriceAndCenteredness(balancesBefore, balancesAfter, virtualBalancesBefore, virtualBalancesAfter); |
| 95 | + } |
| 96 | + |
| 97 | + function _checkPriceAndCenteredness( |
| 98 | + uint256[] memory balancesBefore, |
| 99 | + uint256[] memory balancesAfter, |
| 100 | + uint256[] memory virtualBalancesBefore, |
| 101 | + uint256[] memory virtualBalancesAfter |
| 102 | + ) internal view { |
| 103 | + // Check if price is constant. |
| 104 | + uint256 daiPriceBefore = (balancesBefore[usdcIdx] + virtualBalancesBefore[usdcIdx]).divDown( |
| 105 | + balancesBefore[daiIdx] + virtualBalancesBefore[daiIdx] |
| 106 | + ); |
| 107 | + uint256 daiPriceAfter = (balancesAfter[usdcIdx] + virtualBalancesAfter[usdcIdx]).divDown( |
| 108 | + balancesAfter[daiIdx] + virtualBalancesAfter[daiIdx] |
| 109 | + ); |
| 110 | + assertApproxEqAbs(daiPriceAfter, daiPriceBefore, _MAX_PRICE_ERROR_ABS, "Price changed"); |
| 111 | + |
| 112 | + // Check if centeredness is constant. |
| 113 | + uint256 centerednessBefore = AclAmmMath.calculateCenteredness(balancesBefore, virtualBalancesBefore); |
| 114 | + uint256 centerednessAfter = AclAmmMath.calculateCenteredness(balancesAfter, virtualBalancesAfter); |
| 115 | + assertApproxEqAbs(centerednessAfter, centerednessBefore, _MAX_CENTEREDNESS_ERROR_ABS, "Centeredness changed"); |
| 116 | + } |
| 117 | + |
| 118 | + function _setPoolBalances(uint256 initialDaiBalance, uint256 initialUsdcBalance) internal { |
| 119 | + initialDaiBalance = bound(initialDaiBalance, 1e10, dai.balanceOf(address(vault))); |
| 120 | + initialUsdcBalance = bound(initialUsdcBalance, 1e10, usdc.balanceOf(address(vault))); |
| 121 | + |
| 122 | + uint256[] memory initialBalances = new uint256[](2); |
| 123 | + initialBalances[daiIdx] = initialDaiBalance; |
| 124 | + initialBalances[usdcIdx] = initialUsdcBalance; |
| 125 | + |
| 126 | + vault.manualSetPoolBalances(pool, initialBalances, initialBalances); |
| 127 | + } |
| 128 | +} |
0 commit comments