Crowd Pooling test

This commit is contained in:
mingda
2020-12-13 17:47:47 +08:00
parent dc12b1d42d
commit b398f5b87d
10 changed files with 411 additions and 46 deletions

View File

@@ -33,6 +33,8 @@ contract CP is CPVesting {
6. poolFactory
*/
require(addressList.length == 7, "LIST_LENGTH_WRONG");
initOwner(addressList[0]);
_MAINTAINER_ = addressList[1];
_BASE_TOKEN_ = IERC20(addressList[2]);
@@ -49,7 +51,7 @@ contract CP is CPVesting {
3. freeze duration
*/
require(block.timestamp <= timeLine[0], "TIMELINE_WRONG");
require(timeLine.length == 4, "LIST_LENGTH_WRONG");
_PHASE_BID_STARTTIME_ = timeLine[0];
_PHASE_BID_ENDTIME_ = _PHASE_BID_STARTTIME_.add(timeLine[1]);
@@ -57,26 +59,27 @@ contract CP is CPVesting {
_FREEZE_DURATION_ = timeLine[3];
require(block.timestamp <= _PHASE_BID_STARTTIME_, "TIMELINE_WRONG");
/*
Value List
0. pool quote cap
1. pool base reserve
2. owner quote ratio
3. k
4 i
1. owner quote ratio
2. k
3. i
*/
require(valueList[4] > 0 && valueList[4] <= 10**36, "I_VALUE_WRONG");
require(valueList[3] <= 10**18, "K_VALUE_WRONG");
require(valueList[2] <= 10**18, "OWNER_RATIO_WRONG");
require(valueList.length == 4, "LIST_LENGTH_WRONG");
_POOL_QUOTE_CAP_ = valueList[0];
_POOL_BASE_RESERVE_ = valueList[1];
_OWNER_QUOTE_RATIO_ = valueList[2];
_OWNER_QUOTE_RATIO_ = valueList[1];
_K_ = valueList[2];
_I_ = valueList[3];
require(_I_ > 0 && _I_ <= 10**36, "I_VALUE_WRONG");
require(_K_ <= 10**18, "K_VALUE_WRONG");
require(_OWNER_QUOTE_RATIO_ <= 10**18, "OWNER_RATIO_WRONG");
_TOTAL_BASE_ = _BASE_TOKEN_.balanceOf(address(this));
require(_TOTAL_BASE_ >= _POOL_BASE_RESERVE_, "BASE_TOKEN_NOT_ENOUGH");
}
}

View File

@@ -55,8 +55,7 @@ contract CPFunding is CPStorage {
// ============ SETTLEMENT ============
function settle() external phaseSettlement preventReentrant {
require(!_SETTLED_, "ALREADY_SETTLED");
_SETTLED_ = true;
_settle();
(uint256 poolBase, uint256 poolQuote, uint256 ownerQuote) = getSettleResult();
_UNUSED_QUOTE_ = _QUOTE_TOKEN_.balanceOf(address(this)).sub(poolQuote).sub(ownerQuote);
@@ -80,15 +79,17 @@ contract CPFunding is CPStorage {
_poolQuoteToken = address(_QUOTE_TOKEN_);
_poolI = 1;
} else if (poolQuote < baseDepth) {
// poolI up round
_poolBaseToken = address(_BASE_TOKEN_);
_poolQuoteToken = address(_QUOTE_TOKEN_);
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divFloor(poolQuote, baseDepth));
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divCeil(poolQuote, baseDepth));
_poolI = avgPrice.mul(ratio).mul(ratio).divCeil(DecimalMath.ONE2);
} else if (poolQuote > baseDepth) {
// poolI down round
_poolBaseToken = address(_QUOTE_TOKEN_);
_poolQuoteToken = address(_BASE_TOKEN_);
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divFloor(baseDepth, poolQuote));
_poolI = DecimalMath.reciprocalFloor(avgPrice).mul(ratio).mul(ratio).divCeil(
_poolI = DecimalMath.reciprocalFloor(avgPrice).mul(ratio).mul(ratio).div(
DecimalMath.ONE2
);
}
@@ -96,11 +97,11 @@ contract CPFunding is CPStorage {
address(this),
_poolBaseToken,
_poolQuoteToken,
3e15,
0,
3e15, // 0.3%
_poolI,
DecimalMath.ONE
);
_AVG_SETTLED_PRICE_ = avgPrice;
}
_transferBaseOut(_POOL_, poolBase);
@@ -112,16 +113,18 @@ contract CPFunding is CPStorage {
// in case something wrong with base token contract
function emergencySettle() external phaseSettlement preventReentrant {
require(!_SETTLED_, "ALREADY_SETTLED");
require(
block.timestamp > _PHASE_CALM_ENDTIME_.add(_SETTLEMENT_EXPIRED_TIME_),
"NOT_EMERGENCY"
);
_SETTLED_ = true;
require(block.timestamp > _PHASE_CALM_ENDTIME_.add(_SETTLEMENT_EXPIRE_), "NOT_EMERGENCY");
_settle();
_UNUSED_QUOTE_ = _QUOTE_TOKEN_.balanceOf(address(this));
_UNUSED_BASE_ = _BASE_TOKEN_.balanceOf(address(this));
}
function _settle() internal {
require(!_SETTLED_, "ALREADY_SETTLED");
_SETTLED_ = true;
_SETTLED_TIME_ = block.timestamp;
}
// ============ Pricing ============
function getSettleResult()
@@ -139,9 +142,6 @@ contract CPFunding is CPStorage {
}
(uint256 soldBase, ) = PMMPricing.sellQuoteToken(_getPMMState(), poolQuote);
poolBase = _TOTAL_BASE_.sub(soldBase);
if (poolBase < _POOL_BASE_RESERVE_) {
poolBase = _POOL_BASE_RESERVE_;
}
ownerQuote = DecimalMath.mulFloor(poolQuote, _OWNER_QUOTE_RATIO_);
poolQuote = poolQuote.sub(ownerQuote);
}
@@ -156,6 +156,16 @@ contract CPFunding is CPStorage {
state.R = PMMPricing.RState.ONE;
}
function getExpectedAvgPrice() external view returns (uint256) {
require(!_SETTLED_, "ALREADY_SETTLED");
(uint256 poolBase, uint256 poolQuote, uint256 ownerQuote) = getSettleResult();
return
DecimalMath.divCeil(
poolQuote.add(ownerQuote),
_BASE_TOKEN_.balanceOf(address(this)).sub(poolBase)
);
}
// ============ Asset In ============
function _getQuoteInput() internal view returns (uint256 input) {
@@ -184,4 +194,10 @@ contract CPFunding is CPStorage {
_QUOTE_TOKEN_.safeTransfer(to, amount);
}
}
// ============ Asset Out ============
function getShares(address user) external view returns (uint256) {
return _SHARES_[user];
}
}

