From 2a65ed3429b00cdc1358723573c73d9001c136b5 Mon Sep 17 00:00:00 2001 From: teddy Date: Thu, 25 Jul 2024 11:30:25 -0300 Subject: [PATCH 1/4] chore: rename BPoolBase.sol BPoolBase.t.sol --- test/unit/BCoWPool/BCoWPoolBase.sol | 2 +- test/unit/BCoWPool/BCoWPoolBase.t.sol | 2 +- test/unit/BPool/BPool.t.sol | 2 +- test/unit/BPool/{BPoolBase.sol => BPoolBase.t.sol} | 0 test/unit/BPool/BPool_Bind.t.sol | 2 +- test/unit/BPool/BPool_ExitPool.t.sol | 2 +- test/unit/BPool/BPool_ExitswapExternAmountOut.t.sol | 2 +- test/unit/BPool/BPool_ExitswapPoolAmountIn.t.sol | 2 +- test/unit/BPool/BPool_JoinPool.t.sol | 2 +- test/unit/BPool/BPool_JoinswapExternAmountIn.t.sol | 2 +- test/unit/BPool/BPool_JoinswapPoolAmountOut.t.sol | 2 +- test/unit/BPool/BPool_SwapExactAmountIn.t.sol | 2 +- test/unit/BPool/BPool_SwapExactAmountOut.t.sol | 2 +- test/unit/BPool/BPool_Unbind.t.sol | 2 +- 14 files changed, 13 insertions(+), 13 deletions(-) rename test/unit/BPool/{BPoolBase.sol => BPoolBase.t.sol} (100%) diff --git a/test/unit/BCoWPool/BCoWPoolBase.sol b/test/unit/BCoWPool/BCoWPoolBase.sol index 3418e59e..a819340b 100644 --- a/test/unit/BCoWPool/BCoWPoolBase.sol +++ b/test/unit/BCoWPool/BCoWPoolBase.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from '../BPool/BPoolBase.sol'; +import {BPoolBase} from '../BPool/BPoolBase.t.sol'; import {BCoWConst} from 'contracts/BCoWConst.sol'; import {BNum} from 'contracts/BNum.sol'; diff --git a/test/unit/BCoWPool/BCoWPoolBase.t.sol b/test/unit/BCoWPool/BCoWPoolBase.t.sol index 3418e59e..a819340b 100644 --- a/test/unit/BCoWPool/BCoWPoolBase.t.sol +++ b/test/unit/BCoWPool/BCoWPoolBase.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from '../BPool/BPoolBase.sol'; +import {BPoolBase} from '../BPool/BPoolBase.t.sol'; import {BCoWConst} from 'contracts/BCoWConst.sol'; import {BNum} from 'contracts/BNum.sol'; diff --git a/test/unit/BPool/BPool.t.sol b/test/unit/BPool/BPool.t.sol index 0a8264da..a14f3d9f 100644 --- a/test/unit/BPool/BPool.t.sol +++ b/test/unit/BPool/BPool.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20Errors} from '@openzeppelin/contracts/interfaces/draft-IERC6093.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; diff --git a/test/unit/BPool/BPoolBase.sol b/test/unit/BPool/BPoolBase.t.sol similarity index 100% rename from test/unit/BPool/BPoolBase.sol rename to test/unit/BPool/BPoolBase.t.sol diff --git a/test/unit/BPool/BPool_Bind.t.sol b/test/unit/BPool/BPool_Bind.t.sol index 12fb07a4..1cab92f7 100644 --- a/test/unit/BPool/BPool_Bind.t.sol +++ b/test/unit/BPool/BPool_Bind.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {IBPool} from 'interfaces/IBPool.sol'; diff --git a/test/unit/BPool/BPool_ExitPool.t.sol b/test/unit/BPool/BPool_ExitPool.t.sol index 11259ba6..143fe7ba 100644 --- a/test/unit/BPool/BPool_ExitPool.t.sol +++ b/test/unit/BPool/BPool_ExitPool.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; diff --git a/test/unit/BPool/BPool_ExitswapExternAmountOut.t.sol b/test/unit/BPool/BPool_ExitswapExternAmountOut.t.sol index 74c64e0d..48f7aeb7 100644 --- a/test/unit/BPool/BPool_ExitswapExternAmountOut.t.sol +++ b/test/unit/BPool/BPool_ExitswapExternAmountOut.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; diff --git a/test/unit/BPool/BPool_ExitswapPoolAmountIn.t.sol b/test/unit/BPool/BPool_ExitswapPoolAmountIn.t.sol index c9ecdff2..e856630c 100644 --- a/test/unit/BPool/BPool_ExitswapPoolAmountIn.t.sol +++ b/test/unit/BPool/BPool_ExitswapPoolAmountIn.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; diff --git a/test/unit/BPool/BPool_JoinPool.t.sol b/test/unit/BPool/BPool_JoinPool.t.sol index eeb01efe..1b954745 100644 --- a/test/unit/BPool/BPool_JoinPool.t.sol +++ b/test/unit/BPool/BPool_JoinPool.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {BNum} from 'contracts/BNum.sol'; diff --git a/test/unit/BPool/BPool_JoinswapExternAmountIn.t.sol b/test/unit/BPool/BPool_JoinswapExternAmountIn.t.sol index afa67556..1ac173e2 100644 --- a/test/unit/BPool/BPool_JoinswapExternAmountIn.t.sol +++ b/test/unit/BPool/BPool_JoinswapExternAmountIn.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {BNum} from 'contracts/BNum.sol'; diff --git a/test/unit/BPool/BPool_JoinswapPoolAmountOut.t.sol b/test/unit/BPool/BPool_JoinswapPoolAmountOut.t.sol index 78ef1fbb..7707fdb0 100644 --- a/test/unit/BPool/BPool_JoinswapPoolAmountOut.t.sol +++ b/test/unit/BPool/BPool_JoinswapPoolAmountOut.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {BNum} from 'contracts/BNum.sol'; diff --git a/test/unit/BPool/BPool_SwapExactAmountIn.t.sol b/test/unit/BPool/BPool_SwapExactAmountIn.t.sol index 9537bf7f..51af608d 100644 --- a/test/unit/BPool/BPool_SwapExactAmountIn.t.sol +++ b/test/unit/BPool/BPool_SwapExactAmountIn.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {BNum} from 'contracts/BNum.sol'; diff --git a/test/unit/BPool/BPool_SwapExactAmountOut.t.sol b/test/unit/BPool/BPool_SwapExactAmountOut.t.sol index 0792a6d2..1f379b5b 100644 --- a/test/unit/BPool/BPool_SwapExactAmountOut.t.sol +++ b/test/unit/BPool/BPool_SwapExactAmountOut.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {BNum} from 'contracts/BNum.sol'; diff --git a/test/unit/BPool/BPool_Unbind.t.sol b/test/unit/BPool/BPool_Unbind.t.sol index 7034e85c..5bcbece2 100644 --- a/test/unit/BPool/BPool_Unbind.t.sol +++ b/test/unit/BPool/BPool_Unbind.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.25; -import {BPoolBase} from './BPoolBase.sol'; +import {BPoolBase} from './BPoolBase.t.sol'; import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {IBPool} from 'interfaces/IBPool.sol'; From 91d3afd2922fc4115559384f5601fc4521f9179a Mon Sep 17 00:00:00 2001 From: teddy Date: Thu, 25 Jul 2024 11:35:30 -0300 Subject: [PATCH 2/4] chore: remove BCoWPoolBase.sol --- test/unit/BCoWPool/BCoWPool.t.sol | 2 +- test/unit/BCoWPool/BCoWPoolBase.sol | 28 ------------------- .../BCoWPool/BCoWPool_IsValidSignature.t.sol | 2 +- 3 files changed, 2 insertions(+), 30 deletions(-) delete mode 100644 test/unit/BCoWPool/BCoWPoolBase.sol diff --git a/test/unit/BCoWPool/BCoWPool.t.sol b/test/unit/BCoWPool/BCoWPool.t.sol index 66d4a825..5c8c79f3 100644 --- a/test/unit/BCoWPool/BCoWPool.t.sol +++ b/test/unit/BCoWPool/BCoWPool.t.sol @@ -3,7 +3,7 @@ pragma solidity 0.8.25; import {IERC20} from '@cowprotocol/interfaces/IERC20.sol'; -import {BCoWPoolBase} from './BCoWPoolBase.sol'; +import {BCoWPoolBase} from './BCoWPoolBase.t.sol'; import {IBCoWFactory} from 'interfaces/IBCoWFactory.sol'; diff --git a/test/unit/BCoWPool/BCoWPoolBase.sol b/test/unit/BCoWPool/BCoWPoolBase.sol deleted file mode 100644 index a819340b..00000000 --- a/test/unit/BCoWPool/BCoWPoolBase.sol +++ /dev/null @@ -1,28 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.25; - -import {BPoolBase} from '../BPool/BPoolBase.t.sol'; -import {BCoWConst} from 'contracts/BCoWConst.sol'; -import {BNum} from 'contracts/BNum.sol'; - -import {ISettlement} from 'interfaces/ISettlement.sol'; -import {MockBCoWPool} from 'test/manual-smock/MockBCoWPool.sol'; - -contract BCoWPoolBase is BPoolBase, BCoWConst, BNum { - bytes32 public appData = bytes32('appData'); - address public cowSolutionSettler = makeAddr('cowSolutionSettler'); - bytes32 public domainSeparator = bytes32(bytes2(0xf00b)); - address public vaultRelayer = makeAddr('vaultRelayer'); - address public tokenIn; - address public tokenOut; - MockBCoWPool bCoWPool; - - function setUp() public virtual override { - super.setUp(); - tokenIn = tokens[0]; - tokenOut = tokens[1]; - 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); - } -} diff --git a/test/unit/BCoWPool/BCoWPool_IsValidSignature.t.sol b/test/unit/BCoWPool/BCoWPool_IsValidSignature.t.sol index aeb8086b..ba1a58bb 100644 --- a/test/unit/BCoWPool/BCoWPool_IsValidSignature.t.sol +++ b/test/unit/BCoWPool/BCoWPool_IsValidSignature.t.sol @@ -6,7 +6,7 @@ import {IERC20} from '@cowprotocol/interfaces/IERC20.sol'; import {GPv2Order} from '@cowprotocol/libraries/GPv2Order.sol'; import {IERC1271} from '@openzeppelin/contracts/interfaces/IERC1271.sol'; -import {BCoWPoolBase} from './BCoWPoolBase.sol'; +import {BCoWPoolBase} from './BCoWPoolBase.t.sol'; import {IBCoWPool} from 'interfaces/IBCoWPool.sol'; contract BCoWPoolIsValidSignature is BCoWPoolBase { From 3800427638ac37faa3cad0a696af1967d67a4035 Mon Sep 17 00:00:00 2001 From: teddy Date: Thu, 25 Jul 2024 11:42:56 -0300 Subject: [PATCH 3/4] chore: remove token{In,Out} from base setup --- test/unit/BCoWPool/BCoWPoolBase.t.sol | 4 ---- test/unit/BCoWPool/BCoWPool_Verify.t.sol | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/BCoWPool/BCoWPoolBase.t.sol b/test/unit/BCoWPool/BCoWPoolBase.t.sol index a819340b..db328d1d 100644 --- a/test/unit/BCoWPool/BCoWPoolBase.t.sol +++ b/test/unit/BCoWPool/BCoWPoolBase.t.sol @@ -13,14 +13,10 @@ contract BCoWPoolBase is BPoolBase, BCoWConst, BNum { address public cowSolutionSettler = makeAddr('cowSolutionSettler'); bytes32 public domainSeparator = bytes32(bytes2(0xf00b)); address public vaultRelayer = makeAddr('vaultRelayer'); - address public tokenIn; - address public tokenOut; MockBCoWPool bCoWPool; function setUp() public virtual override { super.setUp(); - tokenIn = tokens[0]; - tokenOut = tokens[1]; 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); diff --git a/test/unit/BCoWPool/BCoWPool_Verify.t.sol b/test/unit/BCoWPool/BCoWPool_Verify.t.sol index 3af73f50..fce924be 100644 --- a/test/unit/BCoWPool/BCoWPool_Verify.t.sol +++ b/test/unit/BCoWPool/BCoWPool_Verify.t.sol @@ -10,6 +10,8 @@ import {IBPool} from 'interfaces/IBPool.sol'; contract BCoWPoolVerify is BCoWPoolBase { // Valid scenario: + address public tokenIn; + address public tokenOut; uint256 public tokenAmountIn = 1e18; uint256 public tokenInBalance = 100e18; uint256 public tokenOutBalance = 80e18; @@ -22,6 +24,8 @@ contract BCoWPoolVerify is BCoWPoolBase { function setUp() public virtual override { super.setUp(); + tokenIn = tokens[0]; + tokenOut = tokens[1]; bCoWPool.set__tokens(tokens); bCoWPool.set__records(tokenIn, IBPool.Record({bound: true, index: 0, denorm: tokenInWeight})); bCoWPool.set__records(tokenOut, IBPool.Record({bound: true, index: 1, denorm: tokenOutWeight})); From 4cbefe2d352e04a4ac83b26832efc867518f65e9 Mon Sep 17 00:00:00 2001 From: teddy Date: Thu, 25 Jul 2024 11:45:06 -0300 Subject: [PATCH 4/4] chore: give token to bind its own variable --- test/unit/BPool/BPool_Bind.t.sol | 40 +++++++++++++++++--------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/test/unit/BPool/BPool_Bind.t.sol b/test/unit/BPool/BPool_Bind.t.sol index 1cab92f7..8e8c0bc5 100644 --- a/test/unit/BPool/BPool_Bind.t.sol +++ b/test/unit/BPool/BPool_Bind.t.sol @@ -6,22 +6,24 @@ import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; import {IBPool} from 'interfaces/IBPool.sol'; contract BPoolBind is BPoolBase { + address public token; uint256 public tokenBindBalance = 100e18; uint256 public tokenWeight = 1e18; uint256 public totalWeight = 10e18; function setUp() public virtual override { super.setUp(); + token = tokens[0]; - vm.mockCall(tokens[0], abi.encodePacked(IERC20.transferFrom.selector), abi.encode()); - vm.mockCall(tokens[0], abi.encodePacked(IERC20.transfer.selector), abi.encode()); + vm.mockCall(token, abi.encodePacked(IERC20.transferFrom.selector), abi.encode()); + vm.mockCall(token, abi.encodePacked(IERC20.transfer.selector), abi.encode()); } function test_RevertWhen_ReentrancyLockIsSet() external { bPool.call__setLock(_MUTEX_TAKEN); vm.expectRevert(IBPool.BPool_Reentrancy.selector); // it should revert - bPool.bind(tokens[0], tokenBindBalance, tokenWeight); + bPool.bind(token, tokenBindBalance, tokenWeight); } function test_RevertWhen_CallerIsNOTController(address _caller) external { @@ -29,53 +31,53 @@ contract BPoolBind is BPoolBase { vm.assume(_caller != address(this)); vm.prank(_caller); vm.expectRevert(IBPool.BPool_CallerIsNotController.selector); - bPool.bind(tokens[0], tokenBindBalance, tokenWeight); + bPool.bind(token, tokenBindBalance, tokenWeight); } function test_RevertWhen_TokenIsAlreadyBound() external { - bPool.set__records(tokens[0], IBPool.Record({bound: true, index: 0, denorm: tokenWeight})); + bPool.set__records(token, IBPool.Record({bound: true, index: 0, denorm: tokenWeight})); // it should revert vm.expectRevert(IBPool.BPool_TokenAlreadyBound.selector); - bPool.bind(tokens[0], tokenBindBalance, tokenWeight); + bPool.bind(token, tokenBindBalance, tokenWeight); } function test_RevertWhen_PoolIsFinalized() external { bPool.set__finalized(true); // it should revert vm.expectRevert(IBPool.BPool_PoolIsFinalized.selector); - bPool.bind(tokens[0], tokenBindBalance, tokenWeight); + bPool.bind(token, tokenBindBalance, tokenWeight); } function test_RevertWhen_MAX_BOUND_TOKENSTokensAreAlreadyBound() external { _setRandomTokens(MAX_BOUND_TOKENS); // it should revert vm.expectRevert(IBPool.BPool_TokensAboveMaximum.selector); - bPool.bind(tokens[0], tokenBindBalance, tokenWeight); + bPool.bind(token, tokenBindBalance, tokenWeight); } function test_RevertWhen_TokenWeightIsTooLow() external { // it should revert vm.expectRevert(IBPool.BPool_WeightBelowMinimum.selector); - bPool.bind(tokens[0], tokenBindBalance, MIN_WEIGHT - 1); + bPool.bind(token, tokenBindBalance, MIN_WEIGHT - 1); } function test_RevertWhen_TokenWeightIsTooHigh() external { // it should revert vm.expectRevert(IBPool.BPool_WeightAboveMaximum.selector); - bPool.bind(tokens[0], tokenBindBalance, MAX_WEIGHT + 1); + bPool.bind(token, tokenBindBalance, MAX_WEIGHT + 1); } function test_RevertWhen_TooLittleBalanceIsProvided() external { // it should revert vm.expectRevert(IBPool.BPool_BalanceBelowMinimum.selector); - bPool.bind(tokens[0], MIN_BALANCE - 1, tokenWeight); + bPool.bind(token, MIN_BALANCE - 1, tokenWeight); } function test_RevertWhen_WeightSumExceedsMAX_TOTAL_WEIGHT() external { bPool.set__totalWeight(2 * MAX_TOTAL_WEIGHT / 3); // it should revert vm.expectRevert(IBPool.BPool_TotalWeightAboveMaximum.selector); - bPool.bind(tokens[0], tokenBindBalance, MAX_TOTAL_WEIGHT / 2); + bPool.bind(token, tokenBindBalance, MAX_TOTAL_WEIGHT / 2); } function test_WhenTokenCanBeBound(uint256 _existingTokens) external { @@ -84,24 +86,24 @@ contract BPoolBind is BPoolBase { bPool.set__totalWeight(totalWeight); // it calls _pullUnderlying - bPool.expectCall__pullUnderlying(tokens[0], address(this), tokenBindBalance); + bPool.expectCall__pullUnderlying(token, address(this), tokenBindBalance); // it sets the reentrancy lock bPool.expectCall__setLock(_MUTEX_TAKEN); // it emits LOG_CALL event vm.expectEmit(); - bytes memory _data = abi.encodeWithSelector(IBPool.bind.selector, tokens[0], tokenBindBalance, tokenWeight); + bytes memory _data = abi.encodeWithSelector(IBPool.bind.selector, token, tokenBindBalance, tokenWeight); emit IBPool.LOG_CALL(IBPool.bind.selector, address(this), _data); - bPool.bind(tokens[0], tokenBindBalance, tokenWeight); + bPool.bind(token, tokenBindBalance, tokenWeight); // it clears the reentrancy lock assertEq(bPool.call__getLock(), _MUTEX_FREE); // it adds token to the tokens array - assertEq(bPool.call__tokens()[_existingTokens], tokens[0]); + assertEq(bPool.call__tokens()[_existingTokens], token); // it sets the token record - assertEq(bPool.call__records(tokens[0]).bound, true); - assertEq(bPool.call__records(tokens[0]).denorm, tokenWeight); - assertEq(bPool.call__records(tokens[0]).index, _existingTokens); + assertEq(bPool.call__records(token).bound, true); + assertEq(bPool.call__records(token).denorm, tokenWeight); + assertEq(bPool.call__records(token).index, _existingTokens); // it sets total weight assertEq(bPool.call__totalWeight(), totalWeight + tokenWeight); }