-
Notifications
You must be signed in to change notification settings - Fork 107
Add uniswap v3 index exchange adapter #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
612f742
Add external Uniswap V3 contracts
0xSachinK 5db6add
Add UniswapV3 Index Exchange Adapter contract
0xSachinK 7200248
Add tests
0xSachinK 3896fd3
Fix coverage
0xSachinK 6d91eaa
Improve javadocs and add getEncodedTradePath() test
0xSachinK a72cbf1
Use soldityPack in getEncodedTradePath test
0xSachinK 28ce969
Change compiler version to 0.6.10
0xSachinK 33a934e
Use exactInputSingle and exactOutputSingle functions instead of exact…
0xSachinK 1754237
Merge branch 'master' into sachin/uniswap-v3-index-exchange-adapter
0xSachinK 7d54407
Merge branch 'master' into sachin/uniswap-v3-index-exchange-adapter
0xSachinK 700820a
Move BytesLib to external/contracts/uniswap/v3
0xSachinK 283fca4
Add Note to set exchange data in GIM
0xSachinK 61e6367
Remove @uniswap/v3-core dependency
0xSachinK 9ce9ffe
Add getEncodedFeeData function and add suggested changes
0xSachinK 99812ff
Merge branch 'master' into sachin/uniswap-v3-index-exchange-adapter
0xSachinK 7e8ab6a
Refactor tests to use UniswapV3 Fixture
0xSachinK 3379a1d
Fix uniswap/v3/SwapRotuer ABI
0xSachinK 464f71f
Add integration tests with GIM
0xSachinK 78b26d5
Fix comment
0xSachinK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
contracts/protocol/integration/index-exchange/UniswapV3IndexExchangeAdapter.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
0xSachinK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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( | ||
0xSachinK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
0xSachinK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.