dsa-connectors-old/contracts/connectors/curve.sol

228 lines
8.1 KiB
Solidity
Raw Normal View History

2020-05-05 01:54:10 +00:00
pragma solidity ^0.6.0;
2020-05-08 02:56:43 +00:00
// import files from common directory
import { TokenInterface , MemoryInterface, EventInterface} from "../common/interfaces.sol";
import { Stores } from "../common/stores.sol";
import { DSMath } from "../common/math.sol";
2020-05-05 01:54:10 +00:00
interface ICurve {
2020-05-05 08:29:17 +00:00
function underlying_coins(int128 tokenId) external view returns (address token);
2020-05-05 01:54:10 +00:00
function calc_token_amount(uint256[4] calldata amounts, bool deposit) external returns (uint256 amount);
function add_liquidity(uint256[4] calldata amounts, uint256 min_mint_amount) external;
2020-05-05 08:29:17 +00:00
function get_dy(int128 sellTokenId, int128 buyTokenId, uint256 sellTokenAmt) external returns (uint256 buyTokenAmt);
function exchange(int128 sellTokenId, int128 buyTokenId, uint256 sellTokenAmt, uint256 minBuyToken) external;
function remove_liquidity_imbalance(uint256[4] calldata amounts, uint256 max_burn_amount) external;
2020-05-05 01:54:10 +00:00
}
interface ICurveZap {
function calc_withdraw_one_coin(uint256 _token_amount, int128 i) external returns (uint256 amount);
}
2020-05-05 06:05:36 +00:00
contract CurveHelpers is Stores, DSMath {
/**
* @dev Return Curve Swap Address
*/
function getCurveSwapAddr() internal pure returns (address) {
return 0xA5407eAE9Ba41422680e2e00537571bcC53efBfD;
}
2020-05-05 01:54:10 +00:00
2020-05-05 06:05:36 +00:00
/**
* @dev Return Curve Token Address
*/
function getCurveTokenAddr() internal pure returns (address) {
return 0xC25a3A3b969415c80451098fa907EC722572917F;
}
2020-05-05 01:54:10 +00:00
2020-05-05 06:05:36 +00:00
/**
* @dev Return Curve Zap Address
*/
function getCurveZapAddr() internal pure returns (address) {
return 0xFCBa3E75865d2d561BE8D220616520c171F12851;
}
2020-05-05 01:54:10 +00:00
2020-05-05 06:05:36 +00:00
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
amt = (_amt / 10 ** (18 - _dec));
}
2020-05-05 01:54:10 +00:00
2020-05-05 06:05:36 +00:00
function convertTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
amt = mul(_amt, 10 ** (18 - _dec));
2020-05-05 01:54:10 +00:00
}
2020-05-05 06:05:36 +00:00
function getTokenI(address token) internal pure returns (int128 i) {
if (token == address(0x6B175474E89094C44Da98b954EedeAC495271d0F)) {
// DAI Token
i = 0;
} else if (token == address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48)) {
// USDC Token
i = 1;
} else if (token == address(0xdAC17F958D2ee523a2206206994597C13D831ec7)) {
// USDT Token
i = 2;
} else if (token == address(0x57Ab1ec28D129707052df4dF418D58a2D46d5f51)) {
// sUSD Token
i = 3;
} else {
revert("token-not-found.");
2020-05-05 06:05:36 +00:00
}
2020-05-05 01:54:10 +00:00
}
2020-05-05 06:05:36 +00:00
function getTokenAddr(ICurve curve, uint256 i) internal view returns (address token) {
token = curve.underlying_coins(int128(i));
require(token != address(0), "token-not-found.");
2020-05-05 01:54:10 +00:00
}
2020-05-05 06:05:36 +00:00
}
contract CurveProtocol is CurveHelpers {
event LogSell(
address indexed buyToken,
address indexed sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 getId,
uint256 setId
);
2020-05-05 12:30:53 +00:00
event LogDeposit(address token, uint256 amt, uint256 mintAmt, uint256 getId, uint256 setId);
event LogWithdraw(address token, uint256 amt, uint256 burnAmt, uint256 getId, uint256 setId);
2020-05-08 02:12:57 +00:00
/**
* @dev Sell Stable ERC20_Token.
* @param buyAddr buying token address.
* @param sellAddr selling token amount.
* @param sellAmt selling token amount.
* @param unitAmt unit amount of buyAmt/sellAmt with slippage.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
2020-05-05 07:47:07 +00:00
function sell(
address buyAddr,
address sellAddr,
uint sellAmt,
uint unitAmt,
uint getId,
uint setId
2020-05-08 19:49:09 +00:00
) external payable {
2020-05-05 06:05:36 +00:00
uint _sellAmt = getUint(getId, sellAmt);
ICurve curve = ICurve(getCurveSwapAddr());
TokenInterface _buyToken = TokenInterface(buyAddr);
TokenInterface _sellToken = TokenInterface(sellAddr);
_sellAmt = _sellAmt == uint(-1) ? _sellToken.balanceOf(address(this)) : _sellAmt;
_sellToken.approve(address(curve), _sellAmt);
2020-05-08 02:21:05 +00:00
uint _slippageAmt = convert18ToDec(_buyToken.decimals(), wmul(unitAmt, convertTo18(_sellToken.decimals(), _sellAmt)));
2020-05-05 06:05:36 +00:00
2020-05-05 08:29:17 +00:00
uint _buyAmt = curve.get_dy(getTokenI(sellAddr), getTokenI(buyAddr), _sellAmt);
2020-05-05 01:54:10 +00:00
2020-05-05 07:47:07 +00:00
curve.exchange(getTokenI(sellAddr), getTokenI(buyAddr), _sellAmt, _slippageAmt);
2020-05-05 06:05:36 +00:00
setUint(setId, _buyAmt);
emit LogSell(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
bytes32 _eventCode = keccak256("LogSell(address,address,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
emitEvent(_eventCode, _eventParam);
2020-05-05 01:54:10 +00:00
}
2020-05-08 02:12:57 +00:00
/**
* @dev Deposit Token.
* @param token token address.
* @param amt token amount.
* @param unitAmt unit amount of curve_amt/token_amt with slippage.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
2020-05-05 12:30:53 +00:00
function deposit(
address token,
uint amt,
uint unitAmt,
uint getId,
uint setId
2020-05-08 19:49:09 +00:00
) external payable {
2020-05-05 08:29:17 +00:00
uint256 _amt = getUint(getId, amt);
2020-05-05 23:11:16 +00:00
TokenInterface tokenContract = TokenInterface(token);
2020-05-05 06:05:36 +00:00
2020-05-05 23:11:16 +00:00
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
2020-05-05 08:29:17 +00:00
uint[4] memory _amts;
_amts[uint(getTokenI(token))] = _amt;
2020-05-06 01:35:34 +00:00
tokenContract.approve(getCurveSwapAddr(), _amt);
2020-05-05 23:11:16 +00:00
uint _amt18 = convertTo18(tokenContract.decimals(), _amt);
2020-05-05 08:29:17 +00:00
uint _slippageAmt = wmul(unitAmt, _amt18);
TokenInterface curveTokenContract = TokenInterface(getCurveTokenAddr());
uint initialCurveBal = curveTokenContract.balanceOf(address(this));
ICurve(getCurveSwapAddr()).add_liquidity(_amts, _slippageAmt);
uint finalCurveBal = curveTokenContract.balanceOf(address(this));
2020-05-05 01:54:10 +00:00
2020-05-05 08:29:17 +00:00
uint mintAmt = sub(finalCurveBal, initialCurveBal);
2020-05-05 06:05:36 +00:00
2020-05-06 05:06:15 +00:00
setUint(setId, mintAmt);
2020-05-05 23:12:52 +00:00
2020-05-05 12:30:53 +00:00
emit LogDeposit(token, _amt, mintAmt, getId, setId);
bytes32 _eventCode = keccak256("LogDeposit(address,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, _amt, mintAmt, getId, setId);
2020-05-05 06:05:36 +00:00
emitEvent(_eventCode, _eventParam);
2020-05-05 01:54:10 +00:00
}
2020-05-08 02:12:57 +00:00
/**
* @dev Withdraw Token.
* @param token token address.
* @param amt token amount.
* @param unitAmt unit amount of curve_amt/token_amt with slippage.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
2020-05-06 05:05:03 +00:00
function withdraw(
address token,
uint256 amt,
uint256 unitAmt,
uint getId,
uint setId
2020-05-08 19:49:09 +00:00
) external payable {
2020-05-05 06:05:36 +00:00
uint _amt = getUint(getId, amt);
2020-05-05 12:30:53 +00:00
int128 tokenId = getTokenI(token);
2020-05-05 06:05:36 +00:00
TokenInterface curveTokenContract = TokenInterface(getCurveTokenAddr());
ICurveZap curveZap = ICurveZap(getCurveZapAddr());
2020-05-06 02:33:13 +00:00
ICurve curveSwap = ICurve(getCurveSwapAddr());
2020-05-05 06:05:36 +00:00
2020-05-06 01:35:34 +00:00
uint _curveAmt;
2020-05-06 02:33:13 +00:00
uint[4] memory _amts;
2020-05-06 01:35:34 +00:00
if (_amt == uint(-1)) {
_curveAmt = curveTokenContract.balanceOf(address(this));
_amt = curveZap.calc_withdraw_one_coin(_curveAmt, tokenId);
2020-05-06 02:33:13 +00:00
_amts[uint(tokenId)] = _amt;
2020-05-06 01:35:34 +00:00
} else {
_amts[uint(tokenId)] = _amt;
2020-05-06 02:33:13 +00:00
_curveAmt = curveSwap.calc_token_amount(_amts, false);
2020-05-06 01:35:34 +00:00
}
2020-05-05 01:54:10 +00:00
2020-05-05 12:30:53 +00:00
uint _amt18 = convertTo18(TokenInterface(token).decimals(), _amt);
uint _slippageAmt = wmul(unitAmt, _amt18);
2020-05-05 06:05:36 +00:00
curveTokenContract.approve(address(curveSwap), 0);
2020-05-09 04:01:24 +00:00
curveTokenContract.approve(address(curveSwap), _slippageAmt);
2020-05-06 02:33:13 +00:00
curveSwap.remove_liquidity_imbalance(_amts, _slippageAmt);
2020-05-05 01:54:10 +00:00
2020-05-06 04:56:33 +00:00
setUint(setId, _amt);
2020-05-05 23:12:52 +00:00
2020-05-05 12:30:53 +00:00
emit LogWithdraw(token, _amt, _curveAmt, getId, setId);
bytes32 _eventCode = keccak256("LogWithdraw(address,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, _amt, _curveAmt, getId, setId);
2020-05-05 06:05:36 +00:00
emitEvent(_eventCode, _eventParam);
2020-05-05 01:54:10 +00:00
}
}
contract ConnectCurve is CurveProtocol {
2020-05-06 05:09:43 +00:00
string public name = "Curve-susd-v1";
2020-05-05 01:54:10 +00:00
}