Skip to content

Commit

Permalink
feat: improving and cleaning BMath tree
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase committed Jul 16, 2024
1 parent 7bc9015 commit 755dbc0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
28 changes: 18 additions & 10 deletions test/unit/BMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,6 @@ contract BMathTest is Test, BConst {
bMath.calcOutGivenIn(balanceIn, weightIn, balanceOut, weightOut, amountIn, _swapFee);
}

function test_CalcOutGivenInWhenSwapFeeEqualsBONE() external virtual {
uint256 _swapFee = BONE;

// it should return zero
uint256 _amountOut = bMath.calcOutGivenIn(balanceIn, weightIn, balanceOut, weightOut, amountIn, _swapFee);

assertEq(_amountOut, 0);
}

function test_CalcOutGivenInRevertWhen_TokenAmountInTooBig(uint256 _amountIn) external {
_amountIn = bound(_amountIn, type(uint256).max / (BONE - swapFee) + 1, type(uint256).max);

Expand Down Expand Up @@ -154,6 +145,15 @@ contract BMathTest is Test, BConst {
bMath.calcOutGivenIn(_balanceIn, weightIn, balanceOut, weightOut, amountIn, _swapFee);
}

function test_CalcOutGivenInWhenSwapFeeEqualsBONE() external virtual {
uint256 _swapFee = BONE;

// it should return zero
uint256 _amountOut = bMath.calcOutGivenIn(balanceIn, weightIn, balanceOut, weightOut, amountIn, _swapFee);

assertEq(_amountOut, 0);
}

function test_CalcOutGivenInWhenTokenWeightInIsZero() external virtual {
uint256 _weightIn = 0;

Expand Down Expand Up @@ -477,7 +477,7 @@ contract BMathTest is Test, BConst {
bMath.calcPoolInGivenSingleOut(_balanceOut, weightOut, poolSupply, totalWeight, amountOut, swapFee);
}

function test_CalcPoolInGivenSingleOutRevertWhen_SwapFeeIs1AndTokenWeightOutIsZero() external {
function test_CalcPoolInGivenSingleOutRevertWhen_SwapFeeEqualsBONEAndTokenWeightOutIsZero() external {
uint256 _swapFee = BONE;
uint256 _weightOut = 0;

Expand All @@ -488,6 +488,14 @@ contract BMathTest is Test, BConst {
bMath.calcPoolInGivenSingleOut(balanceOut, _weightOut, poolSupply, totalWeight, amountOut, _swapFee);
}

function test_CalcPoolInGivenSingleOutRevertWhen_SwapFeeGreaterThanBONE() external {
// it should revert
// subtraction underflow
vm.expectRevert(BNum.BNum_SubUnderflow.selector);

bMath.calcPoolInGivenSingleOut(balanceOut, weightOut, poolSupply, totalWeight, amountOut, BONE + 1);
}

function test_CalcPoolInGivenSingleOutWhenTokenAmountOutIsZero() external virtual {
uint256 _amountOut = 0;

Expand Down
18 changes: 9 additions & 9 deletions test/unit/BMath.tree
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ BMathTest::calcSpotPrice
│ └── it should revert // division by zero
├── when weighted token balance out is zero
│ └── it should revert // division by zero
├── when swapFee greater than BONE
├── when swap fee greater than BONE
│ └── it should revert // subtraction underflow
├── when swapFee equals BONE
├── when swap fee equals BONE
│ └── it should revert // division by zero
├── when swap fee is zero
│ └── it should return correct value
Expand All @@ -21,14 +21,14 @@ BMathTest::calcOutGivenIn
│ └── it should revert // division by zero
├── when swap fee greater than BONE
│ └── it should revert // subtraction underflow
├── when swap fee equals BONE
│ └── it should return zero
├── when token amount in too big
│ └── it should revert // ai * (1 - sf) > uint256 max
├── when token balance in and amount in are zero
│ └── it should revert // bi + (ai * (1 - swapFee)) = 0
│ └── it should revert // bi + (ai * (1 - sf)) = 0
├── when token balance in is zero and swap fee equals BONE
│ └── it should revert // bi + (ai * (1 - swapFee)) = 0
│ └── it should revert // bi + (ai * (1 - sf)) = 0
├── when swap fee equals BONE
│ └── it should return zero
├── when token weight in is zero
│ └── it should return zero
├── when token weights are equal
Expand All @@ -53,9 +53,9 @@ BMathTest::calcInGivenOut
│ └── it should revert // subtraction underflow
├── when token amount out equals token balance out
│ └── it should revert // division by zero
├── when swapFee greater than BONE
├── when swap fee greater than BONE
│ └── it should revert // subtraction underflow
├── when swapFee equals BONE
├── when swap fee equals BONE
│ └── it should revert // division by zero
├── when token weight out is zero
│ └── it should return zero
Expand Down Expand Up @@ -121,7 +121,7 @@ BMathTest::calcSingleOutGivenPoolIn
BMathTest::calcPoolInGivenSingleOut
├── when token balance out is zero
│ └── it should revert // subtraction underflow
├── when swap fee is 1 and token weight out is zero
├── when swap fee greater than BONE
│ └── it should revert // division by zero
├── when token amount out is zero
│ └── it should return zero
Expand Down

0 comments on commit 755dbc0

Please sign in to comment.