-
Notifications
You must be signed in to change notification settings - Fork 103
feat(adapter): Add Curve trade adapter [SIM-173] #238
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 21 commits into
SetProtocol:master
from
FlattestWhite:curve-trade-adapter
Apr 6, 2022
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8c482ba
copy paste and add tests
FlattestWhite 5d97e43
rename to exchange
FlattestWhite 47c1df3
add abi and bytecode of curve ethsteth exchange
FlattestWhite f22ecde
tests fail at exchange
FlattestWhite b0aac25
add more tests
FlattestWhite ee3c77e
update comments
FlattestWhite 4e9e70c
get forked mainnet tests passing
FlattestWhite 63febcd
add test coverage
FlattestWhite a05d250
fix test
FlattestWhite cfb1662
add natspec
FlattestWhite a5454ad
remove yarn lock changes
FlattestWhite dc725fa
review comments part 1
FlattestWhite f5eff9f
forked mainnet tests from review wip
FlattestWhite 56bd44e
test
FlattestWhite 20ab186
comment out unused parts for now
FlattestWhite f085d6c
Add Curve trade adapter ALM integration test
cgewecke 960fefc
Remove Curve adapter TradeModule integration test
cgewecke 32d6c08
Fix tsc / lint
cgewecke 73cc58b
Remove stray log line in tests
cgewecke e3ce683
Fix test comment
cgewecke 26120b1
Revert test comment change
cgewecke 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
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; | ||
|
||
/** | ||
* Curve StableSwap pool for stETH. | ||
*/ | ||
interface IStableSwapStEth { | ||
function exchange( | ||
int128 i, | ||
int128 j, | ||
uint256 dx, | ||
uint256 min_dy | ||
) external payable returns (uint256); | ||
|
||
function coins(uint256) external view returns (address); | ||
} |
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,81 @@ | ||
/* | ||
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; | ||
|
||
import { Address } from "@openzeppelin/contracts/utils/Address.sol"; | ||
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; | ||
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; | ||
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol"; | ||
|
||
// Minimal Curve Eth/StEth Stableswap Pool | ||
contract CurveStEthStableswapMock is ReentrancyGuard { | ||
|
||
using SafeERC20 for IERC20; | ||
using SafeMath for uint256; | ||
using SafeMath for int128; | ||
using Address for address; | ||
|
||
address[] tokens; | ||
|
||
constructor(address[] memory _tokens) public { | ||
require(_tokens[1] != address(0)); | ||
tokens = _tokens; | ||
} | ||
|
||
function add_liquidity(uint256[] memory _amounts, uint256 _min_mint_amount) payable external nonReentrant returns (uint256) { | ||
require(_amounts[0] == msg.value, "Eth sent should equal amount"); | ||
IERC20(tokens[1]).safeTransferFrom(msg.sender, address(this), _amounts[1]); | ||
return _min_mint_amount; | ||
} | ||
|
||
/** | ||
* @dev Index values can be found via the `coins` public getter method | ||
* @param _i Index value for the coin to send | ||
* @param _j Index value of the coin to receive | ||
* @param _dx Amount of `i` being exchanged | ||
* @param _min_dy Minimum amount of `j` to receive | ||
* @return Actual amount of `j` received | ||
*/ | ||
function exchange(int128 _i, int128 _j, uint256 _dx, uint256 _min_dy) payable external nonReentrant returns (uint256) { | ||
require(_i != _j); | ||
require(_dx == _min_dy); | ||
if (_i == 0 && _j == 1) { | ||
// The caller has sent eth receive stETH | ||
require(_dx == msg.value); | ||
IERC20(tokens[1]).safeTransfer(msg.sender, _dx); | ||
} else if (_j == 0 && _i == 1) { | ||
// The caller has sent stETH to receive ETH | ||
IERC20(tokens[1]).safeTransferFrom(msg.sender, address(this), _dx); | ||
Address.sendValue(msg.sender, _dx); | ||
} else { | ||
revert("Invalid index values"); | ||
} | ||
return _dx; | ||
} | ||
|
||
/** | ||
* @param _index Index to look up address for. | ||
* | ||
* @return address Address of the token at index | ||
*/ | ||
function coins(uint256 _index) external view returns (address) { | ||
return tokens[_index]; | ||
} | ||
} |
205 changes: 205 additions & 0 deletions
205
contracts/protocol/integration/exchange/CurveStEthExchangeAdapter.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,205 @@ | ||
/* | ||
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 { IStableSwapStEth } from "../../../interfaces/external/IStableSwapStEth.sol"; | ||
import { IWETH } from "../../../interfaces/external/IWETH.sol"; | ||
import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol"; | ||
|
||
/** | ||
* @title CurveStEthExchangeAdapter | ||
* @author FlattestWhite & ncitron | ||
* | ||
* Exchange adapter for the specialized Curve stETH <-> ETH | ||
* exchange contracts. Implements helper functionality for | ||
* wrapping and unwrapping WETH since the curve exchange uses | ||
* raw ETH. | ||
cgewecke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* This contract is intended to be used by trade modules to rebalance | ||
* SetTokens that hold stETH as part of its components. | ||
*/ | ||
contract CurveStEthExchangeAdapter { | ||
|
||
using SafeMath for uint256; | ||
using PreciseUnitMath for uint256; | ||
|
||
/* ========= State Variables ========= */ | ||
|
||
// Address of WETH token. | ||
IWETH immutable public weth; | ||
// Address of stETH token. | ||
IERC20 immutable public stETH; | ||
// Address of Curve Eth/StEth stableswap pool. | ||
IStableSwapStEth immutable public stableswap; | ||
// Index for ETH for Curve stableswap pool. | ||
int128 internal constant ETH_INDEX = 0; | ||
// Index for stETH for Curve stableswap pool. | ||
int128 internal constant STETH_INDEX = 1; | ||
|
||
/* ========= Constructor ========== */ | ||
|
||
/** | ||
* Set state variables | ||
* | ||
* @param _weth Address of WETH token | ||
* @param _stETH Address of stETH token | ||
* @param _stableswap Address of Curve Eth/StEth Stableswap pool | ||
*/ | ||
constructor( | ||
IWETH _weth, | ||
IERC20 _stETH, | ||
IStableSwapStEth _stableswap | ||
) | ||
public | ||
{ | ||
weth = _weth; | ||
stETH = _stETH; | ||
stableswap = _stableswap; | ||
|
||
require(_stableswap.coins(0) == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, "Stableswap pool has invalid ETH_INDEX"); | ||
require(_stableswap.coins(1) == address(_stETH), "Stableswap pool has invalid STETH_INDEX"); | ||
|
||
_stETH.approve(address(_stableswap), PreciseUnitMath.maxUint256()); | ||
} | ||
|
||
/* ======== External Functions ======== */ | ||
|
||
/** | ||
* Buys stEth using WETH | ||
* | ||
* @param _sourceQuantity The amount of WETH as input. | ||
* @param _minDestinationQuantity The minimum amounnt of stETH to receive. | ||
* @param _destinationAddress The address to send the trade proceeds to. | ||
*/ | ||
function buyStEth( | ||
uint256 _sourceQuantity, | ||
uint256 _minDestinationQuantity, | ||
address _destinationAddress | ||
) | ||
external | ||
{ | ||
// transfer weth | ||
weth.transferFrom(msg.sender, address(this), _sourceQuantity); | ||
|
||
// unwrap weth | ||
weth.withdraw(_sourceQuantity); | ||
|
||
// buy stETH | ||
uint256 amountOut = stableswap.exchange{value: _sourceQuantity} ( | ||
ETH_INDEX, | ||
STETH_INDEX, | ||
_sourceQuantity, | ||
_minDestinationQuantity | ||
); | ||
|
||
// transfer proceeds | ||
stETH.transfer(_destinationAddress, amountOut); | ||
} | ||
|
||
/** | ||
* Sells stETH for WETH | ||
* | ||
* @param _sourceQuantity The amount of stETH as input. | ||
* @param _minDestinationQuantity The minimum amount of WETH to receive. | ||
* @param _destinationAddress The address to send the trade proceeds to. | ||
*/ | ||
function sellStEth( | ||
uint256 _sourceQuantity, | ||
uint256 _minDestinationQuantity, | ||
address _destinationAddress | ||
) | ||
external | ||
{ | ||
// transfer stETH | ||
stETH.transferFrom(msg.sender, address(this), _sourceQuantity); | ||
|
||
// sell stETH | ||
uint256 amountOut = stableswap.exchange(STETH_INDEX, ETH_INDEX, _sourceQuantity, _minDestinationQuantity); | ||
|
||
// wrap eth | ||
weth.deposit{value: amountOut}(); | ||
|
||
// transfer proceeds | ||
weth.transfer(_destinationAddress, amountOut); | ||
} | ||
|
||
/* ============ External Getter Functions ============ */ | ||
|
||
/** | ||
* Calculate Curve trade encoded calldata. To be invoked on the SetToken. | ||
* | ||
* @param _sourceToken Either WETH or stETH. The input token. | ||
* @param _destinationToken Either WETH or stETH. 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) | ||
{ | ||
if (_sourceToken == address(weth) && _destinationToken == address(stETH)) { | ||
bytes memory callData = abi.encodeWithSignature( | ||
"buyStEth(uint256,uint256,address)", | ||
_sourceQuantity, | ||
_minDestinationQuantity, | ||
_destinationAddress | ||
); | ||
return (address(this), 0, callData); | ||
} else if (_sourceToken == address(stETH) && _destinationToken == address(weth)) { | ||
bytes memory callData = abi.encodeWithSignature( | ||
"sellStEth(uint256,uint256,address)", | ||
_sourceQuantity, | ||
_minDestinationQuantity, | ||
_destinationAddress | ||
); | ||
return (address(this), 0, callData); | ||
} else { | ||
revert("Must swap between weth and stETH"); | ||
} | ||
} | ||
|
||
/** | ||
* 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 function is invoked when: | ||
* 1. WETH is withdrawn for ETH. | ||
* 2. ETH is received from Curve stableswap pool on exchange call. | ||
*/ | ||
receive() external payable {} | ||
cgewecke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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.