Skip to content

Commit

Permalink
chore: cleaning up remanent test code
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase committed Jul 23, 2024
1 parent dad92aa commit 9bf87ed
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 280 deletions.
48 changes: 0 additions & 48 deletions test/unit/BCoWPool.t.sol

This file was deleted.

232 changes: 0 additions & 232 deletions test/unit/BPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 9bf87ed

Please sign in to comment.