Skip to content

Commit

Permalink
use DexAdapterV3 in new FlashMint contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
pblivin0x committed Oct 15, 2024
1 parent b4b7216 commit bfb385b
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 60 deletions.
26 changes: 13 additions & 13 deletions contracts/exchangeIssuance/FlashMintDex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { IController } from "../interfaces/IController.sol";
import { ISetToken } from "../interfaces/ISetToken.sol";
import { IWETH } from "../interfaces/IWETH.sol";
import { PreciseUnitMath } from "../lib/PreciseUnitMath.sol";
import { DEXAdapterV2 } from "./DEXAdapterV2.sol";
import { DEXAdapterV3 } from "./DEXAdapterV3.sol";

/**
* @title FlashMintDex
Expand All @@ -41,7 +41,7 @@ import { DEXAdapterV2 } from "./DEXAdapterV2.sol";
* The FlashMint SDK (https://github.com/IndexCoop/flash-mint-sdk) provides a unified interface for this and other FlashMint contracts.
*/
contract FlashMintDex is Ownable, ReentrancyGuard {
using DEXAdapterV2 for DEXAdapterV2.Addresses;
using DEXAdapterV3 for DEXAdapterV3.Addresses;
using Address for address payable;
using SafeMath for uint256;
using PreciseUnitMath for uint256;
Expand All @@ -58,22 +58,22 @@ contract FlashMintDex is Ownable, ReentrancyGuard {
address public immutable WETH;
IController public immutable setController;
IController public immutable indexController;
DEXAdapterV2.Addresses public dexAdapter;
DEXAdapterV3.Addresses public dexAdapter;

/* ============ Structs ============ */
struct IssueRedeemParams {
ISetToken setToken; // The address of the SetToken to be issued/redeemed
uint256 amountSetToken; // The amount of SetTokens to issue/redeem
DEXAdapterV2.SwapData[] componentSwapData; // The swap data from WETH to each component token
DEXAdapterV3.SwapData[] componentSwapData; // The swap data from WETH to each component token
address issuanceModule; // The address of the issuance module to be used
bool isDebtIssuance; // A flag indicating whether the issuance module is a debt issuance module
}

struct PaymentInfo {
IERC20 token; // The address of the input/output token for issuance/redemption
uint256 limitAmt; // Max/min amount of payment token spent/received
DEXAdapterV2.SwapData swapDataTokenToWeth; // The swap data from payment token to WETH
DEXAdapterV2.SwapData swapDataWethToToken; // The swap data from WETH back to payment token
DEXAdapterV3.SwapData swapDataTokenToWeth; // The swap data from payment token to WETH
DEXAdapterV3.SwapData swapDataWethToToken; // The swap data from WETH back to payment token
}

/* ============ Events ============ */
Expand Down Expand Up @@ -120,7 +120,7 @@ contract FlashMintDex is Ownable, ReentrancyGuard {
constructor(
IController _setController,
IController _indexController,
DEXAdapterV2.Addresses memory _dexAddresses
DEXAdapterV3.Addresses memory _dexAddresses
)
public
{
Expand Down Expand Up @@ -208,7 +208,7 @@ contract FlashMintDex is Ownable, ReentrancyGuard {
*/
function getIssueExactSet(
IssueRedeemParams memory _issueParams,
DEXAdapterV2.SwapData memory _swapDataInputTokenToWeth
DEXAdapterV3.SwapData memory _swapDataInputTokenToWeth
)
external
returns (uint256)
Expand All @@ -230,7 +230,7 @@ contract FlashMintDex is Ownable, ReentrancyGuard {
*/
function getRedeemExactSet(
IssueRedeemParams memory _redeemParams,
DEXAdapterV2.SwapData memory _swapDataWethToOutputToken
DEXAdapterV3.SwapData memory _swapDataWethToOutputToken
)
external
returns (uint256)
Expand Down Expand Up @@ -375,7 +375,7 @@ contract FlashMintDex is Ownable, ReentrancyGuard {
}

/**
* Swaps a given amount of an ERC20 token for WETH using the DEXAdapter.
* Swaps a given amount of an ERC20 token for WETH using the DEXAdapterV3.
*
* @param _paymentToken Address of the ERC20 payment token
* @param _paymentTokenAmount Amount of payment token to swap
Expand All @@ -386,7 +386,7 @@ contract FlashMintDex is Ownable, ReentrancyGuard {
function _swapPaymentTokenForWeth(
IERC20 _paymentToken,
uint256 _paymentTokenAmount,
DEXAdapterV2.SwapData memory _swapData
DEXAdapterV3.SwapData memory _swapData
)
internal
returns (uint256 amountWethOut)
Expand All @@ -403,15 +403,15 @@ contract FlashMintDex is Ownable, ReentrancyGuard {
}

/**
* Swaps a given amount of an WETH for ERC20 using the DEXAdapter.
* Swaps a given amount of an WETH for ERC20 using the DEXAdapterV3.
*
* @param _wethAmount Amount of WETH to swap for input token
* @param _paymentToken Address of the input token
* @param _swapData Swap data from WETH to input token
*
* @return amountOut Amount of ERC20 received after the swap
*/
function _swapWethForPaymentToken(uint256 _wethAmount, IERC20 _paymentToken, DEXAdapterV2.SwapData memory _swapData)
function _swapWethForPaymentToken(uint256 _wethAmount, IERC20 _paymentToken, DEXAdapterV3.SwapData memory _swapData)
internal
returns (uint256 amountOut)
{
Expand Down
28 changes: 14 additions & 14 deletions contracts/exchangeIssuance/FlashMintNAV.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ import { IController } from "../interfaces/IController.sol";
import { ISetToken } from "../interfaces/ISetToken.sol";
import { IWETH } from "../interfaces/IWETH.sol";
import { PreciseUnitMath } from "../lib/PreciseUnitMath.sol";
import { DEXAdapterV2 } from "./DEXAdapterV2.sol";
import { DEXAdapterV3 } from "./DEXAdapterV3.sol";

/**
* @title FlashMintNAV
* @author Index Cooperative
* @notice Part of a family of FlashMint contracts that allows users to issue and redeem SetTokens with a single input/output token (ETH/ERC20).
* Allows the caller to combine a DEX swap and a SetToken issuance or redemption in a single transaction.
* Supports SetTokens that use a NAV Issuance Module, and does not require use of off-chain APIs for swap quotes.
* The SetToken must be configured with a reserve asset that has liquidity on the exchanges supported by the DEXAdapterV2 library.
* The SetToken must be configured with a reserve asset that has liquidity on the exchanges supported by the DEXAdapterV3 library.
*
* See the FlashMint SDK for integrating any FlashMint contract (https://github.com/IndexCoop/flash-mint-sdk).
*/
contract FlashMintNAV is Ownable, ReentrancyGuard {
using DEXAdapterV2 for DEXAdapterV2.Addresses;
using DEXAdapterV3 for DEXAdapterV3.Addresses;
using Address for address payable;
using SafeMath for uint256;
using PreciseUnitMath for uint256;
Expand All @@ -64,7 +64,7 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {

/* ============ State Variables ============ */

DEXAdapterV2.Addresses public dexAdapter;
DEXAdapterV3.Addresses public dexAdapter;

/* ============ Events ============ */

Expand All @@ -85,16 +85,16 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {
);

/**
* Initializes the contract with controller, issuance module, and DEXAdapterV2 library addresses.
* Initializes the contract with controller, issuance module, and DEXAdapterV3 library addresses.
*
* @param _setController Address of the protocol controller contract
* @param _navIssuanceModule NAV Issuance Module used to issue and redeem SetTokens
* @param _dexAddresses Struct containing addresses for the DEXAdapterV2 library
* @param _dexAddresses Struct containing addresses for the DEXAdapterV3 library
*/
constructor(
IController _setController,
INAVIssuanceModule _navIssuanceModule,
DEXAdapterV2.Addresses memory _dexAddresses
DEXAdapterV3.Addresses memory _dexAddresses
)
public
{
Expand All @@ -117,7 +117,7 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {
address payable _to
) external payable onlyOwner {
for (uint256 i = 0; i < _tokens.length; i++) {
if (address(_tokens[i]) == DEXAdapterV2.ETH_ADDRESS) {
if (address(_tokens[i]) == DEXAdapterV3.ETH_ADDRESS) {
_to.sendValue(address(this).balance);
} else {
_tokens[i].safeTransfer(_to, _tokens[i].balanceOf(address(this)));
Expand Down Expand Up @@ -162,7 +162,7 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {
ISetToken _setToken,
address _inputToken,
uint256 _inputTokenAmount,
DEXAdapterV2.SwapData memory _reserveAssetSwapData
DEXAdapterV3.SwapData memory _reserveAssetSwapData
)
external
returns (uint256)
Expand Down Expand Up @@ -194,7 +194,7 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {
ISetToken _setToken,
uint256 _setTokenAmount,
address _outputToken,
DEXAdapterV2.SwapData memory _reserveAssetSwapData
DEXAdapterV3.SwapData memory _reserveAssetSwapData
)
external
returns (uint256)
Expand All @@ -219,7 +219,7 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {
function issueSetFromExactETH(
ISetToken _setToken,
uint256 _minSetTokenAmount,
DEXAdapterV2.SwapData memory _reserveAssetSwapData
DEXAdapterV3.SwapData memory _reserveAssetSwapData
)
external
payable
Expand Down Expand Up @@ -257,7 +257,7 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {
uint256 _minSetTokenAmount,
IERC20 _inputToken,
uint256 _inputTokenAmount,
DEXAdapterV2.SwapData memory _reserveAssetSwapData
DEXAdapterV3.SwapData memory _reserveAssetSwapData
)
external
nonReentrant
Expand Down Expand Up @@ -292,7 +292,7 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {
ISetToken _setToken,
uint256 _setTokenAmount,
uint256 _minEthAmount,
DEXAdapterV2.SwapData memory _reserveAssetSwapData
DEXAdapterV3.SwapData memory _reserveAssetSwapData
)
external
nonReentrant
Expand Down Expand Up @@ -334,7 +334,7 @@ contract FlashMintNAV is Ownable, ReentrancyGuard {
uint256 _setTokenAmount,
IERC20 _outputToken,
uint256 _minOutputTokenAmount,
DEXAdapterV2.SwapData memory _reserveAssetSwapData
DEXAdapterV3.SwapData memory _reserveAssetSwapData
)
external
nonReentrant
Expand Down
34 changes: 17 additions & 17 deletions contracts/exchangeIssuance/FlashMintWrapped.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { ISetToken} from "../interfaces/ISetToken.sol";
import { IWETH} from "../interfaces/IWETH.sol";
import { IWrapModuleV2} from "../interfaces/IWrapModuleV2.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { DEXAdapter } from "./DEXAdapter.sol";
import { DEXAdapterV3 } from "./DEXAdapterV3.sol";

/**
* @title FlashMintWrapped
Expand All @@ -53,7 +53,7 @@ import { DEXAdapter } from "./DEXAdapter.sol";
* Set components at index 1 = cDAI; then -> ComponentSwapData[1].underlyingERC20 = DAI; (wrapping will happen)
*/
contract FlashMintWrapped is Ownable, ReentrancyGuard {
using DEXAdapter for DEXAdapter.Addresses;
using DEXAdapterV3 for DEXAdapterV3.Addresses;
using Address for address payable;
using Address for address;
using SafeMath for uint256;
Expand All @@ -65,8 +65,8 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
struct ComponentSwapData {
// unwrapped token version, e.g. DAI
address underlyingERC20;
// swap data for DEX operation: fees, path, etc. see DEXAdapter.SwapData
DEXAdapter.SwapData dexData;
// swap data for DEX operation: fees, path, etc. see DEXAdapterV3.SwapData
DEXAdapterV3.SwapData dexData;
// ONLY relevant for issue, not used for redeem:
// amount that has to be bought of the unwrapped token version (to cover required wrapped component amounts for issuance)
// this amount has to be computed beforehand through the exchange rate of wrapped Component <> unwrappedComponent
Expand All @@ -92,7 +92,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {

/* ============ State Variables ============ */

DEXAdapter.Addresses public dexAdapter;
DEXAdapterV3.Addresses public dexAdapter;

/* ============ Events ============ */

Expand Down Expand Up @@ -139,12 +139,12 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
if (_inputToken != _outputToken) {
require(
_path[0] == _inputToken ||
(_inputToken == dexAdapter.weth && _path[0] == DEXAdapter.ETH_ADDRESS),
(_inputToken == dexAdapter.weth && _path[0] == DEXAdapterV3.ETH_ADDRESS),
"FlashMint: INPUT_TOKEN_NOT_IN_PATH"
);
require(
_path[_path.length - 1] == _outputToken ||
(_outputToken == dexAdapter.weth && _path[_path.length - 1] == DEXAdapter.ETH_ADDRESS),
(_outputToken == dexAdapter.weth && _path[_path.length - 1] == DEXAdapterV3.ETH_ADDRESS),
"FlashMint: OUTPUT_TOKEN_NOT_IN_PATH"
);
}
Expand All @@ -162,7 +162,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
* @param _wrapModule WrapModuleV2 used to obtain a valid wrap adapter
*/
constructor(
DEXAdapter.Addresses memory _dexAddresses,
DEXAdapterV3.Addresses memory _dexAddresses,
IController _setController,
IDebtIssuanceModule _issuanceModule,
address _wrapModule
Expand All @@ -187,7 +187,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
*/
function withdrawTokens(IERC20[] calldata _tokens, address payable _to) external onlyOwner payable {
for(uint256 i = 0; i < _tokens.length; i++) {
if(address(_tokens[i]) == DEXAdapter.ETH_ADDRESS){
if(address(_tokens[i]) == DEXAdapterV3.ETH_ADDRESS){
_to.sendValue(address(this).balance);
}
else{
Expand All @@ -206,7 +206,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
function approveSetToken(ISetToken _setToken) external isSetToken(_setToken) {
address[] memory _components = _setToken.getComponents();
for (uint256 i = 0; i < _components.length; ++i) {
DEXAdapter._safeApprove(IERC20(_components[i]), address(issuanceModule), MAX_UINT256);
DEXAdapterV3._safeApprove(IERC20(_components[i]), address(issuanceModule), MAX_UINT256);
}
}

Expand Down Expand Up @@ -503,7 +503,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
emit FlashMint(
msg.sender,
_setToken,
_issueFromETH ? IERC20(DEXAdapter.ETH_ADDRESS) : _inputToken,
_issueFromETH ? IERC20(DEXAdapterV3.ETH_ADDRESS) : _inputToken,
spentInputTokenAmount,
_amountSetToken
);
Expand Down Expand Up @@ -567,7 +567,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
emit FlashRedeem(
msg.sender,
_setToken,
_redeemToETH ? IERC20(DEXAdapter.ETH_ADDRESS) : _outputToken,
_redeemToETH ? IERC20(DEXAdapterV3.ETH_ADDRESS) : _outputToken,
_amountSetToken,
totalOutputTokenObtained
);
Expand Down Expand Up @@ -796,7 +796,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
* @param _outputToken Output token that will be bought
* @param _amount Amount that will be bought
* @param _maxAmountIn Maximum aount of input token that can be spent
* @param _swapDexData DEXAdapter.SwapData with path, fees, etc. for inputToken -> outputToken swap
* @param _swapDexData DEXAdapterV3.SwapData with path, fees, etc. for inputToken -> outputToken swap
*
* @return Amount of spent _inputToken
*/
Expand All @@ -805,7 +805,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
IERC20 _outputToken,
uint256 _amount,
uint256 _maxAmountIn,
DEXAdapter.SwapData calldata _swapDexData
DEXAdapterV3.SwapData calldata _swapDexData
)
internal
isValidPath(_swapDexData.path, address(_inputToken), address(_outputToken))
Expand All @@ -822,15 +822,15 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
* @param _inputToken Input token that will be sold
* @param _outputToken Output token that will be bought
* @param _amount Amount that will be sold
* @param _swapDexData DEXAdapter.SwapData with path, fees, etc. for inputToken -> outputToken swap
* @param _swapDexData DEXAdapterV3.SwapData with path, fees, etc. for inputToken -> outputToken swap
*
* @return amount of received _outputToken
*/
function _swapFromExact(
IERC20 _inputToken,
IERC20 _outputToken,
uint256 _amount,
DEXAdapter.SwapData calldata _swapDexData
DEXAdapterV3.SwapData calldata _swapDexData
)
internal
isValidPath(_swapDexData.path, address(_inputToken), address(_outputToken))
Expand Down Expand Up @@ -881,7 +881,7 @@ contract FlashMintWrapped is Ownable, ReentrancyGuard {
);

// 3. approve token transfer from this to _wrapCallTarget
DEXAdapter._safeApprove(
DEXAdapterV3._safeApprove(
IERC20(_underlyingToken),
_wrapCallTarget,
_wrapAmount
Expand Down
Loading

0 comments on commit bfb385b

Please sign in to comment.