Merge branch 'feature/V2' of github.com:DODOEX/contractV2 into feature/V2

This commit is contained in:
mingda
2020-11-23 00:47:34 +08:00
28 changed files with 458 additions and 206 deletions

View File

@@ -0,0 +1,51 @@
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
import {IFeeRateModel} from "../../lib/FeeRateModel.sol";
import {IPermissionManager} from "../../lib/PermissionManager.sol";
import {IExternalValue} from "../../lib/ExternalValue.sol";
import {IERC20} from "../../intf/IERC20.sol";
import {DPPTrader} from "./DPPTrader.sol";
import {ISmartApprove} from "../../intf/ISmartApprove.sol";
contract DPP is DPPTrader {
constructor() public {
_FACTORY_ = msg.sender;
}
function init(
address owner,
address maintainer,
address baseTokenAddress,
address quoteTokenAddress,
address iSmartApprove,
address[] memory configAddresses
) external {
require(msg.sender == _FACTORY_, 'INIT FORBIDDEN');
initOwner(owner);
_MAINTAINER_ = maintainer;
_BASE_TOKEN_ = IERC20(baseTokenAddress);
_QUOTE_TOKEN_ = IERC20(quoteTokenAddress);
_DODO_SMART_APPROVE_ = ISmartApprove(iSmartApprove);
_LP_FEE_RATE_MODEL_ = IFeeRateModel(configAddresses[0]);
_MT_FEE_RATE_MODEL_ = IFeeRateModel(configAddresses[1]);
_GAS_PRICE_LIMIT_ = IExternalValue(configAddresses[2]);
_I_ = IExternalValue(configAddresses[3]);
_K_ = IExternalValue(configAddresses[4]);
_TRADE_PERMISSION_ = IPermissionManager(configAddresses[5]);
_resetTargetAndReserve();
}
// ============ Version Control ============
function version() external pure returns (uint256) {
return 100; // 1.0.0
}
}

View File

