Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: btt tests for bcowpool constructor #163

Merged
merged 29 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
5321bbf
test: btt tests for bpool.swapExactAmountIn
0xteddybear Jul 9, 2024
432f63a
chore: delete preexisting unit tests
0xteddybear Jul 9, 2024
e66ed9f
test: small renames from feedback
0xteddybear Jul 10, 2024
1acff62
test: be explicit about untestable code
0xteddybear Jul 10, 2024
86f1950
chore: merge dev
0xteddybear Jul 11, 2024
d111e8b
test: adding skipped test for unreachable condition
0xteddybear Jul 11, 2024
9f074dc
test: code wasnt so unreachable after all
0xteddybear Jul 11, 2024
3f85e43
refactor: get rid of _setRecord
0xteddybear Jul 11, 2024
63b6058
test: btt tests for bcowpool.verify
0xteddybear Jul 11, 2024
1d3a30f
chore: delete preexisting unit tests
0xteddybear Jul 11, 2024
c9d196b
chore: testcase renaming from review
0xteddybear Jul 15, 2024
69f49e7
chore: get rid of _setTokens altogether
0xteddybear Jul 15, 2024
23c5a1b
test: fuzz all possible valid order.sellAmount values
0xteddybear Jul 15, 2024
a134e2c
chore: rename correctOrder -> validOrder
0xteddybear Jul 15, 2024
2668552
chore: merge dev
0xteddybear Jul 15, 2024
b5e712c
chore: merge dev
0xteddybear Jul 16, 2024
62d9d3a
test: btt tests for bpool.finalize
0xteddybear Jul 16, 2024
284322a
test: btt tests for bcowpool.finalize
0xteddybear Jul 16, 2024
31e634a
chore: remove preexisting unit tests replaced by ones in this pr
0xteddybear Jul 16, 2024
3bf22e8
fix: feedback from review
0xteddybear Jul 16, 2024
bd2342b
refactor: make caller==controller default scenario
0xteddybear Jul 16, 2024
57fb9ca
test: btt tests for bcowpool constructor
0xteddybear Jul 16, 2024
ac4399e
chore: remove preexisting unit tests replaced by ones in this pr
0xteddybear Jul 16, 2024
15eac0e
chore: merge dev
0xteddybear Jul 22, 2024
c4e89a2
fix: feedback from review
0xteddybear Jul 22, 2024
88037dc
fix: make bulloak happy
0xteddybear Jul 22, 2024
45054df
chore: merge dev
0xteddybear Jul 22, 2024
4967b34
fix: mergeback mistake
0xteddybear Jul 22, 2024
32f249c
chore: merge dev
wei3erHase Jul 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions test/manual-smock/MockBCoWPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,45 @@ contract MockBCoWPool is BCoWPool, Test {
vm.expectCall(address(this), abi.encodeWithSignature('_pushUnderlying(address,address,uint256)', token, to, amount));
}

function mock_call__pushPoolShare(address to, uint256 amount) public {
vm.mockCall(address(this), abi.encodeWithSignature('_pushPoolShare(address,uint256)', to, amount), abi.encode());
}

function _pushPoolShare(address to, uint256 amount) internal override {
(bool _success, bytes memory _data) =
address(this).call(abi.encodeWithSignature('_pushPoolShare(address,uint256)', to, amount));

if (_success) return abi.decode(_data, ());
else return super._pushPoolShare(to, amount);
}

function call__pushPoolShare(address to, uint256 amount) public {
return _pushPoolShare(to, amount);
}

function expectCall__pushPoolShare(address to, uint256 amount) public {
vm.expectCall(address(this), abi.encodeWithSignature('_pushPoolShare(address,uint256)', to, amount));
}

function mock_call__mintPoolShare(uint256 amount) public {
vm.mockCall(address(this), abi.encodeWithSignature('_mintPoolShare(uint256)', amount), abi.encode());
}

