Skip to content

Commit

Permalink
fix: revert cantina 111
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpawlowski committed Jul 18, 2024
1 parent d0df443 commit ea80add
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Synths/PegStabilityModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ contract PegStabilityModule is EVCUtil {
/// @return The amount of synth swapped.
function quoteToUnderlyingGivenOut(uint256 amountOut) public view returns (uint256) {
return amountOut.mulDiv(
(BPS_SCALE + TO_UNDERLYING_FEE) * PRICE_SCALE, BPS_SCALE * CONVERSION_PRICE, Math.Rounding.Ceil
BPS_SCALE * PRICE_SCALE, (BPS_SCALE - TO_UNDERLYING_FEE) * CONVERSION_PRICE, Math.Rounding.Ceil
);
}

Expand All @@ -166,6 +166,6 @@ contract PegStabilityModule is EVCUtil {
/// @return The amount of underlying swapped.
function quoteToSynthGivenOut(uint256 amountOut) public view returns (uint256) {
return
amountOut.mulDiv((BPS_SCALE + TO_SYNTH_FEE) * CONVERSION_PRICE, BPS_SCALE * PRICE_SCALE, Math.Rounding.Ceil);
amountOut.mulDiv(BPS_SCALE * CONVERSION_PRICE, (BPS_SCALE - TO_SYNTH_FEE) * PRICE_SCALE, Math.Rounding.Ceil);
}
}
14 changes: 8 additions & 6 deletions test/unit/pegStabilityModules/PSM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ contract PSMTest is Test {
);

// Give PSM and wallets some underlying
uint128 amount = uint128(100 * 10 ** underlyingDecimals);
uint128 amount = uint128(1e6 * 10 ** underlyingDecimals);
underlying.mint(address(psm), amount);
underlying.mint(wallet1, amount);
underlying.mint(wallet2, amount);
Expand All @@ -74,7 +74,7 @@ contract PSMTest is Test {
underlying.approve(address(psm), type(uint256).max);

// Set PSM as minter
amount = 100 * 10 ** 18;
amount = 1e6 * 10 ** 18;
vm.prank(owner);
synth.setCapacity(address(psm), amount);

Expand Down Expand Up @@ -171,11 +171,12 @@ contract PSMTest is Test {
function testSwapToUnderlyingGivenOut(uint8 underlyingDecimals, uint256 fee, uint256 amountOutNoDecimals) public {
underlyingDecimals = uint8(bound(underlyingDecimals, 6, 18));
fee = bound(fee, 0, BPS_SCALE - 1);
amountOutNoDecimals = bound(amountOutNoDecimals, 1, 50);
amountOutNoDecimals = bound(amountOutNoDecimals, 1, 100);
fuzzSetUp(underlyingDecimals, fee, 0, 10 ** underlyingDecimals);

uint256 amountOut = amountOutNoDecimals * 10 ** underlyingDecimals;
uint256 expectedAmountIn = amountOutNoDecimals * 10 ** 18 * (BPS_SCALE + fee) / BPS_SCALE;
uint256 expectedAmountIn =
(amountOutNoDecimals * 10 ** 18 * BPS_SCALE + BPS_SCALE - fee - 1) / (BPS_SCALE - fee);

assertEq(psm.quoteToUnderlyingGivenOut(amountOut), expectedAmountIn);

Expand Down Expand Up @@ -225,11 +226,12 @@ contract PSMTest is Test {
function testSwapToSynthGivenOut(uint8 underlyingDecimals, uint256 fee, uint256 amountOutNoDecimals) public {
underlyingDecimals = uint8(bound(underlyingDecimals, 6, 18));
fee = bound(fee, 0, BPS_SCALE - 1);
amountOutNoDecimals = bound(amountOutNoDecimals, 1, 50);
amountOutNoDecimals = bound(amountOutNoDecimals, 1, 100);
fuzzSetUp(underlyingDecimals, 0, fee, 10 ** underlyingDecimals);

uint256 amountOut = amountOutNoDecimals * 10 ** 18;
uint256 expectedAmountIn = amountOutNoDecimals * 10 ** underlyingDecimals * (BPS_SCALE + fee) / BPS_SCALE;
uint256 expectedAmountIn =
(amountOutNoDecimals * 10 ** underlyingDecimals * BPS_SCALE + BPS_SCALE - fee - 1) / (BPS_SCALE - fee);

assertEq(psm.quoteToSynthGivenOut(amountOut), expectedAmountIn);

Expand Down

0 comments on commit ea80add

Please sign in to comment.