From e39d8f40afb705fb66eac898b4ee22e0563364b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wei=C3=9Fer=20Hase?= Date: Fri, 26 Jul 2024 15:09:27 +0200 Subject: [PATCH] test: adding e2e extremeCase and improving boundries --- test/integration/BPool.t.sol | 55 ++++++++++++++++++++++++++++++++ test/invariants/fuzz/BMath.t.sol | 12 +++---- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/test/integration/BPool.t.sol b/test/integration/BPool.t.sol index 6684ca2e..16d99c07 100644 --- a/test/integration/BPool.t.sol +++ b/test/integration/BPool.t.sol @@ -113,6 +113,61 @@ abstract contract BPoolIntegrationTest is Test, GasSnapshot { whitnessPool.finalize(); } + function test_extremeCase() public virtual { + uint256 _DAI_WEIGHT = 21095347289582322017; + uint256 _WETH_WEIGHT = 14053292305279328903; + uint256 _DAI_BALANCE = 1000000000000000000; + uint256 _WETH_BALANCE = 839390325460697805479509969833437577; + uint256 _DAI_AMOUNT_OUT = 1000000000000000000; + uint256 _FEE = 1e18 / 10 ** 6; + uint256 _WHITNESS_FEE = 0.999999e18; + + vm.startPrank(lp); + pool = factory.newBPool(); + whitnessPool = factory.newBPool(); + + deal(address(dai), lp, type(uint248).max); + deal(address(weth), lp, type(uint248).max); + + deal(address(dai), swapper.addr, type(uint248).max); + deal(address(weth), swapperInverse.addr, type(uint248).max); + + dai.approve(address(pool), type(uint256).max); + weth.approve(address(pool), type(uint256).max); + + pool.bind(address(dai), _DAI_BALANCE, _DAI_WEIGHT); + pool.bind(address(weth), _WETH_BALANCE, _WETH_WEIGHT); + + dai.approve(address(whitnessPool), type(uint256).max); + weth.approve(address(whitnessPool), type(uint256).max); + + whitnessPool.bind(address(dai), _DAI_BALANCE, _DAI_WEIGHT); + whitnessPool.bind(address(weth), _WETH_BALANCE, _WETH_WEIGHT); + + // set swap fee + // NOTE: original pool keeps min swap fee + pool.setSwapFee(_FEE); + whitnessPool.setSwapFee(_WHITNESS_FEE); + + // finalize + pool.finalize(); + whitnessPool.finalize(); + + + vm.startPrank(swapper.addr); + dai.approve(address(pool), type(uint256).max); + dai.approve(address(whitnessPool), type(uint256).max); + + (uint256 amountIn,) = + pool.swapExactAmountOut(address(dai), type(uint256).max, address(weth), _DAI_AMOUNT_OUT, type(uint256).max); + + // NOTE: fails with BPool_TokenAmountInAboveMaxRatio() + uint256 whitnessBPT = whitnessPool.joinswapExternAmountIn(address(dai), amountIn, 0); + uint256 whitnessAmountOut = whitnessPool.exitswapPoolAmountIn(address(weth), whitnessBPT, 0); + + assertGt(_DAI_AMOUNT_OUT, whitnessAmountOut); + } + function testIndirectSwap_ExactIn() public { // checks that pool.swapExactAmountIn >= whitnesPool.joinswapExternAmountIn + whitnesPool.exitswapPoolAmountIn diff --git a/test/invariants/fuzz/BMath.t.sol b/test/invariants/fuzz/BMath.t.sol index be02568d..75131362 100644 --- a/test/invariants/fuzz/BMath.t.sol +++ b/test/invariants/fuzz/BMath.t.sol @@ -159,9 +159,9 @@ contract FuzzBMath is EchidnaTest { tokenWeightOut = clamp(tokenWeightOut, MIN_WEIGHT, MAX_TOTAL_WEIGHT - tokenWeightIn); totalWeight = clamp(totalWeight, tokenWeightIn + tokenWeightOut, MAX_TOTAL_WEIGHT); totalWeight = tokenWeightIn + tokenWeightOut; - tokenBalanceIn = clamp(tokenBalanceIn, BONE, type(uint256).max); - tokenBalanceOut = clamp(tokenBalanceOut, BONE, type(uint256).max); - tokenAmountIn = clamp(tokenAmountIn, BONE, type(uint256).max); + tokenBalanceIn = clamp(tokenBalanceIn, 1e6, type(uint256).max); + tokenBalanceOut = clamp(tokenBalanceOut, 1e6, type(uint256).max); + tokenAmountIn = clamp(tokenAmountIn, 1, type(uint256).max); poolSupply = clamp(poolSupply, 100 * BONE, type(uint256).max); swapFee = clamp(swapFee, MIN_FEE, MAX_FEE); @@ -210,10 +210,10 @@ contract FuzzBMath is EchidnaTest { tokenWeightOut = clamp(tokenWeightOut, MIN_WEIGHT, MAX_TOTAL_WEIGHT - tokenWeightIn); totalWeight = clamp(totalWeight, tokenWeightIn + tokenWeightOut, MAX_TOTAL_WEIGHT); totalWeight = tokenWeightIn + tokenWeightOut; - tokenBalanceIn = clamp(tokenBalanceIn, BONE, type(uint256).max); - tokenBalanceOut = clamp(tokenBalanceOut, BONE, type(uint256).max); + tokenBalanceIn = clamp(tokenBalanceIn, 1e6, type(uint256).max); + tokenBalanceOut = clamp(tokenBalanceOut, 1e6, type(uint256).max); poolSupply = clamp(poolSupply, 100 * BONE, type(uint256).max); - tokenAmountOut = clamp(tokenAmountOut, BONE, type(uint256).max); + tokenAmountOut = clamp(tokenAmountOut, 1, type(uint256).max); swapFee = clamp(swapFee, MIN_FEE, MAX_FEE); emit Log('tokenWeightIn', tokenWeightIn);