function _mintPoolShare(uint256 amount) internal override {
(bool _success, bytes memory _data) = address(this).call(abi.encodeWithSignature('_mintPoolShare(uint256)', amount));

if (_success) return abi.decode(_data, ());
else return super._mintPoolShare(amount);
}

function call__mintPoolShare(uint256 amount) public {
return _mintPoolShare(amount);
}

function expectCall__mintPoolShare(uint256 amount) public {
vm.expectCall(address(this), abi.encodeWithSignature('_mintPoolShare(uint256)', amount));
}

function call__afterFinalize() public {
return _afterFinalize();
}
Expand Down
238 changes: 1 addition & 237 deletions test/unit/BCoWPool.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BasePoolTest, SwapExactAmountInUtils} from './BPool.t.sol';
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';
Expand Down Expand Up @@ -47,242 +47,6 @@ abstract contract BaseCoWPoolTest is BasePoolTest, BCoWConst {
}
}

contract BCoWPool_Unit_Constructor is BaseCoWPoolTest {
function test_Set_SolutionSettler(address _settler) public {
assumeNotForgeAddress(_settler);
vm.mockCall(_settler, abi.encodePacked(ISettlement.domainSeparator.selector), abi.encode(domainSeparator));
vm.mockCall(_settler, abi.encodePacked(ISettlement.vaultRelayer.selector), abi.encode(vaultRelayer));
MockBCoWPool pool = new MockBCoWPool(_settler, appData);
assertEq(address(pool.SOLUTION_SETTLER()), _settler);
}

function test_Set_DomainSeparator(address _settler, bytes32 _separator) public {
assumeNotForgeAddress(_settler);
vm.mockCall(_settler, abi.encodePacked(ISettlement.domainSeparator.selector), abi.encode(_separator));
vm.mockCall(_settler, abi.encodePacked(ISettlement.vaultRelayer.selector), abi.encode(vaultRelayer));
MockBCoWPool pool = new MockBCoWPool(_settler, appData);
assertEq(pool.SOLUTION_SETTLER_DOMAIN_SEPARATOR(), _separator);
}

function test_Set_VaultRelayer(address _settler, address _relayer) public {
assumeNotForgeAddress(_settler);
vm.mockCall(_settler, abi.encodePacked(ISettlement.domainSeparator.selector), abi.encode(domainSeparator));
vm.mockCall(_settler, abi.encodePacked(ISettlement.vaultRelayer.selector), abi.encode(_relayer));
MockBCoWPool pool = new MockBCoWPool(_settler, appData);
assertEq(pool.VAULT_RELAYER(), _relayer);
}

function test_Set_AppData(bytes32 _appData) public {
MockBCoWPool pool = new MockBCoWPool(cowSolutionSettler, _appData);
assertEq(pool.APP_DATA(), _appData);
}
}

contract BCoWPool_Unit_Finalize is BaseCoWPoolTest {
function setUp() public virtual override {
super.setUp();

for (uint256 i = 0; i < TOKENS_AMOUNT; i++) {
vm.mockCall(tokens[i], abi.encodePacked(IERC20.approve.selector), abi.encode(true));
}

vm.mockCall(address(bCoWPool.FACTORY()), abi.encodeWithSelector(IBCoWFactory.logBCoWPool.selector), abi.encode());
}

function test_Set_Approvals() public {
for (uint256 i = 0; i < TOKENS_AMOUNT; i++) {
vm.expectCall(tokens[i], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)), 1);
}
bCoWPool.finalize();
}

function test_Log_IfRevert() public {
vm.mockCallRevert(
address(bCoWPool.FACTORY()), abi.encodeWithSelector(IBCoWFactory.logBCoWPool.selector), abi.encode()
);

vm.expectEmit(address(bCoWPool));
emit IBCoWFactory.COWAMMPoolCreated(address(bCoWPool));

bCoWPool.finalize();
}

function test_Call_LogBCoWPool() public {
vm.expectCall(address(bCoWPool.FACTORY()), abi.encodeWithSelector(IBCoWFactory.logBCoWPool.selector), 1);
bCoWPool.finalize();
}
}

