Skip to content

Commit

Permalink
chore: merge dev
Browse files Browse the repository at this point in the history
  • Loading branch information
0xteddybear committed Jul 22, 2024
2 parents e4d7108 + 76ea1ef commit 7810b28
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 33 deletions.
29 changes: 0 additions & 29 deletions test/unit/BCoWPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,3 @@ contract BCoWPool_Unit_Constructor is BaseCoWPoolTest {
assertEq(pool.APP_DATA(), _appData);
}
}

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);
}
}
35 changes: 31 additions & 4 deletions test/unit/BCoWPool/BCoWPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import {IERC20} from '@cowprotocol/interfaces/IERC20.sol';
import {BCoWPoolBase} from './BCoWPoolBase.sol';

import {IBCoWFactory} from 'interfaces/IBCoWFactory.sol';

import {IBCoWPool} from 'interfaces/IBCoWPool.sol';
import {IBPool} from 'interfaces/IBPool.sol';

contract BCoWPool_afterFinalize is BCoWPoolBase {
contract BCoWPool is BCoWPoolBase {
bytes32 public commitmentValue = bytes32(uint256(0xf00ba5));
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}));
Expand All @@ -23,7 +27,7 @@ contract BCoWPool_afterFinalize is BCoWPoolBase {
vm.mockCall(tokens[1], abi.encodeCall(IERC20.approve, (vaultRelayer, type(uint256).max)), abi.encode(true));
}

