Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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/external/ISwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ interface ISwapRouter {
/// @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,158 @@
/*
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;

/* ============ 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))";

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

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

/* ============ 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. This is different from UniswapV3ExchangeAdapter,
* where `_data` represents UniswapV3 trade path vs just the pool fees percentage.
*
* @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 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;
}

/**
* Helper that returns encoded fee value.
*
* @param fee UniswapV3 pool fee percentage, expressed in hundredths of a bip
*
* @return bytes Encoded fee value
*/
function getEncodedFeeData(uint24 fee) external view returns (bytes memory) {
return abi.encodePacked(fee);
}
}
14 changes: 7 additions & 7 deletions external/abi/uniswap/v3/SwapRouter.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion external/contracts/uniswap/v3/UniswapV3Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ contract UniswapV3Factory is IUniswapV3Factory, UniswapV3PoolDeployer, NoDelegat
feeAmountTickSpacing[fee] = tickSpacing;
emit FeeAmountEnabled(fee, tickSpacing);
}
}
}
16 changes: 8 additions & 8 deletions external/contracts/uniswap/v3/lib/BytesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
* @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity.
* The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
*/
pragma solidity >=0.5.0 <0.8.0;
pragma solidity 0.6.10;

library BytesLib {
function slice(
bytes memory _bytes,
uint256 _start,
uint256 _length
) internal pure returns (bytes memory) {
require(_length + 31 >= _length, 'slice_overflow');
require(_start + _length >= _start, 'slice_overflow');
require(_bytes.length >= _start + _length, 'slice_outOfBounds');
require(_length + 31 >= _length, "slice_overflow");
require(_start + _length >= _start, "slice_overflow");
require(_bytes.length >= _start + _length, "slice_outOfBounds");

bytes memory tempBytes;

Expand Down Expand Up @@ -76,8 +76,8 @@ library BytesLib {
}

function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) {
require(_start + 20 >= _start, 'toAddress_overflow');
require(_bytes.length >= _start + 20, 'toAddress_outOfBounds');
require(_start + 20 >= _start, "toAddress_overflow");
require(_bytes.length >= _start + 20, "toAddress_outOfBounds");
address tempAddress;

assembly {
Expand All @@ -88,8 +88,8 @@ library BytesLib {
}

function toUint24(bytes memory _bytes, uint256 _start) internal pure returns (uint24) {
require(_start + 3 >= _start, 'toUint24_overflow');
require(_bytes.length >= _start + 3, 'toUint24_outOfBounds');
require(_start + 3 >= _start, "toUint24_overflow");
require(_bytes.length >= _start + 3, "toUint24_outOfBounds");
uint24 tempUint;

assembly {
Expand Down
Loading