contract BCoWPool_Unit_Commit is BaseCoWPoolTest {
function test_Revert_NonSolutionSettler(address sender, bytes32 orderHash) public {
vm.assume(sender != cowSolutionSettler);
vm.prank(sender);
vm.expectRevert(IBCoWPool.CommitOutsideOfSettlement.selector);
bCoWPool.commit(orderHash);
}

function test_Revert_CommitmentAlreadySet(bytes32 _existingCommitment, bytes32 _newCommitment) public {
vm.assume(_existingCommitment != bytes32(0));
bCoWPool.call__setLock(_existingCommitment);
vm.prank(cowSolutionSettler);
vm.expectRevert(IBPool.BPool_Reentrancy.selector);
bCoWPool.commit(_newCommitment);
}

function test_Call_SetLock(bytes32 orderHash) public {
bCoWPool.expectCall__setLock(orderHash);
vm.prank(cowSolutionSettler);
bCoWPool.commit(orderHash);
}

function test_Set_ReentrancyLock(bytes32 orderHash) public {
vm.prank(cowSolutionSettler);
bCoWPool.commit(orderHash);
assertEq(bCoWPool.call__getLock(), orderHash);
}
}

contract BCoWPool_Unit_Verify is BaseCoWPoolTest, SwapExactAmountInUtils {
function setUp() public virtual override(BaseCoWPoolTest, BasePoolTest) {
BaseCoWPoolTest.setUp();
}

function _assumeHappyPath(SwapExactAmountIn_FuzzScenario memory _fuzz) internal pure override {
// safe bound assumptions
_fuzz.tokenInDenorm = bound(_fuzz.tokenInDenorm, MIN_WEIGHT, MAX_WEIGHT);
_fuzz.tokenOutDenorm = bound(_fuzz.tokenOutDenorm, MIN_WEIGHT, MAX_WEIGHT);
// LP fee when swapping via CoW will always be zero
_fuzz.swapFee = 0;

// min - max - calcSpotPrice (spotPriceBefore)
_fuzz.tokenInBalance = bound(_fuzz.tokenInBalance, MIN_BALANCE, type(uint256).max / _fuzz.tokenInDenorm);
_fuzz.tokenOutBalance = bound(_fuzz.tokenOutBalance, MIN_BALANCE, type(uint256).max / _fuzz.tokenOutDenorm);

// MAX_IN_RATIO
vm.assume(_fuzz.tokenAmountIn <= bmul(_fuzz.tokenInBalance, MAX_IN_RATIO));

_assumeCalcOutGivenIn(_fuzz.tokenInBalance, _fuzz.tokenAmountIn, _fuzz.swapFee);
uint256 _tokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance,
_fuzz.tokenInDenorm,
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.tokenAmountIn,
_fuzz.swapFee
);
vm.assume(_tokenAmountOut > BONE);
}

modifier assumeNotBoundToken(address _token) {
for (uint256 i = 0; i < TOKENS_AMOUNT; i++) {
vm.assume(tokens[i] != _token);
}
_;
}

function test_Revert_NonBoundBuyToken(address _otherToken) public assumeNotBoundToken(_otherToken) {
GPv2Order.Data memory order = correctOrder;
order.buyToken = IERC20(_otherToken);
vm.expectRevert(IBPool.BPool_TokenNotBound.selector);
bCoWPool.verify(order);
}

function test_Revert_NonBoundSellToken(address _otherToken) public assumeNotBoundToken(_otherToken) {
GPv2Order.Data memory order = correctOrder;
order.sellToken = IERC20(_otherToken);
vm.expectRevert(IBPool.BPool_TokenNotBound.selector);
bCoWPool.verify(order);
}

function test_Revert_ReceiverIsNotBCoWPool(address _receiver) public {
vm.assume(_receiver != GPv2Order.RECEIVER_SAME_AS_OWNER);
GPv2Order.Data memory order = correctOrder;
order.receiver = _receiver;
vm.expectRevert(IBCoWPool.BCoWPool_ReceiverIsNotBCoWPool.selector);
bCoWPool.verify(order);
}

