From ea80addd43e6edb0407dfcdca3438812340af7b4 Mon Sep 17 00:00:00 2001 From: kasperpawlowski Date: Thu, 18 Jul 2024 08:35:20 +0200 Subject: [PATCH] fix: revert cantina 111 --- src/Synths/PegStabilityModule.sol | 4 ++-- test/unit/pegStabilityModules/PSM.t.sol | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Synths/PegStabilityModule.sol b/src/Synths/PegStabilityModule.sol index 8f0afb9f..799935cc 100644 --- a/src/Synths/PegStabilityModule.sol +++ b/src/Synths/PegStabilityModule.sol @@ -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 ); } @@ -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); } } diff --git a/test/unit/pegStabilityModules/PSM.t.sol b/test/unit/pegStabilityModules/PSM.t.sol index 499e090a..e77e356f 100644 --- a/test/unit/pegStabilityModules/PSM.t.sol +++ b/test/unit/pegStabilityModules/PSM.t.sol @@ -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); @@ -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); @@ -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); @@ -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);