43
contracts/SmartRoute/adapter/CurveUnderlyingAdapter.sol
Normal file
43
contracts/SmartRoute/adapter/CurveUnderlyingAdapter.sol
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Copyright 2020 DODO ZOO.
|
||||||
|
SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
pragma solidity 0.6.9;
|
||||||
|
|
||||||
|
import {IDODOAdapter} from "../intf/IDODOAdapter.sol";
|
||||||
|
import {ICurve} from "../intf/ICurve.sol";
|
||||||
|
import {IERC20} from "../../intf/IERC20.sol";
|
||||||
|
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||||
|
import {UniversalERC20} from "../lib/UniversalERC20.sol";
|
||||||
|
import {SafeERC20} from "../../lib/SafeERC20.sol";
|
||||||
|
|
||||||
|
// for two tokens
|
||||||
|
contract CurveUnderlyingAdapter is IDODOAdapter {
|
||||||
|
using SafeMath for uint;
|
||||||
|
|
||||||
|
function _curveSwap(address to, address pool, bytes memory moreInfo) internal {
|
||||||
|
(address fromToken, address toToken, int128 i, int128 j) = abi.decode(moreInfo, (address, address, int128, int128));
|
||||||
|
require(fromToken == ICurve(pool).underlying_coins(i), 'CurveAdapter: WRONG_TOKEN');
|
||||||
|
require(toToken == ICurve(pool).underlying_coins(j), 'CurveAdapter: WRONG_TOKEN');
|
||||||
|
uint256 sellAmount = IERC20(fromToken).balanceOf(address(this));
|
||||||
|
|
||||||
|
// approve
|
||||||
|
IERC20(fromToken).approve(pool, sellAmount);
|
||||||
|
// swap
|
||||||
|
ICurve(pool).exchange_underlying(i, j, sellAmount, 0);
|
||||||
|
if(to != address(this)) {
|
||||||
|
SafeERC20.safeTransfer(IERC20(toToken), to, IERC20(toToken).balanceOf(address(this)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sellBase(address to, address pool, bytes memory moreInfo) external override {
|
||||||
|
_curveSwap(to, pool, moreInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
function sellQuote(address to, address pool, bytes memory moreInfo) external override {
|
||||||
|
_curveSwap(to, pool, moreInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Copyright 2020 DODO ZOO.
|
Copyright 2020 DODO ZOO.
|
||||||
SPDX-License-Identifier: Apache-2.0
|
SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pragma solidity 0.6.9;
|
pragma solidity 0.6.9;
|
||||||
|
|||||||
21
contracts/SmartRoute/intf/ICurve.sol
Normal file
21
contracts/SmartRoute/intf/ICurve.sol
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
pragma solidity 0.6.9;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
interface ICurve {
|
||||||
|
// solium-disable-next-line mixedcase
|
||||||
|
function get_dy_underlying(int128 i, int128 j, uint256 dx) external view returns(uint256 dy);
|
||||||
|
|
||||||
|
// solium-disable-next-line mixedcase
|
||||||
|
function get_dy(int128 i, int128 j, uint256 dx) external view returns(uint256 dy);
|
||||||
|
|
||||||
|
// solium-disable-next-line mixedcase
|
||||||
|
function exchange_underlying(int128 i, int128 j, uint256 dx, uint256 minDy) external;
|
||||||
|
|
||||||
|
// solium-disable-next-line mixedcase
|
||||||
|
function exchange(int128 i, int128 j, uint256 dx, uint256 minDy) external;
|
||||||
|
|
||||||
|
// view coins address
|
||||||
|
function underlying_coins(int128 arg0) external view returns(address out);
|
||||||
|
function coins(int128 arg0) external view returns(address out);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -33,6 +33,15 @@ contract DODORouteProxy {
|
|||||||
address public immutable _WETH_;
|
address public immutable _WETH_;
|
||||||
address public immutable _DODO_APPROVE_PROXY_;
|
address public immutable _DODO_APPROVE_PROXY_;
|
||||||
|
|
||||||
|
struct PoolInfo {
|
||||||
|
uint256 direction;
|
||||||
|
uint256 poolEdition;
|
||||||
|
uint256 weight;
|
||||||
|
address pool;
|
||||||
|
address adapter;
|
||||||
|
bytes moreInfo;
|
||||||
|
}
|
||||||
|
|
||||||
// ============ Events ============
|
// ============ Events ============
|
||||||
|
|
||||||
event OrderHistory(
|
event OrderHistory(
|
||||||
@@ -62,6 +71,59 @@ contract DODORouteProxy {
|
|||||||
_DODO_APPROVE_PROXY_ = dodoApproveProxy;
|
_DODO_APPROVE_PROXY_ = dodoApproveProxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mixSwap(
|
||||||
|
address fromToken,
|
||||||
|
address toToken,
|
||||||
|
uint256 fromTokenAmount,
|
||||||
|
uint256 minReturnAmount,
|
||||||
|
address[] memory mixAdapters,
|
||||||
|
address[] memory mixPairs,
|
||||||
|
address[] memory assetTo,
|
||||||
|
uint256 directions,
|
||||||
|
bool,
|
||||||
|
uint256 deadLine
|
||||||
|
) external payable judgeExpired(deadLine) returns (uint256 returnAmount) {
|
||||||
|
require(mixPairs.length > 0, "DODORouteProxy: PAIRS_EMPTY");
|
||||||
|
require(mixPairs.length == mixAdapters.length, "DODORouteProxy: PAIR_ADAPTER_NOT_MATCH");
|
||||||
|
require(mixPairs.length == assetTo.length - 1, "DODORouteProxy: PAIR_ASSETTO_NOT_MATCH");
|
||||||
|
require(minReturnAmount > 0, "DODORouteProxy: RETURN_AMOUNT_ZERO");
|
||||||
|
|
||||||
|
address _fromToken = fromToken;
|
||||||
|
address _toToken = toToken;
|
||||||
|
uint256 _fromTokenAmount = fromTokenAmount;
|
||||||
|
|
||||||
|
uint256 toTokenOriginBalance = IERC20(_toToken).universalBalanceOf(msg.sender);
|
||||||
|
|
||||||
|
_deposit(msg.sender, assetTo[0], _fromToken, _fromTokenAmount, _fromToken == _ETH_ADDRESS_);
|
||||||
|
|
||||||
|
for (uint256 i = 0; i < mixPairs.length; i++) {
|
||||||
|
if (directions & 1 == 0) {
|
||||||
|
IDODOAdapter(mixAdapters[i]).sellBase(assetTo[i + 1],mixPairs[i], "");
|
||||||
|
} else {
|
||||||
|
IDODOAdapter(mixAdapters[i]).sellQuote(assetTo[i + 1],mixPairs[i], "");
|
||||||
|
}
|
||||||
|
directions = directions >> 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(_toToken == _ETH_ADDRESS_) {
|
||||||
|
returnAmount = IWETH(_WETH_).balanceOf(address(this));
|
||||||
|
IWETH(_WETH_).withdraw(returnAmount);
|
||||||
|
msg.sender.transfer(returnAmount);
|
||||||
|
}else {
|
||||||
|
returnAmount = IERC20(_toToken).tokenBalanceOf(msg.sender).sub(toTokenOriginBalance);
|
||||||
|
}
|
||||||
|
|
||||||
|
require(returnAmount >= minReturnAmount, "DODORouteProxy: Return amount is not enough");
|
||||||
|
|
||||||
|
emit OrderHistory(
|
||||||
|
_fromToken,
|
||||||
|
_toToken,
|
||||||
|
msg.sender,
|
||||||
|
_fromTokenAmount,
|
||||||
|
returnAmount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function dodoMutliSwap(
|
function dodoMutliSwap(
|
||||||
uint256 fromTokenAmount,
|
uint256 fromTokenAmount,
|
||||||
uint256 minReturnAmount,
|
uint256 minReturnAmount,
|
||||||
@@ -119,27 +181,34 @@ contract DODORouteProxy {
|
|||||||
uint256 curTotalWeight = totalWeight[i-1];
|
uint256 curTotalWeight = totalWeight[i-1];
|
||||||
|
|
||||||
for(uint256 j = splitNumber[i-1]; j < splitNumber[i]; j++) {
|
for(uint256 j = splitNumber[i-1]; j < splitNumber[i]; j++) {
|
||||||
(address pool, address adapter, uint256 mixPara) = abi.decode(swapSequence[j], (address, address, uint256));
|
PoolInfo memory curPoolInfo;
|
||||||
uint256 direction = mixPara >> 17;
|
{
|
||||||
uint256 weight = (0xffff & mixPara) >> 9;
|
(address pool, address adapter, uint256 mixPara, bytes memory moreInfo) = abi.decode(swapSequence[j], (address, address, uint256, bytes));
|
||||||
uint256 poolEdition = (0xff & mixPara);
|
|
||||||
|
curPoolInfo.direction = mixPara >> 17;
|
||||||
|
curPoolInfo.weight = (0xffff & mixPara) >> 9;
|
||||||
|
curPoolInfo.poolEdition = (0xff & mixPara);
|
||||||
|
curPoolInfo.pool = pool;
|
||||||
|
curPoolInfo.adapter = adapter;
|
||||||
|
curPoolInfo.moreInfo = moreInfo;
|
||||||
|
}
|
||||||
|
|
||||||
if(assetFrom[i-1] == address(this)) {
|
if(assetFrom[i-1] == address(this)) {
|
||||||
uint256 curAmount = curTotalAmount.div(curTotalWeight).mul(weight);
|
uint256 curAmount = curTotalAmount.div(curTotalWeight).mul(curPoolInfo.weight);
|
||||||
|
|
||||||
if(poolEdition == 1) {
|
if(curPoolInfo.poolEdition == 1) {
|
||||||
//For using transferFrom pool (like dodoV1)
|
//For using transferFrom pool (like dodoV1, Curve)
|
||||||
IERC20(midToken[i]).transfer(adapter, curAmount);
|
IERC20(midToken[i]).transfer(curPoolInfo.adapter, curAmount);
|
||||||
} else {
|
} else {
|
||||||
//For using transfer pool (like dodoV2)
|
//For using transfer pool (like dodoV2)
|
||||||
IERC20(midToken[i]).transfer(pool, curAmount);
|
IERC20(midToken[i]).transfer(curPoolInfo.pool, curAmount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(direction == 0) {
|
if(curPoolInfo.direction == 0) {
|
||||||
IDODOAdapter(adapter).sellBase(assetFrom[i], pool, "");
|
IDODOAdapter(curPoolInfo.adapter).sellBase(assetFrom[i], curPoolInfo.pool, curPoolInfo.moreInfo);
|
||||||
} else {
|
} else {
|
||||||
IDODOAdapter(adapter).sellQuote(assetFrom[i], pool, "");
|
IDODOAdapter(curPoolInfo.adapter).sellQuote(assetFrom[i], curPoolInfo.pool, curPoolInfo.moreInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -475,3 +475,8 @@ network type: heco
|
|||||||
Deploy time: 2021/4/29 上午10:33:01
|
Deploy time: 2021/4/29 上午10:33:01
|
||||||
Deploy type: DODOCpProxy
|
Deploy type: DODOCpProxy
|
||||||
CpProxy address: 0x8930101c6cFbe0f3cb31E7526a16E72255388E97
|
CpProxy address: 0x8930101c6cFbe0f3cb31E7526a16E72255388E97
|
||||||
|
====================================================
|
||||||
|
network type: heco
|
||||||
|
Deploy time: 2021/5/7 下午5:07:08
|
||||||
|
Deploy type: test - Adapter
|
||||||
|
test_Adapter Address: 0x31AC053c31a77055b2ae2d3899091C0A9c19cE3a
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ const DODOV2RouteHelper = artifacts.require("DODOV2RouteHelper");
|
|||||||
const ERC20Mine = artifacts.require("ERC20Mine");
|
const ERC20Mine = artifacts.require("ERC20Mine");
|
||||||
const vDODOMine = artifacts.require("vDODOMine");
|
const vDODOMine = artifacts.require("vDODOMine");
|
||||||
|
|
||||||
|
const CurveAdapter = artifacts.require("CurveUnderlyingAdapter");
|
||||||
|
|
||||||
module.exports = async (deployer, network, accounts) => {
|
module.exports = async (deployer, network, accounts) => {
|
||||||
let CONFIG = GetConfig(network, accounts)
|
let CONFIG = GetConfig(network, accounts)
|
||||||
if (CONFIG == null) return;
|
if (CONFIG == null) return;
|
||||||
@@ -370,4 +372,14 @@ module.exports = async (deployer, network, accounts) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(deploySwitch.test_ADAPTER) {
|
||||||
|
logger.log("====================================================");
|
||||||
|
logger.log("network type: " + network);
|
||||||
|
logger.log("Deploy time: " + new Date().toLocaleString());
|
||||||
|
logger.log("Deploy type: test - Adapter");
|
||||||
|
|
||||||
|
await deployer.deploy(CurveAdapter);
|
||||||
|
|
||||||
|
logger.log("test_Adapter Address: ", CurveAdapter.address);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user