function test_Revert_LargeDurationOrder(uint256 _timeOffset) public {
_timeOffset = bound(_timeOffset, MAX_ORDER_DURATION + 1, type(uint32).max - block.timestamp);
GPv2Order.Data memory order = correctOrder;
order.validTo = uint32(block.timestamp + _timeOffset);
vm.expectRevert(IBCoWPool.BCoWPool_OrderValidityTooLong.selector);
bCoWPool.verify(order);
}

function test_Revert_NonZeroFee(uint256 _fee) public {
_fee = bound(_fee, 1, type(uint256).max);
GPv2Order.Data memory order = correctOrder;
order.feeAmount = _fee;
vm.expectRevert(IBCoWPool.BCoWPool_FeeMustBeZero.selector);
bCoWPool.verify(order);
}

function test_Revert_InvalidOrderKind(bytes32 _orderKind) public {
vm.assume(_orderKind != GPv2Order.KIND_SELL);
GPv2Order.Data memory order = correctOrder;
order.kind = _orderKind;
vm.expectRevert(IBCoWPool.BCoWPool_InvalidOperation.selector);
bCoWPool.verify(order);
}

function test_Revert_InvalidBuyBalanceKind(bytes32 _balanceKind) public {
vm.assume(_balanceKind != GPv2Order.BALANCE_ERC20);
GPv2Order.Data memory order = correctOrder;
order.buyTokenBalance = _balanceKind;
vm.expectRevert(IBCoWPool.BCoWPool_InvalidBalanceMarker.selector);
bCoWPool.verify(order);
}

function test_Revert_InvalidSellBalanceKind(bytes32 _balanceKind) public {
vm.assume(_balanceKind != GPv2Order.BALANCE_ERC20);
GPv2Order.Data memory order = correctOrder;
order.sellTokenBalance = _balanceKind;
vm.expectRevert(IBCoWPool.BCoWPool_InvalidBalanceMarker.selector);
bCoWPool.verify(order);
}

function test_Revert_TokenAmountInAboveMaxIn(
SwapExactAmountIn_FuzzScenario memory _fuzz,
uint256 _offset
) public happyPath(_fuzz) {
_offset = bound(_offset, 1, type(uint256).max - _fuzz.tokenInBalance);
uint256 _tokenAmountIn = bmul(_fuzz.tokenInBalance, MAX_IN_RATIO) + _offset;
GPv2Order.Data memory order = correctOrder;
order.buyAmount = _tokenAmountIn;

vm.expectRevert(IBPool.BPool_TokenAmountInAboveMaxRatio.selector);
bCoWPool.verify(order);
}

function test_Revert_InsufficientReturn(
SwapExactAmountIn_FuzzScenario memory _fuzz,
uint256 _offset
) public happyPath(_fuzz) {
uint256 _tokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, _fuzz.tokenAmountIn, 0
);
_offset = bound(_offset, 1, _tokenAmountOut);
GPv2Order.Data memory order = correctOrder;
order.buyAmount = _fuzz.tokenAmountIn;
order.sellAmount = _tokenAmountOut + _offset;

vm.expectRevert(IBPool.BPool_TokenAmountOutBelowMinOut.selector);
bCoWPool.verify(order);
}

function test_Success_HappyPath(SwapExactAmountIn_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _tokenAmountOut = calcOutGivenIn(
_fuzz.tokenInBalance, _fuzz.tokenInDenorm, _fuzz.tokenOutBalance, _fuzz.tokenOutDenorm, _fuzz.tokenAmountIn, 0
);
GPv2Order.Data memory order = correctOrder;
order.buyAmount = _fuzz.tokenAmountIn;
order.sellAmount = _tokenAmountOut;

bCoWPool.verify(order);
}
}

