Skip to content

Commit

Permalink
fix: eth -> yt swap
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC committed Jul 15, 2024
1 parent 2aa7b1c commit 4521be2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
18 changes: 8 additions & 10 deletions src/RMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -226,7 +224,7 @@ contract RMM is ERC20 {
address(SY),
address(YT),
swap.amountTokenIn + swap.amountNativeIn - debitSurplus,
swap.realYtOut,
ytOut,
deltaLiquidity
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/invariant/RMMHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/SwapExactTokenForYt.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);

Expand Down

0 comments on commit 4521be2

Please sign in to comment.