View File

@@ -18,15 +18,16 @@ import {IERC20} from "../../intf/IERC20.sol";
contract CPStorage is InitializableOwnable, ReentrancyGuard {
using SafeMath for uint256;
uint256 internal constant _SETTLEMENT_EXPIRED_TIME_ = 86400 * 7;
uint256 internal constant _SETTLEMENT_EXPIRE_ = 86400 * 7;
// ============ Timeline ============
uint256 _PHASE_BID_STARTTIME_;
uint256 _PHASE_BID_ENDTIME_;
uint256 _PHASE_CALM_ENDTIME_;
uint256 _FREEZE_DURATION_;
bool _SETTLED_;
uint256 public _PHASE_BID_STARTTIME_;
uint256 public _PHASE_BID_ENDTIME_;
uint256 public _PHASE_CALM_ENDTIME_;
uint256 public _SETTLED_TIME_;
uint256 public _FREEZE_DURATION_;
bool public _SETTLED_;
// ============ Core Address ============
@@ -35,11 +36,10 @@ contract CPStorage is InitializableOwnable, ReentrancyGuard {
// ============ Distribution Parameters ============
uint256 _OWNER_QUOTE_RATIO_; // 抽取一部分
uint256 _TOTAL_BASE_;
uint256 public _OWNER_QUOTE_RATIO_; // 抽取一部分
uint256 public _TOTAL_BASE_;
uint256 _POOL_QUOTE_CAP_;
uint256 _POOL_BASE_RESERVE_;
uint256 public _POOL_QUOTE_CAP_;
// ============ Settlement ============
@@ -53,8 +53,9 @@ contract CPStorage is InitializableOwnable, ReentrancyGuard {
mapping(address => bool) internal _QUOTE_CLAIMED_;
mapping(address => bool) internal _BASE_CLAIMED_;
address _POOL_FACTORY_;
address _POOL_;
address public _POOL_FACTORY_;
address public _POOL_;
uint256 public _AVG_SETTLED_PRICE_;
// ============ Advanced Control ============

View File

@@ -32,7 +32,7 @@ contract CPVesting is CPFunding {
}
modifier afterFreeze() {
require(block.timestamp >= _PHASE_CALM_ENDTIME_.add(_FREEZE_DURATION_), "FREEZED");
require(block.timestamp >= _SETTLED_TIME_.add(_FREEZE_DURATION_), "FREEZED");
_;
}