refactor mixSwap && add trade adapter
This commit is contained in:
52
contracts/SmartRoute/adapter/DODOV1Adapter.sol
Normal file
52
contracts/SmartRoute/adapter/DODOV1Adapter.sol
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {IDODOV1} from "../intf/IDODOV1.sol";
|
||||
import {IDODOSellHelper} from "../helper/DODOSellHelper.sol";
|
||||
import {UniversalERC20} from "../lib/UniversalERC20.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {IDODOAdapter} from "../intf/IDODOAdapter.sol";
|
||||
|
||||
contract DODOV1Adapter is IDODOAdapter {
|
||||
using SafeMath for uint256;
|
||||
using UniversalERC20 for IERC20;
|
||||
|
||||
address public immutable _DODO_SELL_HELPER_;
|
||||
|
||||
constructor(address dodoSellHelper) public {
|
||||
_DODO_SELL_HELPER_ = dodoSellHelper;
|
||||
}
|
||||
|
||||
function sellBase(address to, address pool) external override {
|
||||
address curBase = IDODOV1(pool)._BASE_TOKEN_();
|
||||
uint256 curAmountIn = IERC20(curBase).balanceOf(address(this));
|
||||
IERC20(curBase).universalApproveMax(pool, curAmountIn);
|
||||
IDODOV1(pool).sellBaseToken(curAmountIn, 0, "");
|
||||
if(to == msg.sender) {
|
||||
address curQuote = IDODOV1(pool)._QUOTE_TOKEN_();
|
||||
IERC20(curQuote).transfer(to,IERC20(curQuote).balanceOf(address(this)));
|
||||
}
|
||||
}
|
||||
|
||||
function sellQuote(address to, address pool) external override {
|
||||
address curQuote = IDODOV1(pool)._QUOTE_TOKEN_();
|
||||
uint256 curAmountIn = IERC20(curQuote).balanceOf(address(this));
|
||||
IERC20(curQuote).universalApproveMax(pool, curAmountIn);
|
||||
uint256 canBuyBaseAmount = IDODOSellHelper(_DODO_SELL_HELPER_).querySellQuoteToken(
|
||||
pool,
|
||||
curAmountIn
|
||||
);
|
||||
IDODOV1(pool).buyBaseToken(canBuyBaseAmount, curAmountIn, "");
|
||||
if(to == msg.sender) {
|
||||
address curBase = IDODOV1(pool)._BASE_TOKEN_();
|
||||
IERC20(curBase).transfer(to,IERC20(curBase).balanceOf(address(this)));
|
||||
}
|
||||
}
|
||||
}
|
||||
21
contracts/SmartRoute/adapter/DODOV2Adapter.sol
Normal file
21
contracts/SmartRoute/adapter/DODOV2Adapter.sol
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import {IDODOV2} from "../intf/IDODOV2.sol";
|
||||
import {IDODOAdapter} from "../intf/IDODOAdapter.sol";
|
||||
|
||||
contract DODOV2Adapter is IDODOAdapter {
|
||||
function sellBase(address to, address pool) external override {
|
||||
IDODOV2(pool).sellBase(to);
|
||||
}
|
||||
|
||||
function sellQuote(address to, address pool) external override {
|
||||
IDODOV2(pool).sellQuote(to);
|
||||
}
|
||||
}
|
||||
49
contracts/SmartRoute/adapter/UniAdapter.sol
Normal file
49
contracts/SmartRoute/adapter/UniAdapter.sol
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
|
||||
import {IDODOAdapter} from "../intf/IDODOAdapter.sol";
|
||||
import {IUni} from "../intf/IUni.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
|
||||
contract UniAdapter is IDODOAdapter {
|
||||
using SafeMath for uint;
|
||||
|
||||
//fromToken == token0
|
||||
function sellBase(address to, address pool) external override {
|
||||
address baseToken = IUni(pool).token0();
|
||||
(uint reserveIn, uint reserveOut,) = IUni(pool).getReserves();
|
||||
require(reserveIn > 0 && reserveOut > 0, 'UniAdapter: INSUFFICIENT_LIQUIDITY');
|
||||
|
||||
uint balance0 = IERC20(baseToken).balanceOf(pool);
|
||||
uint sellBaseAmount = balance0 - reserveIn;
|
||||
|
||||
uint sellBaseAmountWithFee = sellBaseAmount.mul(997);
|
||||
uint numerator = sellBaseAmountWithFee.mul(reserveOut);
|
||||
uint denominator = reserveIn.mul(1000).add(sellBaseAmountWithFee);
|
||||
uint receiveQuoteAmount = numerator / denominator;
|
||||
IUni(pool).swap(0, receiveQuoteAmount, to, new bytes(0));
|
||||
}
|
||||
|
||||
//fromToken == token1
|
||||
function sellQuote(address to, address pool) external override {
|
||||
address quoteToken = IUni(pool).token1();
|
||||
(uint reserveOut, uint reserveIn,) = IUni(pool).getReserves();
|
||||
require(reserveIn > 0 && reserveOut > 0, 'UniAdapter: INSUFFICIENT_LIQUIDITY');
|
||||
|
||||
uint balance1 = IERC20(quoteToken).balanceOf(pool);
|
||||
uint sellQuoteAmount = balance1 - reserveIn;
|
||||
|
||||
uint sellQuoteAmountWithFee = sellQuoteAmount.mul(997);
|
||||
uint numerator = sellQuoteAmountWithFee.mul(reserveOut);
|
||||
uint denominator = reserveIn.mul(1000).add(sellQuoteAmountWithFee);
|
||||
uint receiveBaseAmount = numerator / denominator;
|
||||
IUni(pool).swap(receiveBaseAmount, 0, to, new bytes(0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user