Skip to content

Commit

Permalink
chore: remove preexisting unit tests replaced by ones in this pr
Browse files Browse the repository at this point in the history
  • Loading branch information
0xteddybear committed Jul 23, 2024
1 parent 38ff223 commit d102f94
Showing 1 changed file with 0 additions and 251 deletions.
251 changes: 0 additions & 251 deletions test/unit/BPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -477,257 +477,6 @@ contract BPool_Unit_JoinswapPoolAmountOut is BasePoolTest {
}
}

contract BPool_Unit_ExitswapExternAmountOut is BasePoolTest {
address tokenOut;

struct ExitswapExternAmountOut_FuzzScenario {
uint256 tokenAmountOut;
uint256 tokenOutBalance;
uint256 tokenOutDenorm;
uint256 totalSupply;
uint256 totalWeight;
uint256 swapFee;
}

function _setValues(ExitswapExternAmountOut_FuzzScenario memory _fuzz, uint256 _poolAmountIn) internal {
tokenOut = tokens[0];

// Create mocks for tokenOut
_mockTransfer(tokenOut);

// Set balances
bPool.set__records(
tokenOut,
IBPool.Record({
bound: true,
index: 0, // NOTE: irrelevant for this method
denorm: _fuzz.tokenOutDenorm
})
);
_mockPoolBalance(tokenOut, _fuzz.tokenOutBalance);

// Set swapFee
_setSwapFee(_fuzz.swapFee);
// Set finalize
_setFinalize(true);
// Set balance
_setPoolBalance(address(this), _poolAmountIn); // give LP tokens to fn caller
// Set totalSupply
_setTotalSupply(_fuzz.totalSupply - _poolAmountIn);
// Set totalWeight
_setTotalWeight(_fuzz.totalWeight);
}

function _assumeHappyPath(ExitswapExternAmountOut_FuzzScenario memory _fuzz)
internal
pure
returns (uint256 _poolAmountIn)
{
// safe bound assumptions
_fuzz.tokenOutDenorm = bound(_fuzz.tokenOutDenorm, 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);

// min
_fuzz.totalSupply = bound(_fuzz.totalSupply, INIT_POOL_SUPPLY, type(uint256).max / BONE);

// MAX_OUT_RATIO
vm.assume(_fuzz.tokenOutBalance < type(uint256).max / MAX_OUT_RATIO);
vm.assume(_fuzz.tokenAmountOut <= bmul(_fuzz.tokenOutBalance, MAX_OUT_RATIO));

// min
vm.assume(_fuzz.tokenOutBalance >= MIN_BALANCE);

// max
vm.assume(_fuzz.tokenOutBalance < type(uint256).max - _fuzz.tokenAmountOut);

// internal calculation for calcPoolInGivenSingleOut
_assumeCalcPoolInGivenSingleOut(
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.totalSupply,
_fuzz.totalWeight,
_fuzz.tokenAmountOut,
_fuzz.swapFee
);

_poolAmountIn = calcPoolInGivenSingleOut(
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.totalSupply,
_fuzz.totalWeight,
_fuzz.tokenAmountOut,
_fuzz.swapFee
);

// min
vm.assume(_poolAmountIn > 0);

// max
vm.assume(_poolAmountIn < _fuzz.totalSupply);
vm.assume(_fuzz.totalSupply < type(uint256).max - _poolAmountIn);
}

modifier happyPath(ExitswapExternAmountOut_FuzzScenario memory _fuzz) {
uint256 _poolAmountIn = _assumeHappyPath(_fuzz);
_setValues(_fuzz, _poolAmountIn);
_;
}

function test_Revert_NotFinalized(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public {
_setFinalize(false);

vm.expectRevert(IBPool.BPool_PoolNotFinalized.selector);
bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
}

function test_Revert_NotBound(
ExitswapExternAmountOut_FuzzScenario memory _fuzz,
address _tokenOut
) public happyPath(_fuzz) {
vm.assume(_tokenOut != tokenOut);
assumeNotForgeAddress(_tokenOut);

vm.expectRevert(IBPool.BPool_TokenNotBound.selector);
bPool.exitswapExternAmountOut(_tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
}

function test_Revert_TokenAmountOutAboveMaxOut(ExitswapExternAmountOut_FuzzScenario memory _fuzz)
public
happyPath(_fuzz)
{
uint256 _maxTokenAmountOut = bmul(_fuzz.tokenOutBalance, MAX_OUT_RATIO);

vm.expectRevert(IBPool.BPool_TokenAmountOutAboveMaxOut.selector);
bPool.exitswapExternAmountOut(tokenOut, _maxTokenAmountOut + 1, type(uint256).max);
}

function test_Revert_InvalidPoolAmountIn(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
_fuzz.tokenAmountOut = 0;

vm.expectRevert(IBPool.BPool_InvalidPoolAmountIn.selector);
bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
}

function test_Revert_PoolAmountInAboveMaxPoolAmountIn(
ExitswapExternAmountOut_FuzzScenario memory _fuzz,
uint256 _maxPoolAmountIn
) public happyPath(_fuzz) {
uint256 _poolAmountIn = calcPoolInGivenSingleOut(
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.totalSupply,
_fuzz.totalWeight,
_fuzz.tokenAmountOut,
_fuzz.swapFee
);
_maxPoolAmountIn = bound(_maxPoolAmountIn, 0, _poolAmountIn - 1);

vm.expectRevert(IBPool.BPool_PoolAmountInAboveMaxPoolAmountIn.selector);
bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, _maxPoolAmountIn);
}

function test_Revert_Reentrancy(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public {
_expectRevertByReentrancy();
bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
}

function test_Emit_LogExit(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
vm.expectEmit();
emit IBPool.LOG_EXIT(address(this), tokenOut, _fuzz.tokenAmountOut);

bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
}

function test_Set_ReentrancyLock(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
_expectSetReentrancyLock();
bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
}

function test_Pull_PoolShare(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _balanceBefore = bPool.balanceOf(address(this));
uint256 _poolAmountIn = calcPoolInGivenSingleOut(
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.totalSupply,
_fuzz.totalWeight,
_fuzz.tokenAmountOut,
_fuzz.swapFee
);
uint256 _exitFee = bmul(_poolAmountIn, EXIT_FEE);

bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);

assertEq(bPool.balanceOf(address(this)), _balanceBefore - bsub(_poolAmountIn, _exitFee));
}

function test_Burn_PoolShare(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _totalSupplyBefore = bPool.totalSupply();
uint256 _poolAmountIn = calcPoolInGivenSingleOut(
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.totalSupply,
_fuzz.totalWeight,
_fuzz.tokenAmountOut,
_fuzz.swapFee
);
uint256 _exitFee = bmul(_poolAmountIn, EXIT_FEE);

bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);

assertEq(bPool.totalSupply(), _totalSupplyBefore - bsub(_poolAmountIn, _exitFee));
}

function test_Push_PoolShare(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
address _factoryAddress = bPool.FACTORY();
uint256 _balanceBefore = bPool.balanceOf(_factoryAddress);
uint256 _poolAmountIn = calcPoolInGivenSingleOut(
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.totalSupply,
_fuzz.totalWeight,
_fuzz.tokenAmountOut,
_fuzz.swapFee
);
uint256 _exitFee = bmul(_poolAmountIn, EXIT_FEE);

bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);

assertEq(bPool.balanceOf(_factoryAddress), _balanceBefore - _poolAmountIn + _exitFee);
}

