Skip to content

Commit

Permalink
Add redemSetForExactETH method
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoopmann committed Apr 12, 2024
1 parent e9794b8 commit 2daa061
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
46 changes: 45 additions & 1 deletion contracts/exchangeIssuance/FlashMintLeveragedExtended.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,44 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged {

}

function redeemSetForExactETH(
ISetToken _setToken,
uint256 _maxSetAmount,
uint256 _outputTokenAmount,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataOutputToken,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken,
uint256 _priceEstimateInflator,
uint256 _maxDust
)
external
nonReentrant
{
uint256 wethBalanceBefore = IERC20(addresses.weth).balanceOf(address(this));
_initiateRedemption(
_setToken,
_maxSetAmount,
DEXAdapter.ETH_ADDRESS,
_outputTokenAmount,
_swapDataCollateralForDebt,
_swapDataOutputToken
);

_issueSetFromExcessOutput(
_setToken,
_maxSetAmount,
DEXAdapter.ETH_ADDRESS,
_outputTokenAmount,
_swapDataDebtForCollateral,
_swapDataInputToken,
_priceEstimateInflator,
_maxDust,
wethBalanceBefore
);

}

function _issueSetFromExcessOutput(
ISetToken _setToken,
uint256 _maxSetAmount,
Expand All @@ -114,7 +152,13 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged {
)
internal
{
uint256 obtainedOutputAmount = IERC20(_outputToken).balanceOf(address(this)).sub(_outputTokenBalanceBefore);
uint256 obtainedOutputAmount;
if( _outputToken == DEXAdapter.ETH_ADDRESS) {
obtainedOutputAmount = IERC20(addresses.weth).balanceOf(address(this)).sub(_outputTokenBalanceBefore);
} else {
obtainedOutputAmount = IERC20(_outputToken).balanceOf(address(this)).sub(_outputTokenBalanceBefore);
}

uint256 excessOutputTokenAmount = obtainedOutputAmount.sub(_outputTokenAmount);
uint256 priceEstimate = _maxSetAmount.mul(1 ether).div(obtainedOutputAmount);
uint256 minSetAmount = excessOutputTokenAmount.mul(priceEstimate).div(1 ether);
Expand Down
3 changes: 2 additions & 1 deletion test/integration/ethereum/flashMintLeveraged.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Account, Address } from "@utils/types";
import DeployHelper from "@utils/deploys";
import { getAccounts, getWaffleExpect, preciseMul } from "@utils/index";
import { setBlockNumber } from "@utils/test/testingUtils";
import { cacheBeforeEach } from "@utils/test";
import { ethers } from "hardhat";
import { BigNumber, utils } from "ethers";
import { FlashMintLeveraged } from "@utils/contracts/index";
Expand Down Expand Up @@ -212,7 +213,7 @@ if (process.env.INTEGRATIONTEST) {
describe(`When input/output token is ${inputTokenName}`, () => {
let subjectSetAmount: BigNumber;
let amountIn: BigNumber;
beforeEach(async () => {
cacheBeforeEach(async () => {
amountIn = ether(2);
subjectSetAmount = ether(1);
});
Expand Down

0 comments on commit 2daa061

Please sign in to comment.