|
| 1 | +/* |
| 2 | + Copyright 2022 Set Labs Inc. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + Unless required by applicable law or agreed to in writing, software |
| 8 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + See the License for the specific language governing permissions and |
| 11 | + limitations under the License. |
| 12 | + SPDX-License-Identifier: Apache License, Version 2.0 |
| 13 | +*/ |
| 14 | + |
| 15 | +pragma solidity 0.6.10; |
| 16 | +pragma experimental "ABIEncoderV2"; |
| 17 | + |
| 18 | +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 19 | +import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol"; |
| 20 | + |
| 21 | +import { IStableSwapPool } from "../../../interfaces/external/IStableSwapPool.sol"; |
| 22 | +import { IWETH } from "../../../interfaces/external/IWETH.sol"; |
| 23 | +import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol"; |
| 24 | + |
| 25 | +/** |
| 26 | + * @title CurveExchangeAdapter |
| 27 | + * @author FlattestWhite |
| 28 | + * |
| 29 | + * Exchange adapter for Curve pools for ERC20 <-> ERC20 |
| 30 | + * exchange contracts. This contract assumes that all tokens |
| 31 | + * being traded are ERC20 tokens. |
| 32 | + * |
| 33 | + * This contract is intended to be used by trade modules to rebalance |
| 34 | + * SetTokens that hold ERC20 tokens as part of its components. |
| 35 | + */ |
| 36 | +contract CurveExchangeAdapter { |
| 37 | + |
| 38 | + using SafeMath for uint256; |
| 39 | + using PreciseUnitMath for uint256; |
| 40 | + |
| 41 | + /* ========= State Variables ========= */ |
| 42 | + |
| 43 | + // Address of ERC20 tokenA |
| 44 | + IERC20 immutable public tokenA; |
| 45 | + // Address of ERC20 tokenB |
| 46 | + IERC20 immutable public tokenB; |
| 47 | + // Index of tokenA |
| 48 | + int128 immutable public tokenAIndex; |
| 49 | + // Index of tokenB |
| 50 | + int128 immutable public tokenBIndex; |
| 51 | + // Address of Curve tokenA/tokenB stableswap pool. |
| 52 | + IStableSwapPool immutable public stableswap; |
| 53 | + |
| 54 | + /* ========= Constructor ========== */ |
| 55 | + |
| 56 | + /** |
| 57 | + * Set state variables |
| 58 | + * |
| 59 | + * @param _tokenA Address of tokenA |
| 60 | + * @param _tokenB Address of tokenB |
| 61 | + * @param _tokenAIndex Index of tokenA in stableswap pool |
| 62 | + * @param _tokenBIndex Index of tokenB in stableswap pool |
| 63 | + * @param _stableswap Address of Curve Stableswap pool |
| 64 | + */ |
| 65 | + constructor( |
| 66 | + IERC20 _tokenA, |
| 67 | + IERC20 _tokenB, |
| 68 | + int128 _tokenAIndex, |
| 69 | + int128 _tokenBIndex, |
| 70 | + IStableSwapPool _stableswap |
| 71 | + ) |
| 72 | + public |
| 73 | + { |
| 74 | + require(_stableswap.coins(uint256(_tokenAIndex)) == address(_tokenA), "Stableswap pool has invalid index for tokenA"); |
| 75 | + require(_stableswap.coins(uint256(_tokenBIndex)) == address(_tokenB), "Stableswap pool has invalid index for tokenB"); |
| 76 | + |
| 77 | + tokenA = _tokenA; |
| 78 | + tokenB = _tokenB; |
| 79 | + tokenAIndex = _tokenAIndex; |
| 80 | + tokenBIndex = _tokenBIndex; |
| 81 | + stableswap = _stableswap; |
| 82 | + |
| 83 | + _tokenA.approve(address(_stableswap), PreciseUnitMath.maxUint256()); |
| 84 | + _tokenB.approve(address(_stableswap), PreciseUnitMath.maxUint256()); |
| 85 | + } |
| 86 | + |
| 87 | + /* ============ External Getter Functions ============ */ |
| 88 | + |
| 89 | + /** |
| 90 | + * Calculate Curve trade encoded calldata. To be invoked on the SetToken. |
| 91 | + * |
| 92 | + * @param _sourceToken The input token. |
| 93 | + * @param _destinationToken The output token. |
| 94 | + * @param _destinationAddress The address where the proceeds of the output is sent to. |
| 95 | + * @param _sourceQuantity Amount of input token. |
| 96 | + * @param _minDestinationQuantity The minimum amount of output token to be received. |
| 97 | + * |
| 98 | + * @return address Target contract address |
| 99 | + * @return uint256 Call value |
| 100 | + * @return bytes Trade calldata |
| 101 | + */ |
| 102 | + function getTradeCalldata( |
| 103 | + address _sourceToken, |
| 104 | + address _destinationToken, |
| 105 | + address _destinationAddress, |
| 106 | + uint256 _sourceQuantity, |
| 107 | + uint256 _minDestinationQuantity, |
| 108 | + bytes memory /* data */ |
| 109 | + ) |
| 110 | + external |
| 111 | + view |
| 112 | + returns (address, uint256, bytes memory) |
| 113 | + { |
| 114 | + require(_sourceToken != _destinationToken, "_sourceToken must not be the same as _destinationToken"); |
| 115 | + require(_sourceToken == address(tokenA) || _sourceToken == address(tokenB), "Invalid sourceToken"); |
| 116 | + require(_destinationToken == address(tokenA) || _destinationToken == address(tokenB), "Invalid destinationToken"); |
| 117 | + |
| 118 | + bytes memory callData = abi.encodeWithSignature("trade(address,address,uint256,uint256,address)", |
| 119 | + _sourceToken, |
| 120 | + _destinationToken, |
| 121 | + _sourceQuantity, |
| 122 | + _minDestinationQuantity, |
| 123 | + _destinationAddress |
| 124 | + ); |
| 125 | + return (address(this), 0, callData); |
| 126 | + } |
| 127 | + |
| 128 | + /* ============ External Functions ============ */ |
| 129 | + |
| 130 | + /** |
| 131 | + * Invokes an exchange on Curve Stableswap pool. To be invoked on the SetToken. |
| 132 | + * |
| 133 | + * @param _sourceToken The input token. |
| 134 | + * @param _destinationToken The output token. |
| 135 | + * @param _sourceQuantity Amount of input token. |
| 136 | + * @param _minDestinationQuantity The minimum amount of output token to be received. |
| 137 | + * @param _destinationAddress The address where the proceeds of the output is sent to. |
| 138 | + */ |
| 139 | + function trade( |
| 140 | + address _sourceToken, |
| 141 | + address _destinationToken, |
| 142 | + uint256 _sourceQuantity, |
| 143 | + uint256 _minDestinationQuantity, |
| 144 | + address _destinationAddress |
| 145 | + ) external { |
| 146 | + require(_sourceToken != _destinationToken, "_sourceToken must not be the same as _destinationToken"); |
| 147 | + if (_sourceToken == address(tokenA) && _destinationToken == address(tokenB)) { |
| 148 | + // Transfers sourceToken |
| 149 | + IERC20(_sourceToken).transferFrom(msg.sender, address(this), _sourceQuantity); |
| 150 | + |
| 151 | + // Exchange sourceToken for destinationToken |
| 152 | + uint256 amountOut = stableswap.exchange(tokenAIndex, tokenBIndex, _sourceQuantity, _minDestinationQuantity); |
| 153 | + |
| 154 | + // Transfer destinationToken to destinationAddress |
| 155 | + IERC20(_destinationToken).transfer(_destinationAddress, amountOut); |
| 156 | + } else if (_sourceToken == address(tokenB) && _destinationToken == address(tokenA)) { |
| 157 | + // Transfers sourceToken |
| 158 | + IERC20(_sourceToken).transferFrom(msg.sender, address(this), _sourceQuantity); |
| 159 | + |
| 160 | + // Exchange sourceToken for destinationToken |
| 161 | + uint256 amountOut = stableswap.exchange(tokenBIndex, tokenAIndex, _sourceQuantity, _minDestinationQuantity) ; |
| 162 | + |
| 163 | + // Transfer destinationToken to destinationAddress |
| 164 | + IERC20(_destinationToken).transfer(_destinationAddress, amountOut); |
| 165 | + } else { |
| 166 | + revert("Invalid _sourceToken or _destinationToken or both"); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Returns the address to approve source tokens to for trading. In this case, the address of this contract. |
| 172 | + * |
| 173 | + * @return address Address of the contract to approve tokens to. |
| 174 | + */ |
| 175 | + function getSpender() external view returns (address) { |
| 176 | + return address(this); |
| 177 | + } |
| 178 | +} |
0 commit comments