@@ -14,7 +14,8 @@ import {DecimalMath} from "../../lib/DecimalMath.sol";
import {ReentrancyGuard} from "../../lib/ReentrancyGuard.sol";
import {IPermissionManager} from "../../lib/PermissionManager.sol";
import {IExternalValue} from "../../lib/ExternalValue.sol";
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
import {IFeeRateModel} from "../../lib/FeeRateModel.sol";
import {ISmartApprove} from "../../intf/ISmartApprove.sol";
import {IERC20} from "../../intf/IERC20.sol";
import {PMMPricing} from "../../lib/PMMPricing.sol";
@@ -27,6 +28,9 @@ import {PMMPricing} from "../../lib/PMMPricing.sol";
contract DPPStorage is InitializableOwnable, ReentrancyGuard {
using SafeMath for uint256;
address public _FACTORY_;
ISmartApprove public _DODO_SMART_APPROVE_;
// ============ Variables for Control ============
IExternalValue public _GAS_PRICE_LIMIT_;
@@ -80,6 +84,14 @@ contract DPPStorage is InitializableOwnable, ReentrancyGuard {
_GAS_PRICE_LIMIT_ = IExternalValue(newGasPriceLimitSource);
}
function setISource(address newISource) external onlyOwner {
_I_ = IExternalValue(newISource);
}
function setKSource(address newKSource) external onlyOwner {
_K_ = IExternalValue(newKSource);
}
function setBuy(bool open) external onlyOwner {
_BUYING_CLOSE_ = !open;
}

View File

@@ -25,10 +25,7 @@ contract DPPTrader is DPPVault {
}
modifier isSellAllow(address trader) {
require(
!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(trader),
"TRADER_SELL_NOT_ALLOWED"
);
require(!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(trader),"TRADER_SELL_NOT_ALLOWED");
_;
}
@@ -50,7 +47,8 @@ contract DPPTrader is DPPVault {
uint256 mtFee;
uint256 newBaseTarget;
PMMPricing.RState newRState;
(receiveQuoteAmount, mtFee, newRState, newBaseTarget) = querySellBase(tx.origin, baseInput);
//TODO: confirm
(receiveQuoteAmount, mtFee, newRState, newBaseTarget) = querySellBase(to, baseInput);
_transferQuoteOut(to, receiveQuoteAmount);
_transferQuoteOut(_MAINTAINER_, mtFee);
@@ -76,11 +74,9 @@ contract DPPTrader is DPPVault {
uint256 quoteInput = getQuoteInput();
uint256 mtFee;
uint256 newQuoteTarget;
PMMPricing.RState newRState;
(receiveBaseAmount, mtFee, newRState, newQuoteTarget) = querySellQuote(
tx.origin,
quoteInput
);
(receiveBaseAmount, mtFee, newRState, newQuoteTarget) = querySellQuote(to,quoteInput);
_transferBaseOut(to, receiveBaseAmount);
_transferBaseOut(_MAINTAINER_, mtFee);

View File

@@ -20,7 +20,7 @@ contract DPPVault is DPPStorage {
using SafeMath for uint256;
using SafeERC20 for IERC20;
// input
// ============ Get Input ============
function getInput() public view returns (uint256 baseInput, uint256 quoteInput) {
return (
@@ -39,25 +39,41 @@ contract DPPVault is DPPStorage {
// ============ Set Status ============
function _syncReserve() internal {
_BASE_RESERVE_ = _BASE_TOKEN_.balanceOf(address(this));
_QUOTE_RESERVE_ = _QUOTE_TOKEN_.balanceOf(address(this));
}
//TODO:对应前端哪个操作?
function setTarget(uint256 baseTarget, uint256 quoteTarget) public onlyOwner {
_BASE_TARGET_ = baseTarget;
_QUOTE_TARGET_ = quoteTarget;
_checkStatus();
}
function _syncReserve() internal {
_BASE_RESERVE_ = _BASE_TOKEN_.balanceOf(address(this));
_QUOTE_RESERVE_ = _QUOTE_TOKEN_.balanceOf(address(this));
}
// todo 这里需要考虑怎么一个tx同时更新k i 和 fee并reset
function reset() public onlyOwner {
function _resetTargetAndReserve() internal {
_BASE_TARGET_ = _BASE_TOKEN_.balanceOf(address(this));
_QUOTE_TARGET_ = _QUOTE_TOKEN_.balanceOf(address(this));
_BASE_RESERVE_ = _BASE_TARGET_;
_QUOTE_RESERVE_ = _QUOTE_TARGET_;
}
function reset(
uint256 newLpFeeRate,
uint256 newMtFeeRate,
uint256 newI,
uint256 newK
) public {
//TODO: 讨论
require(msg.sender == _DODO_SMART_APPROVE_.getSmartSwap() || msg.sender == _OWNER_, "RESET FORBIDDEN");
require(newK > 0 && newK <= 10**18, "K OUT OF RANGE!");
_resetTargetAndReserve();
_LP_FEE_RATE_MODEL_.setFeeRate(newLpFeeRate);
_MT_FEE_RATE_MODEL_.setFeeRate(newMtFeeRate);
_I_.set(newI);
_K_.set(newK);
}
function _checkStatus() internal view {
require(
!(_BASE_RESERVE_ < _BASE_TARGET_ && _QUOTE_RESERVE_ < _QUOTE_TARGET_),
@@ -67,20 +83,21 @@ contract DPPVault is DPPStorage {
// ============ Assets Transfer ============
function withdraw(
address to,
uint256 baseAmount,
uint256 quoteAmount,
bytes calldata data
) public onlyOwner {
_transferBaseOut(to, baseAmount);
_transferQuoteOut(to, quoteAmount);
_BASE_TARGET_ = _BASE_TARGET_.sub(baseAmount);
_QUOTE_TARGET_ = _QUOTE_TARGET_.sub(quoteAmount);
if (data.length > 0) {
IDODOCallee(to).DPPWithdrawCall(msg.sender, baseAmount, quoteAmount, data);
}
}
// function withdraw(
// address to,
// uint256 baseAmount,
// uint256 quoteAmount,
// bytes calldata data
// ) public onlyOwner {
// _transferBaseOut(to, baseAmount);
// _transferQuoteOut(to, quoteAmount);
// _BASE_TARGET_ = _BASE_TARGET_.sub(baseAmount);
// _QUOTE_TARGET_ = _QUOTE_TARGET_.sub(quoteAmount);
// _syncReserve();
// if (data.length > 0) {
// IDODOCallee(to).DPPWithdrawCall(msg.sender, baseAmount, quoteAmount, data);
// }
// }
function _transferBaseOut(address to, uint256 amount) internal {
if (amount > 0) {
@@ -94,15 +111,13 @@ contract DPPVault is DPPStorage {
}
}
// todo 高级功能,需要讨论
// 如果单独执行这个功能会导致状态失衡
function retrieve(
address payable to,
address token,
uint256 amount
) external onlyOwner {
require(to != address(_BASE_TOKEN_) && to != address(_QUOTE_TOKEN_), "USE_WITHDRAW");
if (token == 0x000000000000000000000000000000000000000E) {
if (token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) {
to.transfer(amount);
} else {
IERC20(token).safeTransfer(msg.sender, amount);