From 9bf87edf5b255bc793d993809929164a08d100df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wei=C3=9Fer=20Hase?= Date: Tue, 23 Jul 2024 11:41:57 +0200 Subject: [PATCH] chore: cleaning up remanent test code --- test/unit/BCoWPool.t.sol | 48 -------- test/unit/BPool.t.sol | 232 --------------------------------------- 2 files changed, 280 deletions(-) delete mode 100644 test/unit/BCoWPool.t.sol diff --git a/test/unit/BCoWPool.t.sol b/test/unit/BCoWPool.t.sol deleted file mode 100644 index eb8acad5..00000000 --- a/test/unit/BCoWPool.t.sol +++ /dev/null @@ -1,48 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.25; - -import {BasePoolTest} from './BPool.t.sol'; -import {IERC20} from '@cowprotocol/interfaces/IERC20.sol'; -import {GPv2Order} from '@cowprotocol/libraries/GPv2Order.sol'; -import {IERC1271} from '@openzeppelin/contracts/interfaces/IERC1271.sol'; -import {BCoWConst} from 'contracts/BCoWConst.sol'; -import {IBCoWFactory} from 'interfaces/IBCoWFactory.sol'; -import {IBCoWPool} from 'interfaces/IBCoWPool.sol'; -import {IBPool} from 'interfaces/IBPool.sol'; -import {ISettlement} from 'interfaces/ISettlement.sol'; -import {MockBCoWPool} from 'test/manual-smock/MockBCoWPool.sol'; -import {MockBPool} from 'test/smock/MockBPool.sol'; - -abstract contract BaseCoWPoolTest is BasePoolTest, BCoWConst { - address public cowSolutionSettler = makeAddr('cowSolutionSettler'); - bytes32 public domainSeparator = bytes32(bytes2(0xf00b)); - address public vaultRelayer = makeAddr('vaultRelayer'); - bytes32 public appData = bytes32('appData'); - - GPv2Order.Data correctOrder; - - MockBCoWPool bCoWPool; - - function setUp() public virtual override { - super.setUp(); - vm.mockCall(cowSolutionSettler, abi.encodePacked(ISettlement.domainSeparator.selector), abi.encode(domainSeparator)); - vm.mockCall(cowSolutionSettler, abi.encodePacked(ISettlement.vaultRelayer.selector), abi.encode(vaultRelayer)); - bCoWPool = new MockBCoWPool(cowSolutionSettler, appData); - bPool = MockBPool(address(bCoWPool)); - _setRandomTokens(TOKENS_AMOUNT); - correctOrder = GPv2Order.Data({ - sellToken: IERC20(tokens[1]), - buyToken: IERC20(tokens[0]), - receiver: GPv2Order.RECEIVER_SAME_AS_OWNER, - sellAmount: 0, - buyAmount: 0, - validTo: uint32(block.timestamp + 1 minutes), - appData: appData, - feeAmount: 0, - kind: GPv2Order.KIND_SELL, - partiallyFillable: false, - sellTokenBalance: GPv2Order.BALANCE_ERC20, - buyTokenBalance: GPv2Order.BALANCE_ERC20 - }); - } -} diff --git a/test/unit/BPool.t.sol b/test/unit/BPool.t.sol index 9010ae93..5e1f0abc 100644 --- a/test/unit/BPool.t.sol +++ b/test/unit/BPool.t.sol @@ -245,238 +245,6 @@ abstract contract BasePoolTest is Test, BConst, Utils, BMath { } } -contract BPool_Unit_JoinswapPoolAmountOut is BasePoolTest { - address tokenIn; - - struct JoinswapPoolAmountOut_FuzzScenario { - uint256 poolAmountOut; - uint256 tokenInBalance; - uint256 tokenInDenorm; - uint256 totalSupply; - uint256 totalWeight; - uint256 swapFee; - } - - function _setValues(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) internal { - tokenIn = tokens[0]; - - // Create mocks for tokenIn - _mockTransferFrom(tokenIn); - - // Set balances - bPool.set__records( - tokenIn, - IBPool.Record({ - bound: true, - index: 0, // NOTE: irrelevant for this method - denorm: _fuzz.tokenInDenorm - }) - ); - _mockPoolBalance(tokenIn, _fuzz.tokenInBalance); - - // Set swapFee - _setSwapFee(_fuzz.swapFee); - // Set finalize - _setFinalize(true); - // Set totalSupply - _setTotalSupply(_fuzz.totalSupply); - // Set totalWeight - _setTotalWeight(_fuzz.totalWeight); - } - - function _assumeHappyPath(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) internal view { - // safe bound assumptions - _fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT); - _fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * TOKENS_AMOUNT, MAX_TOTAL_WEIGHT); - - _fuzz.poolAmountOut = bound(_fuzz.poolAmountOut, INIT_POOL_SUPPLY, type(uint256).max - INIT_POOL_SUPPLY); - _fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max - _fuzz.poolAmountOut); - - // min - vm.assume(_fuzz.tokenInBalance >= MIN_BALANCE); - - // internal calculation for calcSingleInGivenPoolOut - _assumeCalcSingleInGivenPoolOut( - _fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.totalSupply, _fuzz.totalWeight, _fuzz.poolAmountOut - ); - - uint256 _tokenAmountIn = calcSingleInGivenPoolOut( - _fuzz.tokenInBalance, - _fuzz.tokenInDenorm, - _fuzz.totalSupply, - _fuzz.totalWeight, - _fuzz.poolAmountOut, - _fuzz.swapFee - ); - - // L428 BPool.sol - vm.assume(_tokenAmountIn > 0); - - // max - vm.assume(_fuzz.tokenInBalance < type(uint256).max - _tokenAmountIn); - - // MAX_IN_RATIO - vm.assume(_fuzz.tokenInBalance < type(uint256).max / MAX_IN_RATIO); - vm.assume(_tokenAmountIn <= bmul(_fuzz.tokenInBalance, MAX_IN_RATIO)); - } - - modifier happyPath(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) { - _assumeHappyPath(_fuzz); - _setValues(_fuzz); - _; - } - - function test_Revert_NotFinalized(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - _setFinalize(false); - - vm.expectRevert(IBPool.BPool_PoolNotFinalized.selector); - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } - - function test_Revert_NotBound( - JoinswapPoolAmountOut_FuzzScenario memory _fuzz, - address _tokenIn - ) public happyPath(_fuzz) { - assumeNotForgeAddress(_tokenIn); - - vm.expectRevert(IBPool.BPool_TokenNotBound.selector); - bPool.joinswapPoolAmountOut(_tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } - - function test_Revert_InvalidTokenAmountIn(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - _fuzz.poolAmountOut = 0; - - vm.expectRevert(IBPool.BPool_InvalidTokenAmountIn.selector); - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } - - function test_Revert_TokenAmountInAboveMaxAmountIn( - JoinswapPoolAmountOut_FuzzScenario memory _fuzz, - uint256 _maxAmountIn - ) public happyPath(_fuzz) { - uint256 _tokenAmountIn = calcSingleInGivenPoolOut( - _fuzz.tokenInBalance, - _fuzz.tokenInDenorm, - _fuzz.totalSupply, - _fuzz.totalWeight, - _fuzz.poolAmountOut, - _fuzz.swapFee - ); - _maxAmountIn = bound(_maxAmountIn, 0, _tokenAmountIn - 1); - - vm.expectRevert(IBPool.BPool_TokenAmountInAboveMaxAmountIn.selector); - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, _maxAmountIn); - } - - function test_Revert_TokenAmountInAboveMaxIn(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public { - // Replicating _assumeHappyPath, but removing irrelevant assumptions and conditioning the revert - _fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT); - _fuzz.swapFee = bound(_fuzz.swapFee, MIN_FEE, MAX_FEE); - _fuzz.totalWeight = bound(_fuzz.totalWeight, MIN_WEIGHT * TOKENS_AMOUNT, MAX_TOTAL_WEIGHT); - _fuzz.tokenInBalance = bound(_fuzz.tokenInBalance, MIN_BALANCE, type(uint256).max / MAX_IN_RATIO); - _fuzz.poolAmountOut = bound(_fuzz.poolAmountOut, INIT_POOL_SUPPLY, type(uint256).max - INIT_POOL_SUPPLY); - _fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max - _fuzz.poolAmountOut); - _assumeCalcSingleInGivenPoolOut( - _fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.totalSupply, _fuzz.totalWeight, _fuzz.poolAmountOut - ); - uint256 _tokenAmountIn = calcSingleInGivenPoolOut( - _fuzz.tokenInBalance, - _fuzz.tokenInDenorm, - _fuzz.totalSupply, - _fuzz.totalWeight, - _fuzz.poolAmountOut, - _fuzz.swapFee - ); - vm.assume(_tokenAmountIn > bmul(_fuzz.tokenInBalance, MAX_IN_RATIO)); - - _setValues(_fuzz); - - vm.expectRevert(IBPool.BPool_TokenAmountInAboveMaxRatio.selector); - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } - - function test_Revert_Reentrancy(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public { - _expectRevertByReentrancy(); - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } - - function test_Emit_LogJoin(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - uint256 _tokenAmountIn = calcSingleInGivenPoolOut( - _fuzz.tokenInBalance, - _fuzz.tokenInDenorm, - _fuzz.totalSupply, - _fuzz.totalWeight, - _fuzz.poolAmountOut, - _fuzz.swapFee - ); - - vm.expectEmit(); - emit IBPool.LOG_JOIN(address(this), tokenIn, _tokenAmountIn); - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } - - function test_Set_ReentrancyLock(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - _expectSetReentrancyLock(); - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } - - function test_Mint_PoolShare(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - - assertEq(bPool.totalSupply(), _fuzz.totalSupply + _fuzz.poolAmountOut); - } - - function test_Push_PoolShare(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - uint256 _balanceBefore = bPool.balanceOf(address(this)); - - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - - assertEq(bPool.balanceOf(address(this)), _balanceBefore + _fuzz.poolAmountOut); - } - - function test_Pull_Underlying(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - uint256 _tokenAmountIn = calcSingleInGivenPoolOut( - _fuzz.tokenInBalance, - _fuzz.tokenInDenorm, - _fuzz.totalSupply, - _fuzz.totalWeight, - _fuzz.poolAmountOut, - _fuzz.swapFee - ); - - vm.expectCall( - address(tokenIn), - abi.encodeWithSelector(IERC20.transferFrom.selector, address(this), address(bPool), _tokenAmountIn) - ); - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } - - function test_Returns_TokenAmountIn(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - uint256 _expectedTokenAmountIn = calcSingleInGivenPoolOut( - _fuzz.tokenInBalance, - _fuzz.tokenInDenorm, - _fuzz.totalSupply, - _fuzz.totalWeight, - _fuzz.poolAmountOut, - _fuzz.swapFee - ); - - (uint256 _tokenAmountIn) = bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - - assertEq(_expectedTokenAmountIn, _tokenAmountIn); - } - - function test_Emit_LogCall(JoinswapPoolAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) { - vm.expectEmit(); - bytes memory _data = - abi.encodeWithSelector(BPool.joinswapPoolAmountOut.selector, tokenIn, _fuzz.poolAmountOut, type(uint256).max); - emit IBPool.LOG_CALL(BPool.joinswapPoolAmountOut.selector, address(this), _data); - - bPool.joinswapPoolAmountOut(tokenIn, _fuzz.poolAmountOut, type(uint256).max); - } -} - contract BPool_Unit_ExitswapExternAmountOut is BasePoolTest { address tokenOut;