// SPDX-License-Identifier: MIT
pragma solidity >=0.6.2 <0.8.0;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.
uint256 size;
// solhint-disable-next-line no-inline-assembly
assembly { size := extcodesize(account) }
return size > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain`call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = target.call{ value: value }(data);
return _verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = target.delegatecall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
// solhint-disable-next-line no-inline-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
/*
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with GSN meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
return msg.data;
}
}
/*
Copyright 2022 Index Cooperative
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
import { IUniswapV2Router02 } from "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { ICurveCalculator } from "../interfaces/external/ICurveCalculator.sol";
import { ICurveAddressProvider } from "../interfaces/external/ICurveAddressProvider.sol";
import { ICurvePoolRegistry } from "../interfaces/external/ICurvePoolRegistry.sol";
import { ICurvePool } from "../interfaces/external/ICurvePool.sol";
import { ISwapRouter02 } from "../interfaces/external/ISwapRouter02.sol";
import { IQuoter } from "../interfaces/IQuoter.sol";
import { IWETH } from "../interfaces/IWETH.sol";
import { PreciseUnitMath } from "../lib/PreciseUnitMath.sol";
/**
* @title DEXAdapter
* @author Index Coop
*
* Adapter to execute swaps on different DEXes
*/
library DEXAdapter {
using SafeERC20 for IERC20;
using PreciseUnitMath for uint256;
using SafeMath for uint256;
/* ============ Constants ============= */
uint256 constant private MAX_UINT256 = type(uint256).max;
address public constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
uint256 public constant ROUNDING_ERROR_MARGIN = 2;
/* ============ Enums ============ */
enum Exchange { None, Quickswap, Sushiswap, UniV3, Curve }
/* ============ Structs ============ */
struct Addresses {
address quickRouter;
address sushiRouter;
address uniV3Router;
address uniV3Quoter;
address curveAddressProvider;
address curveCalculator;
// Wrapped native token (WMATIC on polygon)
address weth;
}
struct SwapData {
address[] path;
uint24[] fees;
address pool;
Exchange exchange;
}
struct CurvePoolData {
int128 nCoins;
uint256[8] balances;
uint256 A;
uint256 fee;
uint256[8] rates;
uint256[8] decimals;
}
/**
* Swap exact tokens for another token on a given DEX.
*
* @param _addresses Struct containing relevant smart contract addresses.
* @param _amountIn The amount of input token to be spent
* @param _minAmountOut Minimum amount of output token to receive
* @param _swapData Swap data containing the path and fee levels (latter only used for uniV3)
*
* @return amountOut The amount of output tokens
*/
function swapExactTokensForTokens(
Addresses memory _addresses,
uint256 _amountIn,
uint256 _minAmountOut,
SwapData memory _swapData
)
external
returns (uint256)
{
if (_swapData.path.length == 0 || _swapData.path[0] == _swapData.path[_swapData.path.length -1]) {
return _amountIn;
}
if(_swapData.exchange == Exchange.Curve){
return _swapExactTokensForTokensCurve(
_swapData.path,
_swapData.pool,
_amountIn,
_minAmountOut,
_addresses
);
}
if(_swapData.exchange== Exchange.UniV3){
return _swapExactTokensForTokensUniV3(
_swapData.path,
_swapData.fees,
_amountIn,
_minAmountOut,
ISwapRouter02(_addresses.uniV3Router)
);
} else {
return _swapExactTokensForTokensUniV2(
_swapData.path,
_amountIn,
_minAmountOut,
_getRouter(_swapData.exchange, _addresses)
);
}
}
/**
* Swap tokens for exact amount of output tokens on a given DEX.
*
* @param _addresses Struct containing relevant smart contract addresses.
* @param _amountOut The amount of output token required
* @param _maxAmountIn Maximum amount of input token to be spent
* @param _swapData Swap data containing the path and fee levels (latter only used for uniV3)
*
* @return amountIn The amount of input tokens spent
*/
function swapTokensForExactTokens(
Addresses memory _addresses,
uint256 _amountOut,
uint256 _maxAmountIn,
SwapData memory _swapData
)
external
returns (uint256 amountIn)
{
if (_swapData.path.length == 0 || _swapData.path[0] == _swapData.path[_swapData.path.length -1]) {
return _amountOut;
}
if(_swapData.exchange == Exchange.Curve){
return _swapTokensForExactTokensCurve(
_swapData.path,
_swapData.pool,
_amountOut,
_maxAmountIn,
_addresses
);
}
if(_swapData.exchange == Exchange.UniV3){
return _swapTokensForExactTokensUniV3(
_swapData.path,
_swapData.fees,
_amountOut,
_maxAmountIn,
ISwapRouter02(_addresses.uniV3Router)
);
} else {
return _swapTokensForExactTokensUniV2(
_swapData.path,
_amountOut,
_maxAmountIn,
_getRouter(_swapData.exchange, _addresses)
);
}
}
/**
* Gets the output amount of a token swap.
*
* @param _swapData the swap parameters
* @param _addresses Struct containing relevant smart contract addresses.
* @param _amountIn the input amount of the trade
*
* @return the output amount of the swap
*/
function getAmountOut(
Addresses memory _addresses,
SwapData memory _swapData,
uint256 _amountIn
)
external
returns (uint256)
{
if (_swapData.path.length == 0 || _swapData.path[0] == _swapData.path[_swapData.path.length-1]) {
return _amountIn;
}
if (_swapData.exchange == Exchange.UniV3) {
return _getAmountOutUniV3(_swapData, _addresses.uniV3Quoter, _amountIn);
} else if (_swapData.exchange == Exchange.Curve) {
(int128 i, int128 j) = _getCoinIndices(
_swapData.pool,
_swapData.path[0],
_swapData.path[1],
ICurveAddressProvider(_addresses.curveAddressProvider)
);
return _getAmountOutCurve(_swapData.pool, i, j, _amountIn, _addresses);
} else {
return _getAmountOutUniV2(
_swapData,
_getRouter(_swapData.exchange, _addresses),
_amountIn
);
}
}
/**
* Gets the input amount of a fixed output swap.
*
* @param _swapData the swap parameters
* @param _addresses Struct containing relevant smart contract addresses.
* @param _amountOut the output amount of the swap
*
* @return the input amount of the swap
*/
function getAmountIn(
Addresses memory _addresses,
SwapData memory _swapData,
uint256 _amountOut
)
external
returns (uint256)
{
if (_swapData.path.length == 0 || _swapData.path[0] == _swapData.path[_swapData.path.length-1]) {
return _amountOut;
}
if (_swapData.exchange == Exchange.UniV3) {
return _getAmountInUniV3(_swapData, _addresses.uniV3Quoter, _amountOut);
} else if (_swapData.exchange == Exchange.Curve) {
(int128 i, int128 j) = _getCoinIndices(
_swapData.pool,
_swapData.path[0],
_swapData.path[1],
ICurveAddressProvider(_addresses.curveAddressProvider)
);
return _getAmountInCurve(_swapData.pool, i, j, _amountOut, _addresses);
} else {
return _getAmountInUniV2(
_swapData,
_getRouter(_swapData.exchange, _addresses),
_amountOut
);
}
}
/**
* Sets a max approval limit for an ERC20 token, provided the current allowance
* is less than the required allownce.
*
* @param _token Token to approve
* @param _spender Spender address to approve
* @param _requiredAllowance Target allowance to set
*/
function _safeApprove(
IERC20 _token,
address _spender,
uint256 _requiredAllowance
)
internal
{
uint256 allowance = _token.allowance(address(this), _spender);
if (allowance < _requiredAllowance) {
_token.safeIncreaseAllowance(_spender, MAX_UINT256 - allowance);
}
}
/* ============ Private Methods ============ */
/**
* Execute exact output swap via a UniV2 based DEX. (such as sushiswap);
*
* @param _path List of token address to swap via.
* @param _amountOut The amount of output token required
* @param _maxAmountIn Maximum amount of input token to be spent
* @param _router Address of the uniV2 router to use
*
* @return amountIn The amount of input tokens spent
*/
function _swapTokensForExactTokensUniV2(
address[] memory _path,
uint256 _amountOut,
uint256 _maxAmountIn,
IUniswapV2Router02 _router
)
private
returns (uint256)
{
_safeApprove(IERC20(_path[0]), address(_router), _maxAmountIn);
return _router.swapTokensForExactTokens(_amountOut, _maxAmountIn, _path, address(this), block.timestamp)[0];
}
/**
* Execute exact output swap via UniswapV3
*
* @param _path List of token address to swap via. (In the order as
* expected by uniV2, the first element being the input toen)
* @param _fees List of fee levels identifying the pools to swap via.
* (_fees[0] refers to pool between _path[0] and _path[1])
* @param _amountOut The amount of output token required
* @param _maxAmountIn Maximum amount of input token to be spent
* @param _uniV3Router Address of the uniswapV3 router
*
* @return amountIn The amount of input tokens spent
*/
function _swapTokensForExactTokensUniV3(
address[] memory _path,
uint24[] memory _fees,
uint256 _amountOut,
uint256 _maxAmountIn,
ISwapRouter02 _uniV3Router
)
private
returns(uint256)
{
require(_path.length == _fees.length + 1, "ExchangeIssuance: PATHS_FEES_MISMATCH");
_safeApprove(IERC20(_path[0]), address(_uniV3Router), _maxAmountIn);
if(_path.length == 2){
ISwapRouter02.ExactOutputSingleParams memory params =
ISwapRouter02.ExactOutputSingleParams({
tokenIn: _path[0],
tokenOut: _path[1],
fee: _fees[0],
recipient: address(this),
amountOut: _amountOut,
amountInMaximum: _maxAmountIn,
sqrtPriceLimitX96: 0
});
return _uniV3Router.exactOutputSingle(params);
} else {
bytes memory pathV3 = _encodePathV3(_path, _fees, true);
ISwapRouter02.ExactOutputParams memory params =
ISwapRouter02.ExactOutputParams({
path: pathV3,
recipient: address(this),
amountOut: _amountOut,
amountInMaximum: _maxAmountIn
});
return _uniV3Router.exactOutput(params);
}
}
/**
* Execute exact input swap via Curve
*
* @param _path Path (has to be of length 2)
* @param _pool Address of curve pool to use
* @param _amountIn The amount of input token to be spent
* @param _minAmountOut Minimum amount of output token to receive
* @param _addresses Struct containing relevant smart contract addresses.
*
* @return amountOut The amount of output token obtained
*/
function _swapExactTokensForTokensCurve(
address[] memory _path,
address _pool,
uint256 _amountIn,
uint256 _minAmountOut,
Addresses memory _addresses
)
private
returns (uint256 amountOut)
{
require(_path.length == 2, "ExchangeIssuance: CURVE_WRONG_PATH_LENGTH");
(int128 i, int128 j) = _getCoinIndices(_pool, _path[0], _path[1], ICurveAddressProvider(_addresses.curveAddressProvider));
if(_path[0] == ETH_ADDRESS){
IWETH(_addresses.weth).withdraw(_amountIn);
}
amountOut = _exchangeCurve(i, j, _pool, _amountIn, _minAmountOut, _path[0]);
if(_path[_path.length-1] == ETH_ADDRESS){
IWETH(_addresses.weth).deposit{value: amountOut}();
}
}
/**
* Execute exact output swap via Curve
*
* @param _path Path (has to be of length 2)
* @param _pool Address of curve pool to use
* @param _amountOut The amount of output token required
* @param _maxAmountIn Maximum amount of input token to be spent
*
* @return amountOut The amount of output token obtained
*/
function _swapTokensForExactTokensCurve(
address[] memory _path,
address _pool,
uint256 _amountOut,
uint256 _maxAmountIn,
Addresses memory _addresses
)
private
returns (uint256)
{
require(_path.length == 2, "ExchangeIssuance: CURVE_WRONG_PATH_LENGTH");
(int128 i, int128 j) = _getCoinIndices(_pool, _path[0], _path[1], ICurveAddressProvider(_addresses.curveAddressProvider));
if(_path[0] == ETH_ADDRESS){
IWETH(_addresses.weth).withdraw(_maxAmountIn);
}
uint256 returnedAmountOut = _exchangeCurve(i, j, _pool, _maxAmountIn, _amountOut, _path[0]);
require(_amountOut <= returnedAmountOut, "ExchangeIssuance: CURVE_UNDERBOUGHT");
uint256 swappedBackAmountIn;
if(returnedAmountOut > _amountOut){
swappedBackAmountIn = _exchangeCurve(j, i, _pool, returnedAmountOut.sub(_amountOut), 0, _path[1]);
if(_path[0] == ETH_ADDRESS){
IWETH(_addresses.weth).deposit{ value: swappedBackAmountIn }();
}
}
if(_path[_path.length-1] == ETH_ADDRESS){
IWETH(_addresses.weth).deposit{ value: _amountOut }();
}
return _maxAmountIn.sub(swappedBackAmountIn);
}
function _exchangeCurve(
int128 _i,
int128 _j,
address _pool,
uint256 _amountIn,
uint256 _minAmountOut,
address _from
)
private
returns (uint256 amountOut)
{
ICurvePool pool = ICurvePool(_pool);
if(_from == ETH_ADDRESS){
amountOut = pool.exchange{value: _amountIn}(
_i,
_j,
_amountIn,
_minAmountOut
);
}
else {
IERC20(_from).approve(_pool, _amountIn);
amountOut = pool.exchange(
_i,
_j,
_amountIn,
_minAmountOut
);
}
}
/**
* Calculate required input amount to get a given output amount via Curve swap
*
* @param _i Index of input token as per the ordering of the pools tokens
* @param _j Index of output token as per the ordering of the pools tokens
* @param _pool Address of curve pool to use
* @param _amountOut The amount of output token to be received
* @param _addresses Struct containing relevant smart contract addresses.
*
* @return amountOut The amount of output token obtained
*/
function _getAmountInCurve(
address _pool,
int128 _i,
int128 _j,
uint256 _amountOut,
Addresses memory _addresses
)
private
view
returns (uint256)
{
CurvePoolData memory poolData = _getCurvePoolData(_pool, ICurveAddressProvider(_addresses.curveAddressProvider));
return ICurveCalculator(_addresses.curveCalculator).get_dx(
poolData.nCoins,
poolData.balances,
poolData.A,
poolData.fee,
poolData.rates,
poolData.decimals,
false,
_i,
_j,
_amountOut
) + ROUNDING_ERROR_MARGIN;
}
/**
* Calculate output amount of a Curve swap
*
* @param _i Index of input token as per the ordering of the pools tokens
* @param _j Index of output token as per the ordering of the pools tokens
* @param _pool Address of curve pool to use
* @param _amountIn The amount of output token to be received
* @param _addresses Struct containing relevant smart contract addresses.
*
* @return amountOut The amount of output token obtained
*/
function _getAmountOutCurve(
address _pool,
int128 _i,
int128 _j,
uint256 _amountIn,
Addresses memory _addresses
)
private
view
returns (uint256)
{
return ICurvePool(_pool).get_dy(_i, _j, _amountIn);
}
/**
* Get metadata on curve pool required to calculate input amount from output amount
*
* @param _pool Address of curve pool to use
* @param _curveAddressProvider Address of curve address provider
*
* @return Struct containing all required data to perform getAmountInCurve calculation
*/
function _getCurvePoolData(
address _pool,
ICurveAddressProvider _curveAddressProvider
) private view returns(CurvePoolData memory)
{
ICurvePoolRegistry registry = ICurvePoolRegistry(_curveAddressProvider.get_registry());
return CurvePoolData(
int128(registry.get_n_coins(_pool)[0]),
registry.get_balances(_pool),
registry.get_A(_pool),
registry.get_fees(_pool)[0],
registry.get_rates(_pool),
registry.get_decimals(_pool)
);
}
/**
* Get token indices for given pool
* NOTE: This was necessary sine the get_coin_indices function of the CurvePoolRegistry did not work for StEth/ETH pool
*
* @param _pool Address of curve pool to use
* @param _from Address of input token
* @param _to Address of output token
* @param _curveAddressProvider Address of curve address provider
*
* @return i Index of input token
* @return j Index of output token
*/
function _getCoinIndices(
address _pool,
address _from,
address _to,
ICurveAddressProvider _curveAddressProvider
)
private
view
returns (int128 i, int128 j)
{
ICurvePoolRegistry registry = ICurvePoolRegistry(_curveAddressProvider.get_registry());
// Set to out of range index to signal the coin is not found yet
i = 9;
j = 9;
address[8] memory poolCoins = registry.get_coins(_pool);
for(uint256 k = 0; k < 8; k++){
if(poolCoins[k] == _from){
i = int128(k);
}
else if(poolCoins[k] == _to){
j = int128(k);
}
// ZeroAddress signals end of list
if(poolCoins[k] == address(0) || (i != 9 && j != 9)){
break;
}
}
require(i != 9, "ExchangeIssuance: CURVE_FROM_NOT_FOUND");
require(j != 9, "ExchangeIssuance: CURVE_TO_NOT_FOUND");
return (i, j);
}
/**
* Execute exact input swap via UniswapV3
*
* @param _path List of token address to swap via.
* @param _fees List of fee levels identifying the pools to swap via.
* (_fees[0] refers to pool between _path[0] and _path[1])
* @param _amountIn The amount of input token to be spent
* @param _minAmountOut Minimum amount of output token to receive
* @param _uniV3Router Address of the uniswapV3 router
*
* @return amountOut The amount of output token obtained
*/
function _swapExactTokensForTokensUniV3(
address[] memory _path,
uint24[] memory _fees,
uint256 _amountIn,
uint256 _minAmountOut,
ISwapRouter02 _uniV3Router
)
private
returns (uint256)
{
require(_path.length == _fees.length + 1, "ExchangeIssuance: PATHS_FEES_MISMATCH");
_safeApprove(IERC20(_path[0]), address(_uniV3Router), _amountIn);
if(_path.length == 2){
ISwapRouter02.ExactInputSingleParams memory params =
ISwapRouter02.ExactInputSingleParams({
tokenIn: _path[0],
tokenOut: _path[1],
fee: _fees[0],
recipient: address(this),
amountIn: _amountIn,
amountOutMinimum: _minAmountOut,
sqrtPriceLimitX96: 0
});
return _uniV3Router.exactInputSingle(params);
} else {
bytes memory pathV3 = _encodePathV3(_path, _fees, false);
ISwapRouter02.ExactInputParams memory params =
ISwapRouter02.ExactInputParams({
path: pathV3,
recipient: address(this),
amountIn: _amountIn,
amountOutMinimum: _minAmountOut
});
uint amountOut = _uniV3Router.exactInput(params);
return amountOut;
}
}
/**
* Execute exact input swap via UniswapV2
*
* @param _path List of token address to swap via.
* @param _amountIn The amount of input token to be spent
* @param _minAmountOut Minimum amount of output token to receive
* @param _router Address of uniV2 router to use
*
* @return amountOut The amount of output token obtained
*/
function _swapExactTokensForTokensUniV2(
address[] memory _path,
uint256 _amountIn,
uint256 _minAmountOut,
IUniswapV2Router02 _router
)
private
returns (uint256)
{
_safeApprove(IERC20(_path[0]), address(_router), _amountIn);
// NOTE: The following was changed from always returning result at position [1] to returning the last element of the result array
// With this change, the actual output is correctly returned also for multi-hop swaps
// See https://github.com/IndexCoop/index-coop-smart-contracts/pull/116
uint256[] memory result = _router.swapExactTokensForTokens(_amountIn, _minAmountOut, _path, address(this), block.timestamp);
// result = uint[] memory The input token amount and all subsequent output token amounts.
// we are usually only interested in the actual amount of the output token (so result element at the last place)
return result[result.length-1];
}
/**
* Gets the output amount of a token swap on Uniswap V2
*
* @param _swapData the swap parameters
* @param _router the uniswap v2 router address
* @param _amountIn the input amount of the trade
*
* @return the output amount of the swap
*/
function _getAmountOutUniV2(
SwapData memory _swapData,
IUniswapV2Router02 _router,
uint256 _amountIn
)
private
view
returns (uint256)
{
return _router.getAmountsOut(_amountIn, _swapData.path)[_swapData.path.length-1];
}
/**
* Gets the input amount of a fixed output swap on Uniswap V2.
*
* @param _swapData the swap parameters
* @param _router the uniswap v2 router address
* @param _amountOut the output amount of the swap
*
* @return the input amount of the swap
*/
function _getAmountInUniV2(
SwapData memory _swapData,
IUniswapV2Router02 _router,
uint256 _amountOut
)
private
view
returns (uint256)
{
return _router.getAmountsIn(_amountOut, _swapData.path)[0];
}
/**
* Gets the output amount of a token swap on Uniswap V3.
*
* @param _swapData the swap parameters
* @param _quoter the uniswap v3 quoter
* @param _amountIn the input amount of the trade
*
* @return the output amount of the swap
*/
function _getAmountOutUniV3(
SwapData memory _swapData,
address _quoter,
uint256 _amountIn
)
private
returns (uint256)
{
bytes memory path = _encodePathV3(_swapData.path, _swapData.fees, false);
return IQuoter(_quoter).quoteExactInput(path, _amountIn);
}
/**
* Gets the input amount of a fixed output swap on Uniswap V3.
*
* @param _swapData the swap parameters
* @param _quoter uniswap v3 quoter
* @param _amountOut the output amount of the swap
*
* @return the input amount of the swap
*/
function _getAmountInUniV3(
SwapData memory _swapData,
address _quoter,
uint256 _amountOut
)
private
returns (uint256)
{
bytes memory path = _encodePathV3(_swapData.path, _swapData.fees, true);
return IQuoter(_quoter).quoteExactOutput(path, _amountOut);
}
/**
* Encode path / fees to bytes in the format expected by UniV3 router
*
* @param _path List of token address to swap via (starting with input token)
* @param _fees List of fee levels identifying the pools to swap via.
* (_fees[0] refers to pool between _path[0] and _path[1])
* @param _reverseOrder Boolean indicating if path needs to be reversed to start with output token.
* (which is the case for exact output swap)
*
* @return encodedPath Encoded path to be forwared to uniV3 router
*/
function _encodePathV3(
address[] memory _path,
uint24[] memory _fees,
bool _reverseOrder
)
private
pure
returns(bytes memory encodedPath)
{
if(_reverseOrder){
encodedPath = abi.encodePacked(_path[_path.length-1]);
for(uint i = 0; i < _fees.length; i++){
uint index = _fees.length - i - 1;
encodedPath = abi.encodePacked(encodedPath, _fees[index], _path[index]);
}
} else {
encodedPath = abi.encodePacked(_path[0]);
for(uint i = 0; i < _fees.length; i++){
encodedPath = abi.encodePacked(encodedPath, _fees[i], _path[i+1]);
}
}
}
function _getRouter(
Exchange _exchange,
Addresses memory _addresses
)
private
pure
returns (IUniswapV2Router02)
{
return IUniswapV2Router02(
(_exchange == Exchange.Quickswap) ? _addresses.quickRouter : _addresses.sushiRouter
);
}
}
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.10;
/**
* @dev This is the Aave V2 DataTypes library.
*/
library DataTypes {
// refer to the whitepaper, section 1.1 basic concepts for a formal description of these properties.
struct ReserveData {
//stores the reserve configuration
ReserveConfigurationMap configuration;
//the liquidity index. Expressed in ray
uint128 liquidityIndex;
//variable borrow index. Expressed in ray
uint128 variableBorrowIndex;
//the current supply rate. Expressed in ray
uint128 currentLiquidityRate;
//the current variable borrow rate. Expressed in ray
uint128 currentVariableBorrowRate;
//the current stable borrow rate. Expressed in ray
uint128 currentStableBorrowRate;
uint40 lastUpdateTimestamp;
//tokens addresses
address aTokenAddress;
address stableDebtTokenAddress;
address variableDebtTokenAddress;
//address of the interest rate strategy
address interestRateStrategyAddress;
//the id of the reserve. Represents the position in the list of the active reserves
uint8 id;
}
struct ReserveConfigurationMap {
//bit 0-15: LTV
//bit 16-31: Liq. threshold
//bit 32-47: Liq. bonus
//bit 48-55: Decimals
//bit 56: Reserve is active
//bit 57: reserve is frozen
//bit 58: borrowing is enabled
//bit 59: stable rate borrowing enabled
//bit 60-63: reserved
//bit 64-79: reserve factor
uint256 data;
}
struct UserConfigurationMap {
uint256 data;
}
enum InterestRateMode {NONE, STABLE, VARIABLE}
}
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.6.10;
library DataTypes {
struct ReserveData {
//stores the reserve configuration
ReserveConfigurationMap configuration;
//the liquidity index. Expressed in ray
uint128 liquidityIndex;
//the current supply rate. Expressed in ray
uint128 currentLiquidityRate;
//variable borrow index. Expressed in ray
uint128 variableBorrowIndex;
//the current variable borrow rate. Expressed in ray
uint128 currentVariableBorrowRate;
//the current stable borrow rate. Expressed in ray
uint128 currentStableBorrowRate;
//timestamp of last update
uint40 lastUpdateTimestamp;
//the id of the reserve. Represents the position in the list of the active reserves
uint16 id;
//aToken address
address aTokenAddress;
//stableDebtToken address
address stableDebtTokenAddress;
//variableDebtToken address
address variableDebtTokenAddress;
//address of the interest rate strategy
address interestRateStrategyAddress;
//the current treasury balance, scaled
uint128 accruedToTreasury;
//the outstanding unbacked aTokens minted through the bridging feature
uint128 unbacked;
//the outstanding debt borrowed against this asset in isolation mode
uint128 isolationModeTotalDebt;
}
struct ReserveConfigurationMap {
//bit 0-15: LTV
//bit 16-31: Liq. threshold
//bit 32-47: Liq. bonus
//bit 48-55: Decimals
//bit 56: reserve is active
//bit 57: reserve is frozen
//bit 58: borrowing is enabled
//bit 59: stable rate borrowing enabled
//bit 60: asset is paused
//bit 61: borrowing in isolation mode is enabled
//bit 62-63: reserved
//bit 64-79: reserve factor
//bit 80-115 borrow cap in whole tokens, borrowCap == 0 => no cap
//bit 116-151 supply cap in whole tokens, supplyCap == 0 => no cap
//bit 152-167 liquidation protocol fee
//bit 168-175 eMode category
//bit 176-211 unbacked mint cap in whole tokens, unbackedMintCap == 0 => minting disabled
//bit 212-251 debt ceiling for isolation mode with (ReserveConfiguration::DEBT_CEILING_DECIMALS) decimals
//bit 252-255 unused
uint256 data;
}
struct UserConfigurationMap {
/**
* @dev Bitmap of the users collaterals and borrows. It is divided in pairs of bits, one pair per asset.
* The first bit indicates if an asset is used as collateral by the user, the second whether an
* asset is borrowed by the user.
*/
uint256 data;
}
struct EModeCategory {
// each eMode category has a custom ltv and liquidation threshold
uint16 ltv;
uint16 liquidationThreshold;
uint16 liquidationBonus;
// each eMode category may or may not have a custom oracle to override the individual assets price oracles
address priceSource;
string label;
}
enum InterestRateMode {
NONE,
STABLE,
VARIABLE
}
struct ReserveCache {
uint256 currScaledVariableDebt;
uint256 nextScaledVariableDebt;
uint256 currPrincipalStableDebt;
uint256 currAvgStableBorrowRate;
uint256 currTotalStableDebt;
uint256 nextAvgStableBorrowRate;
uint256 nextTotalStableDebt;
uint256 currLiquidityIndex;
uint256 nextLiquidityIndex;
uint256 currVariableBorrowIndex;
uint256 nextVariableBorrowIndex;
uint256 currLiquidityRate;
uint256 currVariableBorrowRate;
uint256 reserveFactor;
ReserveConfigurationMap reserveConfiguration;
address aTokenAddress;
address stableDebtTokenAddress;
address variableDebtTokenAddress;
uint40 reserveLastUpdateTimestamp;
uint40 stableDebtLastUpdateTimestamp;
}
struct ExecuteLiquidationCallParams {
uint256 reservesCount;
uint256 debtToCover;
address collateralAsset;
address debtAsset;
address user;
bool receiveAToken;
address priceOracle;
uint8 userEModeCategory;
address priceOracleSentinel;
}
struct ExecuteSupplyParams {
address asset;
uint256 amount;
address onBehalfOf;
uint16 referralCode;
}
struct ExecuteBorrowParams {
address asset;
address user;
address onBehalfOf;
uint256 amount;
InterestRateMode interestRateMode;
uint16 referralCode;
bool releaseUnderlying;
uint256 maxStableRateBorrowSizePercent;
uint256 reservesCount;
address oracle;
uint8 userEModeCategory;
address priceOracleSentinel;
}
struct ExecuteRepayParams {
address asset;
uint256 amount;
InterestRateMode interestRateMode;
address onBehalfOf;
bool useATokens;
}
struct ExecuteWithdrawParams {
address asset;
uint256 amount;
address to;
uint256 reservesCount;
address oracle;
uint8 userEModeCategory;
}
struct ExecuteSetUserEModeParams {
uint256 reservesCount;
address oracle;
uint8 categoryId;
}
struct FinalizeTransferParams {
address asset;
address from;
address to;
uint256 amount;
uint256 balanceFromBefore;
uint256 balanceToBefore;
uint256 reservesCount;
address oracle;
uint8 fromEModeCategory;
}
struct FlashloanParams {
address receiverAddress;
address[] assets;
uint256[] amounts;
uint256[] interestRateModes;
address onBehalfOf;
bytes params;
uint16 referralCode;
uint256 flashLoanPremiumToProtocol;
uint256 flashLoanPremiumTotal;
uint256 maxStableRateBorrowSizePercent;
uint256 reservesCount;
address addressesProvider;
uint8 userEModeCategory;
bool isAuthorizedFlashBorrower;
}
struct FlashloanSimpleParams {
address receiverAddress;
address asset;
uint256 amount;
bytes params;
uint16 referralCode;
uint256 flashLoanPremiumToProtocol;
uint256 flashLoanPremiumTotal;
}
struct FlashLoanRepaymentParams {
uint256 amount;
uint256 totalPremium;
uint256 flashLoanPremiumToProtocol;
address asset;
address receiverAddress;
uint16 referralCode;
}
struct CalculateUserAccountDataParams {
UserConfigurationMap userConfig;
uint256 reservesCount;
address user;
address oracle;
uint8 userEModeCategory;
}
struct ValidateBorrowParams {
ReserveCache reserveCache;
UserConfigurationMap userConfig;
address asset;
address userAddress;
uint256 amount;
InterestRateMode interestRateMode;
uint256 maxStableLoanPercent;
uint256 reservesCount;
address oracle;
uint8 userEModeCategory;
address priceOracleSentinel;
bool isolationModeActive;
address isolationModeCollateralAddress;
uint256 isolationModeDebtCeiling;
}
struct ValidateLiquidationCallParams {
ReserveCache debtReserveCache;
uint256 totalDebt;
uint256 healthFactor;
address priceOracleSentinel;
}
struct CalculateInterestRatesParams {
uint256 unbacked;
uint256 liquidityAdded;
uint256 liquidityTaken;
uint256 totalStableDebt;
uint256 totalVariableDebt;
uint256 averageStableBorrowRate;
uint256 reserveFactor;
address reserve;
address aToken;
}
struct InitReserveParams {
address asset;
address aTokenAddress;
address stableDebtAddress;
address variableDebtAddress;
address interestRateStrategyAddress;
uint16 reservesCount;
uint16 maxNumberReserves;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
import "../../utils/Context.sol";
import "./IERC20.sol";
import "../../math/SafeMath.sol";
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin guidelines: functions revert instead
* of returning `false` on failure. This behavior is nonetheless conventional
* and does not conflict with the expectations of ERC20 applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
uint8 private _decimals;
/**
* @dev Sets the values for {name} and {symbol}, initializes {decimals} with
* a default value of 18.
*
* To select a different value for {decimals}, use {_setupDecimals}.
*
* All three of these values are immutable: they can only be set once during
* construction.
*/
constructor (string memory name_, string memory symbol_) public {
_name = name_;
_symbol = symbol_;
_decimals = 18;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
* called.
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual returns (uint8) {
return _decimals;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* Requirements:
*
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for ``sender``'s tokens of at least
* `amount`.
*/
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
return true;
}
/**
* @dev Moves tokens `amount` from `sender` to `recipient`.
*
* This is internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
function _transfer(address sender, address recipient, uint256 amount) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(sender, recipient, amount);
_balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `to` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
_balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
_totalSupply = _totalSupply.sub(amount);
emit Transfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(address owner, address spender, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Sets {decimals} to a value other than the default one of 18.
*
* WARNING: This function should only be called from the constructor. Most
* applications that interact with token contracts will not expect
* {decimals} to ever change, and may work incorrectly if it does.
*/
function _setupDecimals(uint8 decimals_) internal virtual {
_decimals = decimals_;
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be to transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.8;
import { SafeMath } from '@openzeppelin/contracts/math/SafeMath.sol';
import { IERC20 } from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import { SafeERC20 } from '@openzeppelin/contracts/token/ERC20/SafeERC20.sol';
import { IFlashLoanReceiverV2 } from './../../../contracts/interfaces/IFlashLoanReceiverV2.sol';
import { ILendingPoolAddressesProviderV2 } from './../../../contracts/interfaces/ILendingPoolAddressesProviderV2.sol';
import { ILendingPoolV2 } from './../../../contracts/interfaces/ILendingPoolV2.sol';
import "./utils/Withdrawable.sol";
/**
!!!
Never keep funds permanently on your FlashLoanReceiverBase contract as they could be
exposed to a 'griefing' attack, where the stored funds are used by an attacker.
!!!
*/
abstract contract FlashLoanReceiverBaseV2 is IFlashLoanReceiverV2 {
using SafeERC20 for IERC20;
using SafeMath for uint256;
ILendingPoolAddressesProviderV2 public immutable override ADDRESSES_PROVIDER;
ILendingPoolV2 public immutable override LENDING_POOL;
constructor(address provider) public {
ADDRESSES_PROVIDER = ILendingPoolAddressesProviderV2(provider);
LENDING_POOL = ILendingPoolV2(ILendingPoolAddressesProviderV2(provider).getLendingPool());
}
receive() payable external {}
}
/*
Copyright 2022 Index Cooperative
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
import { Address } from "@openzeppelin/contracts/utils/Address.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Math } from "@openzeppelin/contracts/math/Math.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import { IAToken } from "../interfaces/IAToken.sol";
import { IAaveLeverageModule } from "../interfaces/IAaveLeverageModule.sol";
import { IDebtIssuanceModule } from "../interfaces/IDebtIssuanceModule.sol";
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 { UniSushiV2Library } from "../../external/contracts/UniSushiV2Library.sol";
import { FlashLoanReceiverBaseV2 } from "../../external/contracts/aaveV2/FlashLoanReceiverBaseV2.sol";
import { DEXAdapter } from "./DEXAdapter.sol";
import {IVault, IFlashLoanRecipient} from "../interfaces/external/balancer-v2/IVault.sol";
import {IPool} from "../interfaces/IPool.sol";
/**
* @title FlashMintLeveraged
* @author Index Coop
*
* Contract for issuing and redeeming a leveraged Set Token
* Supports all tokens with one collateral Position in the form of an AToken and one debt position
* Both the collateral as well as the debt token have to be available for flashloan from balancer and be
* tradeable against each other on Sushi / Quickswap
*/
contract FlashMintLeveraged is ReentrancyGuard, IFlashLoanRecipient{
using DEXAdapter for DEXAdapter.Addresses;
using Address for address payable;
using SafeMath for uint256;
using PreciseUnitMath for uint256;
using SafeERC20 for IERC20;
using SafeERC20 for ISetToken;
/* ============ Structs ============ */
struct LeveragedTokenData {
address collateralAToken;
address collateralToken;
uint256 collateralAmount;
address debtToken;
uint256 debtAmount;
}
struct DecodedParams {
ISetToken setToken;
uint256 setAmount;
address originalSender;
bool isIssuance;
address paymentToken;
uint256 limitAmount;
LeveragedTokenData leveragedTokenData;
DEXAdapter.SwapData collateralAndDebtSwapData;
DEXAdapter.SwapData paymentTokenSwapData;
}
/* ============ Constants ============= */
uint256 constant private MAX_UINT256 = type(uint256).max;
uint256 public constant ROUNDING_ERROR_MARGIN = 2;
/* ============ State Variables ============ */
IController public immutable setController;
IDebtIssuanceModule public immutable debtIssuanceModule;
IAaveLeverageModule public immutable aaveLeverageModule;
DEXAdapter.Addresses public addresses;
IVault public immutable balancerV2Vault;
IPool public immutable LENDING_POOL;
address private flashLoanBenefactor;
/* ============ Events ============ */
event FlashMint(
address indexed _recipient, // The recipient address of the issued SetTokens
ISetToken indexed _setToken, // The issued SetToken
address indexed _inputToken, // The address of the input asset(ERC20/ETH) used to issue the SetTokens
uint256 _amountInputToken, // The amount of input tokens used for issuance
uint256 _amountSetIssued // The amount of SetTokens received by the recipient
);
event FlashRedeem(
address indexed _recipient, // The recipient address which redeemed the SetTokens
ISetToken indexed _setToken, // The redeemed SetToken
address indexed _outputToken, // The address of output asset(ERC20/ETH) received by the recipient
uint256 _amountSetRedeemed, // The amount of SetTokens redeemed for output tokens
uint256 _amountOutputToken // The amount of output tokens received by the recipient
);
/* ============ Modifiers ============ */
modifier onlyBalancerV2Vault() {
require(msg.sender == address(balancerV2Vault), "ExchangeIssuance: BalancerV2 Vault ONLY");
_;
}
modifier isValidPath(
address[] memory _path,
address _inputToken,
address _outputToken
)
{
if(_inputToken != _outputToken){
require(
_path[0] == _inputToken || (_inputToken == addresses.weth && _path[0] == DEXAdapter.ETH_ADDRESS),
"ExchangeIssuance: INPUT_TOKEN_NOT_IN_PATH"
);
require(
_path[_path.length-1] == _outputToken ||
(_outputToken == addresses.weth && _path[_path.length-1] == DEXAdapter.ETH_ADDRESS),
"ExchangeIssuance: OUTPUT_TOKEN_NOT_IN_PATH"
);
}
_;
}
/* ============ Constructor ============ */
/**
* Sets various contract addresses
*
* @param _addresses dex adapter addreses
* @param _setController SetToken controller used to verify a given token is a set
* @param _debtIssuanceModule DebtIssuanceModule used to issue and redeem tokens
* @param _aaveLeverageModule AaveLeverageModule to sync before every issuance / redemption
* @param _aaveV3Pool Address of address provider for aaves addresses
* @param _vault Balancer Vault to flashloan from
*/
constructor(
DEXAdapter.Addresses memory _addresses,
IController _setController,
IDebtIssuanceModule _debtIssuanceModule,
IAaveLeverageModule _aaveLeverageModule,
address _aaveV3Pool,
address _vault
)
public
{
setController = _setController;
debtIssuanceModule = _debtIssuanceModule;
aaveLeverageModule = _aaveLeverageModule;
addresses = _addresses;
LENDING_POOL = IPool(_aaveV3Pool);
balancerV2Vault = IVault(_vault);
}
/* ============ External Functions ============ */
/**
* Returns the collateral / debt token addresses and amounts for a leveraged index
*
* @param _setToken Address of the SetToken to be issued / redeemed
* @param _setAmount Amount of SetTokens to issue / redeem
* @param _isIssuance Boolean indicating if the SetToken is to be issued or redeemed
*
* @return Struct containing the collateral / debt token addresses and amounts
*/
function getLeveragedTokenData(
ISetToken _setToken,
uint256 _setAmount,
bool _isIssuance
)
external
view
returns (LeveragedTokenData memory)
{
return _getLeveragedTokenData(_setToken, _setAmount, _isIssuance);
}
/**
* Runs all the necessary approval functions required for a given ERC20 token.
* This function can be called when a new token is added to a SetToken during a
* rebalance.
*
* @param _token Address of the token which needs approval
*/
function approveToken(IERC20 _token) external {
_approveToken(_token);
}
/**
* Gets the input cost of issuing a given amount of a set token. This
* function is not marked view, but should be static called from frontends.
* This constraint is due to the need to interact with the Uniswap V3 quoter
* contract and call sync on AaveLeverageModule. Note: If the two SwapData
* paths contain the same tokens, there will be a slight error introduced
* in the result.
*
* @param _setToken the set token to issue
* @param _setAmount amount of set tokens
* @param _swapDataDebtForCollateral swap data for the debt to collateral swap
* @param _swapDataInputToken swap data for the input token to collateral swap
*
* @return the amount of input tokens required to perfrom the issuance
*/
function getIssueExactSet(
ISetToken _setToken,
uint256 _setAmount,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken
)
external
returns (uint256)
{
aaveLeverageModule.sync(_setToken);
LeveragedTokenData memory issueInfo = _getLeveragedTokenData(_setToken, _setAmount, true);
uint256 collateralOwed = issueInfo.collateralAmount.preciseMul(1.0009 ether);
uint256 borrowSaleProceeds = DEXAdapter.getAmountOut(addresses, _swapDataDebtForCollateral, issueInfo.debtAmount);
collateralOwed = collateralOwed.sub(borrowSaleProceeds);
return DEXAdapter.getAmountIn(addresses, _swapDataInputToken, collateralOwed);
}
/**
* Gets the proceeds of a redemption of a given amount of a set token. This
* function is not marked view, but should be static called from frontends.
* This constraint is due to the need to interact with the Uniswap V3 quoter
* contract and call sync on AaveLeverageModule. Note: If the two SwapData
* paths contain the same tokens, there will be a slight error introduced
* in the result.
*
* @param _setToken the set token to issue
* @param _setAmount amount of set tokens
* @param _swapDataCollateralForDebt swap data for the collateral to debt swap
* @param _swapDataOutputToken swap data for the collateral token to the output token
*
* @return amount of _outputToken that would be obtained from the redemption
*/
function getRedeemExactSet(
ISetToken _setToken,
uint256 _setAmount,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataOutputToken
)
external
returns (uint256)
{
aaveLeverageModule.sync(_setToken);
LeveragedTokenData memory redeemInfo = _getLeveragedTokenData(_setToken, _setAmount, false);
uint256 debtOwed = redeemInfo.debtAmount.preciseMul(1.0009 ether);
uint256 debtPurchaseCost = DEXAdapter.getAmountIn(addresses, _swapDataCollateralForDebt, debtOwed);
uint256 extraCollateral = redeemInfo.collateralAmount.sub(debtPurchaseCost);
return DEXAdapter.getAmountOut(addresses, _swapDataOutputToken, extraCollateral);
}
/**
* Trigger redemption of set token to pay the user with Eth
*
* @param _setToken Set token to redeem
* @param _setAmount Amount to redeem
* @param _minAmountOutputToken Minimum amount of ETH to send to the user
* @param _swapDataCollateralForDebt Data (token path and fee levels) describing the swap from Collateral Token to Debt Token
* @param _swapDataOutputToken Data (token path and fee levels) describing the swap from Collateral Token to Eth
*/
function redeemExactSetForETH(
ISetToken _setToken,
uint256 _setAmount,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataOutputToken
)
external
virtual
nonReentrant
{
_initiateRedemption(
_setToken,
_setAmount,
DEXAdapter.ETH_ADDRESS,
_minAmountOutputToken,
_swapDataCollateralForDebt,
_swapDataOutputToken
);
}
/**
* Trigger redemption of set token to pay the user with an arbitrary ERC20
*
* @param _setToken Set token to redeem
* @param _setAmount Amount to redeem
* @param _outputToken Address of the ERC20 token to send to the user
* @param _minAmountOutputToken Minimum amount of output token to send to the user
* @param _swapDataCollateralForDebt Data (token path and fee levels) describing the swap from Collateral Token to Debt Token
* @param _swapDataOutputToken Data (token path and fee levels) describing the swap from Collateral Token to Output token
*/
function redeemExactSetForERC20(
ISetToken _setToken,
uint256 _setAmount,
address _outputToken,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataOutputToken
)
external
virtual
nonReentrant
{
_initiateRedemption(
_setToken,
_setAmount,
_outputToken,
_minAmountOutputToken,
_swapDataCollateralForDebt,
_swapDataOutputToken
);
}
/**
* Trigger issuance of set token paying with any arbitrary ERC20 token
*
* @param _setToken Set token to issue
* @param _setAmount Amount to issue
* @param _inputToken Input token to pay with
* @param _maxAmountInputToken Maximum amount of input token to spend
* @param _swapDataDebtForCollateral Data (token addresses and fee levels) to describe the swap path from Debt to collateral token
* @param _swapDataInputToken Data (token addresses and fee levels) to describe the swap path from input to collateral token
*/
function issueExactSetFromERC20(
ISetToken _setToken,
uint256 _setAmount,
address _inputToken,
uint256 _maxAmountInputToken,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken
)
external
virtual
nonReentrant
{
_initiateIssuance(
_setToken,
_setAmount,
_inputToken,
_maxAmountInputToken,
_swapDataDebtForCollateral,
_swapDataInputToken
);
}
/**
* Trigger issuance of set token paying with Eth
*
* @param _setToken Set token to issue
* @param _setAmount Amount to issue
* @param _swapDataDebtForCollateral Data (token addresses and fee levels) to describe the swap path from Debt to collateral token
* @param _swapDataInputToken Data (token addresses and fee levels) to describe the swap path from eth to collateral token
*/
function issueExactSetFromETH(
ISetToken _setToken,
uint256 _setAmount,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken
)
external
virtual
payable
nonReentrant
{
_initiateIssuance(
_setToken,
_setAmount,
DEXAdapter.ETH_ADDRESS,
msg.value,
_swapDataDebtForCollateral,
_swapDataInputToken
);
}
/**
* This is the callback function that will be called by the Balancerv2 Pool after flashloaned tokens have been sent
* to this contract.
* After exiting this function the Vault enforces that we transfer back the loaned tokens + interest. If that check fails
* the whole transaction gets reverted
*
* @param tokens Addresses of all assets that were borrowed
* @param amounts Amounts that were borrowed
* @param feeAmounts Interest to be paid on top of borrowed amount
* @param userData Encoded bytestring of other parameters from the original contract call to be used downstream
*
*/
function receiveFlashLoan(
IERC20[] memory tokens,
uint256[] memory amounts,
uint256[] memory feeAmounts,
bytes memory userData
)
external
override
onlyBalancerV2Vault
{
DecodedParams memory decodedParams = abi.decode(userData, (DecodedParams));
require(flashLoanBenefactor == decodedParams.originalSender, "Flashloan not initiated by this contract");
if(decodedParams.isIssuance){
_performIssuance(address(tokens[0]), amounts[0], feeAmounts[0], decodedParams);
} else {
_performRedemption(address(tokens[0]), amounts[0], feeAmounts[0], decodedParams);
}
for(uint256 i = 0; i < tokens.length; i++) {
tokens[i].safeTransfer(address(balancerV2Vault), amounts[i]+ feeAmounts[i]);
}
}
/**
* Runs all the necessary approval functions required for a list of ERC20 tokens.
*
* @param _tokens Addresses of the tokens which need approval
*/
function approveTokens(IERC20[] memory _tokens) external {
for (uint256 i = 0; i < _tokens.length; i++) {
_approveToken(_tokens[i]);
}
}
/**
* Runs all the necessary approval functions required before issuing
* or redeeming a SetToken. This function need to be called only once before the first time
* this smart contract is used on any particular SetToken.
*
* @param _setToken Address of the SetToken being initialized
*/
function approveSetToken(ISetToken _setToken) external {
LeveragedTokenData memory leveragedTokenData = _getLeveragedTokenData(_setToken, 1 ether, true);
_approveToken(IERC20(leveragedTokenData.collateralAToken));
_approveTokenToLendingPool(IERC20(leveragedTokenData.collateralToken));
_approveToken(IERC20(leveragedTokenData.debtToken));
_approveTokenToLendingPool(IERC20(leveragedTokenData.debtToken));
}
/* ============ Internal Functions ============ */
/**
* Performs all the necessary steps for issuance using the collateral tokens obtained in the flashloan
*
* @param _collateralToken Address of the underlying collateral token that was loaned
* @param _collateralTokenAmountNet Amount of collateral token that was received as flashloan
* @param _premium Premium / Interest that has to be returned to the lending pool on top of the loaned amount
* @param _decodedParams Struct containing token addresses / amounts to perform issuance
*/
function _performIssuance(
address _collateralToken,
uint256 _collateralTokenAmountNet,
uint256 _premium,
DecodedParams memory _decodedParams
)
internal
{
// Deposit collateral token obtained from flashloan to get the respective aToken position required for issuance
_depositCollateralToken(_collateralToken, _collateralTokenAmountNet);
// Issue set using the aToken returned by deposit step
_issueSet(_decodedParams.setToken, _decodedParams.setAmount, _decodedParams.originalSender);
// Obtain necessary collateral tokens to repay flashloan
uint amountInputTokenSpent = _obtainCollateralTokens(
_collateralToken,
_collateralTokenAmountNet + _premium,
_decodedParams
);
require(amountInputTokenSpent <= _decodedParams.limitAmount, "ExchangeIssuance: INSUFFICIENT INPUT AMOUNT");
}
/**
* Performs all the necessary steps for redemption using the debt tokens obtained in the flashloan
*
* @param _debtToken Address of the debt token that was loaned
* @param _debtTokenAmountNet Amount of debt token that was received as flashloan
* @param _premium Premium / Interest that has to be returned to the lending pool on top of the loaned amount
* @param _decodedParams Struct containing token addresses / amounts to perform redemption
*/
function _performRedemption(
address _debtToken,
uint256 _debtTokenAmountNet,
uint256 _premium,
DecodedParams memory _decodedParams
)
internal
{
// Redeem set using debt tokens obtained from flashloan
_redeemSet(
_decodedParams.setToken,
_decodedParams.setAmount,
_decodedParams.originalSender
);
// Withdraw underlying collateral token from the aToken position returned by redeem step
_withdrawCollateralToken(
_decodedParams.leveragedTokenData.collateralToken,
_decodedParams.leveragedTokenData.collateralAmount - ROUNDING_ERROR_MARGIN
);
// Obtain debt tokens required to repay flashloan by swapping the underlying collateral tokens obtained in withdraw step
uint256 collateralTokenSpent = _swapCollateralForDebtToken(
_debtTokenAmountNet + _premium,
_debtToken,
_decodedParams.leveragedTokenData.collateralAmount,
_decodedParams.leveragedTokenData.collateralToken,
_decodedParams.collateralAndDebtSwapData
);
// Liquidate remaining collateral tokens for the payment token specified by user
uint256 amountOutputToken = _liquidateCollateralTokens(
collateralTokenSpent,
_decodedParams.setToken,
_decodedParams.setAmount,
_decodedParams.originalSender,
_decodedParams.paymentToken,
_decodedParams.limitAmount,
_decodedParams.leveragedTokenData.collateralToken,
_decodedParams.leveragedTokenData.collateralAmount - 2*ROUNDING_ERROR_MARGIN,
_decodedParams.paymentTokenSwapData
);
require(amountOutputToken >= _decodedParams.limitAmount, "ExchangeIssuance: INSUFFICIENT OUTPUT AMOUNT");
}
/**
* Returns the collateral / debt token addresses and amounts for a leveraged index
*
* @param _setToken Address of the SetToken to be issued / redeemed
* @param _setAmount Amount of SetTokens to issue / redeem
* @param _isIssuance Boolean indicating if the SetToken is to be issued or redeemed
*
* @return Struct containing the collateral / debt token addresses and amounts
*/
function _getLeveragedTokenData(
ISetToken _setToken,
uint256 _setAmount,
bool _isIssuance
)
internal
view
returns (LeveragedTokenData memory)
{
address[] memory components;
uint256[] memory equityPositions;
uint256[] memory debtPositions;
if(_isIssuance){
(components, equityPositions, debtPositions) = debtIssuanceModule.getRequiredComponentIssuanceUnits(_setToken, _setAmount);
} else {
(components, equityPositions, debtPositions) = debtIssuanceModule.getRequiredComponentRedemptionUnits(_setToken, _setAmount);
}
require(components.length == 2, "ExchangeIssuance: TOO MANY COMPONENTS");
require(equityPositions[0] == 0 || equityPositions[1] == 0, "ExchangeIssuance: TOO MANY EQUITY POSITIONS");
require(debtPositions[0] == 0 || debtPositions[1] == 0, "ExchangeIssuance: TOO MANY DEBT POSITIONS");
if(equityPositions[0] > 0){
return LeveragedTokenData(
components[0],
IAToken(components[0]).UNDERLYING_ASSET_ADDRESS(),
equityPositions[0] + ROUNDING_ERROR_MARGIN,
components[1],
debtPositions[1]
);
} else {
return LeveragedTokenData(
components[1],
IAToken(components[1]).UNDERLYING_ASSET_ADDRESS(),
equityPositions[1] + ROUNDING_ERROR_MARGIN,
components[0],
debtPositions[0]
);
}
}
/**
* Approves max amount of given token to all exchange routers and the debt issuance module
*
* @param _token Address of the token to be approved
*/
function _approveToken(IERC20 _token) internal {
_safeApprove(_token, address(debtIssuanceModule), MAX_UINT256);
}
/**
* Initiates a flashloan call with the correct parameters for issuing set tokens in the callback
* Borrows correct amount of collateral token and and forwards encoded memory to controll issuance in the callback.
*
* @param _setToken Address of the SetToken being initialized
* @param _setAmount Amount of the SetToken being initialized
* @param _inputToken Address of the input token to pay with
* @param _maxAmountInputToken Maximum amount of input token to pay
* @param _swapDataDebtForCollateral Data (token addresses and fee levels) to describe the swap path from Debt to collateral token
* @param _swapDataInputToken Data (token addresses and fee levels) to describe the swap path from input to collateral token
*/
function _initiateIssuance(
ISetToken _setToken,
uint256 _setAmount,
address _inputToken,
uint256 _maxAmountInputToken,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken
)
internal
{
aaveLeverageModule.sync(_setToken);
LeveragedTokenData memory leveragedTokenData = _getLeveragedTokenData(_setToken, _setAmount, true);
address[] memory assets = new address[](1);
assets[0] = leveragedTokenData.collateralToken;
uint[] memory amounts = new uint[](1);
amounts[0] = leveragedTokenData.collateralAmount;
bytes memory params = abi.encode(
DecodedParams(
_setToken,
_setAmount,
msg.sender,
true,
_inputToken,
_maxAmountInputToken,
leveragedTokenData,
_swapDataDebtForCollateral,
_swapDataInputToken
)
);
_flashloan(assets, amounts, params);
}
/**
* Initiates a flashloan call with the correct parameters for redeeming set tokens in the callback
*
* @param _setToken Address of the SetToken to redeem
* @param _setAmount Amount of the SetToken to redeem
* @param _outputToken Address of token to return to the user
* @param _minAmountOutputToken Minimum amount of output token to receive
* @param _swapDataCollateralForDebt Data (token path and fee levels) describing the swap from Collateral Token to Debt Token
* @param _swapDataOutputToken Data (token path and fee levels) describing the swap from Collateral Token to Output token
*/
function _initiateRedemption(
ISetToken _setToken,
uint256 _setAmount,
address _outputToken,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataOutputToken
)
internal
{
aaveLeverageModule.sync(_setToken);
LeveragedTokenData memory leveragedTokenData = _getLeveragedTokenData(_setToken, _setAmount, false);
address[] memory assets = new address[](1);
assets[0] = leveragedTokenData.debtToken;
uint[] memory amounts = new uint[](1);
amounts[0] = leveragedTokenData.debtAmount;
bytes memory params = abi.encode(
DecodedParams(
_setToken,
_setAmount,
msg.sender,
false,
_outputToken,
_minAmountOutputToken,
leveragedTokenData,
_swapDataCollateralForDebt,
_swapDataOutputToken
)
);
_flashloan(assets, amounts, params);
}
/**
* Gets rid of the obtained collateral tokens from redemption by either sending them to the user
* directly or converting them to the payment token and sending those out.
*
* @param _collateralTokenSpent Amount of collateral token spent to obtain the debt token required for redemption
* @param _setToken Address of the SetToken to be issued
* @param _setAmount Amount of SetTokens to issue
* @param _originalSender Address of the user who initiated the redemption
* @param _outputToken Address of token to return to the user
* @param _collateralToken Address of the collateral token to sell
* @param _collateralAmount Amount of collateral token to sell
* @param _minAmountOutputToken Minimum amount of output token to return to the user
* @param _swapData Struct containing path and fee data for swap
*
* @return Amount of output token returned to the user
*/
function _liquidateCollateralTokens(
uint256 _collateralTokenSpent,
ISetToken _setToken,
uint256 _setAmount,
address _originalSender,
address _outputToken,
uint256 _minAmountOutputToken,
address _collateralToken,
uint256 _collateralAmount,
DEXAdapter.SwapData memory _swapData
)
internal
returns (uint256)
{
require(_collateralAmount >= _collateralTokenSpent, "ExchangeIssuance: OVERSPENT COLLATERAL TOKEN");
uint256 amountToReturn = _collateralAmount.sub(_collateralTokenSpent);
uint256 outputAmount;
if(_outputToken == DEXAdapter.ETH_ADDRESS){
outputAmount = _liquidateCollateralTokensForETH(
_collateralToken,
amountToReturn,
_originalSender,
_minAmountOutputToken,
_swapData
);
} else {
outputAmount = _liquidateCollateralTokensForERC20(
_collateralToken,
amountToReturn,
_originalSender,
IERC20(_outputToken),
_minAmountOutputToken,
_swapData
);
}
emit FlashMint(_originalSender, _setToken, _outputToken, _setAmount, outputAmount);
return outputAmount;
}
/**
* Returns the collateralToken directly to the user
*
* @param _collateralToken Address of the the collateral token
* @param _collateralRemaining Amount of the collateral token remaining after buying required debt tokens
* @param _originalSender Address of the original sender to return the tokens to
*/
function _returnCollateralTokensToSender(
address _collateralToken,
uint256 _collateralRemaining,
address _originalSender
)
internal
{
IERC20(_collateralToken).transfer(_originalSender, _collateralRemaining);
}
/**
* Sells the collateral tokens for the selected output ERC20 and returns that to the user
*
* @param _collateralToken Address of the collateral token
* @param _collateralRemaining Amount of the collateral token remaining after buying required debt tokens
* @param _originalSender Address of the original sender to return the tokens to
* @param _outputToken Address of token to return to the user
* @param _minAmountOutputToken Minimum amount of output token to return to the user
* @param _swapData Data (token path and fee levels) describing the swap path from Collateral Token to Output token
*
* @return Amount of output token returned to the user
*/
function _liquidateCollateralTokensForERC20(
address _collateralToken,
uint256 _collateralRemaining,
address _originalSender,
IERC20 _outputToken,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapData
)
internal
virtual
returns (uint256)
{
if(address(_outputToken) == _collateralToken){
_returnCollateralTokensToSender(_collateralToken, _collateralRemaining, _originalSender);
return _collateralRemaining;
}
uint256 outputTokenAmount = _swapCollateralForOutputToken(
_collateralToken,
_collateralRemaining,
address(_outputToken),
_minAmountOutputToken,
_swapData
);
_outputToken.transfer(_originalSender, outputTokenAmount);
return outputTokenAmount;
}
/**
* Sells the remaining collateral tokens for weth, withdraws that and returns native eth to the user
*
* @param _collateralToken Address of the collateral token
* @param _collateralRemaining Amount of the collateral token remaining after buying required debt tokens
* @param _originalSender Address of the original sender to return the eth to
* @param _minAmountOutputToken Minimum amount of output token to return to user
* @param _swapData Data (token path and fee levels) describing the swap path from Collateral Token to eth
*
* @return Amount of eth returned to the user
*/
function _liquidateCollateralTokensForETH(
address _collateralToken,
uint256 _collateralRemaining,
address _originalSender,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapData
)
internal
virtual
isValidPath(_swapData.path, _collateralToken, addresses.weth)
returns(uint256)
{
uint256 ethAmount = _swapCollateralForOutputToken(
_collateralToken,
_collateralRemaining,
addresses.weth,
_minAmountOutputToken,
_swapData
);
if (ethAmount > 0) {
IWETH(addresses.weth).withdraw(ethAmount);
(payable(_originalSender)).sendValue(ethAmount);
}
return ethAmount;
}
/**
* Obtains the tokens necessary to return the flashloan by swapping the debt tokens obtained
* from issuance and making up the shortfall using the users funds.
*
* @param _collateralToken collateral token to obtain
* @param _amountRequired Amount of collateralToken required to repay the flashloan
* @param _decodedParams Struct containing decoded data from original call passed through via flashloan
*
* @return Amount of input token spent
*/
function _obtainCollateralTokens(
address _collateralToken,
uint256 _amountRequired,
DecodedParams memory _decodedParams
)
internal
returns (uint256)
{
uint collateralTokenObtained = _swapDebtForCollateralToken(
_collateralToken,
_decodedParams.leveragedTokenData.debtToken,
_decodedParams.leveragedTokenData.debtAmount,
_decodedParams.collateralAndDebtSwapData
);
uint collateralTokenShortfall = _amountRequired.sub(collateralTokenObtained) + ROUNDING_ERROR_MARGIN;
uint amountInputToken;
if(_decodedParams.paymentToken == DEXAdapter.ETH_ADDRESS){
amountInputToken = _makeUpShortfallWithETH(
_collateralToken,
collateralTokenShortfall,
_decodedParams.originalSender,
_decodedParams.limitAmount,
_decodedParams.paymentTokenSwapData
);
} else {
amountInputToken = _makeUpShortfallWithERC20(
_collateralToken,
collateralTokenShortfall,
_decodedParams.originalSender,
IERC20(_decodedParams.paymentToken),
_decodedParams.limitAmount,
_decodedParams.paymentTokenSwapData
);
}
emit FlashRedeem(
_decodedParams.originalSender,
_decodedParams.setToken,
_decodedParams.paymentToken,
amountInputToken,
_decodedParams.setAmount
);
return amountInputToken;
}
/**
* Issues set token using the previously obtained collateral token
* Results in debt token being returned to the contract
*
* @param _setToken Address of the SetToken to be issued
* @param _setAmount Amount of SetTokens to issue
* @param _originalSender Adress that initiated the token issuance, which will receive the set tokens
*/
function _issueSet(ISetToken _setToken, uint256 _setAmount, address _originalSender) internal {
debtIssuanceModule.issue(_setToken, _setAmount, _originalSender);
}
/**
* Redeems set token using the previously obtained debt token
* Results in collateral token being returned to the contract
*
* @param _setToken Address of the SetToken to be redeemed
* @param _setAmount Amount of SetTokens to redeem
* @param _originalSender Adress that initiated the token redemption which is the source of the set tokens to be redeemed
*/
function _redeemSet(ISetToken _setToken, uint256 _setAmount, address _originalSender) internal {
_setToken.safeTransferFrom(_originalSender, address(this), _setAmount);
debtIssuanceModule.redeem(_setToken, _setAmount, address(this));
}
/**
* Transfers the shortfall between the amount of tokens required to return flashloan and what was obtained
* from swapping the debt tokens from the users address
*
* @param _token Address of the token to transfer from user
* @param _shortfall Collateral token shortfall required to return the flashloan
* @param _originalSender Adress that initiated the token issuance, which is the adresss form which to transfer the tokens
*/
function _transferShortfallFromSender(
address _token,
uint256 _shortfall,
address _originalSender
)
internal
{
if(_shortfall>0){
IERC20(_token).safeTransferFrom(_originalSender, address(this), _shortfall);
}
}
/**
* Makes up the collateral token shortfall with user specified ERC20 token
*
* @param _collateralToken Address of the collateral token
* @param _collateralTokenShortfall Shortfall of collateral token that was not covered by selling the debt tokens
* @param _originalSender Address of the original sender to return the tokens to
* @param _inputToken Input token to pay with
* @param _maxAmountInputToken Maximum amount of input token to spend
*
* @return Amount of input token spent
*/
function _makeUpShortfallWithERC20(
address _collateralToken,
uint256 _collateralTokenShortfall,
address _originalSender,
IERC20 _inputToken,
uint256 _maxAmountInputToken,
DEXAdapter.SwapData memory _swapData
)
internal
virtual
returns (uint256)
{
if(address(_inputToken) == _collateralToken){
_transferShortfallFromSender(_collateralToken, _collateralTokenShortfall, _originalSender);
return _collateralTokenShortfall;
} else {
_inputToken.transferFrom(_originalSender, address(this), _maxAmountInputToken);
uint256 amountInputToken = _swapInputForCollateralToken(
_collateralToken,
_collateralTokenShortfall,
address(_inputToken),
_maxAmountInputToken,
_swapData
);
if(amountInputToken < _maxAmountInputToken){
_inputToken.transfer(_originalSender, _maxAmountInputToken.sub(amountInputToken));
}
return amountInputToken;
}
}
/**
* Makes up the collateral token shortfall with native eth
*
* @param _collateralToken Address of the collateral token
* @param _collateralTokenShortfall Shortfall of collateral token that was not covered by selling the debt tokens
* @param _originalSender Address of the original sender to return the tokens to
* @param _maxAmountEth Maximum amount of eth to pay
*
* @return Amount of eth spent
*/
function _makeUpShortfallWithETH(
address _collateralToken,
uint256 _collateralTokenShortfall,
address _originalSender,
uint256 _maxAmountEth,
DEXAdapter.SwapData memory _swapData
)
internal
virtual
returns(uint256)
{
IWETH(addresses.weth).deposit{value: _maxAmountEth}();
uint256 amountEth = _swapInputForCollateralToken(
_collateralToken,
_collateralTokenShortfall,
addresses.weth,
_maxAmountEth,
_swapData
);
if(_maxAmountEth > amountEth){
uint256 amountEthReturn = _maxAmountEth.sub(amountEth);
IWETH(addresses.weth).withdraw(amountEthReturn);
(payable(_originalSender)).sendValue(amountEthReturn);
}
return amountEth;
}
/**
* Swaps the debt tokens obtained from issuance for the collateral
*
* @param _collateralToken Address of the collateral token buy
* @param _debtToken Address of the debt token to sell
* @param _debtAmount Amount of debt token to sell
* @param _swapData Struct containing path and fee data for swap
*
* @return Amount of collateral token obtained
*/
function _swapDebtForCollateralToken(
address _collateralToken,
address _debtToken,
uint256 _debtAmount,
DEXAdapter.SwapData memory _swapData
)
internal
isValidPath(_swapData.path, _debtToken, _collateralToken)
returns (uint256)
{
return addresses.swapExactTokensForTokens(
_debtAmount,
// minAmountOut is 0 here since we are going to make up the shortfall with the input token.
// Sandwich protection is provided by the check at the end against _maxAmountInputToken parameter specified by the user
0,
_swapData
);
}
/**
* Acquires debt tokens needed for flashloan repayment by swapping a portion of the collateral tokens obtained from redemption
*
* @param _debtAmount Amount of debt token to buy
* @param _debtToken Address of debt token
* @param _collateralAmount Amount of collateral token available to spend / used as maxAmountIn parameter
* @param _collateralToken Address of collateral token
* @param _swapData Struct containing path and fee data for swap
*
* @return Amount of collateral token spent
*/
function _swapCollateralForDebtToken(
uint256 _debtAmount,
address _debtToken,
uint256 _collateralAmount,
address _collateralToken,
DEXAdapter.SwapData memory _swapData
)
internal
isValidPath(_swapData.path, _collateralToken, _debtToken)
returns (uint256)
{
return addresses.swapTokensForExactTokens(
_debtAmount,
_collateralAmount,
_swapData
);
}
/**
* Acquires the required amount of collateral tokens by swapping the input tokens
* Does nothing if collateral and input token are indentical
*
* @param _collateralToken Address of collateral token
* @param _amountRequired Remaining amount of collateral token required to repay flashloan, after having swapped debt tokens for collateral
* @param _inputToken Address of input token to swap
* @param _maxAmountInputToken Maximum amount of input token to spend
* @param _swapData Data (token addresses and fee levels) describing the swap path
*
* @return Amount of input token spent
*/
function _swapInputForCollateralToken(
address _collateralToken,
uint256 _amountRequired,
address _inputToken,
uint256 _maxAmountInputToken,
DEXAdapter.SwapData memory _swapData
)
internal
isValidPath(
_swapData.path,
_inputToken,
_collateralToken
)
returns (uint256)
{
if(_collateralToken == _inputToken) return _amountRequired;
return addresses.swapTokensForExactTokens(
_amountRequired,
_maxAmountInputToken,
_swapData
);
}
/**
* Swaps the collateral tokens obtained from redemption for the selected output token
* If both tokens are the same, does nothing
*
* @param _collateralToken Address of collateral token
* @param _collateralTokenAmount Amount of colalteral token to swap
* @param _outputToken Address of the ERC20 token to swap into
* @param _minAmountOutputToken Minimum amount of output token to return to the user
* @param _swapData Data (token addresses and fee levels) describing the swap path
*
* @return Amount of output token obtained
*/
function _swapCollateralForOutputToken(
address _collateralToken,
uint256 _collateralTokenAmount,
address _outputToken,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapData
)
internal
isValidPath(_swapData.path, _collateralToken, _outputToken)
returns (uint256)
{
return addresses.swapExactTokensForTokens(
_collateralTokenAmount,
_minAmountOutputToken,
_swapData
);
}
/**
* Deposit collateral to aave to obtain collateralAToken for issuance
*
* @param _collateralToken Address of collateral token
* @param _depositAmount Amount to deposit
*/
function _depositCollateralToken(
address _collateralToken,
uint256 _depositAmount
) internal {
LENDING_POOL.deposit(_collateralToken, _depositAmount, address(this), 0);
}
/**
* Convert collateralAToken from set redemption to collateralToken by withdrawing underlying from Aave
*
* @param _collateralToken Address of the collateralToken to withdraw from Aave lending pool
* @param _collateralAmount Amount of collateralToken to withdraw
*/
function _withdrawCollateralToken(
address _collateralToken,
uint256 _collateralAmount
) internal {
LENDING_POOL.withdraw(_collateralToken, _collateralAmount, address(this));
}
/**
* Sets a max approval limit for an ERC20 token, provided the current allowance
* is less than the required allownce.
*
* @param _token Token to approve
* @param _spender Spender address to approve
* @param _requiredAllowance Target allowance to set
*/
function _safeApprove(
IERC20 _token,
address _spender,
uint256 _requiredAllowance
)
internal
{
uint256 allowance = _token.allowance(address(this), _spender);
if (allowance < _requiredAllowance) {
_token.safeIncreaseAllowance(_spender, MAX_UINT256 - allowance);
}
}
/**
* Approves max amount of token to lending pool
*
* @param _token Address of the token to approve
*/
function _approveTokenToLendingPool(
IERC20 _token
)
internal
{
uint256 allowance = _token.allowance(address(this), address(LENDING_POOL));
if (allowance > 0) {
_token.approve(address(LENDING_POOL), 0);
}
_token.approve(address(LENDING_POOL), MAX_UINT256);
}
/**
* Triggers the flashloan from the BalancerV2 Vault
*
* @param assets Addresses of tokens to loan
* @param amounts Amounts to loan
* @param params Encoded memory to forward to the executeOperation method
*/
function _flashloan(
address[] memory assets,
uint256[] memory amounts,
bytes memory params
)
internal
{
require(flashLoanBenefactor == address(0), "Flashloan already taken");
flashLoanBenefactor = msg.sender;
balancerV2Vault.flashLoan(this, assets, amounts, params);
flashLoanBenefactor = address(0);
}
/**
* Redeems a given amount of SetToken.
*
* @param _setToken Address of the SetToken to be redeemed
* @param _amount Amount of SetToken to be redeemed
*/
function _redeemExactSet(ISetToken _setToken, uint256 _amount) internal returns (uint256) {
_setToken.safeTransferFrom(msg.sender, address(this), _amount);
debtIssuanceModule.redeem(_setToken, _amount, address(this));
}
receive() external payable {}
}
/*
Copyright 2024 Index Cooperative
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { FlashMintLeveraged } from "./FlashMintLeveraged.sol";
import { DEXAdapter } from "./DEXAdapter.sol";
import { IController } from "../interfaces/IController.sol";
import { IAaveLeverageModule } from "../interfaces/IAaveLeverageModule.sol";
import { IDebtIssuanceModule } from "../interfaces/IDebtIssuanceModule.sol";
import { IAToken } from "../interfaces/IAToken.sol";
import { ISetToken } from "../interfaces/ISetToken.sol";
import { IWETH } from "../interfaces/IWETH.sol";
/**
* @title FlashMintLeveragedExtended
* @author Index Coop
*
* Extended version of FlashMintLeveraged which allows for exactInputIssuance and exactOutputRedemption
*/
contract FlashMintLeveragedExtended is FlashMintLeveraged, Ownable {
uint256 public maxIterations = 10;
uint256 public maxGasRebate = 0.01 ether;
/* ============ Constructor ============ */
/**
* Sets various contract addresses
*
* @param _addresses dex adapter addreses
* @param _setController SetToken controller used to verify a given token is a set
* @param _debtIssuanceModule DebtIssuanceModule used to issue and redeem tokens
* @param _aaveLeverageModule AaveLeverageModule to sync before every issuance / redemption
* @param _aaveV3Pool Address of address provider for aaves addresses
* @param _vault Balancer Vault to flashloan from
*/
constructor(
DEXAdapter.Addresses memory _addresses,
IController _setController,
IDebtIssuanceModule _debtIssuanceModule,
IAaveLeverageModule _aaveLeverageModule,
address _aaveV3Pool,
address _vault
) Ownable()
public
FlashMintLeveraged(_addresses, _setController, _debtIssuanceModule, _aaveLeverageModule, _aaveV3Pool, _vault)
{
}
/**
* Redeems a variable amount of setTokens to return exactly the specified amount of outputToken to the user
*
* @param _setToken Set token to redeem
* @param _maxSetAmount Maximum amout of set tokens to redeem
* @param _outputToken Address of output token to return to the user
* @param _outputTokenAmount Amount of output token to return to the user
* @param _swapDataCollateralForDebt Data (token path and fee levels) describing the swap from Collateral Token to Debt Token
* @param _swapDataCollateralForOutputToken Data (token path and fee levels) describing the swap from Collateral Token to Output TOken
* @param _swapDataDebtForCollateral Data (token path and fee levels) describing the swap from Debt Token to Collateral Token
* @param _swapDataOutputTokenForCollateral Data (token path and fee levels) describing the swap from Output Token to Collateral Token
* @param _swapDataOutputTokenForETH Data (token path and fee levels) describing the swap from Output Token to ETH
* @param _priceEstimateInflator Factor by which to increase the estimated price from the previous iteration to account for used up liquidity
* @param _maxDust Minimum accuracy for approximating the output token amount. Excess will be swapped to eth and returned to user as gas rebate
*/
function redeemSetForExactERC20(
ISetToken _setToken,
uint256 _maxSetAmount,
address _outputToken,
uint256 _outputTokenAmount,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataCollateralForOutputToken,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataOutputTokenForCollateral,
DEXAdapter.SwapData memory _swapDataOutputTokenForETH,
uint256 _priceEstimateInflator,
uint256 _maxDust
)
external
nonReentrant
returns(uint256 setAmount)
{
uint256 setBalanceBefore = _setToken.balanceOf(msg.sender);
uint256 outputTokenBalanceBefore = IERC20(_outputToken).balanceOf(address(this));
_initiateRedemption(
_setToken,
_maxSetAmount,
_outputToken,
_outputTokenAmount,
_swapDataCollateralForDebt,
_swapDataCollateralForOutputToken
);
_issueSetFromExcessOutput(
_setToken,
_maxSetAmount,
_outputToken,
_outputTokenAmount,
_swapDataDebtForCollateral,
_swapDataOutputTokenForCollateral,
_priceEstimateInflator,
_maxDust,
outputTokenBalanceBefore
);
_sendOutputTokenAndETHToUser(_outputToken, outputTokenBalanceBefore, _outputTokenAmount, _swapDataOutputTokenForETH);
return setBalanceBefore.sub(_setToken.balanceOf(msg.sender));
}
/**
* Redeems a variable amount of setTokens to return exactly the specified amount of ETH to the user
*
* @param _setToken Set token to redeem
* @param _maxSetAmount Maximum amout of set tokens to redeem
* @param _outputTokenAmount Amount of eth to return to the user
* @param _swapDataCollateralForDebt Data (token path and fee levels) describing the swap from Collateral Token to Debt Token
* @param _swapDataCollateralForOutputToken Data (token path and fee levels) describing the swap from Collateral Token to eth
* @param _swapDataDebtForCollateral Data (token path and fee levels) describing the swap from Debt Token to Collateral Token
* @param _swapDataOutputTokenForCollateral Data (token path and fee levels) describing the swap from eth to Collateral Token
* @param _priceEstimateInflator Factor by which to increase the estimated price from the previous iteration to account for used up liquidity
* @param _maxDust Minimum accuracy for approximating the eth amount. Excess will be swapped to eth and returned to user as gas rebate
*/
function redeemSetForExactETH(
ISetToken _setToken,
uint256 _maxSetAmount,
uint256 _outputTokenAmount,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataCollateralForOutputToken,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataOutputTokenForCollateral,
uint256 _priceEstimateInflator,
uint256 _maxDust
)
external
nonReentrant
returns(uint256)
{
uint256 wethBalanceBefore = IERC20(addresses.weth).balanceOf(address(this));
uint256 setBalanceBefore = _setToken.balanceOf(msg.sender);
_initiateRedemption(
_setToken,
_maxSetAmount,
DEXAdapter.ETH_ADDRESS,
_outputTokenAmount,
_swapDataCollateralForDebt,
_swapDataCollateralForOutputToken
);
_issueSetFromExcessOutput(
_setToken,
_maxSetAmount,
DEXAdapter.ETH_ADDRESS,
_outputTokenAmount,
_swapDataDebtForCollateral,
_swapDataOutputTokenForCollateral,
_priceEstimateInflator,
_maxDust,
wethBalanceBefore
);
uint256 wethObtained = IERC20(addresses.weth).balanceOf(address(this)).sub(wethBalanceBefore);
require(wethObtained >= _outputTokenAmount, "IWO");
require(wethObtained - _outputTokenAmount <= maxGasRebate, "MGR");
IWETH(addresses.weth).withdraw(wethObtained);
(payable(msg.sender)).sendValue(wethObtained);
return setBalanceBefore.sub(_setToken.balanceOf(msg.sender));
}
/**
* Trigger redemption of set token to pay the user with Eth
*
* @param _setToken Set token to redeem
* @param _setAmount Amount to redeem
* @param _minAmountOutputToken Minimum amount of ETH to send to the user
* @param _swapDataCollateralForDebt Data (token path and fee levels) describing the swap from Collateral Token to Debt Token
* @param _swapDataOutputToken Data (token path and fee levels) describing the swap from Collateral Token to Eth
*/
function redeemExactSetForETH(
ISetToken _setToken,
uint256 _setAmount,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataOutputToken
)
external
override
nonReentrant
{
uint256 wethBalanceBefore = IWETH(addresses.weth).balanceOf(address(this));
_initiateRedemption(
_setToken,
_setAmount,
DEXAdapter.ETH_ADDRESS,
_minAmountOutputToken,
_swapDataCollateralForDebt,
_swapDataOutputToken
);
uint256 amountToReturn = IWETH(addresses.weth).balanceOf(address(this)).sub(wethBalanceBefore);
IWETH(addresses.weth).withdraw(amountToReturn);
(payable(msg.sender)).sendValue(amountToReturn);
}
/**
* Trigger redemption of set token to pay the user with an arbitrary ERC20
*
* @param _setToken Set token to redeem
* @param _setAmount Amount to redeem
* @param _outputToken Address of the ERC20 token to send to the user
* @param _minAmountOutputToken Minimum amount of output token to send to the user
* @param _swapDataCollateralForDebt Data (token path and fee levels) describing the swap from Collateral Token to Debt Token
* @param _swapDataOutputToken Data (token path and fee levels) describing the swap from Collateral Token to Output token
*/
function redeemExactSetForERC20(
ISetToken _setToken,
uint256 _setAmount,
address _outputToken,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapDataCollateralForDebt,
DEXAdapter.SwapData memory _swapDataOutputToken
)
external
override
nonReentrant
{
uint256 outputTokenBalanceBefore = IERC20(_outputToken).balanceOf(address(this));
_initiateRedemption(
_setToken,
_setAmount,
_outputToken,
_minAmountOutputToken,
_swapDataCollateralForDebt,
_swapDataOutputToken
);
uint256 outputTokenAmount = IERC20(_outputToken).balanceOf(address(this)).sub(outputTokenBalanceBefore);
IERC20(_outputToken).transfer(msg.sender, outputTokenAmount);
}
/**
* Trigger issuance of set token paying with any arbitrary ERC20 token
*
* @param _setToken Set token to issue
* @param _setAmount Amount to issue
* @param _inputToken Input token to pay with
* @param _maxAmountInputToken Maximum amount of input token to spend
* @param _swapDataDebtForCollateral Data (token addresses and fee levels) to describe the swap path from Debt to collateral token
* @param _swapDataInputToken Data (token addresses and fee levels) to describe the swap path from input to collateral token
*/
function issueExactSetFromERC20(
ISetToken _setToken,
uint256 _setAmount,
address _inputToken,
uint256 _maxAmountInputToken,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken
)
external
override
nonReentrant
{
uint256 inputTokenBalanceBefore = IERC20(_inputToken).balanceOf(address(this));
IERC20(_inputToken).transferFrom(msg.sender, address(this), _maxAmountInputToken);
_initiateIssuance(
_setToken,
_setAmount,
_inputToken,
_maxAmountInputToken,
_swapDataDebtForCollateral,
_swapDataInputToken
);
uint256 amountToReturn = IERC20(_inputToken).balanceOf(address(this)).sub(inputTokenBalanceBefore);
IERC20(_inputToken).transfer(msg.sender, amountToReturn);
}
/**
* Trigger issuance of set token paying with Eth
*
* @param _setToken Set token to issue
* @param _setAmount Amount to issue
* @param _swapDataDebtForCollateral Data (token addresses and fee levels) to describe the swap path from Debt to collateral token
* @param _swapDataInputToken Data (token addresses and fee levels) to describe the swap path from eth to collateral token
*/
function issueExactSetFromETH(
ISetToken _setToken,
uint256 _setAmount,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken
)
external
override
payable
nonReentrant
{
uint256 inputTokenBalanceBefore = IERC20(addresses.weth).balanceOf(address(this));
IWETH(addresses.weth).deposit{value: msg.value}();
_initiateIssuance(
_setToken,
_setAmount,
DEXAdapter.ETH_ADDRESS,
msg.value,
_swapDataDebtForCollateral,
_swapDataInputToken
);
uint256 amountToReturn = IERC20(addresses.weth).balanceOf(address(this)).sub(inputTokenBalanceBefore);
IWETH(addresses.weth).withdraw(amountToReturn);
msg.sender.transfer(amountToReturn);
}
/**
* Issues a variable amount of set tokens for a fixed amount of input tokenss
*
* @param _setToken Set token to redeem
* @param _minSetAmount Minimum amount of Set Tokens to issue
* @param _inputToken Address of input token for which to issue set tokens
* @param _inputTokenAmount Amount of inputToken to return to the user
* @param _swapDataDebtForCollateral Data (token path and fee levels) describing the swap from Debt Token to Collateral Token
* @param _swapDataInputTokenForCollateral Data (token path and fee levels) describing the swap from input token to Collateral Token
* @param _swapDataInputTokenForETH Data (token path and fee levels) describing the swap from unspent input token to ETH, to use as gas rebate
* @param _priceEstimateInflator Factor by which to increase the estimated price from the previous iteration to account for used up liquidity
* @param _maxDust Minimum accuracy for approximating the input token amount. Excess will be swapped to input token and returned to user as gas rebate
*/
function issueSetFromExactERC20(
ISetToken _setToken,
uint256 _minSetAmount,
address _inputToken,
uint256 _inputTokenAmount,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputTokenForCollateral,
DEXAdapter.SwapData memory _swapDataInputTokenForETH,
uint256 _priceEstimateInflator,
uint256 _maxDust
)
external
nonReentrant
returns(uint256)
{
uint256 setBalanceBefore = _setToken.balanceOf(msg.sender);
IERC20(_inputToken).transferFrom(msg.sender, address(this), _inputTokenAmount);
uint256 inputAmountLeft = _issueSetFromExactInput(
_setToken,
_minSetAmount,
_inputToken,
_inputTokenAmount,
_swapDataDebtForCollateral,
_swapDataInputTokenForCollateral,
_priceEstimateInflator,
_maxDust
);
_swapTokenForETHAndReturnToUser(_inputToken, inputAmountLeft, _swapDataInputTokenForETH);
return _setToken.balanceOf(msg.sender).sub(setBalanceBefore);
}
/**
* Issues a variable amount of set tokens for a fixed amount of eth
*
* @param _setToken Set token to redeem
* @param _minSetAmount Minimum amount of Set Tokens to issue
* @param _swapDataDebtForCollateral Data (token path and fee levels) describing the swap from Debt Token to Collateral Token
* @param _swapDataInputTokenForCollateral Data (token path and fee levels) describing the swap from eth to Collateral Token
* @param _priceEstimateInflator Factor by which to increase the estimated price from the previous iteration to account for used up liquidity
* @param _maxDust Minimum accuracy for approximating the eth amount. Excess will be swapped to eth and returned to user as gas rebate
*/
function issueSetFromExactETH(
ISetToken _setToken,
uint256 _minSetAmount,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputTokenForCollateral,
uint256 _priceEstimateInflator,
uint256 _maxDust
)
external
payable
nonReentrant
returns(uint256)
{
uint256 setBalanceBefore = _setToken.balanceOf(msg.sender);
IWETH(addresses.weth).deposit{value: msg.value}();
uint256 inputTokenLeft = _issueSetFromExactInput(
_setToken,
_minSetAmount,
DEXAdapter.ETH_ADDRESS,
msg.value,
_swapDataDebtForCollateral,
_swapDataInputTokenForCollateral,
_priceEstimateInflator,
_maxDust
);
IWETH(addresses.weth).withdraw(inputTokenLeft);
msg.sender.transfer(inputTokenLeft);
return _setToken.balanceOf(msg.sender).sub(setBalanceBefore);
}
/**
* Update maximum number of iterations allowed in the fixed input issuance / fixed output redemption
*
* @param _maxIterations New value to set for maximum number of iterations. If "maxDust" is not met by that iteration the respective transaction will fail
*/
function setMaxIterations(uint256 _maxIterations) external onlyOwner {
maxIterations = _maxIterations;
}
/**
* Update maximum value of gas rebate returned to user in fixed input issuance / fixed output redemption
*
* @param _maxGasRebate New value to set for max gas rebate. Gas rebate above this value is assumed to be a misconfiguration and the respective transaction will fail
*/
function setMaxGasRebate(uint256 _maxGasRebate) external onlyOwner {
maxGasRebate = _maxGasRebate;
}
/* ============ Internal Functions ============ */
// @dev Use excess amout of output token to re-issue set tokens
function _issueSetFromExcessOutput(
ISetToken _setToken,
uint256 _maxSetAmount,
address _outputToken,
uint256 _outputTokenAmount,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken,
uint256 _priceEstimateInflator,
uint256 _maxDust,
uint256 _outputTokenBalanceBefore
)
internal
{
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(_priceEstimateInflator).div(obtainedOutputAmount);
uint256 minSetAmount = excessOutputTokenAmount.mul(priceEstimate).div(1 ether);
_issueSetFromExactInput(
_setToken,
minSetAmount,
_outputToken,
excessOutputTokenAmount,
_swapDataDebtForCollateral,
_swapDataInputToken,
_priceEstimateInflator,
_maxDust
);
}
// @dev Send requested amount of Output tokens to user, sell excess for eth and send to user as gas rebate
function _sendOutputTokenAndETHToUser(
address _outputToken,
uint256 _outputTokenBalanceBefore,
uint256 _outputTokenAmount,
DEXAdapter.SwapData memory _swapDataOutputTokenForETH
)
internal
{
uint256 outputTokenObtained = IERC20(_outputToken).balanceOf(address(this)).sub(_outputTokenBalanceBefore);
require(outputTokenObtained >= _outputTokenAmount, "IOTO");
IERC20(_outputToken).transfer(msg.sender, _outputTokenAmount);
_swapTokenForETHAndReturnToUser(_outputToken, outputTokenObtained - _outputTokenAmount, _swapDataOutputTokenForETH);
}
// @dev Issue Set Tokens for (approximately) the requested amount of input tokens. Works by first issuing minimum requested amount of set tokens and then iteratively using observed exchange rate on previous issuance to spend the remaining input tokens until the difference is les than the specivied _maxDust
function _issueSetFromExactInput(
ISetToken _setToken,
uint256 _minSetAmount,
address _inputToken,
uint256 _inputTokenAmount,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken,
uint256 _priceEstimateInflator,
uint256 _maxDust
)
internal
returns(uint256)
{
require(_inputTokenAmount > _maxDust, "MD");
uint256 iterations = 0;
while (_inputTokenAmount > _maxDust) {
require(iterations < maxIterations, "MI");
uint256 inputTokenAmountSpent = _initiateIssuanceAndReturnInputAmountSpent(
_setToken,
_minSetAmount,
_inputToken,
_inputTokenAmount,
_swapDataDebtForCollateral,
_swapDataInputToken
);
// Update remaining inputTokens left to be spent
_inputTokenAmount = _inputTokenAmount - inputTokenAmountSpent;
// Estimate price of setToken / inputToken, multiplying by provided factor to account for used up liquidity
uint256 priceEstimate = _minSetAmount.mul(_priceEstimateInflator).div(inputTokenAmountSpent);
// Amount to issue in next iteration is equal to the left over amount of input tokens times the price estimate from the previous step
_minSetAmount = _inputTokenAmount.mul(priceEstimate).div(1 ether);
iterations++;
}
return _inputTokenAmount;
}
// @dev Extends original _initiateIssuance by returning the amount of input tokens that was spent in issuance. (requisite for approximation algorithm above
function _initiateIssuanceAndReturnInputAmountSpent(
ISetToken _setToken,
uint256 _minSetAmount,
address _inputToken,
uint256 _inputTokenAmount,
DEXAdapter.SwapData memory _swapDataDebtForCollateral,
DEXAdapter.SwapData memory _swapDataInputToken
)
internal
returns (uint256)
{
uint256 inputTokenBalanceBefore;
if( _inputToken == DEXAdapter.ETH_ADDRESS) {
inputTokenBalanceBefore = IERC20(addresses.weth).balanceOf(address(this));
} else {
inputTokenBalanceBefore = IERC20(_inputToken).balanceOf(address(this));
}
_initiateIssuance(
_setToken,
_minSetAmount,
_inputToken,
_inputTokenAmount,
_swapDataDebtForCollateral,
_swapDataInputToken
);
uint256 inputTokenBalanceAfter;
if( _inputToken == DEXAdapter.ETH_ADDRESS) {
inputTokenBalanceAfter = IERC20(addresses.weth).balanceOf(address(this));
} else {
inputTokenBalanceAfter = IERC20(_inputToken).balanceOf(address(this));
}
return inputTokenBalanceBefore.sub(inputTokenBalanceAfter);
}
/**
* @dev Same as in FlashMintLeveraged but without transfering input token from the user (since this is now done once at the very beginning to avoid transfering multiple times back and forth)
*/
function _makeUpShortfallWithERC20(
address _collateralToken,
uint256 _collateralTokenShortfall,
address _originalSender,
IERC20 _inputToken,
uint256 _maxAmountInputToken,
DEXAdapter.SwapData memory _swapData
)
internal
override
returns (uint256)
{
if(address(_inputToken) == _collateralToken){
return _collateralTokenShortfall;
} else {
uint256 amountInputToken = _swapInputForCollateralToken(
_collateralToken,
_collateralTokenShortfall,
address(_inputToken),
_maxAmountInputToken,
_swapData
);
return amountInputToken;
}
}
/**
* @dev Same as in FlashMintLeveraged but without transfering eth from the user (since this is now done once at the very beginning to avoid transfering multiple times back and forth)
*/
function _makeUpShortfallWithETH(
address _collateralToken,
uint256 _collateralTokenShortfall,
address _originalSender,
uint256 _maxAmountEth,
DEXAdapter.SwapData memory _swapData
)
internal
override
returns(uint256)
{
uint256 amountEth = _swapInputForCollateralToken(
_collateralToken,
_collateralTokenShortfall,
addresses.weth,
_maxAmountEth,
_swapData
);
return amountEth;
}
/**
* @dev Same as in FlashMintLeveraged but without transfering output tokens to the user (since this is now done once at the very end to avoid transfering multiple times back and forth)
*/
function _liquidateCollateralTokensForERC20(
address _collateralToken,
uint256 _collateralRemaining,
address _originalSender,
IERC20 _outputToken,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapData
)
internal
override
returns (uint256)
{
if(address(_outputToken) == _collateralToken){
return _collateralRemaining;
}
uint256 outputTokenAmount = _swapCollateralForOutputToken(
_collateralToken,
_collateralRemaining,
address(_outputToken),
_minAmountOutputToken,
_swapData
);
return outputTokenAmount;
}
/**
* @dev Same as in FlashMintLeveraged but without transfering eth to the user (since this is now done once at the very end to avoid transfering multiple times back and forth)
*/
function _liquidateCollateralTokensForETH(
address _collateralToken,
uint256 _collateralRemaining,
address _originalSender,
uint256 _minAmountOutputToken,
DEXAdapter.SwapData memory _swapData
)
internal
override
isValidPath(_swapData.path, _collateralToken, addresses.weth)
returns(uint256)
{
uint256 ethAmount = _swapCollateralForOutputToken(
_collateralToken,
_collateralRemaining,
addresses.weth,
_minAmountOutputToken,
_swapData
);
return ethAmount;
}
/**
* @dev Used to swap excess input / output tokens for eth and return to user as gas rebate
*/
function _swapTokenForETHAndReturnToUser(
address _inputToken,
uint256 _inputAmount,
DEXAdapter.SwapData memory _swapData
)
internal
{
uint256 ethObtained;
if(_inputToken == addresses.weth) {
ethObtained = _inputAmount;
} else {
// Setting path to empty array means opting out of the gas rebate swap
if(_swapData.path.length == 0) {
return;
}
require(_swapData.path[0] == _inputToken, "ITNF");
require(_swapData.path[_swapData.path.length - 1] == addresses.weth, "FlashMintLeveragedExtended: WETH not last in path");
ethObtained = addresses.swapExactTokensForTokens(
_inputAmount,
0,
_swapData
);
}
require(ethObtained <= maxGasRebate, "MGR");
IWETH(addresses.weth).withdraw(ethObtained);
msg.sender.transfer(ethObtained);
}
}
pragma solidity 0.6.10;
interface IAToken {
event Approval(address indexed owner, address indexed spender, uint256 value);
event BalanceTransfer(address indexed from, address indexed to, uint256 value, uint256 index);
event Burn(address indexed from, address indexed target, uint256 value, uint256 balanceIncrease, uint256 index);
event Initialized(
address indexed underlyingAsset,
address indexed pool,
address treasury,
address incentivesController,
uint8 aTokenDecimals,
string aTokenName,
string aTokenSymbol,
bytes params
);
event Mint(
address indexed caller, address indexed onBehalfOf, uint256 value, uint256 balanceIncrease, uint256 index
);
event Transfer(address indexed from, address indexed to, uint256 value);
function ATOKEN_REVISION() external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
function EIP712_REVISION() external view returns (bytes memory);
function PERMIT_TYPEHASH() external view returns (bytes32);
function POOL() external view returns (address);
function RESERVE_TREASURY_ADDRESS() external view returns (address);
function UNDERLYING_ASSET_ADDRESS() external view returns (address);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function balanceOf(address user) external view returns (uint256);
function burn(address from, address receiverOfUnderlying, uint256 amount, uint256 index) external;
function decimals() external view returns (uint8);
function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool);
function getIncentivesController() external view returns (address);
function getPreviousIndex(address user) external view returns (uint256);
function getScaledUserBalanceAndSupply(address user) external view returns (uint256, uint256);
function handleRepayment(address user, address onBehalfOf, uint256 amount) external;
function increaseAllowance(address spender, uint256 addedValue) external returns (bool);
function initialize(
address initializingPool,
address treasury,
address underlyingAsset,
address incentivesController,
uint8 aTokenDecimals,
string memory aTokenName,
string memory aTokenSymbol,
bytes memory params
) external;
function mint(address caller, address onBehalfOf, uint256 amount, uint256 index) external returns (bool);
function mintToTreasury(uint256 amount, uint256 index) external;
function name() external view returns (string memory);
function nonces(address owner) external view returns (uint256);
function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
external;
function rescueTokens(address token, address to, uint256 amount) external;
function scaledBalanceOf(address user) external view returns (uint256);
function scaledTotalSupply() external view returns (uint256);
function setIncentivesController(address controller) external;
function symbol() external view returns (string memory);
function totalSupply() external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function transferOnLiquidation(address from, address to, uint256 value) external;
function transferUnderlyingTo(address target, uint256 amount) external;
}
/*
Copyright 2021 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
import { ISetToken } from "./ISetToken.sol";
interface IAaveLeverageModule {
function sync(ISetToken _setToken) external virtual;
}
/*
Copyright 2020 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
interface IController {
function addSet(address _setToken) external;
function feeRecipient() external view returns(address);
function getModuleFee(address _module, uint256 _feeType) external view returns(uint256);
function isModule(address _module) external view returns(bool);
function isSet(address _setToken) external view returns(bool);
function isSystemContract(address _contractAddress) external view returns (bool);
function resourceId(uint256 _id) external view returns(address);
function owner() external view returns(address);
function addFactory(address _factory) external;
function addModule(address _module) external;
}
/*
Copyright 2022 Index Cooperative
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
// Implementation: https://etherscan.io/address/0x0000000022d53366457f9d5e68ec105046fc4383#readContract
interface ICurveAddressProvider {
function get_registry() external view returns(address);
function get_address(uint256 _id) external view returns(address);
}
/*
Copyright 2022 Index Cooperative
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
// Implementation: https://etherscan.io/address/0xc1DB00a8E5Ef7bfa476395cdbcc98235477cDE4E#readContract
interface ICurveCalculator {
function get_dx(
int128 n_coins,
uint256[8] memory balances,
uint256 amp,
uint256 fee,
uint256[8] memory rates,
uint256[8] memory precisions,
bool underlying,
int128 i,
int128 j,
uint256 dy
) external view returns(uint256);
function get_dy(
int128 n_coins,
uint256[8] memory balances,
uint256 amp,
uint256 fee,
uint256[8] memory rates,
uint256[8] memory precisions,
bool underlying,
int128 i,
int128 j,
uint256 dx
) external view returns(uint256);
}
/*
Copyright 2022 Index Cooperative
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
// Implementation: https://etherscan.io/address/0x8e764bE4288B842791989DB5b8ec067279829809#writeContract
interface ICurvePool {
function exchange(
int128 i,
int128 j,
uint256 dx,
uint256 min_dy
) external payable returns (uint256);
function get_dy(
int128 i,
int128 j,
uint256 dx
) external view returns (uint256);
}
/*
Copyright 2022 Index Cooperative
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
// Implementation: https://etherscan.io/address/0x90E00ACe148ca3b23Ac1bC8C240C2a7Dd9c2d7f5#readContract
interface ICurvePoolRegistry {
// amplification factor
function get_A(address _pool) external view returns(uint256);
function get_balances(address _pool) external view returns(uint256[8] memory);
function get_coins(address _pool) external view returns(address[8] memory);
function get_coin_indices(address _pool, address _from, address _to) external view returns(int128, int128, bool);
function get_decimals(address _pool) external view returns(uint256[8] memory);
function get_n_coins(address _pool) external view returns(uint256[2] memory);
function get_fees(address _pool) external view returns(uint256[2] memory);
function get_rates(address _pool) external view returns(uint256[8] memory);
}
/*
Copyright 2020 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity >=0.6.10;
import { ISetToken } from "./ISetToken.sol";
import { IManagerIssuanceHook } from "./IManagerIssuanceHook.sol";
interface IDebtIssuanceModule {
function getRequiredComponentIssuanceUnits(
ISetToken _setToken,
uint256 _quantity
) external view returns (address[] memory, uint256[] memory, uint256[] memory);
function getRequiredComponentRedemptionUnits(
ISetToken _setToken,
uint256 _quantity
) external view returns (address[] memory, uint256[] memory, uint256[] memory);
function issue(ISetToken _setToken, uint256 _quantity, address _to) external;
function redeem(ISetToken _token, uint256 _quantity, address _to) external;
function initialize(
ISetToken _setToken,
uint256 _maxManagerFee,
uint256 _managerIssueFee,
uint256 _managerRedeemFee,
address _feeRecipient,
IManagerIssuanceHook _managerIssuanceHook
) external;
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.10;
import { ILendingPoolAddressesProviderV2 } from "./ILendingPoolAddressesProviderV2.sol";
import { ILendingPoolV2 } from "./ILendingPoolV2.sol";
/**
* @title IFlashLoanReceiverV2 interface
* @notice Interface for the Aave fee IFlashLoanReceiver.
* @author Aave
* @dev implement this interface to develop a flashloan-compatible flashLoanReceiver contract
**/
interface IFlashLoanReceiverV2 {
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
) external returns (bool);
function ADDRESSES_PROVIDER() external view returns (ILendingPoolAddressesProviderV2);
function LENDING_POOL() external view returns (ILendingPoolV2);
}
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.10;
/**
* @title LendingPoolAddressesProvider contract
* @dev Main registry of addresses part of or connected to the protocol, including permissioned roles
* - Acting also as factory of proxies and admin of those, so with right to change its implementations
* - Owned by the Aave Governance
* @author Aave
**/
interface ILendingPoolAddressesProviderV2 {
event MarketIdSet(string newMarketId);
event LendingPoolUpdated(address indexed newAddress);
event ConfigurationAdminUpdated(address indexed newAddress);
event EmergencyAdminUpdated(address indexed newAddress);
event LendingPoolConfiguratorUpdated(address indexed newAddress);
event LendingPoolCollateralManagerUpdated(address indexed newAddress);
event PriceOracleUpdated(address indexed newAddress);
event LendingRateOracleUpdated(address indexed newAddress);
event ProxyCreated(bytes32 id, address indexed newAddress);
event AddressSet(bytes32 id, address indexed newAddress, bool hasProxy);
function getMarketId() external view returns (string memory);
function setMarketId(string calldata marketId) external;
function setAddress(bytes32 id, address newAddress) external;
function setAddressAsProxy(bytes32 id, address impl) external;
function getAddress(bytes32 id) external view returns (address);
function getLendingPool() external view returns (address);
function setLendingPoolImpl(address pool) external;
function getLendingPoolConfigurator() external view returns (address);
function setLendingPoolConfiguratorImpl(address configurator) external;
function getLendingPoolCollateralManager() external view returns (address);
function setLendingPoolCollateralManager(address manager) external;
function getPoolAdmin() external view returns (address);
function setPoolAdmin(address admin) external;
function getEmergencyAdmin() external view returns (address);
function setEmergencyAdmin(address admin) external;
function getPriceOracle() external view returns (address);
function setPriceOracle(address priceOracle) external;
function getLendingRateOracle() external view returns (address);
function setLendingRateOracle(address lendingRateOracle) external;
}
// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.6.10;
pragma experimental ABIEncoderV2;
import {ILendingPoolAddressesProviderV2} from "./ILendingPoolAddressesProviderV2.sol";
import {DataTypes} from "../../external/contracts/aaveV2/lib/DataTypes.sol";
interface ILendingPoolV2 {
/**
* @dev Emitted on deposit()
* @param reserve The address of the underlying asset of the reserve
* @param user The address initiating the deposit
* @param onBehalfOf The beneficiary of the deposit, receiving the aTokens
* @param amount The amount deposited
* @param referral The referral code used
**/
event Deposit(
address indexed reserve,
address user,
address indexed onBehalfOf,
uint256 amount,
uint16 indexed referral
);
/**
* @dev Emitted on withdraw()
* @param reserve The address of the underlyng asset being withdrawn
* @param user The address initiating the withdrawal, owner of aTokens
* @param to Address that will receive the underlying
* @param amount The amount to be withdrawn
**/
event Withdraw(address indexed reserve, address indexed user, address indexed to, uint256 amount);
/**
* @dev Emitted on borrow() and flashLoan() when debt needs to be opened
* @param reserve The address of the underlying asset being borrowed
* @param user The address of the user initiating the borrow(), receiving the funds on borrow() or just
* initiator of the transaction on flashLoan()
* @param onBehalfOf The address that will be getting the debt
* @param amount The amount borrowed out
* @param borrowRateMode The rate mode: 1 for Stable, 2 for Variable
* @param borrowRate The numeric rate at which the user has borrowed
* @param referral The referral code used
**/
event Borrow(
address indexed reserve,
address user,
address indexed onBehalfOf,
uint256 amount,
uint256 borrowRateMode,
uint256 borrowRate,
uint16 indexed referral
);
/**
* @dev Emitted on repay()
* @param reserve The address of the underlying asset of the reserve
* @param user The beneficiary of the repayment, getting his debt reduced
* @param repayer The address of the user initiating the repay(), providing the funds
* @param amount The amount repaid
**/
event Repay(
address indexed reserve,
address indexed user,
address indexed repayer,
uint256 amount
);
/**
* @dev Emitted on swapBorrowRateMode()
* @param reserve The address of the underlying asset of the reserve
* @param user The address of the user swapping his rate mode
* @param rateMode The rate mode that the user wants to swap to
**/
event Swap(address indexed reserve, address indexed user, uint256 rateMode);
/**
* @dev Emitted on setUserUseReserveAsCollateral()
* @param reserve The address of the underlying asset of the reserve
* @param user The address of the user enabling the usage as collateral
**/
event ReserveUsedAsCollateralEnabled(address indexed reserve, address indexed user);
/**
* @dev Emitted on setUserUseReserveAsCollateral()
* @param reserve The address of the underlying asset of the reserve
* @param user The address of the user enabling the usage as collateral
**/
event ReserveUsedAsCollateralDisabled(address indexed reserve, address indexed user);
/**
* @dev Emitted on rebalanceStableBorrowRate()
* @param reserve The address of the underlying asset of the reserve
* @param user The address of the user for which the rebalance has been executed
**/
event RebalanceStableBorrowRate(address indexed reserve, address indexed user);
/**
* @dev Emitted on flashLoan()
* @param target The address of the flash loan receiver contract
* @param initiator The address initiating the flash loan
* @param asset The address of the asset being flash borrowed
* @param amount The amount flash borrowed
* @param premium The fee flash borrowed
* @param referralCode The referral code used
**/
event FlashLoan(
address indexed target,
address indexed initiator,
address indexed asset,
uint256 amount,
uint256 premium,
uint16 referralCode
);
/**
* @dev Emitted when the pause is triggered.
*/
event Paused();
/**
* @dev Emitted when the pause is lifted.
*/
event Unpaused();
/**
* @dev Emitted when a borrower is liquidated. This event is emitted by the LendingPool via
* LendingPoolCollateral manager using a DELEGATECALL
* This allows to have the events in the generated ABI for LendingPool.
* @param collateralAsset The address of the underlying asset used as collateral, to receive as result of the liquidation
* @param debtAsset The address of the underlying borrowed asset to be repaid with the liquidation
* @param user The address of the borrower getting liquidated
* @param debtToCover The debt amount of borrowed `asset` the liquidator wants to cover
* @param liquidatedCollateralAmount The amount of collateral received by the liiquidator
* @param liquidator The address of the liquidator
* @param receiveAToken `true` if the liquidators wants to receive the collateral aTokens, `false` if he wants
* to receive the underlying collateral asset directly
**/
event LiquidationCall(
address indexed collateralAsset,
address indexed debtAsset,
address indexed user,
uint256 debtToCover,
uint256 liquidatedCollateralAmount,
address liquidator,
bool receiveAToken
);
/**
* @dev Emitted when the state of a reserve is updated. NOTE: This event is actually declared
* in the ReserveLogic library and emitted in the updateInterestRates() function. Since the function is internal,
* the event will actually be fired by the LendingPool contract. The event is therefore replicated here so it
* gets added to the LendingPool ABI
* @param reserve The address of the underlying asset of the reserve
* @param liquidityRate The new liquidity rate
* @param stableBorrowRate The new stable borrow rate
* @param variableBorrowRate The new variable borrow rate
* @param liquidityIndex The new liquidity index
* @param variableBorrowIndex The new variable borrow index
**/
event ReserveDataUpdated(
address indexed reserve,
uint256 liquidityRate,
uint256 stableBorrowRate,
uint256 variableBorrowRate,
uint256 liquidityIndex,
uint256 variableBorrowIndex
);
/**
* @dev Deposits an `amount` of underlying asset into the reserve, receiving in return overlying aTokens.
* - E.g. User deposits 100 USDC and gets in return 100 aUSDC
* @param asset The address of the underlying asset to deposit
* @param amount The amount to be deposited
* @param onBehalfOf The address that will receive the aTokens, same as msg.sender if the user
* wants to receive them on his own wallet, or a different address if the beneficiary of aTokens
* is a different wallet
* @param referralCode Code used to register the integrator originating the operation, for potential rewards.
* 0 if the action is executed directly by the user, without any middle-man
**/
function deposit(
address asset,
uint256 amount,
address onBehalfOf,
uint16 referralCode
) external;
/**
* @dev Withdraws an `amount` of underlying asset from the reserve, burning the equivalent aTokens owned
* E.g. User has 100 aUSDC, calls withdraw() and receives 100 USDC, burning the 100 aUSDC
* @param asset The address of the underlying asset to withdraw
* @param amount The underlying amount to be withdrawn
* - Send the value type(uint256).max in order to withdraw the whole aToken balance
* @param to Address that will receive the underlying, same as msg.sender if the user
* wants to receive it on his own wallet, or a different address if the beneficiary is a
* different wallet
* @return The final amount withdrawn
**/
function withdraw(
address asset,
uint256 amount,
address to
) external returns (uint256);
/**
* @dev Allows users to borrow a specific `amount` of the reserve underlying asset, provided that the borrower
* already deposited enough collateral, or he was given enough allowance by a credit delegator on the
* corresponding debt token (StableDebtToken or VariableDebtToken)
* - E.g. User borrows 100 USDC passing as `onBehalfOf` his own address, receiving the 100 USDC in his wallet
* and 100 stable/variable debt tokens, depending on the `interestRateMode`
* @param asset The address of the underlying asset to borrow
* @param amount The amount to be borrowed
* @param interestRateMode The interest rate mode at which the user wants to borrow: 1 for Stable, 2 for Variable
* @param referralCode Code used to register the integrator originating the operation, for potential rewards.
* 0 if the action is executed directly by the user, without any middle-man
* @param onBehalfOf Address of the user who will receive the debt. Should be the address of the borrower itself
* calling the function if he wants to borrow against his own collateral, or the address of the credit delegator
* if he has been given credit delegation allowance
**/
function borrow(
address asset,
uint256 amount,
uint256 interestRateMode,
uint16 referralCode,
address onBehalfOf
) external;
/**
* @notice Repays a borrowed `amount` on a specific reserve, burning the equivalent debt tokens owned
* - E.g. User repays 100 USDC, burning 100 variable/stable debt tokens of the `onBehalfOf` address
* @param asset The address of the borrowed underlying asset previously borrowed
* @param amount The amount to repay
* - Send the value type(uint256).max in order to repay the whole debt for `asset` on the specific `debtMode`
* @param rateMode The interest rate mode at of the debt the user wants to repay: 1 for Stable, 2 for Variable
* @param onBehalfOf Address of the user who will get his debt reduced/removed. Should be the address of the
* user calling the function if he wants to reduce/remove his own debt, or the address of any other
* other borrower whose debt should be removed
* @return The final amount repaid
**/
function repay(
address asset,
uint256 amount,
uint256 rateMode,
address onBehalfOf
) external returns (uint256);
/**
* @dev Allows a borrower to swap his debt between stable and variable mode, or viceversa
* @param asset The address of the underlying asset borrowed
* @param rateMode The rate mode that the user wants to swap to
**/
function swapBorrowRateMode(address asset, uint256 rateMode) external;
/**
* @dev Rebalances the stable interest rate of a user to the current stable rate defined on the reserve.
* - Users can be rebalanced if the following conditions are satisfied:
* 1. Usage ratio is above 95%
* 2. the current deposit APY is below REBALANCE_UP_THRESHOLD * maxVariableBorrowRate, which means that too much has been
* borrowed at a stable rate and depositors are not earning enough
* @param asset The address of the underlying asset borrowed
* @param user The address of the user to be rebalanced
**/
function rebalanceStableBorrowRate(address asset, address user) external;
/**
* @dev Allows depositors to enable/disable a specific deposited asset as collateral
* @param asset The address of the underlying asset deposited
* @param useAsCollateral `true` if the user wants to use the deposit as collateral, `false` otherwise
**/
function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) external;
/**
* @dev Function to liquidate a non-healthy position collateral-wise, with Health Factor below 1
* - The caller (liquidator) covers `debtToCover` amount of debt of the user getting liquidated, and receives
* a proportionally amount of the `collateralAsset` plus a bonus to cover market risk
* @param collateralAsset The address of the underlying asset used as collateral, to receive as result of the liquidation
* @param debtAsset The address of the underlying borrowed asset to be repaid with the liquidation
* @param user The address of the borrower getting liquidated
* @param debtToCover The debt amount of borrowed `asset` the liquidator wants to cover
* @param receiveAToken `true` if the liquidators wants to receive the collateral aTokens, `false` if he wants
* to receive the underlying collateral asset directly
**/
function liquidationCall(
address collateralAsset,
address debtAsset,
address user,
uint256 debtToCover,
bool receiveAToken
) external;
/**
* @dev Allows smartcontracts to access the liquidity of the pool within one transaction,
* as long as the amount taken plus a fee is returned.
* IMPORTANT There are security concerns for developers of flashloan receiver contracts that must be kept into consideration.
* For further details please visit https://developers.aave.com
* @param receiverAddress The address of the contract receiving the funds, implementing the IFlashLoanReceiver interface
* @param assets The addresses of the assets being flash-borrowed
* @param amounts The amounts amounts being flash-borrowed
* @param modes Types of the debt to open if the flash loan is not returned:
* 0 -> Don't open any debt, just revert if funds can't be transferred from the receiver
* 1 -> Open debt at stable rate for the value of the amount flash-borrowed to the `onBehalfOf` address
* 2 -> Open debt at variable rate for the value of the amount flash-borrowed to the `onBehalfOf` address
* @param onBehalfOf The address that will receive the debt in the case of using on `modes` 1 or 2
* @param params Variadic packed params to pass to the receiver as extra information
* @param referralCode Code used to register the integrator originating the operation, for potential rewards.
* 0 if the action is executed directly by the user, without any middle-man
**/
function flashLoan(
address receiverAddress,
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata modes,
address onBehalfOf,
bytes calldata params,
uint16 referralCode
) external;
/**
* @dev Returns the user account data across all the reserves
* @param user The address of the user
* @return totalCollateralETH the total collateral in ETH of the user
* @return totalDebtETH the total debt in ETH of the user
* @return availableBorrowsETH the borrowing power left of the user
* @return currentLiquidationThreshold the liquidation threshold of the user
* @return ltv the loan to value of the user
* @return healthFactor the current health factor of the user
**/
function getUserAccountData(address user)
external
view
returns (
uint256 totalCollateralETH,
uint256 totalDebtETH,
uint256 availableBorrowsETH,
uint256 currentLiquidationThreshold,
uint256 ltv,
uint256 healthFactor
);
function initReserve(
address reserve,
address aTokenAddress,
address stableDebtAddress,
address variableDebtAddress,
address interestRateStrategyAddress
) external;
function setReserveInterestRateStrategyAddress(address reserve, address rateStrategyAddress)
external;
function setConfiguration(address reserve, uint256 configuration) external;
/**
* @dev Returns the configuration of the reserve
* @param asset The address of the underlying asset of the reserve
* @return The configuration of the reserve
**/
function getConfiguration(address asset)
external
view
returns (DataTypes.ReserveConfigurationMap memory);
/**
* @dev Returns the configuration of the user across all the reserves
* @param user The user address
* @return The configuration of the user
**/
function getUserConfiguration(address user)
external
view
returns (DataTypes.UserConfigurationMap memory);
/**
* @dev Returns the normalized income normalized income of the reserve
* @param asset The address of the underlying asset of the reserve
* @return The reserve's normalized income
*/
function getReserveNormalizedIncome(address asset) external view returns (uint256);
/**
* @dev Returns the normalized variable debt per unit of asset
* @param asset The address of the underlying asset of the reserve
* @return The reserve normalized variable debt
*/
function getReserveNormalizedVariableDebt(address asset) external view returns (uint256);
/**
* @dev Returns the state and configuration of the reserve
* @param asset The address of the underlying asset of the reserve
* @return The state of the reserve
**/
function getReserveData(address asset) external view returns (DataTypes.ReserveData memory);
function finalizeTransfer(
address asset,
address from,
address to,
uint256 amount,
uint256 balanceFromAfter,
uint256 balanceToBefore
) external;
function getReservesList() external view returns (address[] memory);
function getAddressesProvider() external view returns (ILendingPoolAddressesProviderV2);
function setPause(bool val) external;
function paused() external view returns (bool);
}
/*
Copyright 2020 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
import { ISetToken } from "./ISetToken.sol";
interface IManagerIssuanceHook {
function invokePreIssueHook(ISetToken _setToken, uint256 _issueQuantity, address _sender, address _to) external;
function invokePreRedeemHook(ISetToken _setToken, uint256 _redeemQuantity, address _sender, address _to) external;
}
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
import { DataTypes } from "./Datatypes.sol";
interface IPool {
event BackUnbacked(address indexed reserve, address indexed backer, uint256 amount, uint256 fee);
event Borrow(
address indexed reserve,
address user,
address indexed onBehalfOf,
uint256 amount,
uint8 interestRateMode,
uint256 borrowRate,
uint16 indexed referralCode
);
event FlashLoan(
address indexed target,
address initiator,
address indexed asset,
uint256 amount,
uint8 interestRateMode,
uint256 premium,
uint16 indexed referralCode
);
event IsolationModeTotalDebtUpdated(address indexed asset, uint256 totalDebt);
event LiquidationCall(
address indexed collateralAsset,
address indexed debtAsset,
address indexed user,
uint256 debtToCover,
uint256 liquidatedCollateralAmount,
address liquidator,
bool receiveAToken
);
event MintUnbacked(
address indexed reserve, address user, address indexed onBehalfOf, uint256 amount, uint16 indexed referralCode
);
event MintedToTreasury(address indexed reserve, uint256 amountMinted);
event RebalanceStableBorrowRate(address indexed reserve, address indexed user);
event Repay(
address indexed reserve, address indexed user, address indexed repayer, uint256 amount, bool useATokens
);
event ReserveDataUpdated(
address indexed reserve,
uint256 liquidityRate,
uint256 stableBorrowRate,
uint256 variableBorrowRate,
uint256 liquidityIndex,
uint256 variableBorrowIndex
);
event ReserveUsedAsCollateralDisabled(address indexed reserve, address indexed user);
event ReserveUsedAsCollateralEnabled(address indexed reserve, address indexed user);
event Supply(
address indexed reserve, address user, address indexed onBehalfOf, uint256 amount, uint16 indexed referralCode
);
event SwapBorrowRateMode(address indexed reserve, address indexed user, uint8 interestRateMode);
event UserEModeSet(address indexed user, uint8 categoryId);
event Withdraw(address indexed reserve, address indexed user, address indexed to, uint256 amount);
struct EModeCategory {
uint16 ltv;
uint16 liquidationThreshold;
uint16 liquidationBonus;
address priceSource;
string label;
}
struct ReserveConfigurationMap {
uint256 data;
}
struct ReserveData {
ReserveConfigurationMap configuration;
uint128 liquidityIndex;
uint128 currentLiquidityRate;
uint128 variableBorrowIndex;
uint128 currentVariableBorrowRate;
uint128 currentStableBorrowRate;
uint40 lastUpdateTimestamp;
uint16 id;
address aTokenAddress;
address stableDebtTokenAddress;
address variableDebtTokenAddress;
address interestRateStrategyAddress;
uint128 accruedToTreasury;
uint128 unbacked;
uint128 isolationModeTotalDebt;
}
struct UserConfigurationMap {
uint256 data;
}
function ADDRESSES_PROVIDER() external view returns (address);
function BRIDGE_PROTOCOL_FEE() external view returns (uint256);
function FLASHLOAN_PREMIUM_TOTAL() external view returns (uint128);
function FLASHLOAN_PREMIUM_TO_PROTOCOL() external view returns (uint128);
function MAX_NUMBER_RESERVES() external view returns (uint16);
function MAX_STABLE_RATE_BORROW_SIZE_PERCENT() external view returns (uint256);
function POOL_REVISION() external view returns (uint256);
function backUnbacked(address asset, uint256 amount, uint256 fee) external returns (uint256);
function borrow(address asset, uint256 amount, uint256 interestRateMode, uint16 referralCode, address onBehalfOf)
external;
function configureEModeCategory(uint8 id, DataTypes.EModeCategory memory category) external;
function deposit(address asset, uint256 amount, address onBehalfOf, uint16 referralCode) external;
function dropReserve(address asset) external;
function finalizeTransfer(
address asset,
address from,
address to,
uint256 amount,
uint256 balanceFromBefore,
uint256 balanceToBefore
) external;
function flashLoan(
address receiverAddress,
address[] memory assets,
uint256[] memory amounts,
uint256[] memory interestRateModes,
address onBehalfOf,
bytes memory params,
uint16 referralCode
) external;
function flashLoanSimple(
address receiverAddress,
address asset,
uint256 amount,
bytes memory params,
uint16 referralCode
) external;
function getConfiguration(address asset) external view returns (DataTypes.ReserveConfigurationMap memory);
function getEModeCategoryData(uint8 id) external view returns (DataTypes.EModeCategory memory);
function getReserveAddressById(uint16 id) external view returns (address);
function getReserveData(address asset) external view returns (DataTypes.ReserveData memory);
function getReserveNormalizedIncome(address asset) external view returns (uint256);
function getReserveNormalizedVariableDebt(address asset) external view returns (uint256);
function getReservesList() external view returns (address[] memory);
function getUserAccountData(address user)
external
view
returns (
uint256 totalCollateralBase,
uint256 totalDebtBase,
uint256 availableBorrowsBase,
uint256 currentLiquidationThreshold,
uint256 ltv,
uint256 healthFactor
);
function getUserConfiguration(address user) external view returns (DataTypes.UserConfigurationMap memory);
function getUserEMode(address user) external view returns (uint256);
function initReserve(
address asset,
address aTokenAddress,
address stableDebtAddress,
address variableDebtAddress,
address interestRateStrategyAddress
) external;
function initialize(address provider) external;
function liquidationCall(
address collateralAsset,
address debtAsset,
address user,
uint256 debtToCover,
bool receiveAToken
) external;
function mintToTreasury(address[] memory assets) external;
function mintUnbacked(address asset, uint256 amount, address onBehalfOf, uint16 referralCode) external;
function rebalanceStableBorrowRate(address asset, address user) external;
function repay(address asset, uint256 amount, uint256 interestRateMode, address onBehalfOf)
external
returns (uint256);
function repayWithATokens(address asset, uint256 amount, uint256 interestRateMode) external returns (uint256);
function repayWithPermit(
address asset,
uint256 amount,
uint256 interestRateMode,
address onBehalfOf,
uint256 deadline,
uint8 permitV,
bytes32 permitR,
bytes32 permitS
) external returns (uint256);
function rescueTokens(address token, address to, uint256 amount) external;
function resetIsolationModeTotalDebt(address asset) external;
function setConfiguration(address asset, DataTypes.ReserveConfigurationMap memory configuration) external;
function setReserveInterestRateStrategyAddress(address asset, address rateStrategyAddress) external;
function setUserEMode(uint8 categoryId) external;
function setUserUseReserveAsCollateral(address asset, bool useAsCollateral) external;
function supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode) external;
function supplyWithPermit(
address asset,
uint256 amount,
address onBehalfOf,
uint16 referralCode,
uint256 deadline,
uint8 permitV,
bytes32 permitR,
bytes32 permitS
) external;
function swapBorrowRateMode(address asset, uint256 interestRateMode) external;
function updateBridgeProtocolFee(uint256 protocolFee) external;
function updateFlashloanPremiums(uint128 flashLoanPremiumTotal, uint128 flashLoanPremiumToProtocol) external;
function withdraw(address asset, uint256 amount, address to) external returns (uint256);
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
/// @title Quoter Interface
/// @notice Supports quoting the calculated amounts from exact input or exact output swaps
/// @dev These functions are not marked view because they rely on calling non-view functions and reverting
/// to compute the result. They are also not gas efficient and should not be called on-chain.
interface IQuoter {
/// @notice Returns the amount out received for a given exact input swap without executing the swap
/// @param path The path of the swap, i.e. each token pair and the pool fee
/// @param amountIn The amount of the first token to swap
/// @return amountOut The amount of the last token that would be received
function quoteExactInput(bytes memory path, uint256 amountIn) external returns (uint256 amountOut);
/// @notice Returns the amount out received for a given exact input but for a swap of a single pool
/// @param tokenIn The token being swapped in
/// @param tokenOut The token being swapped out
/// @param fee The fee of the token pool to consider for the pair
/// @param amountIn The desired input amount
/// @param sqrtPriceLimitX96 The price limit of the pool that cannot be exceeded by the swap
/// @return amountOut The amount of `tokenOut` that would be received
function quoteExactInputSingle(
address tokenIn,
address tokenOut,
uint24 fee,
uint256 amountIn,
uint160 sqrtPriceLimitX96
) external returns (uint256 amountOut);
/// @notice Returns the amount in required for a given exact output swap without executing the swap
/// @param path The path of the swap, i.e. each token pair and the pool fee. Path must be provided in reverse order
/// @param amountOut The amount of the last token to receive
/// @return amountIn The amount of first token required to be paid
function quoteExactOutput(bytes memory path, uint256 amountOut) external returns (uint256 amountIn);
/// @notice Returns the amount in required to receive the given exact output amount but for a swap of a single pool
/// @param tokenIn The token being swapped in
/// @param tokenOut The token being swapped out
/// @param fee The fee of the token pool to consider for the pair
/// @param amountOut The desired output amount
/// @param sqrtPriceLimitX96 The price limit of the pool that cannot be exceeded by the swap
/// @return amountIn The amount required as the input for the swap in order to receive `amountOut`
function quoteExactOutputSingle(
address tokenIn,
address tokenOut,
uint24 fee,
uint256 amountOut,
uint160 sqrtPriceLimitX96
) external returns (uint256 amountIn);
}
// SPDX-License-Identifier: Apache License, Version 2.0
pragma solidity 0.6.10;
pragma experimental "ABIEncoderV2";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/**
* @title ISetToken
* @author Set Protocol
*
* Interface for operating with SetTokens.
*/
interface ISetToken is IERC20 {
/* ============ Enums ============ */
enum ModuleState {
NONE,
PENDING,
INITIALIZED
}
/* ============ Structs ============ */
/**
* The base definition of a SetToken Position
*
* @param component Address of token in the Position
* @param module If not in default state, the address of associated module
* @param unit Each unit is the # of components per 10^18 of a SetToken
* @param positionState Position ENUM. Default is 0; External is 1
* @param data Arbitrary data
*/
struct Position {
address component;
address module;
int256 unit;
uint8 positionState;
bytes data;
}
/**
* A struct that stores a component's cash position details and external positions
* This data structure allows O(1) access to a component's cash position units and
* virtual units.
*
* @param virtualUnit Virtual value of a component's DEFAULT position. Stored as virtual for efficiency
* updating all units at once via the position multiplier. Virtual units are achieved
* by dividing a "real" value by the "positionMultiplier"
* @param componentIndex
* @param externalPositionModules List of external modules attached to each external position. Each module
* maps to an external position
* @param externalPositions Mapping of module => ExternalPosition struct for a given component
*/
struct ComponentPosition {
int256 virtualUnit;
address[] externalPositionModules;
mapping(address => ExternalPosition) externalPositions;
}
/**
* A struct that stores a component's external position details including virtual unit and any
* auxiliary data.
*
* @param virtualUnit Virtual value of a component's EXTERNAL position.
* @param data Arbitrary data
*/
struct ExternalPosition {
int256 virtualUnit;
bytes data;
}
/* ============ Functions ============ */
function addComponent(address _component) external;
function removeComponent(address _component) external;
function editDefaultPositionUnit(address _component, int256 _realUnit) external;
function addExternalPositionModule(address _component, address _positionModule) external;
function removeExternalPositionModule(address _component, address _positionModule) external;
function editExternalPositionUnit(address _component, address _positionModule, int256 _realUnit) external;
function editExternalPositionData(address _component, address _positionModule, bytes calldata _data) external;
function invoke(address _target, uint256 _value, bytes calldata _data) external returns(bytes memory);
function editPositionMultiplier(int256 _newMultiplier) external;
function mint(address _account, uint256 _quantity) external;
function burn(address _account, uint256 _quantity) external;
function lock() external;
function unlock() external;
function addModule(address _module) external;
function removeModule(address _module) external;
function initializeModule() external;
function setManager(address _manager) external;
function manager() external view returns (address);
function moduleStates(address _module) external view returns (ModuleState);
function getModules() external view returns (address[] memory);
function getDefaultPositionRealUnit(address _component) external view returns(int256);
function getExternalPositionRealUnit(address _component, address _positionModule) external view returns(int256);
function getComponents() external view returns(address[] memory);
function getExternalPositionModules(address _component) external view returns(address[] memory);
function getExternalPositionData(address _component, address _positionModule) external view returns(bytes memory);
function isExternalPositionModule(address _component, address _module) external view returns(bool);
function isComponent(address _component) external view returns(bool);
function positionMultiplier() external view returns (int256);
function getPositions() external view returns (Position[] memory);
function getTotalComponentRealUnits(address _component) external view returns(int256);
function isInitializedModule(address _module) external view returns(bool);
function isPendingModule(address _module) external view returns(bool);
function isLocked() external view returns (bool);
}
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
interface ISwapRouter02 {
struct IncreaseLiquidityParams {
address token0;
address token1;
uint256 tokenId;
uint256 amount0Min;
uint256 amount1Min;
}
struct MintParams {
address token0;
address token1;
uint24 fee;
int24 tickLower;
int24 tickUpper;
uint256 amount0Min;
uint256 amount1Min;
address recipient;
}
struct ExactInputParams {
bytes path;
address recipient;
uint256 amountIn;
uint256 amountOutMinimum;
}
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}
struct ExactOutputParams {
bytes path;
address recipient;
uint256 amountOut;
uint256 amountInMaximum;
}
struct ExactOutputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}
function WETH9() external view returns (address);
function approveMax(address token) external payable;
function approveMaxMinusOne(address token) external payable;
function approveZeroThenMax(address token) external payable;
function approveZeroThenMaxMinusOne(address token) external payable;
function callPositionManager(bytes memory data) external payable returns (bytes memory result);
function checkOracleSlippage(
bytes[] memory paths,
uint128[] memory amounts,
uint24 maximumTickDivergence,
uint32 secondsAgo
) external view;
function checkOracleSlippage(bytes memory path, uint24 maximumTickDivergence, uint32 secondsAgo) external view;
function exactInput(ExactInputParams memory params) external payable returns (uint256 amountOut);
function exactInputSingle(ExactInputSingleParams memory params) external payable returns (uint256 amountOut);
function exactOutput(ExactOutputParams memory params) external payable returns (uint256 amountIn);
function exactOutputSingle(ExactOutputSingleParams memory params) external payable returns (uint256 amountIn);
function factory() external view returns (address);
function factoryV2() external view returns (address);
function getApprovalType(address token, uint256 amount) external returns (uint8);
function increaseLiquidity(IncreaseLiquidityParams memory params) external payable returns (bytes memory result);
function mint(MintParams memory params) external payable returns (bytes memory result);
function multicall(bytes32 previousBlockhash, bytes[] memory data) external payable returns (bytes[] memory);
function multicall(uint256 deadline, bytes[] memory data) external payable returns (bytes[] memory);
function multicall(bytes[] memory data) external payable returns (bytes[] memory results);
function positionManager() external view returns (address);
function pull(address token, uint256 value) external payable;
function refundETH() external payable;
function selfPermit(address token, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
external
payable;
function selfPermitAllowed(address token, uint256 nonce, uint256 expiry, uint8 v, bytes32 r, bytes32 s)
external
payable;
function selfPermitAllowedIfNecessary(address token, uint256 nonce, uint256 expiry, uint8 v, bytes32 r, bytes32 s)
external
payable;
function selfPermitIfNecessary(address token, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
external
payable;
function swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] memory path, address to)
external
payable
returns (uint256 amountOut);
function swapTokensForExactTokens(uint256 amountOut, uint256 amountInMax, address[] memory path, address to)
external
payable
returns (uint256 amountIn);
function sweepToken(address token, uint256 amountMinimum, address recipient) external payable;
function sweepToken(address token, uint256 amountMinimum) external payable;
function sweepTokenWithFee(address token, uint256 amountMinimum, uint256 feeBips, address feeRecipient)
external
payable;
function sweepTokenWithFee(
address token,
uint256 amountMinimum,
address recipient,
uint256 feeBips,
address feeRecipient
) external payable;
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes memory _data) external;
function unwrapWETH9(uint256 amountMinimum, address recipient) external payable;
function unwrapWETH9(uint256 amountMinimum) external payable;
function unwrapWETH9WithFee(uint256 amountMinimum, address recipient, uint256 feeBips, address feeRecipient)
external
payable;
function unwrapWETH9WithFee(uint256 amountMinimum, uint256 feeBips, address feeRecipient) external payable;
function wrapETH(uint256 value) external payable;
}
pragma solidity >=0.5.0;
interface IUniswapV2Pair {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);
function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function sync() external;
function initialize(address, address) external;
}
pragma solidity >=0.6.2;
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);
function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountA, uint amountB);
function removeLiquidityETHWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}
pragma solidity >=0.6.2;
import './IUniswapV2Router01.sol';
interface IUniswapV2Router02 is IUniswapV2Router01 {
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountETH);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
pragma solidity >=0.6.10 <0.9.0;
pragma experimental ABIEncoderV2;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface IFlashLoanRecipient {
/**
* @dev When `flashLoan` is called on the Vault, it invokes the `receiveFlashLoan` hook on the recipient.
*
* At the time of the call, the Vault will have transferred `amounts` for `tokens` to the recipient. Before this
* call returns, the recipient must have transferred `amounts` plus `feeAmounts` for each token back to the
* Vault, or else the entire flash loan will revert.
*
* `userData` is the same value passed in the `IVault.flashLoan` call.
*/
function receiveFlashLoan(
IERC20[] memory tokens,
uint256[] memory amounts,
uint256[] memory feeAmounts,
bytes memory userData
) external;
}
/**
* Stripped down interface of IVault.
* https://github.com/balancer/balancer-v2-monorepo/blob/master/pkg/interfaces/contracts/vault/IVault.sol
*/
interface IVault {
// Swaps
//
// Users can swap tokens with Pools by calling the `swap` and `batchSwap` functions. To do this,
// they need not trust Pool contracts in any way: all security checks are made by the Vault. They must however be
// aware of the Pools' pricing algorithms in order to estimate the prices Pools will quote.
//
// The `swap` function executes a single swap, while `batchSwap` can perform multiple swaps in sequence.
// In each individual swap, tokens of one kind are sent from the sender to the Pool (this is the 'token in'),
// and tokens of another kind are sent from the Pool to the recipient in exchange (this is the 'token out').
// More complex swaps, such as one token in to multiple tokens out can be achieved by batching together
// individual swaps.
//
// There are two swap kinds:
// - 'given in' swaps, where the amount of tokens in (sent to the Pool) is known, and the Pool determines (via the
// `onSwap` hook) the amount of tokens out (to send to the recipient).
// - 'given out' swaps, where the amount of tokens out (received from the Pool) is known, and the Pool determines
// (via the `onSwap` hook) the amount of tokens in (to receive from the sender).
//
// Additionally, it is possible to chain swaps using a placeholder input amount, which the Vault replaces with
// the calculated output of the previous swap. If the previous swap was 'given in', this will be the calculated
// tokenOut amount. If the previous swap was 'given out', it will use the calculated tokenIn amount. These extended
// swaps are known as 'multihop' swaps, since they 'hop' through a number of intermediate tokens before arriving at
// the final intended token.
//
// In all cases, tokens are only transferred in and out of the Vault (or withdrawn from and deposited into Internal
// Balance) after all individual swaps have been completed, and the net token balance change computed. This makes
// certain swap patterns, such as multihops, or swaps that interact with the same token pair in multiple Pools, cost
// much less gas than they would otherwise.
//
// It also means that under certain conditions it is possible to perform arbitrage by swapping with multiple
// Pools in a way that results in net token movement out of the Vault (profit), with no tokens being sent in (only
// updating the Pool's internal accounting).
//
// To protect users from front-running or the market changing rapidly, they supply a list of 'limits' for each token
// involved in the swap, where either the maximum number of tokens to send (by passing a positive value) or the
// minimum amount of tokens to receive (by passing a negative value) is specified.
//
// Additionally, a 'deadline' timestamp can also be provided, forcing the swap to fail if it occurs after
// this point in time (e.g. if the transaction failed to be included in a block promptly).
//
// If interacting with Pools that hold WETH, it is possible to both send and receive ETH directly: the Vault will do
// the wrapping and unwrapping. To enable this mechanism, the IAsset sentinel value (the zero address) must be
// passed in the `assets` array instead of the WETH address. Note that it is possible to combine ETH and WETH in the
// same swap. Any excess ETH will be sent back to the caller (not the sender, which is relevant for relayers).
//
// Finally, Internal Balance can be used when either sending or receiving tokens.
enum SwapKind {
GIVEN_IN,
GIVEN_OUT
}
/**
* @dev Performs a swap with a single Pool.
*
* If the swap is 'given in' (the number of tokens to send to the Pool is known), it returns the amount of tokens
* taken from the Pool, which must be greater than or equal to `limit`.
*
* If the swap is 'given out' (the number of tokens to take from the Pool is known), it returns the amount of tokens
* sent to the Pool, which must be less than or equal to `limit`.
*
* Internal Balance usage and the recipient are determined by the `funds` struct.
*
* Emits a `Swap` event.
*/
function swap(
SingleSwap memory singleSwap,
FundManagement memory funds,
uint256 limit,
uint256 deadline
) external payable returns (uint256);
/**
* @dev Data for a single swap executed by `swap`. `amount` is either `amountIn` or `amountOut` depending on
* the `kind` value.
*
* `assetIn` and `assetOut` are either token addresses, or the IAsset sentinel value for ETH (the zero address).
* Note that Pools never interact with ETH directly: it will be wrapped to or unwrapped from WETH by the Vault.
*
* The `userData` field is ignored by the Vault, but forwarded to the Pool in the `onSwap` hook, and may be
* used to extend swap behavior.
*/
struct SingleSwap {
bytes32 poolId;
SwapKind kind;
address assetIn;
address assetOut;
uint256 amount;
bytes userData;
}
/**
* @dev Performs a series of swaps with one or multiple Pools. In each individual swap, the caller determines either
* the amount of tokens sent to or received from the Pool, depending on the `kind` value.
*
* Returns an array with the net Vault asset balance deltas. Positive amounts represent tokens (or ETH) sent to the
* Vault, and negative amounts represent tokens (or ETH) sent by the Vault. Each delta corresponds to the asset at
* the same index in the `assets` array.
*
* Swaps are executed sequentially, in the order specified by the `swaps` array. Each array element describes a
* Pool, the token to be sent to this Pool, the token to receive from it, and an amount that is either `amountIn` or
* `amountOut` depending on the swap kind.
*
* Multihop swaps can be executed by passing an `amount` value of zero for a swap. This will cause the amount in/out
* of the previous swap to be used as the amount in for the current one. In a 'given in' swap, 'tokenIn' must equal
* the previous swap's `tokenOut`. For a 'given out' swap, `tokenOut` must equal the previous swap's `tokenIn`.
*
* The `assets` array contains the addresses of all assets involved in the swaps. These are either token addresses,
* or the IAsset sentinel value for ETH (the zero address). Each entry in the `swaps` array specifies tokens in and
* out by referencing an index in `assets`. Note that Pools never interact with ETH directly: it will be wrapped to
* or unwrapped from WETH by the Vault.
*
* Internal Balance usage, sender, and recipient are determined by the `funds` struct. The `limits` array specifies
* the minimum or maximum amount of each token the vault is allowed to transfer.
*
* `batchSwap` can be used to make a single swap, like `swap` does, but doing so requires more gas than the
* equivalent `swap` call.
*
* Emits `Swap` events.
*/
function batchSwap(
SwapKind kind,
BatchSwapStep[] memory swaps,
address[] memory assets,
FundManagement memory funds,
int256[] memory limits,
uint256 deadline
) external payable returns (int256[] memory);
/**
* @dev Data for each individual swap executed by `batchSwap`. The asset in and out fields are indexes into the
* `assets` array passed to that function, and ETH assets are converted to WETH.
*
* If `amount` is zero, the multihop mechanism is used to determine the actual amount based on the amount in/out
* from the previous swap, depending on the swap kind.
*
* The `userData` field is ignored by the Vault, but forwarded to the Pool in the `onSwap` hook, and may be
* used to extend swap behavior.
*/
struct BatchSwapStep {
bytes32 poolId;
uint256 assetInIndex;
uint256 assetOutIndex;
uint256 amount;
bytes userData;
}
/**
* @dev Emitted for each individual swap performed by `swap` or `batchSwap`.
*/
event Swap(
bytes32 indexed poolId,
IERC20 indexed tokenIn,
IERC20 indexed tokenOut,
uint256 amountIn,
uint256 amountOut
);
/**
* @dev All tokens in a swap are either sent from the `sender` account to the Vault, or from the Vault to the
* `recipient` account.
*
* If the caller is not `sender`, it must be an authorized relayer for them.
*
* If `fromInternalBalance` is true, the `sender`'s Internal Balance will be preferred, performing an ERC20
* transfer for the difference between the requested amount and the User's Internal Balance (if any). The `sender`
* must have allowed the Vault to use their tokens via `IERC20.approve()`. This matches the behavior of
* `joinPool`.
*
* If `toInternalBalance` is true, tokens will be deposited to `recipient`'s internal balance instead of
* transferred. This matches the behavior of `exitPool`.
*
* Note that ETH cannot be deposited to or withdrawn from Internal Balance: attempting to do so will trigger a
* revert.
*/
struct FundManagement {
address sender;
bool fromInternalBalance;
address payable recipient;
bool toInternalBalance;
}
/**
* @dev Simulates a call to `batchSwap`, returning an array of Vault asset deltas. Calls to `swap` cannot be
* simulated directly, but an equivalent `batchSwap` call can and will yield the exact same result.
*
* Each element in the array corresponds to the asset at the same index, and indicates the number of tokens (or ETH)
* the Vault would take from the sender (if positive) or send to the recipient (if negative). The arguments it
* receives are the same that an equivalent `batchSwap` call would receive.
*
* Unlike `batchSwap`, this function performs no checks on the sender or recipient field in the `funds` struct.
* This makes it suitable to be called by off-chain applications via eth_call without needing to hold tokens,
* approve them for the Vault, or even know a user's address.
*
* Note that this function is not 'view' (due to implementation details): the client code must explicitly execute
* eth_call instead of eth_sendTransaction.
*/
function queryBatchSwap(
SwapKind kind,
BatchSwapStep[] memory swaps,
address[] memory assets,
FundManagement memory funds
) external returns (int256[] memory assetDeltas);
// Flash Loans
/**
* @dev Performs a 'flash loan', sending tokens to `recipient`, executing the `receiveFlashLoan` hook on it,
* and then reverting unless the tokens plus a proportional protocol fee have been returned.
*
* The `tokens` and `amounts` arrays must have the same length, and each entry in these indicates the loan amount
* for each token contract. `tokens` must be sorted in ascending order.
*
* The 'userData' field is ignored by the Vault, and forwarded as-is to `recipient` as part of the
* `receiveFlashLoan` call.
*
* Emits `FlashLoan` events.
*/
function flashLoan(
IFlashLoanRecipient recipient,
address[] memory tokens,
uint256[] memory amounts,
bytes memory userData
) external;
/**
* @dev Emitted for each individual flash loan performed by `flashLoan`.
*/
event FlashLoan(IFlashLoanRecipient indexed recipient, IERC20 indexed token, uint256 amount, uint256 feeAmount);
}
// SPDX-License-Identifier: Apache License, Version 2.0
pragma solidity >=0.6.10;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface IWETH is IERC20 {
function deposit() external payable;
function withdraw(uint) external;
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow, so we distribute
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor () internal {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
/*
Copyright 2020 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { SignedSafeMath } from "@openzeppelin/contracts/math/SignedSafeMath.sol";
/**
* @title PreciseUnitMath
* @author Set Protocol
*
* Arithmetic for fixed-point numbers with 18 decimals of precision. Some functions taken from
* dYdX's BaseMath library.
*
* CHANGELOG:
* - 9/21/20: Added safePower function
*/
library PreciseUnitMath {
using SafeMath for uint256;
using SignedSafeMath for int256;
// The number One in precise units.
uint256 constant internal PRECISE_UNIT = 10 ** 18;
int256 constant internal PRECISE_UNIT_INT = 10 ** 18;
// Max unsigned integer value
uint256 constant internal MAX_UINT_256 = type(uint256).max;
// Max and min signed integer value
int256 constant internal MAX_INT_256 = type(int256).max;
int256 constant internal MIN_INT_256 = type(int256).min;
/**
* @dev Getter function since constants can't be read directly from libraries.
*/
function preciseUnit() internal pure returns (uint256) {
return PRECISE_UNIT;
}
/**
* @dev Getter function since constants can't be read directly from libraries.
*/
function preciseUnitInt() internal pure returns (int256) {
return PRECISE_UNIT_INT;
}
/**
* @dev Getter function since constants can't be read directly from libraries.
*/
function maxUint256() internal pure returns (uint256) {
return MAX_UINT_256;
}
/**
* @dev Getter function since constants can't be read directly from libraries.
*/
function maxInt256() internal pure returns (int256) {
return MAX_INT_256;
}
/**
* @dev Getter function since constants can't be read directly from libraries.
*/
function minInt256() internal pure returns (int256) {
return MIN_INT_256;
}
/**
* @dev Multiplies value a by value b (result is rounded down). It's assumed that the value b is the significand
* of a number with 18 decimals precision.
*/
function preciseMul(uint256 a, uint256 b) internal pure returns (uint256) {
return a.mul(b).div(PRECISE_UNIT);
}
/**
* @dev Multiplies value a by value b (result is rounded towards zero). It's assumed that the value b is the
* significand of a number with 18 decimals precision.
*/
function preciseMul(int256 a, int256 b) internal pure returns (int256) {
return a.mul(b).div(PRECISE_UNIT_INT);
}
/**
* @dev Multiplies value a by value b (result is rounded up). It's assumed that the value b is the significand
* of a number with 18 decimals precision.
*/
function preciseMulCeil(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0 || b == 0) {
return 0;
}
return a.mul(b).sub(1).div(PRECISE_UNIT).add(1);
}
/**
* @dev Divides value a by value b (result is rounded down).
*/
function preciseDiv(uint256 a, uint256 b) internal pure returns (uint256) {
return a.mul(PRECISE_UNIT).div(b);
}
/**
* @dev Divides value a by value b (result is rounded towards 0).
*/
function preciseDiv(int256 a, int256 b) internal pure returns (int256) {
return a.mul(PRECISE_UNIT_INT).div(b);
}
/**
* @dev Divides value a by value b (result is rounded up or away from 0).
*/
function preciseDivCeil(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "Cant divide by 0");
return a > 0 ? a.mul(PRECISE_UNIT).sub(1).div(b).add(1) : 0;
}
/**
* @dev Divides value a by value b (result is rounded down - positive numbers toward 0 and negative away from 0).
*/
function divDown(int256 a, int256 b) internal pure returns (int256) {
require(b != 0, "Cant divide by 0");
require(a != MIN_INT_256 || b != -1, "Invalid input");
int256 result = a.div(b);
if (a ^ b < 0 && a % b != 0) {
result -= 1;
}
return result;
}
/**
* @dev Multiplies value a by value b where rounding is towards the lesser number.
* (positive values are rounded towards zero and negative values are rounded away from 0).
*/
function conservativePreciseMul(int256 a, int256 b) internal pure returns (int256) {
return divDown(a.mul(b), PRECISE_UNIT_INT);
}
/**
* @dev Divides value a by value b where rounding is towards the lesser number.
* (positive values are rounded towards zero and negative values are rounded away from 0).
*/
function conservativePreciseDiv(int256 a, int256 b) internal pure returns (int256) {
return divDown(a.mul(PRECISE_UNIT_INT), b);
}
/**
* @dev Performs the power on a specified value, reverts on overflow.
*/
function safePower(
uint256 a,
uint256 pow
)
internal
pure
returns (uint256)
{
require(a > 0, "Value must be positive");
uint256 result = 1;
for (uint256 i = 0; i < pow; i++){
uint256 previousResult = result;
// Using safemath multiplication prevents overflows
result = previousResult.mul(a);
}
return result;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor () internal {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and make it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
import "./IERC20.sol";
import "../../math/SafeMath.sol";
import "../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(IERC20 token, address spender, uint256 value) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
// solhint-disable-next-line max-line-length
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) { // Return data is optional
// solhint-disable-next-line max-line-length
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
/**
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) return 0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryDiv}.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
/**
* @title SignedSafeMath
* @dev Signed math operations with safety checks that revert on error.
*/
library SignedSafeMath {
int256 constant private _INT256_MIN = -2**255;
/**
* @dev Returns the multiplication of two signed integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(int256 a, int256 b) internal pure returns (int256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
require(!(a == -1 && b == _INT256_MIN), "SignedSafeMath: multiplication overflow");
int256 c = a * b;
require(c / a == b, "SignedSafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two signed integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(int256 a, int256 b) internal pure returns (int256) {
require(b != 0, "SignedSafeMath: division by zero");
require(!(b == -1 && a == _INT256_MIN), "SignedSafeMath: division overflow");
int256 c = a / b;
return c;
}
/**
* @dev Returns the subtraction of two signed integers, reverting on
* overflow.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(int256 a, int256 b) internal pure returns (int256) {
int256 c = a - b;
require((b >= 0 && c <= a) || (b < 0 && c > a), "SignedSafeMath: subtraction overflow");
return c;
}
/**
* @dev Returns the addition of two signed integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(int256 a, int256 b) internal pure returns (int256) {
int256 c = a + b;
require((b >= 0 && c >= a) || (b < 0 && c < a), "SignedSafeMath: addition overflow");
return c;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
library UniSushiV2Library {
using SafeMath for uint;
// returns sorted token addresses, used to handle return values from pairs sorted in this order
function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
require(tokenA != tokenB, 'UniswapV2Library: IDENTICAL_ADDRESSES');
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2Library: ZERO_ADDRESS');
}
// fetches and sorts the reserves for a pair
function getReserves(address pair, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB) {
(address token0,) = sortTokens(tokenA, tokenB);
(uint reserve0, uint reserve1,) = IUniswapV2Pair(pair).getReserves();
(reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
}
// given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) {
require(amountA > 0, 'UniswapV2Library: INSUFFICIENT_AMOUNT');
require(reserveA > 0 && reserveB > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
amountB = amountA.mul(reserveB) / reserveA;
}
// given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
uint amountInWithFee = amountIn.mul(997);
uint numerator = amountInWithFee.mul(reserveOut);
uint denominator = reserveIn.mul(1000).add(amountInWithFee);
amountOut = numerator / denominator;
}
// given an output amount of an asset and pair reserves, returns a required input amount of the other asset
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn) {
require(amountOut > 0, 'UniswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT');
require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
uint numerator = reserveIn.mul(amountOut).mul(1000);
uint denominator = reserveOut.sub(amountOut).mul(997);
amountIn = (numerator / denominator).add(1);
}
// performs chained getAmountOut calculations on any number of pairs
function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts) {
require(path.length >= 2, 'UniswapV2Library: INVALID_PATH');
amounts = new uint[](path.length);
amounts[0] = amountIn;
for (uint i; i < path.length - 1; i++) {
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]);
amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut);
}
}
// performs chained getAmountIn calculations on any number of pairs
function getAmountsIn(address factory, uint amountOut, address[] memory path) internal view returns (uint[] memory amounts) {
require(path.length >= 2, 'UniswapV2Library: INVALID_PATH');
amounts = new uint[](path.length);
amounts[amounts.length - 1] = amountOut;
for (uint i = path.length - 1; i > 0; i--) {
(uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]);
amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut);
}
}
}
pragma solidity ^0.6.10;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
/**
Ensures that any contract that inherits from this contract is able to
withdraw funds that are accidentally received or stuck.
*/
contract Withdrawable is Ownable {
using SafeERC20 for ERC20;
address constant ETHER = address(0);
event LogWithdraw(
address indexed _from,
address indexed _assetAddress,
uint amount
);
/**
* @dev Withdraw asset.
* @param _assetAddress Asset to be withdrawn.
*/
function withdraw(address _assetAddress) public onlyOwner {
uint assetBalance;
if (_assetAddress == ETHER) {
address self = address(this); // workaround for a possible solidity bug
assetBalance = self.balance;
msg.sender.transfer(assetBalance);
} else {
assetBalance = ERC20(_assetAddress).balanceOf(address(this));
ERC20(_assetAddress).safeTransfer(msg.sender, assetBalance);
}
emit LogWithdraw(msg.sender, _assetAddress, assetBalance);
}
}
{
"compilationTarget": {
"contracts/exchangeIssuance/FlashMintLeveragedExtended.sol": "FlashMintLeveragedExtended"
},
"evmVersion": "istanbul",
"libraries": {
"DEXAdapter": "0x94a8c312ab1d0c63b644facf9c557782320fbb01"
},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"components":[{"internalType":"address","name":"quickRouter","type":"address"},{"internalType":"address","name":"sushiRouter","type":"address"},{"internalType":"address","name":"uniV3Router","type":"address"},{"internalType":"address","name":"uniV3Quoter","type":"address"},{"internalType":"address","name":"curveAddressProvider","type":"address"},{"internalType":"address","name":"curveCalculator","type":"address"},{"internalType":"address","name":"weth","type":"address"}],"internalType":"struct DEXAdapter.Addresses","name":"_addresses","type":"tuple"},{"internalType":"contract IController","name":"_setController","type":"address"},{"internalType":"contract IDebtIssuanceModule","name":"_debtIssuanceModule","type":"address"},{"internalType":"contract IAaveLeverageModule","name":"_aaveLeverageModule","type":"address"},{"internalType":"address","name":"_aaveV3Pool","type":"address"},{"internalType":"address","name":"_vault","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_recipient","type":"address"},{"indexed":true,"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"indexed":true,"internalType":"address","name":"_inputToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"_amountInputToken","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_amountSetIssued","type":"uint256"}],"name":"FlashMint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_recipient","type":"address"},{"indexed":true,"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"indexed":true,"internalType":"address","name":"_outputToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"_amountSetRedeemed","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_amountOutputToken","type":"uint256"}],"name":"FlashRedeem","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"LENDING_POOL","outputs":[{"internalType":"contract IPool","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ROUNDING_ERROR_MARGIN","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"aaveLeverageModule","outputs":[{"internalType":"contract IAaveLeverageModule","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"addresses","outputs":[{"internalType":"address","name":"quickRouter","type":"address"},{"internalType":"address","name":"sushiRouter","type":"address"},{"internalType":"address","name":"uniV3Router","type":"address"},{"internalType":"address","name":"uniV3Quoter","type":"address"},{"internalType":"address","name":"curveAddressProvider","type":"address"},{"internalType":"address","name":"curveCalculator","type":"address"},{"internalType":"address","name":"weth","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"}],"name":"approveSetToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"_token","type":"address"}],"name":"approveToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20[]","name":"_tokens","type":"address[]"}],"name":"approveTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"balancerV2Vault","outputs":[{"internalType":"contract IVault","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"debtIssuanceModule","outputs":[{"internalType":"contract IDebtIssuanceModule","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_setAmount","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataDebtForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataInputToken","type":"tuple"}],"name":"getIssueExactSet","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_setAmount","type":"uint256"},{"internalType":"bool","name":"_isIssuance","type":"bool"}],"name":"getLeveragedTokenData","outputs":[{"components":[{"internalType":"address","name":"collateralAToken","type":"address"},{"internalType":"address","name":"collateralToken","type":"address"},{"internalType":"uint256","name":"collateralAmount","type":"uint256"},{"internalType":"address","name":"debtToken","type":"address"},{"internalType":"uint256","name":"debtAmount","type":"uint256"}],"internalType":"struct FlashMintLeveraged.LeveragedTokenData","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_setAmount","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataCollateralForDebt","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataOutputToken","type":"tuple"}],"name":"getRedeemExactSet","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_setAmount","type":"uint256"},{"internalType":"address","name":"_inputToken","type":"address"},{"internalType":"uint256","name":"_maxAmountInputToken","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataDebtForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataInputToken","type":"tuple"}],"name":"issueExactSetFromERC20","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_setAmount","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataDebtForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataInputToken","type":"tuple"}],"name":"issueExactSetFromETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_minSetAmount","type":"uint256"},{"internalType":"address","name":"_inputToken","type":"address"},{"internalType":"uint256","name":"_inputTokenAmount","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataDebtForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataInputTokenForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataInputTokenForETH","type":"tuple"},{"internalType":"uint256","name":"_priceEstimateInflator","type":"uint256"},{"internalType":"uint256","name":"_maxDust","type":"uint256"}],"name":"issueSetFromExactERC20","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_minSetAmount","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataDebtForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataInputTokenForCollateral","type":"tuple"},{"internalType":"uint256","name":"_priceEstimateInflator","type":"uint256"},{"internalType":"uint256","name":"_maxDust","type":"uint256"}],"name":"issueSetFromExactETH","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"maxGasRebate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxIterations","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20[]","name":"tokens","type":"address[]"},{"internalType":"uint256[]","name":"amounts","type":"uint256[]"},{"internalType":"uint256[]","name":"feeAmounts","type":"uint256[]"},{"internalType":"bytes","name":"userData","type":"bytes"}],"name":"receiveFlashLoan","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_setAmount","type":"uint256"},{"internalType":"address","name":"_outputToken","type":"address"},{"internalType":"uint256","name":"_minAmountOutputToken","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataCollateralForDebt","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataOutputToken","type":"tuple"}],"name":"redeemExactSetForERC20","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_setAmount","type":"uint256"},{"internalType":"uint256","name":"_minAmountOutputToken","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataCollateralForDebt","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataOutputToken","type":"tuple"}],"name":"redeemExactSetForETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_maxSetAmount","type":"uint256"},{"internalType":"address","name":"_outputToken","type":"address"},{"internalType":"uint256","name":"_outputTokenAmount","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataCollateralForDebt","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataCollateralForOutputToken","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataDebtForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataOutputTokenForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataOutputTokenForETH","type":"tuple"},{"internalType":"uint256","name":"_priceEstimateInflator","type":"uint256"},{"internalType":"uint256","name":"_maxDust","type":"uint256"}],"name":"redeemSetForExactERC20","outputs":[{"internalType":"uint256","name":"setAmount","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ISetToken","name":"_setToken","type":"address"},{"internalType":"uint256","name":"_maxSetAmount","type":"uint256"},{"internalType":"uint256","name":"_outputTokenAmount","type":"uint256"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataCollateralForDebt","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataCollateralForOutputToken","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataDebtForCollateral","type":"tuple"},{"components":[{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"uint24[]","name":"fees","type":"uint24[]"},{"internalType":"address","name":"pool","type":"address"},{"internalType":"enum DEXAdapter.Exchange","name":"exchange","type":"uint8"}],"internalType":"struct DEXAdapter.SwapData","name":"_swapDataOutputTokenForCollateral","type":"tuple"},{"internalType":"uint256","name":"_priceEstimateInflator","type":"uint256"},{"internalType":"uint256","name":"_maxDust","type":"uint256"}],"name":"redeemSetForExactETH","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"setController","outputs":[{"internalType":"contract IController","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_maxGasRebate","type":"uint256"}],"name":"setMaxGasRebate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_maxIterations","type":"uint256"}],"name":"setMaxIterations","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]