Skip to content
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

feat(adapter): Add Curve trade adapter [SIM-173] #238

Merged
merged 21 commits into from
Apr 6, 2022
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
29 changes: 29 additions & 0 deletions contracts/interfaces/external/IStableSwapStEth.sol
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);
}
81 changes: 81 additions & 0 deletions contracts/mocks/external/CurveStEthStableswapMock.sol
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 contracts/protocol/integration/exchange/CurveStEthExchangeAdapter.sol
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";
cgewecke marked this conversation as resolved.
Show resolved Hide resolved
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
}
Loading