function test_WhenCalled() external {
function test__afterFinalizeWhenCalled() external {
// 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)));
Expand All @@ -32,16 +36,39 @@ contract BCoWPool_afterFinalize is BCoWPoolBase {
bCoWPool.call__afterFinalize();
}

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

function test_WhenFactorysLogBCoWPoolReverts(bytes memory revertData) external {
function test__afterFinalizeWhenFactorysLogBCoWPoolReverts(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();
}

function test_CommitRevertWhen_ReentrancyLockIsSet(bytes32 lockValue) external {
vm.assume(lockValue != _MUTEX_FREE);
bCoWPool.call__setLock(lockValue);
// it should revert
vm.expectRevert(IBPool.BPool_Reentrancy.selector);
bCoWPool.commit(commitmentValue);
}

function test_CommitRevertWhen_SenderIsNotSolutionSettler(address caller) external {
vm.assume(caller != cowSolutionSettler);
vm.prank(caller);
// it should revert
vm.expectRevert(abi.encodeWithSelector(IBCoWPool.CommitOutsideOfSettlement.selector));
bCoWPool.commit(commitmentValue);
}

function test_CommitWhenPreconditionsAreMet(bytes32 commitmentValue_) external {
vm.prank(cowSolutionSettler);
bCoWPool.commit(commitmentValue_);
// it should set the transient reentrancy lock
assertEq(bCoWPool.call__getLock(), commitmentValue_);
}
}
8 changes: 8 additions & 0 deletions test/unit/BCoWPool/BCoWPool.tree
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ BCoWPool::_afterFinalize
│ └── it returns
└── when factorys logBCoWPool reverts
└── it emits a COWAMMPoolCreated event

BCoWPool::Commit
├── when reentrancy lock is set
│ └──it should revert
├── when sender is not solution settler
│ └──it should revert
└── when preconditions are met
└── it should set the transient reentrancy lock
95 changes: 95 additions & 0 deletions test/unit/BPool/BPool_JoinswapPoolAmountOut.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BPoolBase} from './BPoolBase.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';

import {BNum} from 'contracts/BNum.sol';
import {IBPool} from 'interfaces/IBPool.sol';

contract BPoolJoinswapPoolAmountOut is BPoolBase, BNum {
address public tokenIn;

// Valid scenario
uint256 public poolAmountOut = 1e18;
uint256 public tokenInWeight = 8e18;
uint256 public totalWeight = 10e18;
uint256 public tokenInBalance = 300e18;
// ((((INIT_POOL_SUPPLY+poolAmountOut)/INIT_POOL_SUPPLY)^(1/(tokenInWeight/totalWeight)))*tokenInBalance-tokenInBalance)/(1-((1-(tokenInWeight/totalWeight))*MIN_FEE))
// ((((100+1)/100)^(1/(8/10)))*300-300)/(1-((1-(8/10))*(10^-6)))
// 3.754676583174615979425132956656691
uint256 public maxTokenIn = 3.754676583181324836e18;

function setUp() public virtual override {
super.setUp();
tokenIn = tokens[0];
bPool.set__finalized(true);
// mint an initial amount of pool shares (expected to happen at _finalize)
bPool.call__mintPoolShare(INIT_POOL_SUPPLY);
bPool.set__tokens(_tokensToMemory());
bPool.set__totalWeight(totalWeight);
bPool.set__records(tokenIn, IBPool.Record({bound: true, index: 0, denorm: tokenInWeight}));
vm.mockCall(tokenIn, abi.encodePacked(IERC20.balanceOf.selector), abi.encode(uint256(tokenInBalance)));
}

function test_RevertWhen_ReentrancyLockIsSet() external {
bPool.call__setLock(_MUTEX_TAKEN);
// it should revert
vm.expectRevert(IBPool.BPool_Reentrancy.selector);
bPool.joinswapPoolAmountOut(tokenIn, poolAmountOut, maxTokenIn);
}

function test_RevertWhen_PoolIsNotFinalized() external {
bPool.call__setLock(_MUTEX_TAKEN);
// it should revert
vm.expectRevert(IBPool.BPool_Reentrancy.selector);
bPool.joinswapPoolAmountOut(tokenIn, poolAmountOut, maxTokenIn);
}

function test_RevertWhen_TokenInIsNotBound() external {
// it should revert
vm.expectRevert(IBPool.BPool_TokenNotBound.selector);
bPool.joinswapPoolAmountOut(makeAddr('unknown token'), poolAmountOut, maxTokenIn);
}

function test_RevertWhen_TokenAmountInExceedsMaxRatio() external {
// it should revert
vm.expectRevert(IBPool.BPool_TokenAmountInAboveMaxRatio.selector);
// growing pool supply by 50% -> user has to provide over half of the
// pool's tokenIn (198 in this case, consistent with weight=0.8), while
// MAX_IN_RATIO=0.5
bPool.joinswapPoolAmountOut(tokenIn, 50e18, type(uint256).max);
}

function test_RevertWhen_CalculatedTokenAmountInIsMoreThanExpected() external {
// it should revert
vm.expectRevert(IBPool.BPool_TokenAmountInAboveMaxAmountIn.selector);
bPool.joinswapPoolAmountOut(tokenIn, poolAmountOut, maxTokenIn - 1);
}

function test_WhenPreconditionsAreMet() external {
// it sets reentrancy lock
bPool.expectCall__setLock(_MUTEX_TAKEN);
// it queries token in balance
vm.expectCall(tokenIn, abi.encodeCall(IERC20.balanceOf, (address(bPool))));
// it calls _pullUnderlying for token in
bPool.mock_call__pullUnderlying(tokenIn, address(this), maxTokenIn);
bPool.expectCall__pullUnderlying(tokenIn, address(this), maxTokenIn);
// it mints the pool shares
bPool.expectCall__mintPoolShare(poolAmountOut);
// it sends pool shares to caller
bPool.expectCall__pushPoolShare(address(this), poolAmountOut);
// it emits LOG_CALL event
bytes memory _data =
abi.encodeWithSelector(IBPool.joinswapPoolAmountOut.selector, tokenIn, poolAmountOut, maxTokenIn);
vm.expectEmit();
emit IBPool.LOG_CALL(IBPool.joinswapPoolAmountOut.selector, address(this), _data);
// it emits LOG_JOIN event for token in
vm.expectEmit();
emit IBPool.LOG_JOIN(address(this), tokenIn, maxTokenIn);
bPool.joinswapPoolAmountOut(tokenIn, poolAmountOut, maxTokenIn);

// it clears the reentrancy lock
assertEq(_MUTEX_FREE, bPool.call__getLock());
}
}
20 changes: 20 additions & 0 deletions test/unit/BPool/BPool_JoinswapPoolAmountOut.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
BPool::JoinswapPoolAmountOut
├── when reentrancy lock is set
│ └── it should revert
├── when pool is not finalized
│ └── it should revert
├── when token in is not bound
│ └── it should revert
├── when token amount in exceeds max ratio
│ └── it should revert
├── when calculated token amount in is more than expected
│ └── it should revert
└── when preconditions are met
├── it emits LOG_CALL event
├── it sets the reentrancy lock
├── it queries token in balance
├── it emits LOG_JOIN event for token in
├── it mints the pool shares
├── it sends pool shares to caller
├── it calls _pullUnderlying for token in
└── it clears the reentrancy lock

0 comments on commit 7810b28

Please sign in to comment.