add crowdpooling pmmstate

This commit is contained in:
owen05
2021-01-15 13:24:48 +08:00
parent 450069ec1b
commit 3f707f5604
10 changed files with 206 additions and 156 deletions

View File

@@ -70,61 +70,32 @@ contract CPFunding is CPStorage {
function settle() external phaseSettlement preventReentrant {
_settle();
(uint256 poolBase, uint256 poolQuote) = getSettleResult();
_UNUSED_QUOTE_ = _QUOTE_TOKEN_.balanceOf(address(this)).sub(poolQuote);
_UNUSED_BASE_ = _BASE_TOKEN_.balanceOf(address(this)).sub(poolBase);
(uint256 poolBase, uint256 poolQuote, uint256 poolI, uint256 unUsedBase, uint256 unUsedQuote) = getSettleResult();
_UNUSED_BASE_ = unUsedBase;
_UNUSED_QUOTE_ = unUsedQuote;
// Try to make midPrice equal to avgPrice
// k=1, If quote and base are not balanced, one side must be cut off
// DVM truncated quote, but if more quote than base entering the pool, we need set the quote to the base
address _poolBaseToken;
address _poolQuoteToken;
// m = avgPrice
// i = m (1-quote/(m*base))
// if quote = m*base i = 1
// if quote > m*base reverse
{
address _poolBaseToken;
address _poolQuoteToken;
uint256 _poolI;
uint256 avgPrice = _UNUSED_BASE_ == 0
? _I_
: DecimalMath.divCeil(poolQuote, _UNUSED_BASE_);
uint256 baseDepth = DecimalMath.mulFloor(avgPrice, poolBase);
if (poolQuote == 0) {
// ask side only DVM
_poolBaseToken = address(_BASE_TOKEN_);
_poolQuoteToken = address(_QUOTE_TOKEN_);
_poolI = _I_;
} else if (_UNUSED_BASE_== poolBase) {
// standard bonding curve
_poolBaseToken = address(_BASE_TOKEN_);
_poolQuoteToken = address(_QUOTE_TOKEN_);
_poolI = 1;
} else if (_UNUSED_BASE_ < poolBase) {
// poolI up round
_poolBaseToken = address(_BASE_TOKEN_);
_poolQuoteToken = address(_QUOTE_TOKEN_);
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divFloor(poolQuote, baseDepth));
_poolI = avgPrice.mul(ratio).mul(ratio).divCeil(DecimalMath.ONE2);
} else if (_UNUSED_BASE_ > poolBase) {
// poolI down round
_poolBaseToken = address(_QUOTE_TOKEN_);
_poolQuoteToken = address(_BASE_TOKEN_);
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divCeil(baseDepth, poolQuote));
_poolI = ratio.mul(ratio).div(avgPrice);
}
_POOL_ = IDVMFactory(_POOL_FACTORY_).createDODOVendingMachine(
_poolBaseToken,
_poolQuoteToken,
3e15, // 0.3% lp feeRate
_poolI,
DecimalMath.ONE
);
_AVG_SETTLED_PRICE_ = avgPrice;
if (_UNUSED_BASE_ > poolBase) {
_poolBaseToken = address(_QUOTE_TOKEN_);
_poolQuoteToken = address(_BASE_TOKEN_);
} else {
_poolBaseToken = address(_BASE_TOKEN_);
_poolQuoteToken = address(_QUOTE_TOKEN_);
}
_POOL_ = IDVMFactory(_POOL_FACTORY_).createDODOVendingMachine(
_poolBaseToken,
_poolQuoteToken,
3e15, // 0.3% lp feeRate
poolI,
DecimalMath.ONE
);
uint256 avgPrice = unUsedBase == 0 ? _I_ : DecimalMath.divCeil(poolQuote, unUsedBase);
_AVG_SETTLED_PRICE_ = avgPrice;
_transferBaseOut(_POOL_, poolBase);
_transferQuoteOut(_POOL_, poolQuote);
@@ -148,13 +119,43 @@ contract CPFunding is CPStorage {
// ============ Pricing ============
function getSettleResult() public view returns (uint256 poolBase, uint256 poolQuote) {
function getSettleResult() public view returns (uint256 poolBase, uint256 poolQuote, uint256 poolI, uint256 unUsedBase, uint256 unUsedQuote) {
poolQuote = _QUOTE_TOKEN_.balanceOf(address(this));
if (poolQuote > _POOL_QUOTE_CAP_) {
poolQuote = _POOL_QUOTE_CAP_;
}
(uint256 soldBase,) = PMMPricing.sellQuoteToken(_getPMMState(), poolQuote);
poolBase = _TOTAL_BASE_.sub(soldBase);
unUsedQuote = _QUOTE_TOKEN_.balanceOf(address(this)).sub(poolQuote);
unUsedBase = _BASE_TOKEN_.balanceOf(address(this)).sub(poolBase);
// Try to make midPrice equal to avgPrice
// k=1, If quote and base are not balanced, one side must be cut off
// DVM truncated quote, but if more quote than base entering the pool, we need set the quote to the base
// m = avgPrice
// i = m (1-quote/(m*base))
// if quote = m*base i = 1
// if quote > m*base reverse
uint256 avgPrice = unUsedBase == 0 ? _I_ : DecimalMath.divCeil(poolQuote, unUsedBase);
uint256 baseDepth = DecimalMath.mulFloor(avgPrice, poolBase);
if (poolQuote == 0) {
// ask side only DVM
poolI = _I_;
} else if (unUsedBase== poolBase) {
// standard bonding curve
poolI = 1;
} else if (unUsedBase < poolBase) {
// poolI up round
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divFloor(poolQuote, baseDepth));
poolI = avgPrice.mul(ratio).mul(ratio).divCeil(DecimalMath.ONE2);
} else if (unUsedBase > poolBase) {
// poolI down round
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divCeil(baseDepth, poolQuote));
poolI = ratio.mul(ratio).div(avgPrice);
}
}
function _getPMMState() internal view returns (PMMPricing.PMMState memory state) {
@@ -169,7 +170,7 @@ contract CPFunding is CPStorage {
function getExpectedAvgPrice() external view returns (uint256) {
require(!_SETTLED_, "ALREADY_SETTLED");
(uint256 poolBase, uint256 poolQuote) = getSettleResult();
(uint256 poolBase, uint256 poolQuote, , , ) = getSettleResult();
return DecimalMath.divCeil(poolQuote, _BASE_TOKEN_.balanceOf(address(this)).sub(poolBase));
}