add twap switch
This commit is contained in:
@@ -30,7 +30,8 @@ contract CP is CPVesting {
|
||||
function init(
|
||||
address[] calldata addressList,
|
||||
uint256[] calldata timeLine,
|
||||
uint256[] calldata valueList
|
||||
uint256[] calldata valueList,
|
||||
bool isOpenTWAP
|
||||
) external {
|
||||
/*
|
||||
Address List
|
||||
@@ -94,6 +95,8 @@ contract CP is CPVesting {
|
||||
|
||||
_TOTAL_BASE_ = _BASE_TOKEN_.balanceOf(address(this));
|
||||
|
||||
_IS_OPEN_TWAP_ = isOpenTWAP;
|
||||
|
||||
require(address(this).balance == _SETTEL_FUND_, "SETTLE_FUND_NOT_MATCH");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,8 @@ contract CPFunding is CPStorage {
|
||||
_poolQuoteToken,
|
||||
3e15, // 0.3% lp feeRate
|
||||
poolI,
|
||||
DecimalMath.ONE
|
||||
DecimalMath.ONE,
|
||||
_IS_OPEN_TWAP_
|
||||
);
|
||||
|
||||
uint256 avgPrice = unUsedBase == 0 ? _I_ : DecimalMath.divCeil(poolQuote, unUsedBase);
|
||||
|
||||
@@ -22,6 +22,7 @@ contract CPStorage is InitializableOwnable, ReentrancyGuard {
|
||||
|
||||
uint256 internal constant _SETTLEMENT_EXPIRE_ = 86400 * 7;
|
||||
uint256 internal constant _SETTEL_FUND_ = 200 finney;
|
||||
bool public _IS_OPEN_TWAP_ = false;
|
||||
|
||||
// ============ Timeline ============
|
||||
|
||||
|
||||
@@ -12,7 +12,8 @@ interface ICP {
|
||||
function init(
|
||||
address[] calldata addressList,
|
||||
uint256[] calldata timeLine,
|
||||
uint256[] calldata valueList
|
||||
uint256[] calldata valueList,
|
||||
bool isOpenTWAP
|
||||
) external;
|
||||
|
||||
function bid(address to) external;
|
||||
|
||||
@@ -27,7 +27,8 @@ contract DPP is DPPTrader {
|
||||
uint256 lpFeeRate,
|
||||
address mtFeeRateModel,
|
||||
uint256 k,
|
||||
uint256 i
|
||||
uint256 i,
|
||||
bool isOpenTWAP
|
||||
) external {
|
||||
initOwner(owner);
|
||||
|
||||
@@ -44,6 +45,10 @@ contract DPP is DPPTrader {
|
||||
_LP_FEE_RATE_ = uint64(lpFeeRate);
|
||||
_K_ = uint64(k);
|
||||
_I_ = uint128(i);
|
||||
|
||||
_IS_OPEN_TWAP_ = isOpenTWAP;
|
||||
if(isOpenTWAP) _BLOCK_TIMESTAMP_LAST_ = uint32(block.timestamp % 2**32);
|
||||
|
||||
_resetTargetAndReserve();
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ import {PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
contract DPPStorage is InitializableOwnable, ReentrancyGuard {
|
||||
using SafeMath for uint256;
|
||||
|
||||
bool public _IS_OPEN_TWAP_ = false;
|
||||
|
||||
// ============ Core Address ============
|
||||
|
||||
address public _MAINTAINER_;
|
||||
@@ -26,12 +28,15 @@ contract DPPStorage is InitializableOwnable, ReentrancyGuard {
|
||||
IERC20 public _BASE_TOKEN_;
|
||||
IERC20 public _QUOTE_TOKEN_;
|
||||
|
||||
uint128 public _BASE_RESERVE_;
|
||||
uint128 public _QUOTE_RESERVE_;
|
||||
uint112 public _BASE_RESERVE_;
|
||||
uint112 public _QUOTE_RESERVE_;
|
||||
uint32 public _BLOCK_TIMESTAMP_LAST_;
|
||||
|
||||
uint120 public _BASE_TARGET_;
|
||||
uint120 public _QUOTE_TARGET_;
|
||||
uint16 public _RState_;
|
||||
uint112 public _BASE_TARGET_;
|
||||
uint112 public _QUOTE_TARGET_;
|
||||
uint32 public _RState_;
|
||||
|
||||
uint256 public _BASE_PRICE_CUMULATIVE_LAST_;
|
||||
|
||||
// ============ Variables for Pricing ============
|
||||
|
||||
@@ -40,4 +45,44 @@ contract DPPStorage is InitializableOwnable, ReentrancyGuard {
|
||||
uint64 public _LP_FEE_RATE_;
|
||||
uint64 public _K_;
|
||||
uint128 public _I_;
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
|
||||
state.i = _I_;
|
||||
state.K = _K_;
|
||||
state.B = _BASE_RESERVE_;
|
||||
state.Q = _QUOTE_RESERVE_;
|
||||
state.B0 = _BASE_TARGET_;
|
||||
state.Q0 = _QUOTE_TARGET_;
|
||||
state.R = PMMPricing.RState(_RState_);
|
||||
PMMPricing.adjustedTarget(state);
|
||||
}
|
||||
|
||||
function getPMMStateForCall()
|
||||
external
|
||||
view
|
||||
returns (
|
||||
uint256 i,
|
||||
uint256 K,
|
||||
uint256 B,
|
||||
uint256 Q,
|
||||
uint256 B0,
|
||||
uint256 Q0,
|
||||
uint256 R
|
||||
)
|
||||
{
|
||||
PMMPricing.PMMState memory state = getPMMState();
|
||||
i = state.i;
|
||||
K = state.K;
|
||||
B = state.B;
|
||||
Q = state.Q;
|
||||
B0 = state.B0;
|
||||
Q0 = state.Q0;
|
||||
R = uint256(state.R);
|
||||
}
|
||||
|
||||
function getMidPrice() public view returns (uint256 midPrice) {
|
||||
return PMMPricing.getMidPrice(getPMMState());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +56,10 @@ contract DPPTrader is DPPVault {
|
||||
_setReserve(baseBalance, _QUOTE_TOKEN_.balanceOf(address(this)));
|
||||
|
||||
// update TARGET
|
||||
if (_RState_ != uint16(newRState)) {
|
||||
_RState_ = uint16(newRState);
|
||||
require(newBaseTarget <= uint120(-1),"OVERFLOW");
|
||||
_BASE_TARGET_ = uint120(newBaseTarget);
|
||||
if (_RState_ != uint32(newRState)) {
|
||||
require(newBaseTarget <= uint112(-1),"OVERFLOW");
|
||||
_BASE_TARGET_ = uint112(newBaseTarget);
|
||||
_RState_ = uint32(newRState);
|
||||
emit RChange(newRState);
|
||||
}
|
||||
|
||||
@@ -93,10 +93,10 @@ contract DPPTrader is DPPVault {
|
||||
_setReserve(_BASE_TOKEN_.balanceOf(address(this)), quoteBalance);
|
||||
|
||||
// update TARGET
|
||||
if (_RState_ != uint16(newRState)) {
|
||||
_RState_ = uint16(newRState);
|
||||
require(newQuoteTarget <= uint120(-1),"OVERFLOW");
|
||||
_QUOTE_TARGET_ = uint120(newQuoteTarget);
|
||||
if (_RState_ != uint32(newRState)) {
|
||||
require(newQuoteTarget <= uint112(-1),"OVERFLOW");
|
||||
_QUOTE_TARGET_ = uint112(newQuoteTarget);
|
||||
_RState_ = uint32(newRState);
|
||||
emit RChange(newRState);
|
||||
}
|
||||
|
||||
@@ -144,10 +144,10 @@ contract DPPTrader is DPPVault {
|
||||
require(uint256(_BASE_RESERVE_).sub(baseBalance) <= receiveBaseAmount, "FLASH_LOAN_FAILED");
|
||||
|
||||
_transferBaseOut(_MAINTAINER_, mtFee);
|
||||
if (_RState_ != uint16(newRState)) {
|
||||
_RState_ = uint16(newRState);
|
||||
require(newQuoteTarget <= uint120(-1),"OVERFLOW");
|
||||
_QUOTE_TARGET_ = uint120(newQuoteTarget);
|
||||
if (_RState_ != uint32(newRState)) {
|
||||
require(newQuoteTarget <= uint112(-1),"OVERFLOW");
|
||||
_QUOTE_TARGET_ = uint112(newQuoteTarget);
|
||||
_RState_ = uint32(newRState);
|
||||
emit RChange(newRState);
|
||||
}
|
||||
emit DODOSwap(
|
||||
@@ -173,10 +173,10 @@ contract DPPTrader is DPPVault {
|
||||
require(uint256(_QUOTE_RESERVE_).sub(quoteBalance) <= receiveQuoteAmount, "FLASH_LOAN_FAILED");
|
||||
|
||||
_transferQuoteOut(_MAINTAINER_, mtFee);
|
||||
if (_RState_ != uint16(newRState)) {
|
||||
_RState_ = uint16(newRState);
|
||||
require(newBaseTarget <= uint120(-1),"OVERFLOW");
|
||||
_BASE_TARGET_ = uint120(newBaseTarget);
|
||||
if (_RState_ != uint32(newRState)) {
|
||||
require(newBaseTarget <= uint112(-1),"OVERFLOW");
|
||||
_BASE_TARGET_ = uint112(newBaseTarget);
|
||||
_RState_ = uint32(newRState);
|
||||
emit RChange(newRState);
|
||||
}
|
||||
emit DODOSwap(
|
||||
@@ -239,44 +239,4 @@ contract DPPTrader is DPPVault {
|
||||
.sub(mtFee);
|
||||
newQuoteTarget = state.Q0;
|
||||
}
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
|
||||
state.i = _I_;
|
||||
state.K = _K_;
|
||||
state.B = _BASE_RESERVE_;
|
||||
state.Q = _QUOTE_RESERVE_;
|
||||
state.B0 = _BASE_TARGET_;
|
||||
state.Q0 = _QUOTE_TARGET_;
|
||||
state.R = PMMPricing.RState(_RState_);
|
||||
PMMPricing.adjustedTarget(state);
|
||||
}
|
||||
|
||||
function getPMMStateForCall()
|
||||
external
|
||||
view
|
||||
returns (
|
||||
uint256 i,
|
||||
uint256 K,
|
||||
uint256 B,
|
||||
uint256 Q,
|
||||
uint256 B0,
|
||||
uint256 Q0,
|
||||
uint256 R
|
||||
)
|
||||
{
|
||||
PMMPricing.PMMState memory state = getPMMState();
|
||||
i = state.i;
|
||||
K = state.K;
|
||||
B = state.B;
|
||||
Q = state.Q;
|
||||
B0 = state.B0;
|
||||
Q0 = state.Q0;
|
||||
R = uint256(state.R);
|
||||
}
|
||||
|
||||
function getMidPrice() public view returns (uint256 midPrice) {
|
||||
return PMMPricing.getMidPrice(getPMMState());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,62 +44,81 @@ contract DPPVault is DPPStorage {
|
||||
// ============ Get Input ============
|
||||
|
||||
function getBaseInput() public view returns (uint256 input) {
|
||||
return _BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_);
|
||||
return _BASE_TOKEN_.balanceOf(address(this)).sub(uint256(_BASE_RESERVE_));
|
||||
}
|
||||
|
||||
function getQuoteInput() public view returns (uint256 input) {
|
||||
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
|
||||
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(uint256(_QUOTE_RESERVE_));
|
||||
}
|
||||
|
||||
// ============ TWAP UPDATE ===========
|
||||
|
||||
function _twapUpdate() internal {
|
||||
uint32 blockTimestamp = uint32(block.timestamp % 2**32);
|
||||
uint32 timeElapsed = blockTimestamp - _BLOCK_TIMESTAMP_LAST_;
|
||||
if (timeElapsed > 0 && _BASE_RESERVE_ != 0 && _QUOTE_RESERVE_ != 0) {
|
||||
_BASE_PRICE_CUMULATIVE_LAST_ += getMidPrice() * timeElapsed;
|
||||
}
|
||||
_BLOCK_TIMESTAMP_LAST_ = blockTimestamp;
|
||||
}
|
||||
|
||||
// ============ 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);
|
||||
require(baseReserve <= uint112(-1) && quoteReserve <= uint112(-1), "OVERFLOW");
|
||||
_BASE_RESERVE_ = uint112(baseReserve);
|
||||
_QUOTE_RESERVE_ = uint112(quoteReserve);
|
||||
|
||||
if(_IS_OPEN_TWAP_) _twapUpdate();
|
||||
}
|
||||
|
||||
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");
|
||||
require(baseBalance <= uint112(-1) && quoteBalance <= uint112(-1), "OVERFLOW");
|
||||
|
||||
if (baseBalance != _BASE_RESERVE_) {
|
||||
_BASE_RESERVE_ = uint128(baseBalance);
|
||||
_BASE_RESERVE_ = uint112(baseBalance);
|
||||
}
|
||||
if (quoteBalance != _QUOTE_RESERVE_) {
|
||||
_QUOTE_RESERVE_ = uint128(quoteBalance);
|
||||
_QUOTE_RESERVE_ = uint112(quoteBalance);
|
||||
}
|
||||
|
||||
if(_IS_OPEN_TWAP_) _twapUpdate();
|
||||
}
|
||||
|
||||
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");
|
||||
require(baseBalance <= uint112(-1) && quoteBalance <= uint112(-1), "OVERFLOW");
|
||||
|
||||
_BASE_RESERVE_ = uint128(baseBalance);
|
||||
_QUOTE_RESERVE_ = uint128(quoteBalance);
|
||||
_BASE_TARGET_ = uint120(baseBalance);
|
||||
_QUOTE_TARGET_ = uint120(quoteBalance);
|
||||
_RState_ = uint16(PMMPricing.RState.ONE);
|
||||
_BASE_RESERVE_ = uint112(baseBalance);
|
||||
_QUOTE_RESERVE_ = uint112(quoteBalance);
|
||||
_BASE_TARGET_ = uint112(baseBalance);
|
||||
_QUOTE_TARGET_ = uint112(quoteBalance);
|
||||
_RState_ = uint32(PMMPricing.RState.ONE);
|
||||
|
||||
if(_IS_OPEN_TWAP_) _twapUpdate();
|
||||
}
|
||||
|
||||
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");
|
||||
require(baseBalance <= uint112(-1) && quoteBalance <= uint112(-1), "OVERFLOW");
|
||||
|
||||
if (baseBalance != _BASE_RESERVE_) {
|
||||
_BASE_TARGET_ = uint120(uint256(_BASE_TARGET_).mul(baseBalance).div(uint256(_BASE_RESERVE_)));
|
||||
_BASE_RESERVE_ = uint128(baseBalance);
|
||||
_BASE_TARGET_ = uint112(uint256(_BASE_TARGET_).mul(baseBalance).div(uint256(_BASE_RESERVE_)));
|
||||
_BASE_RESERVE_ = uint112(baseBalance);
|
||||
}
|
||||
if (quoteBalance != _QUOTE_RESERVE_) {
|
||||
_QUOTE_TARGET_ = uint120(uint256(_QUOTE_TARGET_).mul(quoteBalance).div(uint256(_QUOTE_RESERVE_)));
|
||||
_QUOTE_RESERVE_ = uint128(quoteBalance);
|
||||
_QUOTE_TARGET_ = uint112(uint256(_QUOTE_TARGET_).mul(quoteBalance).div(uint256(_QUOTE_RESERVE_)));
|
||||
_QUOTE_RESERVE_ = uint112(quoteBalance);
|
||||
}
|
||||
|
||||
if(_IS_OPEN_TWAP_) _twapUpdate();
|
||||
}
|
||||
|
||||
function reset(
|
||||
|
||||
@@ -17,7 +17,8 @@ interface IDPP {
|
||||
uint256 lpFeeRate,
|
||||
address mtFeeRateModel,
|
||||
uint256 k,
|
||||
uint256 i
|
||||
uint256 i,
|
||||
bool isOpenTWAP
|
||||
) external;
|
||||
|
||||
function _MT_FEE_RATE_MODEL_() external returns (address);
|
||||
|
||||
@@ -28,7 +28,8 @@ contract DVM is DVMTrader, DVMFunding {
|
||||
uint256 lpFeeRate,
|
||||
address mtFeeRateModel,
|
||||
uint256 i,
|
||||
uint256 k
|
||||
uint256 k,
|
||||
bool isOpenTWAP
|
||||
) external {
|
||||
require(baseTokenAddress != quoteTokenAddress, "BASE_QUOTE_CAN_NOT_BE_SAME");
|
||||
_BASE_TOKEN_ = IERC20(baseTokenAddress);
|
||||
@@ -44,6 +45,9 @@ contract DVM is DVMTrader, DVMFunding {
|
||||
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
|
||||
_MAINTAINER_ = maintainer;
|
||||
|
||||
_IS_OPEN_TWAP_ = isOpenTWAP;
|
||||
if(isOpenTWAP) _BLOCK_TIMESTAMP_LAST_ = uint32(block.timestamp % 2**32);
|
||||
|
||||
string memory connect = "_";
|
||||
string memory suffix = "DLP";
|
||||
|
||||
|
||||
@@ -14,10 +14,13 @@ import {DODOMath} from "../../lib/DODOMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {IFeeRateModel} from "../../lib/FeeRateModel.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
|
||||
contract DVMStorage is ReentrancyGuard {
|
||||
using SafeMath for uint256;
|
||||
|
||||
bool public _IS_OPEN_TWAP_ = false;
|
||||
|
||||
// ============ Core Address ============
|
||||
|
||||
address public _MAINTAINER_;
|
||||
@@ -25,8 +28,11 @@ contract DVMStorage is ReentrancyGuard {
|
||||
IERC20 public _BASE_TOKEN_;
|
||||
IERC20 public _QUOTE_TOKEN_;
|
||||
|
||||
uint128 public _BASE_RESERVE_;
|
||||
uint128 public _QUOTE_RESERVE_;
|
||||
uint112 public _BASE_RESERVE_;
|
||||
uint112 public _QUOTE_RESERVE_;
|
||||
uint32 public _BLOCK_TIMESTAMP_LAST_;
|
||||
|
||||
uint256 public _BASE_PRICE_CUMULATIVE_LAST_;
|
||||
|
||||
// ============ Shares (ERC20) ============
|
||||
|
||||
@@ -51,4 +57,45 @@ contract DVMStorage is ReentrancyGuard {
|
||||
IFeeRateModel public _MT_FEE_RATE_MODEL_;
|
||||
uint256 public _K_;
|
||||
uint256 public _I_;
|
||||
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
|
||||
state.i = _I_;
|
||||
state.K = _K_;
|
||||
state.B = _BASE_RESERVE_;
|
||||
state.Q = _QUOTE_RESERVE_;
|
||||
state.B0 = 0; // will be calculated in adjustedTarget
|
||||
state.Q0 = 0;
|
||||
state.R = PMMPricing.RState.ABOVE_ONE;
|
||||
PMMPricing.adjustedTarget(state);
|
||||
}
|
||||
|
||||
function getPMMStateForCall()
|
||||
external
|
||||
view
|
||||
returns (
|
||||
uint256 i,
|
||||
uint256 K,
|
||||
uint256 B,
|
||||
uint256 Q,
|
||||
uint256 B0,
|
||||
uint256 Q0,
|
||||
uint256 R
|
||||
)
|
||||
{
|
||||
PMMPricing.PMMState memory state = getPMMState();
|
||||
i = state.i;
|
||||
K = state.K;
|
||||
B = state.B;
|
||||
Q = state.Q;
|
||||
B0 = state.B0;
|
||||
Q0 = state.Q0;
|
||||
R = uint256(state.R);
|
||||
}
|
||||
|
||||
function getMidPrice() public view returns (uint256 midPrice) {
|
||||
return PMMPricing.getMidPrice(getPMMState());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,44 +177,4 @@ contract DVMTrader is DVMVault {
|
||||
.sub(DecimalMath.mulFloor(receiveBaseAmount, lpFeeRate))
|
||||
.sub(mtFee);
|
||||
}
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
|
||||
state.i = _I_;
|
||||
state.K = _K_;
|
||||
state.B = _BASE_RESERVE_;
|
||||
state.Q = _QUOTE_RESERVE_;
|
||||
state.B0 = 0; // will be calculated in adjustedTarget
|
||||
state.Q0 = 0;
|
||||
state.R = PMMPricing.RState.ABOVE_ONE;
|
||||
PMMPricing.adjustedTarget(state);
|
||||
}
|
||||
|
||||
function getPMMStateForCall()
|
||||
external
|
||||
view
|
||||
returns (
|
||||
uint256 i,
|
||||
uint256 K,
|
||||
uint256 B,
|
||||
uint256 Q,
|
||||
uint256 B0,
|
||||
uint256 Q0,
|
||||
uint256 R
|
||||
)
|
||||
{
|
||||
PMMPricing.PMMState memory state = getPMMState();
|
||||
i = state.i;
|
||||
K = state.K;
|
||||
B = state.B;
|
||||
Q = state.Q;
|
||||
B0 = state.B0;
|
||||
Q0 = state.Q0;
|
||||
R = uint256(state.R);
|
||||
}
|
||||
|
||||
function getMidPrice() public view returns (uint256 midPrice) {
|
||||
return PMMPricing.getMidPrice(getPMMState());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,31 +43,46 @@ contract DVMVault is DVMStorage {
|
||||
// ============ Asset In ============
|
||||
|
||||
function getBaseInput() public view returns (uint256 input) {
|
||||
return _BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_);
|
||||
return _BASE_TOKEN_.balanceOf(address(this)).sub(uint256(_BASE_RESERVE_));
|
||||
}
|
||||
|
||||
function getQuoteInput() public view returns (uint256 input) {
|
||||
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
|
||||
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(uint256(_QUOTE_RESERVE_));
|
||||
}
|
||||
|
||||
// ============ TWAP UPDATE ===========
|
||||
|
||||
function _twapUpdate() internal {
|
||||
uint32 blockTimestamp = uint32(block.timestamp % 2**32);
|
||||
uint32 timeElapsed = blockTimestamp - _BLOCK_TIMESTAMP_LAST_;
|
||||
if (timeElapsed > 0 && _BASE_RESERVE_ != 0 && _QUOTE_RESERVE_ != 0) {
|
||||
_BASE_PRICE_CUMULATIVE_LAST_ += getMidPrice() * timeElapsed;
|
||||
}
|
||||
_BLOCK_TIMESTAMP_LAST_ = blockTimestamp;
|
||||
}
|
||||
|
||||
// ============ Set States ============
|
||||
|
||||
function _setReserve(uint256 baseReserve, uint256 quoteReserve) internal {
|
||||
require(baseReserve <= uint128(-1) && quoteReserve <= uint128(-1), "OVERFLOW");
|
||||
_BASE_RESERVE_ = uint128(baseReserve);
|
||||
_QUOTE_RESERVE_ = uint128(quoteReserve);
|
||||
require(baseReserve <= uint112(-1) && quoteReserve <= uint112(-1), "OVERFLOW");
|
||||
_BASE_RESERVE_ = uint112(baseReserve);
|
||||
_QUOTE_RESERVE_ = uint112(quoteReserve);
|
||||
|
||||
if(_IS_OPEN_TWAP_) _twapUpdate();
|
||||
}
|
||||
|
||||
function _sync() internal {
|
||||
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
|
||||
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
|
||||
require(baseBalance <= uint128(-1) && quoteBalance <= uint128(-1), "OVERFLOW");
|
||||
require(baseBalance <= uint112(-1) && quoteBalance <= uint112(-1), "OVERFLOW");
|
||||
if (baseBalance != _BASE_RESERVE_) {
|
||||
_BASE_RESERVE_ = uint128(baseBalance);
|
||||
_BASE_RESERVE_ = uint112(baseBalance);
|
||||
}
|
||||
if (quoteBalance != _QUOTE_RESERVE_) {
|
||||
_QUOTE_RESERVE_ = uint128(quoteBalance);
|
||||
_QUOTE_RESERVE_ = uint112(quoteBalance);
|
||||
}
|
||||
|
||||
if(_IS_OPEN_TWAP_) _twapUpdate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ interface IDVM {
|
||||
uint256 lpFeeRate,
|
||||
address mtFeeRateModel,
|
||||
uint256 i,
|
||||
uint256 k
|
||||
uint256 k,
|
||||
bool isOpenTWAP
|
||||
) external;
|
||||
|
||||
function _BASE_TOKEN_() external returns (address);
|
||||
|
||||
@@ -105,7 +105,8 @@ contract CrowdPoolingFactory is InitializableOwnable {
|
||||
address baseToken,
|
||||
address quoteToken,
|
||||
uint256[] memory timeLine,
|
||||
uint256[] memory valueList
|
||||
uint256[] memory valueList,
|
||||
bool isOpenTWAP
|
||||
) external valueCheck(cpAddress,baseToken,timeLine,valueList) {
|
||||
{
|
||||
address[] memory addressList = new address[](7);
|
||||
@@ -120,7 +121,8 @@ contract CrowdPoolingFactory is InitializableOwnable {
|
||||
ICP(cpAddress).init(
|
||||
addressList,
|
||||
timeLine,
|
||||
valueList
|
||||
valueList,
|
||||
isOpenTWAP
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,8 @@ contract DPPFactory is InitializableOwnable {
|
||||
address quoteToken,
|
||||
uint256 lpFeeRate,
|
||||
uint256 k,
|
||||
uint256 i
|
||||
uint256 i,
|
||||
bool isOpenTwap
|
||||
) external {
|
||||
{
|
||||
address _dppAddress = dppAddress;
|
||||
@@ -96,7 +97,8 @@ contract DPPFactory is InitializableOwnable {
|
||||
lpFeeRate,
|
||||
_DEFAULT_MT_FEE_RATE_MODEL_,
|
||||
k,
|
||||
i
|
||||
i,
|
||||
isOpenTwap
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,8 @@ interface IDVMFactory {
|
||||
address quoteToken,
|
||||
uint256 lpFeeRate,
|
||||
uint256 i,
|
||||
uint256 k
|
||||
uint256 k,
|
||||
bool isOpenTWAP
|
||||
) external returns (address newVendingMachine);
|
||||
}
|
||||
|
||||
@@ -74,7 +75,8 @@ contract DVMFactory is InitializableOwnable {
|
||||
address quoteToken,
|
||||
uint256 lpFeeRate,
|
||||
uint256 i,
|
||||
uint256 k
|
||||
uint256 k,
|
||||
bool isOpenTWAP
|
||||
) external returns (address newVendingMachine) {
|
||||
newVendingMachine = ICloneFactory(_CLONE_FACTORY_).clone(_DVM_TEMPLATE_);
|
||||
{
|
||||
@@ -85,7 +87,8 @@ contract DVMFactory is InitializableOwnable {
|
||||
lpFeeRate,
|
||||
_DEFAULT_MT_FEE_RATE_MODEL_,
|
||||
i,
|
||||
k
|
||||
k,
|
||||
isOpenTWAP
|
||||
);
|
||||
}
|
||||
_REGISTRY_[baseToken][quoteToken].push(newVendingMachine);
|
||||
|
||||
@@ -114,6 +114,7 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
|
||||
uint256 lpFeeRate,
|
||||
uint256 i,
|
||||
uint256 k,
|
||||
bool isOpenTWAP,
|
||||
uint256 deadLine
|
||||
)
|
||||
external
|
||||
@@ -131,7 +132,8 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
|
||||
_quoteToken,
|
||||
lpFeeRate,
|
||||
i,
|
||||
k
|
||||
k,
|
||||
isOpenTWAP
|
||||
);
|
||||
}
|
||||
|
||||
@@ -234,6 +236,7 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
|
||||
uint256 lpFeeRate,
|
||||
uint256 i,
|
||||
uint256 k,
|
||||
bool isOpenTwap,
|
||||
uint256 deadLine
|
||||
)
|
||||
external
|
||||
@@ -266,7 +269,8 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
|
||||
_quoteToken,
|
||||
lpFeeRate,
|
||||
k,
|
||||
i
|
||||
i,
|
||||
isOpenTwap
|
||||
);
|
||||
}
|
||||
|
||||
@@ -658,6 +662,7 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
|
||||
uint256 baseInAmount,
|
||||
uint256[] memory timeLine,
|
||||
uint256[] memory valueList,
|
||||
bool isOpenTWAP,
|
||||
uint256 deadLine
|
||||
) external override payable preventReentrant judgeExpired(deadLine) returns (address payable newCrowdPooling) {
|
||||
address _baseToken = baseToken;
|
||||
@@ -681,7 +686,8 @@ contract DODOV2Proxy01 is IDODOV2Proxy01, ReentrancyGuard, InitializableOwnable
|
||||
_baseToken,
|
||||
_quoteToken,
|
||||
timeLine,
|
||||
valueList
|
||||
valueList,
|
||||
isOpenTWAP
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,8 @@ interface IDODOV2 {
|
||||
address quoteToken,
|
||||
uint256 lpFeeRate,
|
||||
uint256 i,
|
||||
uint256 k
|
||||
uint256 k,
|
||||
bool isOpenTWAP
|
||||
) external returns (address newVendingMachine);
|
||||
|
||||
function buyShares(address to) external returns (uint256,uint256,uint256);
|
||||
@@ -59,7 +60,8 @@ interface IDODOV2 {
|
||||
address quoteToken,
|
||||
uint256 lpFeeRate,
|
||||
uint256 k,
|
||||
uint256 i
|
||||
uint256 i,
|
||||
bool isOpenTwap
|
||||
) external;
|
||||
|
||||
function reset(
|
||||
@@ -87,7 +89,8 @@ interface IDODOV2 {
|
||||
address baseToken,
|
||||
address quoteToken,
|
||||
uint256[] memory timeLine,
|
||||
uint256[] memory valueList
|
||||
uint256[] memory valueList,
|
||||
bool isOpenTWAP
|
||||
) external;
|
||||
|
||||
function bid(address to) external;
|
||||
|
||||
@@ -48,6 +48,7 @@ interface IDODOV2Proxy01 {
|
||||
uint256 lpFeeRate,
|
||||
uint256 i,
|
||||
uint256 k,
|
||||
bool isOpenTWAP,
|
||||
uint256 deadLine
|
||||
) external payable returns (address newVendingMachine, uint256 shares);
|
||||
|
||||
@@ -76,6 +77,7 @@ interface IDODOV2Proxy01 {
|
||||
uint256 lpFeeRate,
|
||||
uint256 i,
|
||||
uint256 k,
|
||||
bool isOpenTwap,
|
||||
uint256 deadLine
|
||||
) external payable returns (address newPrivatePool);
|
||||
|
||||
@@ -95,6 +97,7 @@ interface IDODOV2Proxy01 {
|
||||
uint256 baseInAmount,
|
||||
uint256[] memory timeLine,
|
||||
uint256[] memory valueList,
|
||||
bool isOpenTWAP,
|
||||
uint256 deadLine
|
||||
) external payable returns (address payable newCrowdPooling);
|
||||
|
||||
@@ -111,7 +114,7 @@ interface IDODOV2Proxy01 {
|
||||
uint256 quoteAmount,
|
||||
uint256 baseMinShares,
|
||||
uint256 quoteMinShares,
|
||||
uint8 flag, // 0 erc20 Out 1 baseOutETH 2 quoteOut ETH
|
||||
uint8 flag, // 0 erc20 Out 1 baseInETH 2 quoteInETH
|
||||
uint256 deadLine
|
||||
) external payable returns(uint256, uint256);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user