Skip to content

Commit

Permalink
Add swap data to ETH for redeem method
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoopmann committed Apr 19, 2024
1 parent 31d9d30 commit e97db32
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
45 changes: 25 additions & 20 deletions contracts/exchangeIssuance/FlashMintLeveragedExtended.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged {
address _outputToken,
uint256 _outputTokenAmount,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataOutputToken,
DEXAdapter.SwapData memory _swapDataCollateralForOutputToken,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken,
DEXAdapter.SwapData memory _swapDataOutputTokenForCollateral,
DEXAdapter.SwapData memory _swapDataOutputTokenForETH,
uint256 _priceEstimateInflator,
uint256 _maxDust
)
Expand All @@ -84,7 +85,7 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged {
_outputToken,
_outputTokenAmount,
_swapDataCollateralForDebt,
_swapDataOutputToken
_swapDataCollateralForOutputToken
);

_issueSetFromExcessOutput(
Expand All @@ -93,14 +94,15 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged {
_outputToken,
_outputTokenAmount,
_swapDataDebtForCollateral,
_swapDataInputToken,
_swapDataOutputTokenForCollateral,
_priceEstimateInflator,
_maxDust,
outputTokenBalanceBefore
);
uint256 outputTokenObtained = IERC20(_outputToken).balanceOf(address(this)).sub(outputTokenBalanceBefore);
require(outputTokenObtained >= _outputTokenAmount, "FlashMintLeveragedExtended: outputTokenBalanceAfter < _outputTokenAmount");
IERC20(_outputToken).transfer(msg.sender, _outputTokenAmount);
_swapTokenForETHAndReturnToUser(_outputToken, outputTokenObtained - _outputTokenAmount, _swapDataOutputTokenForETH);
}

function redeemSetForExactETH(
Expand Down Expand Up @@ -348,9 +350,7 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged {
_maxDust
);

uint256 ethObtained = _swapInputTokenForETH(_inputToken, inputAmountLeft, _swapDataInputTokenToETH);
IWETH(addresses.weth).withdraw(ethObtained);
msg.sender.transfer(ethObtained);
_swapTokenForETHAndReturnToUser(_inputToken, inputAmountLeft, _swapDataInputTokenToETH);
return inputAmountLeft;
}

Expand Down Expand Up @@ -594,27 +594,32 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged {
}


function _swapInputTokenForETH(
function _swapTokenForETHAndReturnToUser(
address _inputToken,
uint256 _inputAmount,
DEXAdapter.SwapData memory _swapData
)
internal
returns (uint256)
{
if(_inputToken == addresses.weth) {
return _inputAmount;
}
if(_swapData.path.length == 0) {
return 0;
return;
}
require(_swapData.path[0] == _inputToken, "FlashMintLeveragedExtended: InputToken not first in path");
require(_swapData.path[_swapData.path.length - 1] == addresses.weth, "FlashMintLeveragedExtended: WETH not last in path");
return addresses.swapExactTokensForTokens(
_inputAmount,
0,
_swapData
);

uint256 ethObtained;
if(_inputToken == addresses.weth) {
ethObtained = _inputAmount;
} else {
require(_swapData.path[0] == _inputToken, "FlashMintLeveragedExtended: InputToken not first in path");
require(_swapData.path[_swapData.path.length - 1] == addresses.weth, "FlashMintLeveragedExtended: WETH not last in path");
ethObtained = addresses.swapExactTokensForTokens(
_inputAmount,
0,
_swapData
);
}

IWETH(addresses.weth).withdraw(ethObtained);
msg.sender.transfer(ethObtained);
}
}

3 changes: 3 additions & 0 deletions test/integration/arbitrum/flashMintLeveragedExtended.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ if (process.env.INTEGRATIONTEST) {
let swapDataOutputToken: SwapData;
let swapDataDebtToCollateral: SwapData;
let swapDataInputToken: SwapData;
let swapDataOutputTokenToETH: SwapData;

let outputToken: StandardTokenMock | IWETH;

Expand Down Expand Up @@ -473,6 +474,7 @@ if (process.env.INTEGRATIONTEST) {
swapDataOutputToken,
swapDataDebtToCollateral,
swapDataInputToken,
swapDataOutputTokenToETH,
subjectPriceEstimateInflater,
subjectMaxDust,
);
Expand Down Expand Up @@ -509,6 +511,7 @@ if (process.env.INTEGRATIONTEST) {
pool: ADDRESS_ZERO,
exchange: Exchange.None,
};
swapDataOutputTokenToETH = swapDataInputToken; // Assumes Collateral Token is WETH

if (inputTokenName === "collateralToken") {
outputToken = weth;
Expand Down

0 comments on commit e97db32

Please sign in to comment.