From 4521be2363492d94728a3720333fe11a0ebffd0a Mon Sep 17 00:00:00 2001 From: kinrezc Date: Mon, 15 Jul 2024 14:53:16 -0400 Subject: [PATCH] fix: eth -> yt swap --- src/RMM.sol | 18 ++++++++---------- test/invariant/RMMHandler.sol | 2 +- test/unit/SwapExactTokenForYt.t.sol | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/RMM.sol b/src/RMM.sol index 83012f5..43f4a51 100644 --- a/src/RMM.sol +++ b/src/RMM.sol @@ -180,7 +180,7 @@ contract RMM is ERC20 { uint256 upperBound, uint256 epsilon, address to - ) external payable lock returns (uint256 amountInWad, uint256 amountOutWad, int256 deltaLiquidity) { + ) external payable lock returns (uint256 ytOut, uint256 amountInWad, uint256 amountOutWad, int256 deltaLiquidity) { SwapToYt memory swap; swap.tokenIn = token; swap.amountTokenIn = token == address(0) ? 0 : amountTokenIn; @@ -193,27 +193,25 @@ contract RMM is ERC20 { PYIndex index = YT.newIndex(); uint256 strike_; - uint256 amountOut; swap.amountPtIn = computeSYToYT(index, swap.realSyMinted, upperBound, block.timestamp, swap.amountPtIn, epsilon); - (amountInWad, amountOutWad, amountOut, deltaLiquidity, strike_) = + (amountInWad, amountOutWad, ytOut, deltaLiquidity, strike_) = prepareSwapPtIn(swap.amountPtIn, block.timestamp, index); _adjust(-toInt(amountOutWad), toInt(amountInWad), deltaLiquidity, strike_, index); // SY is needed to cover the minted PT, so we need to debit the delta from the msg.sender - swap.realYtOut = amountOut + (index.assetToSyUp(amountInWad) - amountOutWad); + ytOut += (index.assetToSyUp(amountInWad) - amountOutWad); // Converts the SY received from minting it into its components PT and YT. - amountOut = mintPtYt(swap.realYtOut, address(this)); - swap.realYtOut = amountOut; + ytOut = mintPtYt(ytOut, address(this)); - if (swap.realYtOut < swap.minYtOut) { - revert InsufficientOutput(amountInWad, swap.minYtOut, swap.realYtOut); + if (ytOut < swap.minYtOut) { + revert InsufficientOutput(amountInWad, swap.minYtOut, ytOut); } - _credit(address(YT), to, swap.realYtOut); + _credit(address(YT), to, ytOut); uint256 debitSurplus = address(this).balance; if (debitSurplus > 0) { @@ -226,7 +224,7 @@ contract RMM is ERC20 { address(SY), address(YT), swap.amountTokenIn + swap.amountNativeIn - debitSurplus, - swap.realYtOut, + ytOut, deltaLiquidity ); } diff --git a/test/invariant/RMMHandler.sol b/test/invariant/RMMHandler.sol index ceed198..f0d828c 100644 --- a/test/invariant/RMMHandler.sol +++ b/test/invariant/RMMHandler.sol @@ -167,7 +167,7 @@ contract RMMHandler is CommonBase, StdUtils, StdCheats { ); weth.approve(address(rmm), amountTokenIn); - (uint256 amountInWad, uint256 amountOutWad, int256 deltaLiquidity) = rmm.swapExactTokenForYt( + (uint256 realYtOut, uint256 amountInWad, uint256 amountOutWad, int256 deltaLiquidity) = rmm.swapExactTokenForYt( address(weth), amountTokenIn, ytOut, diff --git a/test/unit/SwapExactTokenForYt.t.sol b/test/unit/SwapExactTokenForYt.t.sol index ee7a4d3..10c0661 100644 --- a/test/unit/SwapExactTokenForYt.t.sol +++ b/test/unit/SwapExactTokenForYt.t.sol @@ -27,7 +27,7 @@ contract SwapExactTokenForYtTest is SetUp { PYIndex index = YT.newIndex(); (uint256 syMinted, uint256 ytOut) = rmm.computeTokenToYT(index, address(0), amountIn, 0, block.timestamp, 0, 1_000); - (, uint256 amountOut,) = rmm.swapExactTokenForYt{value: amountIn}( + (uint256 amountOut,,,) = rmm.swapExactTokenForYt{value: amountIn}( address(0), amountIn, ytOut, syMinted, ytOut, 0, 0.005 ether, address(this) );