refactor DPP & DVM
This commit is contained in:
90
contracts/DODOPrivatePool/impl/DPPStorage.sol
Normal file
90
contracts/DODOPrivatePool/impl/DPPStorage.sol
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
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 "../../intf/IFeeRateModel.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {RState} from "../../lib/PMMPricing.sol";
|
||||
|
||||
/**
|
||||
* @title Storage
|
||||
* @author DODO Breeder
|
||||
*
|
||||
* @notice Local Variables
|
||||
*/
|
||||
contract DPPStorage is InitializableOwnable, ReentrancyGuard {
|
||||
using SafeMath for uint256;
|
||||
|
||||
// ============ Variables for Control ============
|
||||
|
||||
IExternalValue public _GAS_PRICE_LIMIT_;
|
||||
|
||||
// ============ Advanced Controls ============
|
||||
|
||||
bool public _BUYING_CLOSE_;
|
||||
bool public _SELLING_CLOSE_;
|
||||
|
||||
IPermissionManager public _TRADE_PERMISSION_;
|
||||
|
||||
// ============ Core Address ============
|
||||
|
||||
address public _MAINTAINER_; // collect maintainer fee
|
||||
|
||||
IERC20 public _BASE_TOKEN_;
|
||||
IERC20 public _QUOTE_TOKEN_;
|
||||
|
||||
uint256 public _BASE_RESERVE_;
|
||||
uint256 public _QUOTE_RESERVE_;
|
||||
uint256 public _BASE_TARGET_;
|
||||
uint256 public _QUOTE_TARGET_;
|
||||
RState public _RState_;
|
||||
|
||||
// ============ Variables for Pricing ============
|
||||
|
||||
IFeeRateModel public _LP_FEE_RATE_MODEL_;
|
||||
IFeeRateModel public _MT_FEE_RATE_MODEL_;
|
||||
IExternalValue public _K_;
|
||||
IExternalValue public _I_;
|
||||
|
||||
// ============ Setting Functions ============
|
||||
|
||||
function setLpFeeRateModel(address newLpFeeRateModel) external onlyOwner {
|
||||
_LP_FEE_RATE_MODEL_ = IFeeRateModel(newLpFeeRateModel);
|
||||
}
|
||||
|
||||
function setMtFeeRateModel(address newMtFeeRateModel) external onlyOwner {
|
||||
_MT_FEE_RATE_MODEL_ = IFeeRateModel(newMtFeeRateModel);
|
||||
}
|
||||
|
||||
function setTradePermissionManager(address newTradePermissionManager) external onlyOwner {
|
||||
_TRADE_PERMISSION_ = IPermissionManager(newTradePermissionManager);
|
||||
}
|
||||
|
||||
function setMaintainer(address newMaintainer) external onlyOwner {
|
||||
_MAINTAINER_ = newMaintainer;
|
||||
}
|
||||
|
||||
function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
|
||||
_GAS_PRICE_LIMIT_ = IExternalValue(newGasPriceLimitSource);
|
||||
}
|
||||
|
||||
function setBuy(bool open) external onlyOwner {
|
||||
_BUYING_CLOSE_ = !open;
|
||||
}
|
||||
|
||||
function setSell(bool open) external onlyOwner {
|
||||
_SELLING_CLOSE_ = !open;
|
||||
}
|
||||
}
|
||||
162
contracts/DODOPrivatePool/impl/DPPTrader.sol
Normal file
162
contracts/DODOPrivatePool/impl/DPPTrader.sol
Normal file
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {DPPVault} from "./DPPVault.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {RState, PMMState, PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
|
||||
contract DPPTrader is DPPVault {
|
||||
using SafeMath for uint256;
|
||||
|
||||
// ============ 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 ============
|
||||
|
||||
// todo 看看怎么能加上flash loan
|
||||
|
||||
function sellBase(address to)
|
||||
external
|
||||
preventReentrant
|
||||
limitGasPrice
|
||||
isSellAllow(to)
|
||||
returns (uint256 receiveQuoteAmount)
|
||||
{
|
||||
uint256 baseInput = getBaseInput();
|
||||
uint256 mtFee;
|
||||
uint256 newBaseTarget;
|
||||
RState newRState;
|
||||
(receiveQuoteAmount, mtFee, newRState, newBaseTarget) = querySellBase(tx.origin, baseInput);
|
||||
|
||||
_transferQuoteOut(to, receiveQuoteAmount);
|
||||
_transferQuoteOut(_MAINTAINER_, mtFee);
|
||||
|
||||
// update TARGET
|
||||
if (_RState_ != newRState) {
|
||||
_RState_ = newRState;
|
||||
_BASE_TARGET_ = newBaseTarget;
|
||||
}
|
||||
|
||||
_syncReserve();
|
||||
|
||||
return receiveQuoteAmount;
|
||||
}
|
||||
|
||||
function sellQuote(address to)
|
||||
external
|
||||
preventReentrant
|
||||
limitGasPrice
|
||||
isBuyAllow(to)
|
||||
returns (uint256 receiveBaseAmount)
|
||||
{
|
||||
uint256 quoteInput = getQuoteInput();
|
||||
uint256 mtFee;
|
||||
uint256 newQuoteTarget;
|
||||
RState newRState;
|
||||
(receiveBaseAmount, mtFee, newRState, newQuoteTarget) = querySellBase(
|
||||
tx.origin,
|
||||
quoteInput
|
||||
);
|
||||
|
||||
_transferBaseOut(to, receiveBaseAmount);
|
||||
_transferBaseOut(_MAINTAINER_, mtFee);
|
||||
|
||||
// update TARGET
|
||||
if (_RState_ != newRState) {
|
||||
_RState_ = newRState;
|
||||
_QUOTE_TARGET_ = newQuoteTarget;
|
||||
}
|
||||
|
||||
_syncReserve();
|
||||
|
||||
return receiveBaseAmount;
|
||||
}
|
||||
|
||||
// ============ Query Functions ============
|
||||
|
||||
function querySellBase(address trader, uint256 payBaseAmount)
|
||||
public
|
||||
view
|
||||
returns (
|
||||
uint256 receiveQuoteAmount,
|
||||
uint256 mtFee,
|
||||
RState newRState,
|
||||
uint256 newBaseTarget
|
||||
)
|
||||
{
|
||||
PMMState memory state = getPMMState();
|
||||
(receiveQuoteAmount, newRState) = PMMPricing.sellBaseToken(state, payBaseAmount);
|
||||
|
||||
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
mtFee = DecimalMath.mulCeil(receiveQuoteAmount, mtFeeRate);
|
||||
receiveQuoteAmount = DecimalMath.mulFloor(
|
||||
receiveQuoteAmount,
|
||||
DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
|
||||
);
|
||||
|
||||
return (receiveQuoteAmount, mtFee, newRState, state.B0);
|
||||
}
|
||||
|
||||
function querySellQuote(address trader, uint256 payQuoteAmount)
|
||||
public
|
||||
view
|
||||
returns (
|
||||
uint256 receiveBaseAmount,
|
||||
uint256 mtFee,
|
||||
RState newRState,
|
||||
uint256 newQuoteTarget
|
||||
)
|
||||
{
|
||||
PMMState memory state = getPMMState();
|
||||
(receiveBaseAmount, newRState) = PMMPricing.sellQuoteToken(state, payQuoteAmount);
|
||||
|
||||
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
mtFee = DecimalMath.mulCeil(receiveBaseAmount, mtFeeRate);
|
||||
receiveBaseAmount = DecimalMath.mulFloor(
|
||||
receiveBaseAmount,
|
||||
DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
|
||||
);
|
||||
return (receiveBaseAmount, mtFee, newRState, state.Q0);
|
||||
}
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function getPMMState() public view returns (PMMState memory state) {
|
||||
state.i = _I_.get();
|
||||
state.K = _K_.get();
|
||||
state.B = _BASE_RESERVE_;
|
||||
state.Q = _QUOTE_RESERVE_;
|
||||
state.B0 = _BASE_TARGET_;
|
||||
state.Q0 = _QUOTE_TARGET_;
|
||||
state.R = _RState_;
|
||||
PMMPricing.adjustedTarget(state);
|
||||
return state;
|
||||
}
|
||||
}
|
||||
110
contracts/DODOPrivatePool/impl/DPPVault.sol
Normal file
110
contracts/DODOPrivatePool/impl/DPPVault.sol
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {DPPStorage} from "./DPPStorage.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {SafeERC20} from "../../lib/SafeERC20.sol";
|
||||
import {Ownable} from "../../lib/Ownable.sol";
|
||||
|
||||
contract DPPVault is DPPStorage {
|
||||
using SafeMath for uint256;
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
// input
|
||||
|
||||
function getInput() public view returns (uint256 baseInput, uint256 quoteInput) {
|
||||
return (
|
||||
_BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_),
|
||||
_QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_)
|
||||
);
|
||||
}
|
||||
|
||||
function getBaseInput() public view returns (uint256 input) {
|
||||
return _BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_);
|
||||
}
|
||||
|
||||
function getQuoteInput() public view returns (uint256 input) {
|
||||
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
|
||||
}
|
||||
|
||||
// ============ Set Status ============
|
||||
|
||||
function _syncReserve() internal {
|
||||
_BASE_RESERVE_ = _BASE_TOKEN_.balanceOf(address(this));
|
||||
_QUOTE_RESERVE_ = _QUOTE_TOKEN_.balanceOf(address(this));
|
||||
}
|
||||
|
||||
function setTarget(uint256 baseTarget, uint256 quoteTarget) public onlyOwner {
|
||||
_BASE_TARGET_ = baseTarget;
|
||||
_QUOTE_TARGET_ = quoteTarget;
|
||||
_checkStatus();
|
||||
}
|
||||
|
||||
// todo 这里需要考虑,怎么一个tx同时更新k i 和 fee并reset
|
||||
function reset() public onlyOwner {
|
||||
_BASE_TARGET_ = _BASE_TOKEN_.balanceOf(address(this));
|
||||
_QUOTE_TARGET_ = _QUOTE_TOKEN_.balanceOf(address(this));
|
||||
_BASE_RESERVE_ = _BASE_TARGET_;
|
||||
_QUOTE_RESERVE_ = _QUOTE_TARGET_;
|
||||
}
|
||||
|
||||
function _checkStatus() internal view {
|
||||
require(
|
||||
!(_BASE_RESERVE_ < _BASE_TARGET_ && _QUOTE_RESERVE_ < _QUOTE_TARGET_),
|
||||
"STATUS_WRONG"
|
||||
);
|
||||
}
|
||||
|
||||
// ============ 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 _transferBaseOut(address to, uint256 amount) internal {
|
||||
if (amount > 0) {
|
||||
_BASE_TOKEN_.safeTransfer(to, amount);
|
||||
}
|
||||
}
|
||||
|
||||
function _transferQuoteOut(address to, uint256 amount) internal {
|
||||
if (amount > 0) {
|
||||
_QUOTE_TOKEN_.safeTransfer(to, amount);
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
to.transfer(amount);
|
||||
} else {
|
||||
IERC20(token).safeTransfer(msg.sender, amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user