-
Notifications
You must be signed in to change notification settings - Fork 103
Support Curve stableswap pools #253
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
cgewecke
merged 10 commits into
SetProtocol:master
from
FlattestWhite:support-curve-steth-weth-pool
May 18, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bde3128
.
FlattestWhite 8167c8f
.
FlattestWhite ab431d2
fix test
FlattestWhite 67dd890
.
FlattestWhite 519ee67
add adapter
FlattestWhite 1022fbf
.
FlattestWhite d064c51
.
FlattestWhite 8b48a18
integration test
FlattestWhite f4b8bae
get forked tests passing
FlattestWhite 011b53a
fix steth alm test
FlattestWhite 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
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
178 changes: 178 additions & 0 deletions
178
contracts/protocol/integration/exchange/CurveExchangeAdapter.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,178 @@ | ||
/* | ||
Copyright 2022 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 { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol"; | ||
|
||
import { IStableSwapPool } from "../../../interfaces/external/IStableSwapPool.sol"; | ||
import { IWETH } from "../../../interfaces/external/IWETH.sol"; | ||
import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol"; | ||
|
||
/** | ||
* @title CurveExchangeAdapter | ||
* @author FlattestWhite | ||
* | ||
* Exchange adapter for Curve pools for ERC20 <-> ERC20 | ||
* exchange contracts. This contract assumes that all tokens | ||
* being traded are ERC20 tokens. | ||
* | ||
* This contract is intended to be used by trade modules to rebalance | ||
* SetTokens that hold ERC20 tokens as part of its components. | ||
*/ | ||
contract CurveExchangeAdapter { | ||
|
||
using SafeMath for uint256; | ||
using PreciseUnitMath for uint256; | ||
|
||
/* ========= State Variables ========= */ | ||
|
||
// Address of ERC20 tokenA | ||
IERC20 immutable public tokenA; | ||
// Address of ERC20 tokenB | ||
IERC20 immutable public tokenB; | ||
// Index of tokenA | ||
int128 immutable public tokenAIndex; | ||
// Index of tokenB | ||
int128 immutable public tokenBIndex; | ||
// Address of Curve tokenA/tokenB stableswap pool. | ||
IStableSwapPool immutable public stableswap; | ||
|
||
/* ========= Constructor ========== */ | ||
|
||
/** | ||
* Set state variables | ||
* | ||
* @param _tokenA Address of tokenA | ||
* @param _tokenB Address of tokenB | ||
* @param _tokenAIndex Index of tokenA in stableswap pool | ||
* @param _tokenBIndex Index of tokenB in stableswap pool | ||
* @param _stableswap Address of Curve Stableswap pool | ||
*/ | ||
constructor( | ||
IERC20 _tokenA, | ||
IERC20 _tokenB, | ||
int128 _tokenAIndex, | ||
int128 _tokenBIndex, | ||
IStableSwapPool _stableswap | ||
) | ||
public | ||
{ | ||
require(_stableswap.coins(uint256(_tokenAIndex)) == address(_tokenA), "Stableswap pool has invalid index for tokenA"); | ||
require(_stableswap.coins(uint256(_tokenBIndex)) == address(_tokenB), "Stableswap pool has invalid index for tokenB"); | ||
|
||
tokenA = _tokenA; | ||
tokenB = _tokenB; | ||
tokenAIndex = _tokenAIndex; | ||
tokenBIndex = _tokenBIndex; | ||
stableswap = _stableswap; | ||
|
||
_tokenA.approve(address(_stableswap), PreciseUnitMath.maxUint256()); | ||
_tokenB.approve(address(_stableswap), PreciseUnitMath.maxUint256()); | ||
} | ||
|
||
/* ============ External Getter Functions ============ */ | ||
|
||
/** | ||
* Calculate Curve trade encoded calldata. To be invoked on the SetToken. | ||
* | ||
* @param _sourceToken The input token. | ||
* @param _destinationToken The output token. | ||
* @param _destinationAddress The address where the proceeds of the output is sent to. | ||
* @param _sourceQuantity Amount of input token. | ||
* @param _minDestinationQuantity The minimum amount of output token to be received. | ||
* | ||
* @return address Target contract address | ||
* @return uint256 Call value | ||
* @return bytes Trade calldata | ||
*/ | ||
function getTradeCalldata( | ||
address _sourceToken, | ||
address _destinationToken, | ||
address _destinationAddress, | ||
uint256 _sourceQuantity, | ||
uint256 _minDestinationQuantity, | ||
bytes memory /* data */ | ||
) | ||
external | ||
view | ||
returns (address, uint256, bytes memory) | ||
{ | ||
require(_sourceToken != _destinationToken, "_sourceToken must not be the same as _destinationToken"); | ||
require(_sourceToken == address(tokenA) || _sourceToken == address(tokenB), "Invalid sourceToken"); | ||
require(_destinationToken == address(tokenA) || _destinationToken == address(tokenB), "Invalid destinationToken"); | ||
|
||
bytes memory callData = abi.encodeWithSignature("trade(address,address,uint256,uint256,address)", | ||
_sourceToken, | ||
_destinationToken, | ||
_sourceQuantity, | ||
_minDestinationQuantity, | ||
_destinationAddress | ||
); | ||
return (address(this), 0, callData); | ||
} | ||
|
||
/* ============ External Functions ============ */ | ||
|
||
/** | ||
* Invokes an exchange on Curve Stableswap pool. To be invoked on the SetToken. | ||
* | ||
* @param _sourceToken The input token. | ||
* @param _destinationToken The output token. | ||
* @param _sourceQuantity Amount of input token. | ||
* @param _minDestinationQuantity The minimum amount of output token to be received. | ||
* @param _destinationAddress The address where the proceeds of the output is sent to. | ||
*/ | ||
function trade( | ||
address _sourceToken, | ||
address _destinationToken, | ||
uint256 _sourceQuantity, | ||
uint256 _minDestinationQuantity, | ||
address _destinationAddress | ||
) external { | ||
require(_sourceToken != _destinationToken, "_sourceToken must not be the same as _destinationToken"); | ||
if (_sourceToken == address(tokenA) && _destinationToken == address(tokenB)) { | ||
// Transfers sourceToken | ||
IERC20(_sourceToken).transferFrom(msg.sender, address(this), _sourceQuantity); | ||
|
||
// Exchange sourceToken for destinationToken | ||
uint256 amountOut = stableswap.exchange(tokenAIndex, tokenBIndex, _sourceQuantity, _minDestinationQuantity); | ||
|
||
// Transfer destinationToken to destinationAddress | ||
IERC20(_destinationToken).transfer(_destinationAddress, amountOut); | ||
} else if (_sourceToken == address(tokenB) && _destinationToken == address(tokenA)) { | ||
// Transfers sourceToken | ||
IERC20(_sourceToken).transferFrom(msg.sender, address(this), _sourceQuantity); | ||
|
||
// Exchange sourceToken for destinationToken | ||
uint256 amountOut = stableswap.exchange(tokenBIndex, tokenAIndex, _sourceQuantity, _minDestinationQuantity) ; | ||
|
||
// Transfer destinationToken to destinationAddress | ||
IERC20(_destinationToken).transfer(_destinationAddress, amountOut); | ||
} else { | ||
revert("Invalid _sourceToken or _destinationToken or both"); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the address to approve source tokens to for trading. In this case, the address of this contract. | ||
* | ||
* @return address Address of the contract to approve tokens to. | ||
*/ | ||
function getSpender() external view returns (address) { | ||
return address(this); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support eth transfers for mocking in unit tests.