Skip to content

Commit

Permalink
fix: manually calculating spot price
Browse files Browse the repository at this point in the history
  • Loading branch information
wei3erHase committed Jul 22, 2024
1 parent f1b7abc commit 7f16994
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions test/unit/BPool/BPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ contract BPool is BPoolBase, BMath {
uint256 balanceTokenIn = 10e18;
uint256 balanceTokenOut = 20e18;

// sP = (tokenInBalance / tokenInWeight) / (tokenOutBalance/ tokenOutWeight) * (1 / (1 - swapFee))
// tokenInWeight == tokenOutWeight
// sP = 10 / 20 = 0.5e18
// sPf = (10 / 20) * (1 / (1-0.1)) = 0.555...e18 (round-up)
uint256 public spotPriceWithoutFee = 0.5e18;
uint256 public spotPrice = bmul(spotPriceWithoutFee, bdiv(BONE, bsub(BONE, swapFee)));

function setUp() public virtual override {
super.setUp();

Expand Down Expand Up @@ -327,7 +334,7 @@ contract BPool is BPoolBase, BMath {
vm.mockCall(tokens[1], abi.encodePacked(IERC20.balanceOf.selector), abi.encode(balanceTokenOut));
vm.expectCall(tokens[1], abi.encodeWithSelector(IERC20.balanceOf.selector));
// it returns spot price
uint256 _spotPrice = calcSpotPrice(balanceTokenIn, tokenWeight, balanceTokenOut, tokenWeight, swapFee);
uint256 _spotPrice = 0.555555555555555556e18;
assertEq(bPool.getSpotPrice(tokens[0], tokens[1]), _spotPrice);
}

Expand Down Expand Up @@ -358,7 +365,7 @@ contract BPool is BPoolBase, BMath {
vm.mockCall(tokens[1], abi.encodePacked(IERC20.balanceOf.selector), abi.encode(balanceTokenOut));
vm.expectCall(tokens[1], abi.encodeWithSelector(IERC20.balanceOf.selector));
// it returns spot price
uint256 _spotPrice = calcSpotPrice(balanceTokenIn, tokenWeight, balanceTokenOut, tokenWeight, 0);
uint256 _spotPrice = 0.5e18;
assertEq(bPool.getSpotPriceSansFee(tokens[0], tokens[1]), _spotPrice);
}
}

0 comments on commit 7f16994

Please sign in to comment.