function test_Push_Underlying(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
vm.expectCall(
address(tokenOut), abi.encodeWithSelector(IERC20.transfer.selector, address(this), _fuzz.tokenAmountOut)
);
bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
}

function test_Returns_PoolAmountIn(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
uint256 _expectedPoolAmountIn = calcPoolInGivenSingleOut(
_fuzz.tokenOutBalance,
_fuzz.tokenOutDenorm,
_fuzz.totalSupply,
_fuzz.totalWeight,
_fuzz.tokenAmountOut,
_fuzz.swapFee
);

(uint256 _poolAmountIn) = bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);

assertEq(_expectedPoolAmountIn, _poolAmountIn);
}

function test_Emit_LogCall(ExitswapExternAmountOut_FuzzScenario memory _fuzz) public happyPath(_fuzz) {
vm.expectEmit();
bytes memory _data =
abi.encodeWithSelector(BPool.exitswapExternAmountOut.selector, tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
emit IBPool.LOG_CALL(BPool.exitswapExternAmountOut.selector, address(this), _data);

bPool.exitswapExternAmountOut(tokenOut, _fuzz.tokenAmountOut, type(uint256).max);
}
}

contract BPool_Unit__PullUnderlying is BasePoolTest {
function test_Call_TransferFrom(address _erc20, address _from, uint256 _amount) public {
assumeNotForgeAddress(_erc20);
Expand Down

0 comments on commit d102f94

Please sign in to comment.