Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/interfaces/IIndexExchangeAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma solidity >=0.6.10;

interface IIndexExchangeAdapter {
function getSpender() external view returns(address);
Expand Down
73 changes: 73 additions & 0 deletions contracts/interfaces/external/ISwapRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: GPL-2.0-or-later

/*
pragma solidity >=0.7.5;
pragma abicoder v2;
*/

pragma solidity 0.6.10;
pragma experimental "ABIEncoderV2";

import "@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3SwapCallback.sol";

/// @title Router token swapping functionality
/// @notice Functions for swapping tokens via Uniswap V3
interface ISwapRouter is IUniswapV3SwapCallback {
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}

/// @notice Swaps `amountIn` of one token for as much as possible of another token
/// @param params The parameters necessary for the swap, encoded as `ExactInputSingleParams` in calldata
/// @return amountOut The amount of the received token
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);

struct ExactInputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
}

/// @notice Swaps `amountIn` of one token for as much as possible of another along the specified path
/// @param params The parameters necessary for the multi-hop swap, encoded as `ExactInputParams` in calldata
/// @return amountOut The amount of the received token
function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);

struct ExactOutputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}

/// @notice Swaps as little as possible of one token for `amountOut` of another token
/// @param params The parameters necessary for the swap, encoded as `ExactOutputSingleParams` in calldata
/// @return amountIn The amount of the input token
function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);

struct ExactOutputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountOut;
uint256 amountInMaximum;
}

/// @notice Swaps as little as possible of one token for `amountOut` of another along the specified path (reversed)
/// @param params The parameters necessary for the multi-hop swap, encoded as `ExactOutputParams` in calldata
/// @return amountIn The amount of the input token
function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
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;
pragma experimental "ABIEncoderV2";

import { ISwapRouter } from "contracts/interfaces/external/ISwapRouter.sol";
import { BytesLib } from "external/contracts/uniswap/v3/lib/BytesLib.sol";

import { IIndexExchangeAdapter } from "../../../interfaces/IIndexExchangeAdapter.sol";

/**
* @title UniswapV3IndexExchangeAdapter
* @author Set Protocol
*
* A Uniswap V3 exchange adapter that returns calldata for trading with GeneralIndexModule, allows encoding a trade with a fixed input quantity or
* a fixed output quantity.
*/
contract UniswapV3IndexExchangeAdapter is IIndexExchangeAdapter {

using BytesLib for bytes;

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

// Address of Uniswap V3 SwapRouter contract
address public immutable router;

/* ============ Constants ============ */

// Uniswap router function string for swapping exact amount of input tokens for a minimum of output tokens
string internal constant SWAP_EXACT_INPUT = "exactInputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160))";
// Uniswap router function string for swapping max amoutn of input tokens for an exact amount of output tokens
string internal constant SWAP_EXACT_OUTPUT = "exactOutputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160))";

/* ============ Constructor ============ */

/**
* Set state variables
*
* @param _router Address of Uniswap V3 SwapRouter contract
*/
constructor(address _router) public {
router = _router;
}

/* ============ External Getter Functions ============ */

/**
* Return calldata for trading with Uniswap V3 SwapRouter. Trade paths are created from _sourceToken,
* _destinationToken and pool fees (which is encoded in _data).
*
* ---------------------------------------------------------------------------------------------------------------
* _isSendTokenFixed | Parameter | Amount |
* ---------------------------------------------------------------------------------------------------------------
* True | _sourceQuantity | Fixed amount of _sourceToken to trade |
* | _destinationQuantity | Minimum amount of _destinationToken willing to receive |
* ---------------------------------------------------------------------------------------------------------------
* False | _sourceQuantity | Maximum amount of _sourceToken to trade |
* | _destinationQuantity | Fixed amount of _destinationToken want to receive |
* ---------------------------------------------------------------------------------------------------------------
*
* @param _sourceToken Address of source token to be sold
* @param _destinationToken Address of destination token to buy
* @param _destinationAddress Address that assets should be transferred to
* @param _isSendTokenFixed Boolean indicating if the send quantity is fixed, used to determine correct trade interface
* @param _sourceQuantity Fixed/Max amount of source token to sell
* @param _destinationQuantity Min/Fixed amount of destination token to buy
* @param _data Arbitrary bytes containing fees value, expressed in hundredths of a bip,
* used to determine the pool to trade among similar asset pools on Uniswap V3.
* Note: SetToken manager must set the appropriate pool fees via `setExchangeData` in GeneralIndexModule
* for each component that needs to be traded on UniswapV3
*
* @return address Target contract address
* @return uint256 Call value
* @return bytes Trade calldata
*/
function getTradeCalldata(
address _sourceToken,
address _destinationToken,
address _destinationAddress,
bool _isSendTokenFixed,
uint256 _sourceQuantity,
uint256 _destinationQuantity,
bytes memory _data
)
external
view
override
returns (address, uint256, bytes memory)
{
uint24 fee = _data.toUint24(0);
bytes memory path = abi.encodePacked(_sourceToken, fee, _destinationToken);

bytes memory callData = _isSendTokenFixed
? abi.encodeWithSignature(
SWAP_EXACT_INPUT,
ISwapRouter.ExactInputSingleParams(
_sourceToken,
_destinationToken,
fee,
_destinationAddress,
block.timestamp,
_sourceQuantity,
_destinationQuantity,
0
)
) : abi.encodeWithSignature(
SWAP_EXACT_OUTPUT,
ISwapRouter.ExactOutputSingleParams(
_sourceToken,
_destinationToken,
fee,
_destinationAddress,
block.timestamp,
_destinationQuantity,
_sourceQuantity,
0
)
);

return (router, 0, callData);
}

/**
* Returns the address to approve source tokens to for trading. This is the Uniswap V3 router address.
*
* @return address Address of the contract to approve tokens to
*/
function getSpender() external view override returns (address) {
return router;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { AddressArrayUtils } from "../../../lib/AddressArrayUtils.sol";
import { IController } from "../../../interfaces/IController.sol";
import { IPriceOracle } from "../../../interfaces/IPriceOracle.sol";
import { IUniswapV2Pair } from "../../../interfaces/external/IUniswapV2Pair.sol";
import { UniswapV2Library } from "../../../../external/contracts/uniswap/lib/UniswapV2Library.sol";
import { UniswapV2Library } from "../../../../external/contracts/uniswap/v2/lib/UniswapV2Library.sol";
import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol";
import { ResourceIdentifier } from "../../lib/ResourceIdentifier.sol";

Expand Down
File renamed without changes.
Loading