simplify dpp
This commit is contained in:
@@ -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 ============
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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_;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user