contract BCoWPool_Unit_IsValidSignature is BaseCoWPoolTest {
function setUp() public virtual override {
super.setUp();
Expand Down
69 changes: 69 additions & 0 deletions test/unit/BCoWPool/BCoWPool.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IERC20} from '@cowprotocol/interfaces/IERC20.sol';

import {BCoWPoolBase} from './BCoWPoolBase.sol';

import {IBCoWFactory} from 'interfaces/IBCoWFactory.sol';
import {IBPool} from 'interfaces/IBPool.sol';
import {ISettlement} from 'interfaces/ISettlement.sol';
import {MockBCoWPool} from 'test/manual-smock/MockBCoWPool.sol';

contract BCoWPool is BCoWPoolBase {
function test_ConstructorWhenCalled(
address _settler,
bytes32 _separator,
address _relayer,
bytes32 _appData
) external {
assumeNotForgeAddress(_settler);
vm.mockCall(_settler, abi.encodePacked(ISettlement.domainSeparator.selector), abi.encode(_separator));
vm.mockCall(_settler, abi.encodePacked(ISettlement.vaultRelayer.selector), abi.encode(_relayer));
MockBCoWPool pool = new MockBCoWPool(_settler, _appData);
// it should set the solution settler
assertEq(address(pool.SOLUTION_SETTLER()), _settler);
// it should set the domain separator
assertEq(pool.SOLUTION_SETTLER_DOMAIN_SEPARATOR(), _separator);
// it should set the vault relayer
assertEq(pool.VAULT_RELAYER(), _relayer);
// it should set the app data
assertEq(pool.APP_DATA(), _appData);
}

modifier whenPreconditionsAreMet() {
bCoWPool.set__tokens(tokens);
bCoWPool.set__records(tokens[0], IBPool.Record({bound: true, index: 0, denorm: tokenWeight}));
bCoWPool.set__records(tokens[1], IBPool.Record({bound: true, index: 1, denorm: tokenWeight}));
bCoWPool.mock_call__mintPoolShare(INIT_POOL_SUPPLY);
bCoWPool.mock_call__pushPoolShare(address(this), INIT_POOL_SUPPLY);

vm.mockCall(address(this), abi.encodeCall(IBCoWFactory.logBCoWPool, ()), abi.encode());

vm.mockCall(tokens[0], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)), abi.encode(true));
vm.mockCall(tokens[1], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)), abi.encode(true));
_;
}

function test_FinalizeWhenPreconditionsAreMet() external whenPreconditionsAreMet {
// it calls approve on every bound token
vm.expectCall(tokens[0], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)));
vm.expectCall(tokens[1], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)));
// it calls logBCoWPool on the factory
vm.expectCall(address(this), abi.encodeCall(IBCoWFactory.logBCoWPool, ()));
bCoWPool.finalize();
}

function test_FinalizeWhenFactorysLogBCoWPoolDoesNotRevert() external whenPreconditionsAreMet {
// it returns
bCoWPool.finalize();
}

function test_FinalizeWhenFactorysLogBCoWPoolReverts(bytes memory revertData) external whenPreconditionsAreMet {
vm.mockCallRevert(address(this), abi.encodeCall(IBCoWFactory.logBCoWPool, ()), revertData);
// it emits a COWAMMPoolCreated event
vm.expectEmit(address(bCoWPool));
emit IBCoWFactory.COWAMMPoolCreated(address(bCoWPool));
bCoWPool.finalize();
}
}
15 changes: 15 additions & 0 deletions test/unit/BCoWPool/BCoWPool.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BCoWPool::Constructor
└── when called
├── it should set the solution settler
├── it should set the domain separator
├── it should set the vault relayer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing the external call? should query solution settler for vault relayer + should query solution settler for app data

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ask" doesn't sound good

└── it should set the app data

BCoWPool::Finalize
└── when preconditions are met
├── it calls approve on every bound token
├── it calls logBCoWPool on the factory
├── when factorys logBCoWPool does not revert
│ └── it returns
└── when factorys logBCoWPool reverts
└── it emits a COWAMMPoolCreated event
Loading
Loading