Skip to content

Commit

Permalink
fix(adapter): Support Curve stableswap pools (SetProtocol#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlattestWhite authored May 18, 2022
1 parent 7ef234b commit 777ed94
Show file tree
Hide file tree
Showing 18 changed files with 1,394 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
pragma solidity 0.6.10;

/**
* Curve StableSwap pool for stETH.
* Curve StableSwap ERC20 <-> ERC20 pool.
*/
interface IStableSwapStEth {
interface IStableSwapPool {

function exchange(
int128 i,
int128 j,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.s
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 {
// Minimal Curve Stableswap Pool
contract CurveStableswapMock is ReentrancyGuard {

address public constant ETH_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

using SafeERC20 for IERC20;
using SafeMath for uint256;
Expand All @@ -35,13 +37,20 @@ contract CurveStEthStableswapMock is ReentrancyGuard {
address[] tokens;

constructor(address[] memory _tokens) public {
require(_tokens[1] != address(0));
for (uint i = 0; i < _tokens.length; i++) {
require(_tokens[i] != 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]);
for (uint i = 0; i < _amounts.length; i++) {
if (tokens[i] == ETH_TOKEN_ADDRESS) {
require(_amounts[i] == msg.value, "Eth sent should equal amount");
continue;
}
IERC20(tokens[i]).safeTransferFrom(msg.sender, address(this), _amounts[i]);
}
return _min_mint_amount;
}

Expand All @@ -56,18 +65,19 @@ contract CurveStEthStableswapMock is ReentrancyGuard {
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);

if (tokens[uint256(_i)] == ETH_TOKEN_ADDRESS) {
require(_dx == msg.value);
} else {
IERC20(tokens[uint256(_i)]).transferFrom(msg.sender, address(this), _dx);
}

if (tokens[uint256(_j)] == ETH_TOKEN_ADDRESS) {
Address.sendValue(payable(msg.sender), _min_dy);
} else {
revert("Invalid index values");
IERC20(tokens[uint256(_j)]).transfer(msg.sender, _min_dy);
}
return _dx;
return _min_dy;
}

/**
Expand Down
178 changes: 178 additions & 0 deletions contracts/protocol/integration/exchange/CurveExchangeAdapter.sol
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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 { IStableSwapPool } from "../../../interfaces/external/IStableSwapPool.sol";
import { IWETH } from "../../../interfaces/external/IWETH.sol";
import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol";

Expand Down Expand Up @@ -46,7 +46,7 @@ contract CurveStEthExchangeAdapter {
// Address of stETH token.
IERC20 immutable public stETH;
// Address of Curve Eth/StEth stableswap pool.
IStableSwapStEth immutable public stableswap;
IStableSwapPool immutable public stableswap;
// Index for ETH for Curve stableswap pool.
int128 internal constant ETH_INDEX = 0;
// Index for stETH for Curve stableswap pool.
Expand All @@ -64,7 +64,7 @@ contract CurveStEthExchangeAdapter {
constructor(
IWETH _weth,
IERC20 _stETH,
IStableSwapStEth _stableswap
IStableSwapPool _stableswap
)
public
{
Expand Down
4 changes: 2 additions & 2 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require("dotenv").config();
require('hardhat-contract-sizer');
require("hardhat-contract-sizer");

import chalk from "chalk";
import { HardhatUserConfig } from "hardhat/config";
Expand All @@ -12,7 +12,7 @@ import "./tasks";

const forkingConfig = {
url: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_TOKEN}`,
blockNumber: 12198000,
blockNumber: 14792479,
};

const mochaConfig = {
Expand Down
Loading

0 comments on commit 777ed94

Please sign in to comment.