Skip to content

Commit

Permalink
chore: give token to bind its own variable
Browse files Browse the repository at this point in the history
  • Loading branch information
0xteddybear committed Jul 25, 2024
1 parent 3800427 commit 4cbefe2
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions test/unit/BPool/BPool_Bind.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,78 @@ import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {IBPool} from 'interfaces/IBPool.sol';

contract BPoolBind is BPoolBase {
address public token;
uint256 public tokenBindBalance = 100e18;
uint256 public tokenWeight = 1e18;
uint256 public totalWeight = 10e18;

function setUp() public virtual override {
super.setUp();
token = tokens[0];

vm.mockCall(tokens[0], abi.encodePacked(IERC20.transferFrom.selector), abi.encode());
vm.mockCall(tokens[0], abi.encodePacked(IERC20.transfer.selector), abi.encode());
vm.mockCall(token, abi.encodePacked(IERC20.transferFrom.selector), abi.encode());
vm.mockCall(token, abi.encodePacked(IERC20.transfer.selector), abi.encode());
}

function test_RevertWhen_ReentrancyLockIsSet() external {
bPool.call__setLock(_MUTEX_TAKEN);
vm.expectRevert(IBPool.BPool_Reentrancy.selector);
// it should revert
bPool.bind(tokens[0], tokenBindBalance, tokenWeight);
bPool.bind(token, tokenBindBalance, tokenWeight);
}

function test_RevertWhen_CallerIsNOTController(address _caller) external {
// it should revert
vm.assume(_caller != address(this));
vm.prank(_caller);
vm.expectRevert(IBPool.BPool_CallerIsNotController.selector);
bPool.bind(tokens[0], tokenBindBalance, tokenWeight);
bPool.bind(token, tokenBindBalance, tokenWeight);
}

function test_RevertWhen_TokenIsAlreadyBound() external {
bPool.set__records(tokens[0], IBPool.Record({bound: true, index: 0, denorm: tokenWeight}));
bPool.set__records(token, IBPool.Record({bound: true, index: 0, denorm: tokenWeight}));
// it should revert
vm.expectRevert(IBPool.BPool_TokenAlreadyBound.selector);
bPool.bind(tokens[0], tokenBindBalance, tokenWeight);
bPool.bind(token, tokenBindBalance, tokenWeight);
}

function test_RevertWhen_PoolIsFinalized() external {
bPool.set__finalized(true);
// it should revert
vm.expectRevert(IBPool.BPool_PoolIsFinalized.selector);
bPool.bind(tokens[0], tokenBindBalance, tokenWeight);
bPool.bind(token, tokenBindBalance, tokenWeight);
}

function test_RevertWhen_MAX_BOUND_TOKENSTokensAreAlreadyBound() external {
_setRandomTokens(MAX_BOUND_TOKENS);
// it should revert
vm.expectRevert(IBPool.BPool_TokensAboveMaximum.selector);
bPool.bind(tokens[0], tokenBindBalance, tokenWeight);
bPool.bind(token, tokenBindBalance, tokenWeight);
}

function test_RevertWhen_TokenWeightIsTooLow() external {
// it should revert
vm.expectRevert(IBPool.BPool_WeightBelowMinimum.selector);
bPool.bind(tokens[0], tokenBindBalance, MIN_WEIGHT - 1);
bPool.bind(token, tokenBindBalance, MIN_WEIGHT - 1);
}

function test_RevertWhen_TokenWeightIsTooHigh() external {
// it should revert
vm.expectRevert(IBPool.BPool_WeightAboveMaximum.selector);
bPool.bind(tokens[0], tokenBindBalance, MAX_WEIGHT + 1);
bPool.bind(token, tokenBindBalance, MAX_WEIGHT + 1);
}

function test_RevertWhen_TooLittleBalanceIsProvided() external {
// it should revert
vm.expectRevert(IBPool.BPool_BalanceBelowMinimum.selector);
bPool.bind(tokens[0], MIN_BALANCE - 1, tokenWeight);
bPool.bind(token, MIN_BALANCE - 1, tokenWeight);
}

function test_RevertWhen_WeightSumExceedsMAX_TOTAL_WEIGHT() external {
bPool.set__totalWeight(2 * MAX_TOTAL_WEIGHT / 3);
// it should revert
vm.expectRevert(IBPool.BPool_TotalWeightAboveMaximum.selector);
bPool.bind(tokens[0], tokenBindBalance, MAX_TOTAL_WEIGHT / 2);
bPool.bind(token, tokenBindBalance, MAX_TOTAL_WEIGHT / 2);
}

function test_WhenTokenCanBeBound(uint256 _existingTokens) external {
Expand All @@ -84,24 +86,24 @@ contract BPoolBind is BPoolBase {

bPool.set__totalWeight(totalWeight);
// it calls _pullUnderlying
bPool.expectCall__pullUnderlying(tokens[0], address(this), tokenBindBalance);
bPool.expectCall__pullUnderlying(token, address(this), tokenBindBalance);
// it sets the reentrancy lock
bPool.expectCall__setLock(_MUTEX_TAKEN);
// it emits LOG_CALL event
vm.expectEmit();
bytes memory _data = abi.encodeWithSelector(IBPool.bind.selector, tokens[0], tokenBindBalance, tokenWeight);
bytes memory _data = abi.encodeWithSelector(IBPool.bind.selector, token, tokenBindBalance, tokenWeight);
emit IBPool.LOG_CALL(IBPool.bind.selector, address(this), _data);

bPool.bind(tokens[0], tokenBindBalance, tokenWeight);
bPool.bind(token, tokenBindBalance, tokenWeight);

// it clears the reentrancy lock
assertEq(bPool.call__getLock(), _MUTEX_FREE);
// it adds token to the tokens array
assertEq(bPool.call__tokens()[_existingTokens], tokens[0]);
assertEq(bPool.call__tokens()[_existingTokens], token);
// it sets the token record
assertEq(bPool.call__records(tokens[0]).bound, true);
assertEq(bPool.call__records(tokens[0]).denorm, tokenWeight);
assertEq(bPool.call__records(tokens[0]).index, _existingTokens);
assertEq(bPool.call__records(token).bound, true);
assertEq(bPool.call__records(token).denorm, tokenWeight);
assertEq(bPool.call__records(token).index, _existingTokens);
// it sets total weight
assertEq(bPool.call__totalWeight(), totalWeight + tokenWeight);
}
Expand Down

0 comments on commit 4cbefe2

Please sign in to comment.