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

feat: add btt tests for finalize methods #159

Merged
merged 26 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 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
d43db63
fix: reorganize .tree
0xteddybear Jul 17, 2024
8f5983a
fix: feedback from review
0xteddybear Jul 18, 2024
9c1a88a
chore: merge dev
0xteddybear Jul 22, 2024
35a1689
fix: feedback from review, calling internal method directly
0xteddybear Jul 22, 2024
6b86d48
chore: merge dev
0xteddybear Jul 22, 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
178 changes: 1 addition & 177 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 @@ -78,41 +78,6 @@ contract BCoWPool_Unit_Constructor is BaseCoWPoolTest {
}
}

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);
Expand Down Expand Up @@ -142,147 +107,6 @@ contract BCoWPool_Unit_Commit is BaseCoWPoolTest {
}
}

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
47 changes: 47 additions & 0 deletions test/unit/BCoWPool/BCoWPool.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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';

contract BCoWPool_afterFinalize is BCoWPoolBase {
uint256 public tokenWeight = 1e18;

function setUp() public virtual override {
super.setUp();
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}));

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_WhenCalled() external {
Copy link
Member

Choose a reason for hiding this comment

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

test_What?

Copy link
Author

@0xteddybear 0xteddybear Jul 18, 2024

Choose a reason for hiding this comment

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

is the result of implementing feedback from this: #159 (comment) 😭 what did you have in mind?

Copy link
Member

Choose a reason for hiding this comment

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

is this because Finalize is the only method in the tree for now?

Copy link
Author

Choose a reason for hiding this comment

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

yes, precisely. That's why PRs depending on this all have renames of these methods 😭

// 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.call__afterFinalize();
}

function test_WhenFactorysLogBCoWPoolDoesNotRevert() external {
// it returns
bCoWPool.call__afterFinalize();
}

function test_WhenFactorysLogBCoWPoolReverts(bytes memory revertData) external {
vm.mockCallRevert(address(this), abi.encodeCall(IBCoWFactory.logBCoWPool, ()), revertData);
// it emits a COWAMMPoolCreated event
vm.expectEmit(address(bCoWPool));
emit IBCoWFactory.COWAMMPoolCreated(address(bCoWPool));
bCoWPool.call__afterFinalize();
}
}
8 changes: 8 additions & 0 deletions test/unit/BCoWPool/BCoWPool.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BCoWPool::_afterFinalize
Copy link
Member

Choose a reason for hiding this comment

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

much betta!

├── when called
│ ├── 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
28 changes: 28 additions & 0 deletions test/unit/BCoWPool/BCoWPoolBase.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BPoolBase} from '../BPool/BPoolBase.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);
}
}
Loading
Loading