simplify dpp

This commit is contained in:
owen05
2020-12-30 18:41:13 +08:00
parent 9d2b0f06f5
commit c717e5fb9b
12 changed files with 163 additions and 363 deletions

View File

@@ -9,8 +9,6 @@ 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";
@@ -20,12 +18,10 @@ contract DPP is DPPTrader {
address maintainer,
address baseTokenAddress,
address quoteTokenAddress,
address lpFeeRateModel,
uint256 lpFeeRate,
address mtFeeRateModel,
address kSource,
address iSource,
address gasPriceSource,
address tradePermissionManager
uint256 k,
uint256 i
) external {
initOwner(owner);
@@ -33,16 +29,16 @@ contract DPP is DPPTrader {
_BASE_TOKEN_ = IERC20(baseTokenAddress);
_QUOTE_TOKEN_ = IERC20(quoteTokenAddress);
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
_I_ = IExternalValue(iSource);
_K_ = IExternalValue(kSource);
_GAS_PRICE_LIMIT_ = IExternalValue(gasPriceSource);
_TRADE_PERMISSION_ = IPermissionManager(tradePermissionManager);
_MAINTAINER_ = maintainer;
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
require(lpFeeRate <= 1e18, "LP_FEE_RATE_OUT_OF_RANGE");
require(k <= 1e18, "K_OUT_OF_RANGE");
require(i > 0 && i <= 1e36, "I_OUT_OF_RANGE");
_LP_FEE_RATE_ = uint64(lpFeeRate);
_K_ = uint64(k);
_I_ = uint128(i);
_resetTargetAndReserve();
_checkIK();
}
// ============ Version Control ============

View File

@@ -49,42 +49,6 @@ contract DPPAdmin is InitializableOwnable {
_OPERATOR_ = newOperator;
}
// function setLpFeeRateModel(address newLpFeeRateModel) external notFreezed onlyOwner {
// IDPP(_DPP_).setLpFeeRateModel(newLpFeeRateModel);
// }
// function setMtFeeRateModel(address newMtFeeRateModel) external notFreezed onlyOwner {
// IDPP(_DPP_).setMtFeeRateModel(newMtFeeRateModel);
// }
// function setTradePermissionManager(address newTradePermissionManager) external notFreezed onlyOwner {
// IDPP(_DPP_).setTradePermissionManager(newTradePermissionManager);
// }
function setMaintainer(address newMaintainer) external notFreezed onlyOwner {
IDPP(_DPP_).setMaintainer(newMaintainer);
}
// function setGasPriceSource(address newGasPriceLimitSource) external notFreezed onlyOwner {
// IDPP(_DPP_).setGasPriceSource(newGasPriceLimitSource);
// }
// function setISource(address newISource) external notFreezed onlyOwner {
// IDPP(_DPP_).setISource(newISource);
// }
// function setKSource(address newKSource) external notFreezed onlyOwner {
// IDPP(_DPP_).setKSource(newKSource);
// }
function setBuy(bool open) external notFreezed onlyOwner {
IDPP(_DPP_).setBuy(open);
}
function setSell(bool open) external notFreezed onlyOwner {
IDPP(_DPP_).setSell(open);
}
function retrieve(
address payable to,
address token,
@@ -96,7 +60,6 @@ contract DPPAdmin is InitializableOwnable {
function reset(
address operator,
uint256 newLpFeeRate,
uint256 newMtFeeRate,
uint256 newI,
uint256 newK,
uint256 baseOutAmount,
@@ -114,7 +77,6 @@ contract DPPAdmin is InitializableOwnable {
IDPP(_DPP_).reset(
msg.sender,
newLpFeeRate,
newMtFeeRate,
newI,
newK,
baseOutAmount,

View File

@@ -12,8 +12,6 @@ import {InitializableOwnable} from "../../lib/InitializableOwnable.sol";
import {SafeMath} from "../../lib/SafeMath.sol";
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 "../../lib/FeeRateModel.sol";
import {IERC20} from "../../intf/IERC20.sol";
import {PMMPricing} from "../../lib/PMMPricing.sol";
@@ -27,14 +25,6 @@ import {PMMPricing} from "../../lib/PMMPricing.sol";
contract DPPStorage is InitializableOwnable, ReentrancyGuard {
using SafeMath for uint256;
// ============ Advanced Controls ============
bool public _BUYING_CLOSE_;
bool public _SELLING_CLOSE_;
IPermissionManager public _TRADE_PERMISSION_;
IExternalValue public _GAS_PRICE_LIMIT_;
// ============ Core Address ============
address public _MAINTAINER_;
@@ -42,92 +32,18 @@ contract DPPStorage is InitializableOwnable, ReentrancyGuard {
IERC20 public _BASE_TOKEN_;
IERC20 public _QUOTE_TOKEN_;
uint256 public _BASE_RESERVE_;
uint256 public _QUOTE_RESERVE_;
uint256 public _BASE_TARGET_;
uint256 public _QUOTE_TARGET_;
PMMPricing.RState public _RState_;
uint128 public _BASE_RESERVE_;
uint128 public _QUOTE_RESERVE_;
uint120 public _BASE_TARGET_;
uint120 public _QUOTE_TARGET_;
uint16 public _RState_;
// ============ Variables for Pricing ============
IFeeRateModel public _LP_FEE_RATE_MODEL_;
IFeeRateModel public _MT_FEE_RATE_MODEL_;
IExternalValue public _K_;
IExternalValue public _I_;
// ============ Events ============
event SetLpFeeRateModel(address oldAddr, address newAddr);
event SetMtFeeRateModel(address oldAddr, address newAddr);
event SetTradePermissionManager(address oldAddr, address newAddr);
event SetMaintainer(address oldAddr, address newAddr);
event SetGasPriceSource(address oldAddr, address newAddr);
event SetISource(address oldAddr, address newAddr);
event SetKSource(address oldAddr, address newAddr);
event SetBuy(bool allow);
event SetSell(bool allow);
// ============ Setting Functions ============
function setLpFeeRateModel(address newLpFeeRateModel) external onlyOwner {
emit SetLpFeeRateModel(address(_LP_FEE_RATE_MODEL_), newLpFeeRateModel);
_LP_FEE_RATE_MODEL_ = IFeeRateModel(newLpFeeRateModel);
}
function setMtFeeRateModel(address newMtFeeRateModel) external onlyOwner {
emit SetMtFeeRateModel(address(_MT_FEE_RATE_MODEL_), newMtFeeRateModel);
_MT_FEE_RATE_MODEL_ = IFeeRateModel(newMtFeeRateModel);
}
function setTradePermissionManager(address newTradePermissionManager) external onlyOwner {
emit SetTradePermissionManager(address(_TRADE_PERMISSION_), newTradePermissionManager);
_TRADE_PERMISSION_ = IPermissionManager(newTradePermissionManager);
}
function setMaintainer(address newMaintainer) external onlyOwner {
emit SetMaintainer(address(_MAINTAINER_), newMaintainer);
_MAINTAINER_ = newMaintainer;
}
function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
emit SetGasPriceSource(address(_GAS_PRICE_LIMIT_), newGasPriceLimitSource);
_GAS_PRICE_LIMIT_ = IExternalValue(newGasPriceLimitSource);
}
function setISource(address newISource) external onlyOwner {
emit SetISource(address(_I_), newISource);
_I_ = IExternalValue(newISource);
_checkIK();
}
function setKSource(address newKSource) external onlyOwner {
emit SetKSource(address(_K_), newKSource);
_K_ = IExternalValue(newKSource);
_checkIK();
}
function setBuy(bool open) external onlyOwner {
emit SetBuy(open);
_BUYING_CLOSE_ = !open;
}
function setSell(bool open) external onlyOwner {
emit SetSell(open);
_SELLING_CLOSE_ = !open;
}
function _checkIK() internal view {
uint256 k = _K_.get();
uint256 i = _I_.get();
require(k <= 1e18, "K_OUT_OF_RANGE");
require(i > 0 && i <= 1e36, "I_OUT_OF_RANGE");
}
uint64 public _LP_FEE_RATE_;
uint64 public _K_;
uint128 public _I_;
}

View File

@@ -36,36 +36,15 @@ contract DPPTrader is DPPVault {
event RChange(PMMPricing.RState newRState);
// ============ Modifiers ============
modifier isBuyAllow(address trader) {
require(!_BUYING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(trader), "TRADER_BUY_NOT_ALLOWED");
_;
}
modifier isSellAllow(address trader) {
require(
!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(trader),
"TRADER_SELL_NOT_ALLOWED"
);
_;
}
modifier limitGasPrice() {
require(tx.gasprice <= _GAS_PRICE_LIMIT_.get(), "GAS_PRICE_EXCEED");
_;
}
// ============ Trade Functions ============
function sellBase(address to)
external
preventReentrant
limitGasPrice
isSellAllow(to) // set DVM address in trade permission
returns (uint256 receiveQuoteAmount)
{
uint256 baseInput = getBaseInput();
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 baseInput = baseBalance.sub(uint256(_BASE_RESERVE_));
uint256 mtFee;
uint256 newBaseTarget;
PMMPricing.RState newRState;
@@ -73,12 +52,13 @@ contract DPPTrader is DPPVault {
_transferQuoteOut(to, receiveQuoteAmount);
_transferQuoteOut(_MAINTAINER_, mtFee);
_sync();
_setReserve(baseBalance, _QUOTE_TOKEN_.balanceOf(address(this)));
// update TARGET
if (_RState_ != newRState) {
_RState_ = newRState;
_BASE_TARGET_ = newBaseTarget;
if (_RState_ != uint16(newRState)) {
_RState_ = uint16(newRState);
require(newBaseTarget <= uint120(-1),"OVERFLOW");
_BASE_TARGET_ = uint120(newBaseTarget);
emit RChange(newRState);
}
@@ -94,14 +74,12 @@ contract DPPTrader is DPPVault {
function sellQuote(address to)
external
preventReentrant
limitGasPrice
isBuyAllow(to)
returns (uint256 receiveBaseAmount)
{
uint256 quoteInput = getQuoteInput();
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
uint256 quoteInput = quoteBalance.sub(uint256(_QUOTE_RESERVE_));
uint256 mtFee;
uint256 newQuoteTarget;
PMMPricing.RState newRState;
(receiveBaseAmount, mtFee, newRState, newQuoteTarget) = querySellQuote(
tx.origin,
@@ -110,12 +88,13 @@ contract DPPTrader is DPPVault {
_transferBaseOut(to, receiveBaseAmount);
_transferBaseOut(_MAINTAINER_, mtFee);
_sync();
_setReserve(_BASE_TOKEN_.balanceOf(address(this)), quoteBalance);
// update TARGET
if (_RState_ != newRState) {
_RState_ = newRState;
_QUOTE_TARGET_ = newQuoteTarget;
if (_RState_ != uint16(newRState)) {
_RState_ = uint16(newRState);
require(newQuoteTarget <= uint120(-1),"OVERFLOW");
_QUOTE_TARGET_ = uint120(newQuoteTarget);
emit RChange(newRState);
}
@@ -133,7 +112,7 @@ contract DPPTrader is DPPVault {
uint256 quoteAmount,
address assetTo,
bytes calldata data
) external preventReentrant isSellAllow(assetTo) isBuyAllow(assetTo) {
) external preventReentrant {
_transferBaseOut(assetTo, baseAmount);
_transferQuoteOut(assetTo, quoteAmount);
@@ -152,19 +131,20 @@ contract DPPTrader is DPPVault {
// sell quote case
// quote input + base output
if (baseBalance < _BASE_RESERVE_) {
uint256 quoteInput = quoteBalance.sub(_QUOTE_RESERVE_);
uint256 quoteInput = quoteBalance.sub(uint256(_QUOTE_RESERVE_));
(
uint256 receiveBaseAmount,
uint256 mtFee,
PMMPricing.RState newRState,
uint256 newQuoteTarget
) = querySellQuote(tx.origin, quoteInput); // revert if quoteBalance<quoteReserve
require(_BASE_RESERVE_.sub(baseBalance) <= receiveBaseAmount, "FLASH_LOAN_FAILED");
require(uint256(_BASE_RESERVE_).sub(baseBalance) <= receiveBaseAmount, "FLASH_LOAN_FAILED");
_transferBaseOut(_MAINTAINER_, mtFee);
if (_RState_ != newRState) {
_RState_ = newRState;
_QUOTE_TARGET_ = newQuoteTarget;
if (_RState_ != uint16(newRState)) {
_RState_ = uint16(newRState);
require(newQuoteTarget <= uint120(-1),"OVERFLOW");
_QUOTE_TARGET_ = uint120(newQuoteTarget);
emit RChange(newRState);
}
emit DODOSwap(
@@ -179,19 +159,20 @@ contract DPPTrader is DPPVault {
// sell base case
// base input + quote output
if (quoteBalance < _QUOTE_RESERVE_) {
uint256 baseInput = baseBalance.sub(_BASE_RESERVE_);
uint256 baseInput = baseBalance.sub(uint256(_BASE_RESERVE_));
(
uint256 receiveQuoteAmount,
uint256 mtFee,
PMMPricing.RState newRState,
uint256 newBaseTarget
) = querySellBase(tx.origin, baseInput); // revert if baseBalance<baseReserve
require(_QUOTE_RESERVE_.sub(quoteBalance) <= receiveQuoteAmount, "FLASH_LOAN_FAILED");
require(uint256(_QUOTE_RESERVE_).sub(quoteBalance) <= receiveQuoteAmount, "FLASH_LOAN_FAILED");
_transferQuoteOut(_MAINTAINER_, mtFee);
if (_RState_ != newRState) {
_RState_ = newRState;
_BASE_TARGET_ = newBaseTarget;
if (_RState_ != uint16(newRState)) {
_RState_ = uint16(newRState);
require(newBaseTarget <= uint120(-1),"OVERFLOW");
_BASE_TARGET_ = uint120(newBaseTarget);
emit RChange(newRState);
}
emit DODOSwap(
@@ -223,7 +204,7 @@ contract DPPTrader is DPPVault {
PMMPricing.PMMState memory state = getPMMState();
(receiveQuoteAmount, newRState) = PMMPricing.sellBaseToken(state, payBaseAmount);
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
uint256 lpFeeRate = _LP_FEE_RATE_;
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
mtFee = DecimalMath.mulFloor(receiveQuoteAmount, mtFeeRate);
receiveQuoteAmount = receiveQuoteAmount
@@ -245,7 +226,7 @@ contract DPPTrader is DPPVault {
PMMPricing.PMMState memory state = getPMMState();
(receiveBaseAmount, newRState) = PMMPricing.sellQuoteToken(state, payQuoteAmount);
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
uint256 lpFeeRate = _LP_FEE_RATE_;
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
mtFee = DecimalMath.mulFloor(receiveBaseAmount, mtFeeRate);
receiveBaseAmount = receiveBaseAmount
@@ -257,13 +238,13 @@ contract DPPTrader is DPPVault {
// ============ Helper Functions ============
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
state.i = _I_.get();
state.K = _K_.get();
state.i = _I_;
state.K = _K_;
state.B = _BASE_RESERVE_;
state.Q = _QUOTE_RESERVE_;
state.B0 = _BASE_TARGET_;
state.Q0 = _QUOTE_TARGET_;
state.R = _RState_;
state.R = PMMPricing.RState(_RState_);
PMMPricing.adjustedTarget(state);
}
@@ -277,7 +258,7 @@ contract DPPTrader is DPPVault {
uint256 Q,
uint256 B0,
uint256 Q0,
uint8 R
uint256 R
)
{
PMMPricing.PMMState memory state = getPMMState();
@@ -287,21 +268,10 @@ contract DPPTrader is DPPVault {
Q = state.Q;
B0 = state.B0;
Q0 = state.Q0;
R = uint8(state.R);
R = uint256(state.R);
}
function getMidPrice() public view returns (uint256 midPrice) {
return PMMPricing.getMidPrice(getPMMState());
}
function _sync() internal {
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
if (baseBalance != _BASE_RESERVE_) {
_BASE_RESERVE_ = baseBalance;
}
if (quoteBalance != _QUOTE_RESERVE_) {
_QUOTE_RESERVE_ = quoteBalance;
}
}
}

View File

@@ -23,7 +23,7 @@ contract DPPVault is DPPStorage {
// ============ Events ============
event Reset(uint256 newLpFeeRate, uint256 newMtFeeRate);
event LpFeeRateChange(uint256 newLpFeeRate);
// ============ View Functions ============
@@ -37,19 +37,10 @@ contract DPPVault is DPPStorage {
view
returns (uint256 lpFeeRate, uint256 mtFeeRate)
{
lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(user);
lpFeeRate = _LP_FEE_RATE_;
mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(user);
}
function getUserTradePermission(address user)
external
view
returns (bool isBuyAllow, bool isSellAllow)
{
isBuyAllow = (!_BUYING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(user));
isSellAllow = (!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(user));
}
// ============ Get Input ============
function getBaseInput() public view returns (uint256 input) {
@@ -60,39 +51,80 @@ contract DPPVault is DPPStorage {
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
}
// ============ Set States ============
// ============ Set Status ============
function _setReserve(uint256 baseReserve, uint256 quoteReserve) internal {
require(baseReserve <= uint120(-1) && quoteReserve <= uint120(-1), "OVERFLOW");
_BASE_RESERVE_ = uint128(baseReserve);
_QUOTE_RESERVE_ = uint128(quoteReserve);
}
function _sync() internal {
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
require(baseBalance <= uint120(-1) && quoteBalance <= uint120(-1), "OVERFLOW");
if (baseBalance != _BASE_RESERVE_) {
_BASE_RESERVE_ = uint128(baseBalance);
}
if (quoteBalance != _QUOTE_RESERVE_) {
_QUOTE_RESERVE_ = uint128(quoteBalance);
}
}
function _resetTargetAndReserve() internal {
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
require(baseBalance <= uint120(-1) && quoteBalance <= uint120(-1), "OVERFLOW");
_BASE_RESERVE_ = uint128(baseBalance);
_QUOTE_RESERVE_ = uint128(quoteBalance);
_BASE_TARGET_ = uint120(baseBalance);
_QUOTE_TARGET_ = uint120(quoteBalance);
_setRState();
}
function _setRState() internal {
if (_BASE_RESERVE_ == _BASE_TARGET_ && _QUOTE_RESERVE_ == _QUOTE_TARGET_) {
_RState_ = uint16(PMMPricing.RState.ONE);
} else if (_BASE_RESERVE_ > _BASE_TARGET_ && _QUOTE_RESERVE_ < _QUOTE_TARGET_) {
_RState_ = uint16(PMMPricing.RState.BELOW_ONE);
} else if (_BASE_RESERVE_ < _BASE_TARGET_ && _QUOTE_RESERVE_ > _QUOTE_TARGET_) {
_RState_ = uint16(PMMPricing.RState.ABOVE_ONE);
} else {
require(false, "R_STATE_WRONG");
}
}
function ratioSync() external preventReentrant onlyOwner {
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
require(baseBalance <= uint120(-1) && quoteBalance <= uint120(-1), "OVERFLOW");
if (baseBalance != _BASE_RESERVE_) {
_BASE_TARGET_ = _BASE_TARGET_.mul(baseBalance).div(_BASE_RESERVE_);
_BASE_RESERVE_ = baseBalance;
_BASE_TARGET_ = uint120(uint256(_BASE_TARGET_).mul(baseBalance).div(uint256(_BASE_RESERVE_)));
_BASE_RESERVE_ = uint128(baseBalance);
}
if (quoteBalance != _QUOTE_RESERVE_) {
_QUOTE_TARGET_ = _QUOTE_TARGET_.mul(quoteBalance).div(_QUOTE_RESERVE_);
_QUOTE_RESERVE_ = quoteBalance;
_QUOTE_TARGET_ = uint120(uint256(_QUOTE_TARGET_).mul(quoteBalance).div(uint256(_QUOTE_RESERVE_)));
_QUOTE_RESERVE_ = uint128(quoteBalance);
}
}
function setTarget(uint256 baseTarget, uint256 quoteTarget) public preventReentrant onlyOwner {
_BASE_TARGET_ = baseTarget;
_QUOTE_TARGET_ = quoteTarget;
_setRState();
}
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_;
require(baseTarget <= uint120(-1) && quoteTarget <= uint120(-1), "OVERFLOW");
_BASE_TARGET_ = uint120(baseTarget);
_QUOTE_TARGET_ = uint120(quoteTarget);
_setRState();
}
function reset(
address assetTo,
uint256 newLpFeeRate,
uint256 newMtFeeRate,
uint256 newI,
uint256 newK,
uint256 baseOutAmount,
@@ -104,30 +136,19 @@ contract DPPVault is DPPStorage {
_BASE_RESERVE_ >= minBaseReserve && _QUOTE_RESERVE_ >= minQuoteReserve,
"RESERVE_AMOUNT_IS_NOT_ENOUGH"
);
_LP_FEE_RATE_MODEL_.setFeeRate(newLpFeeRate);
_MT_FEE_RATE_MODEL_.setFeeRate(newMtFeeRate);
_I_.set(newI);
_K_.set(newK);
require(newLpFeeRate <= 1e18, "LP_FEE_RATE_OUT_OF_RANGE");
require(newK <= 1e18, "K_OUT_OF_RANGE");
require(newI > 0 && newI <= 1e36, "I_OUT_OF_RANGE");
_LP_FEE_RATE_ = uint64(newLpFeeRate);
_K_ = uint64(newK);
_I_ = uint128(newI);
_transferBaseOut(assetTo, baseOutAmount);
_transferQuoteOut(assetTo, quoteOutAmount);
_resetTargetAndReserve();
_checkIK();
emit Reset(newLpFeeRate, newMtFeeRate);
emit LpFeeRateChange(newLpFeeRate);
return true;
}
function _setRState() internal {
if (_BASE_RESERVE_ == _BASE_TARGET_ && _QUOTE_RESERVE_ == _QUOTE_TARGET_) {
_RState_ = PMMPricing.RState.ONE;
} else if (_BASE_RESERVE_ > _BASE_TARGET_ && _QUOTE_RESERVE_ < _QUOTE_TARGET_) {
_RState_ = PMMPricing.RState.BELOW_ONE;
} else if (_BASE_RESERVE_ < _BASE_TARGET_ && _QUOTE_RESERVE_ > _QUOTE_TARGET_) {
_RState_ = PMMPricing.RState.ABOVE_ONE;
} else {
require(false, "R_STATE_WRONG");
}
}
// ============ Asset Out ============
function _transferBaseOut(address to, uint256 amount) internal {

View File

@@ -14,37 +14,15 @@ interface IDPP {
address maintainer,
address baseTokenAddress,
address quoteTokenAddress,
address lpFeeRateModel,
uint256 lpFeeRate,
address mtFeeRateModel,
address kSource,
address iSource,
address gasPriceSource,
address tradePermissionManager
uint256 k,
uint256 i
) external;
function _LP_FEE_RATE_MODEL_() external returns (address);
function _MT_FEE_RATE_MODEL_() external returns (address);
//=========== admin ==========
function setLpFeeRateModel(address newLpFeeRateModel) external;
function setMtFeeRateModel(address newMtFeeRateModel) external;
function setTradePermissionManager(address newTradePermissionManager) external;
function setMaintainer(address newMaintainer) external;
function setGasPriceSource(address newGasPriceLimitSource) external;
function setISource(address newISource) external;
function setKSource(address newKSource) external;
function setBuy(bool open) external;
function setSell(bool open) external;
function ratioSync() external;
//==============================
@@ -58,7 +36,6 @@ interface IDPP {
function reset(
address assetTo,
uint256 newLpFeeRate,
uint256 newMtFeeRate,
uint256 newI,
uint256 newK,
uint256 baseOutAmount,

View File

@@ -150,10 +150,11 @@ contract DVMTrader is DVMVault {
{
(receiveQuoteAmount, ) = PMMPricing.sellBaseToken(getPMMState(), payBaseAmount);
uint256 lpFeeRate = _LP_FEE_RATE_;
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
mtFee = DecimalMath.mulFloor(receiveQuoteAmount, mtFeeRate);
receiveQuoteAmount = receiveQuoteAmount
.sub(DecimalMath.mulFloor(receiveQuoteAmount, _LP_FEE_RATE_))
.sub(DecimalMath.mulFloor(receiveQuoteAmount, lpFeeRate))
.sub(mtFee);
}
@@ -164,10 +165,11 @@ contract DVMTrader is DVMVault {
{
(receiveBaseAmount, ) = PMMPricing.sellQuoteToken(getPMMState(), payQuoteAmount);
uint256 lpFeeRate = _LP_FEE_RATE_;
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
mtFee = DecimalMath.mulFloor(receiveBaseAmount, mtFeeRate);
receiveBaseAmount = receiveBaseAmount
.sub(DecimalMath.mulFloor(receiveBaseAmount, _LP_FEE_RATE_))
.sub(DecimalMath.mulFloor(receiveBaseAmount, lpFeeRate))
.sub(mtFee);
}
@@ -194,7 +196,7 @@ contract DVMTrader is DVMVault {
uint256 Q,
uint256 B0,
uint256 Q0,
uint8 R
uint256 R
)
{
PMMPricing.PMMState memory state = getPMMState();
@@ -204,7 +206,7 @@ contract DVMTrader is DVMVault {
Q = state.Q;
B0 = state.B0;
Q0 = state.Q0;
R = uint8(state.R);
R = uint256(state.R);
}
function getMidPrice() public view returns (uint256 midPrice) {

View File

@@ -11,21 +11,17 @@ pragma experimental ABIEncoderV2;
import {InitializableOwnable} from "../lib/InitializableOwnable.sol";
import {ICloneFactory} from "../lib/CloneFactory.sol";
import {IFeeRateModel} from "../lib/FeeRateModel.sol";
import {IExternalValue} from "../lib/ExternalValue.sol";
import {IDPP} from "../DODOPrivatePool/intf/IDPP.sol";
import {IDPPAdmin} from "../DODOPrivatePool/intf/IDPPAdmin.sol";
import {IPermissionManager} from "../lib/PermissionManager.sol";
contract DPPFactory is InitializableOwnable {
// ============ Templates ============
address public immutable _CLONE_FACTORY_;
address public immutable _DPP_TEMPLATE_;
address public immutable _FEE_RATE_MODEL_TEMPLATE_;
address public immutable _PERMISSION_MANAGER_TEMPLATE_;
address public immutable _DEFAULT_GAS_PRICE_SOURCE_;
address public immutable _VALUE_SOURCE_;
address public immutable _DODO_SMART_APPROVE_;
address public immutable _DEFAULT_MAINTAINER_;
address public immutable _DEFAULT_MT_FEE_RATE_MODEL_;
address public immutable _DODO_APPROVE_;
address public _DPP_ADMIN_TEMPLATE_;
// ============ Registry ============
@@ -53,20 +49,16 @@ contract DPPFactory is InitializableOwnable {
address cloneFactory,
address dppTemplate,
address dppAdminTemplate,
address defautFeeRateModelTemplate,
address defaultPermissionManagerTemplate,
address defaultExternalValueTemplate,
address defaultGasPriceSource,
address dodoSmartApprove
address defaultMaintainer,
address defaultMtFeeRateModel,
address dodoApprove
) public {
_CLONE_FACTORY_ = cloneFactory;
_DPP_TEMPLATE_ = dppTemplate;
_DPP_ADMIN_TEMPLATE_ = dppAdminTemplate;
_DODO_SMART_APPROVE_ = dodoSmartApprove;
_FEE_RATE_MODEL_TEMPLATE_ = defautFeeRateModelTemplate;
_PERMISSION_MANAGER_TEMPLATE_ = defaultPermissionManagerTemplate;
_VALUE_SOURCE_ = defaultExternalValueTemplate;
_DEFAULT_GAS_PRICE_SOURCE_ = defaultGasPriceSource;
_DEFAULT_MAINTAINER_ = defaultMaintainer;
_DEFAULT_MT_FEE_RATE_MODEL_ = defaultMtFeeRateModel;
_DODO_APPROVE_ = dodoApprove;
}
function createDODOPrivatePool() external returns (address newPrivatePool) {
@@ -79,7 +71,6 @@ contract DPPFactory is InitializableOwnable {
address baseToken,
address quoteToken,
uint256 lpFeeRate,
uint256 mtFeeRate,
uint256 k,
uint256 i
) external {
@@ -89,19 +80,17 @@ contract DPPFactory is InitializableOwnable {
creator,
_dppAddress,
creator,
_DODO_SMART_APPROVE_
_DODO_APPROVE_
);
IDPP(_dppAddress).init(
adminModel,
creator,
_DEFAULT_MAINTAINER_,
baseToken,
quoteToken,
_createFeeRateModel(_dppAddress, lpFeeRate),
_createFeeRateModel(_dppAddress, mtFeeRate),
_createExternalValueModel(_dppAddress, k),
_createExternalValueModel(_dppAddress, i),
_DEFAULT_GAS_PRICE_SOURCE_,
_createPermissionManager(adminModel)
lpFeeRate,
_DEFAULT_MT_FEE_RATE_MODEL_,
k,
i
);
}
@@ -110,35 +99,14 @@ contract DPPFactory is InitializableOwnable {
emit NewDPP(baseToken, quoteToken, creator, dppAddress);
}
function _createFeeRateModel(address owner, uint256 feeRate)
internal
returns (address feeRateModel)
{
feeRateModel = ICloneFactory(_CLONE_FACTORY_).clone(_FEE_RATE_MODEL_TEMPLATE_);
IFeeRateModel(feeRateModel).init(owner, feeRate);
}
function _createPermissionManager(address owner) internal returns (address permissionManager) {
permissionManager = ICloneFactory(_CLONE_FACTORY_).clone(_PERMISSION_MANAGER_TEMPLATE_);
IPermissionManager(permissionManager).initOwner(owner);
}
function _createExternalValueModel(address owner, uint256 value)
internal
returns (address valueModel)
{
valueModel = ICloneFactory(_CLONE_FACTORY_).clone(_VALUE_SOURCE_);
IExternalValue(valueModel).init(owner, value);
}
function _createDPPAdminModel(
address owner,
address dpp,
address operator,
address dodoSmartApprove
address dodoApprove
) internal returns (address adminModel) {
adminModel = ICloneFactory(_CLONE_FACTORY_).clone(_DPP_ADMIN_TEMPLATE_);
IDPPAdmin(adminModel).init(owner, dpp, operator, dodoSmartApprove);
IDPPAdmin(adminModel).init(owner, dpp, operator, dodoApprove);
}
// ============ Admin Operation Functions ============

View File

@@ -213,7 +213,6 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
uint256 baseInAmount,
uint256 quoteInAmount,
uint256 lpFeeRate,
uint256 mtFeeRate,
uint256 i,
uint256 k,
uint256 deadLine
@@ -247,7 +246,6 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
_baseToken,
_quoteToken,
lpFeeRate,
mtFeeRate,
k,
i
);
@@ -255,7 +253,7 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
function resetDODOPrivatePool(
address dppAddress,
uint256[] memory paramList, //0 - newLpFeeRate, 1 - newMtFeeRate, 2 - newI, 3 - newK
uint256[] memory paramList, //0 - newLpFeeRate, 1 - newI, 2 - newK
uint256[] memory amountList, //0 - baseInAmount, 1 - quoteInAmount, 2 - baseOutAmount, 3- quoteOutAmount
uint8 flag, // 0 - ERC20, 1 - baseInETH, 2 - quoteInETH, 3 - baseOutETH, 4 - quoteOutETH
uint256 minBaseReserve,
@@ -282,7 +280,6 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
paramList[0],
paramList[1],
paramList[2],
paramList[3],
amountList[2],
amountList[3],
minBaseReserve,

View File

@@ -21,7 +21,7 @@ contract DODOV2RouteHelper {
uint256 Q;
uint256 B0;
uint256 Q0;
uint8 R;
uint256 R;
uint256 lpFeeRate;
uint256 mtFeeRate;
address baseToken;
@@ -60,19 +60,17 @@ contract DODOV2RouteHelper {
curRes.quoteToken = token0;
}
if(!IDODOV2(cur)._BUYING_CLOSE_() && !IDODOV2(cur)._SELLING_CLOSE_()){
(
curRes.i,
curRes.K,
curRes.B,
curRes.Q,
curRes.B0,
curRes.Q0,
curRes.R
) = IDODOV2(cur).getPMMStateForCall();
(
curRes.i,
curRes.K,
curRes.B,
curRes.Q,
curRes.B0,
curRes.Q0,
curRes.R
) = IDODOV2(cur).getPMMStateForCall();
(curRes.lpFeeRate, curRes.mtFeeRate) = IDODOV2(cur).getUserFeeRate(userAddr);
}
(curRes.lpFeeRate, curRes.mtFeeRate) = IDODOV2(cur).getUserFeeRate(userAddr);
curRes.curPair = cur;
res[i] = curRes;
}

View File

@@ -22,12 +22,6 @@ interface IDODOV2 {
function _QUOTE_TOKEN_() external view returns (address);
function _BUYING_CLOSE_() external view returns (bool);
function _SELLING_CLOSE_() external view returns (bool);
function _OWNER_() external returns (address);
function getPMMStateForCall() external view returns (
uint256 i,
uint256 K,
@@ -35,7 +29,7 @@ interface IDODOV2 {
uint256 Q,
uint256 B0,
uint256 Q0,
uint8 R
uint256 R
);
function getUserFeeRate(address user) external view returns (uint256 lpFeeRate, uint256 mtFeeRate);
@@ -64,7 +58,6 @@ interface IDODOV2 {
address baseToken,
address quoteToken,
uint256 lpFeeRate,
uint256 mtFeeRate,
uint256 k,
uint256 i
) external;
@@ -72,7 +65,6 @@ interface IDODOV2 {
function reset(
address operator,
uint256 newLpFeeRate,
uint256 newMtFeeRate,
uint256 newI,
uint256 newK,
uint256 baseOutAmount,
@@ -82,6 +74,8 @@ interface IDODOV2 {
) external returns (bool);
function getPrivatePoolBidirection(address token0, address token1) external view returns (address[] memory baseToken0Pool, address[] memory baseToken1Pool);
function _OWNER_() external returns (address);
//========== CrowdPooling ===========

View File

@@ -77,7 +77,6 @@ interface IDODOV2Proxy01 is IDODOV1Proxy01 {
uint256 baseInAmount,
uint256 quoteInAmount,
uint256 lpFeeRate,
uint256 mtFeeRate,
uint256 i,
uint256 k,
uint256 deadLine
@@ -85,7 +84,7 @@ interface IDODOV2Proxy01 is IDODOV1Proxy01 {
function resetDODOPrivatePool(
address dppAddress,
uint256[] memory paramList, //0 - newLpFeeRate, 1 - newMtFeeRate, 2 - newI, 3 - newK
uint256[] memory paramList, //0 - newLpFeeRate, 1 - newI, 2 - newK
uint256[] memory amountList, //0 - baseInAmount, 1 - quoteInAmount, 2 - baseOutAmount, 3 - quoteOutAmount
uint8 flag, // 0 - ERC20, 1 - baseInETH, 2 - quoteInETH, 3 - baseOutETH, 4 - quoteOutETH
uint256 minBaseReserve,