Pool KickStarter
This commit is contained in:
98
contracts/PoolKickstarter/impl/CA.sol
Normal file
98
contracts/PoolKickstarter/impl/CA.sol
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {CAVesting} from "./CAVesting.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
||||
import {IFeeRateModel} from "../../lib/FeeRateModel.sol";
|
||||
|
||||
contract CA is CAVesting {
|
||||
function init(
|
||||
address[] calldata addressList,
|
||||
uint256[] calldata timeLine,
|
||||
uint256[] calldata valueList,
|
||||
bytes calldata basePayBackData,
|
||||
bytes calldata quotePayBackData
|
||||
) external {
|
||||
/*
|
||||
Address List
|
||||
0. owner
|
||||
1. maintainer
|
||||
2. baseToken
|
||||
3. quoteToken
|
||||
4. basePayBack
|
||||
5. quotePayBack
|
||||
6. permissionManager
|
||||
7. feeRateModel
|
||||
*/
|
||||
|
||||
initOwner(addressList[0]);
|
||||
_MAINTAINER_ = addressList[1];
|
||||
_BASE_TOKEN_ = IERC20(addressList[2]);
|
||||
_QUOTE_TOKEN_ = IERC20(addressList[3]);
|
||||
_BASE_PAY_BACK_ = addressList[4];
|
||||
_QUOTE_PAY_BACK_ = addressList[5];
|
||||
_BIDDER_PERMISSION_ = IPermissionManager(addressList[6]);
|
||||
_MT_FEE_RATE_MODEL_ = IFeeRateModel(addressList[7]);
|
||||
|
||||
/*
|
||||
Time Line
|
||||
0. phase bid starttime
|
||||
1. phase bid endtime
|
||||
2. phase calm endtime
|
||||
3. start vesting time
|
||||
4. vesting duration
|
||||
*/
|
||||
|
||||
require(
|
||||
block.timestamp <= timeLine[0] &&
|
||||
timeLine[0] <= timeLine[1] &&
|
||||
timeLine[1] <= timeLine[2] &&
|
||||
timeLine[2] <= timeLine[3],
|
||||
"TIMELINE_WRONG"
|
||||
);
|
||||
|
||||
_PHASE_BID_STARTTIME_ = timeLine[0];
|
||||
_PHASE_BID_ENDTIME_ = timeLine[1];
|
||||
_PHASE_CALM_ENDTIME_ = timeLine[2];
|
||||
_START_VESTING_TIME_ = timeLine[3];
|
||||
|
||||
_VESTING_DURATION_ = timeLine[4];
|
||||
|
||||
/*
|
||||
Value List
|
||||
0. quote cap
|
||||
1. cliff rate
|
||||
2. k
|
||||
3. i
|
||||
4. owner ratio
|
||||
*/
|
||||
|
||||
require(
|
||||
valueList[1] <= 10**18 &&
|
||||
valueList[2] <= 10**18 &&
|
||||
valueList[3] > 0 &&
|
||||
valueList[3] <= 10**36 &&
|
||||
valueList[4] <= 10**18,
|
||||
"VALUE_RANGE_WRONG"
|
||||
);
|
||||
|
||||
_QUOTE_CAP_ = valueList[0];
|
||||
_CLIFF_RATE_ = valueList[1];
|
||||
_K_ = valueList[2];
|
||||
_I_ = valueList[3];
|
||||
_OWNER_RATIO_ = valueList[4];
|
||||
|
||||
// ============ External Call Data ============
|
||||
|
||||
_BASE_PAY_BACK_CALL_DATA_ = basePayBackData;
|
||||
_QUOTE_PAY_BACK_CALL_DATA_ = quotePayBackData;
|
||||
}
|
||||
}
|
||||
191
contracts/PoolKickstarter/impl/CAFunding.sol
Normal file
191
contracts/PoolKickstarter/impl/CAFunding.sol
Normal file
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {SafeERC20} from "../../lib/SafeERC20.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {IDVM} from "../../DODOVendingMachine/intf/IDVM.sol";
|
||||
import {IDVMFactory} from "../../Factory/DVMFactory.sol";
|
||||
import {CAStorage} from "./CAStorage.sol";
|
||||
import {PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
|
||||
contract CAFunding is CAStorage {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
// ============ BID & CALM PHASE ============
|
||||
|
||||
modifier isBidderAllow(address bidder) {
|
||||
require(_BIDDER_PERMISSION_.isAllowed(bidder), "BIDDER_NOT_ALLOWED");
|
||||
_;
|
||||
}
|
||||
|
||||
function bid(address to) external phaseBid preventReentrant isBidderAllow(to) {
|
||||
uint256 input = _getQuoteInput();
|
||||
uint256 mtFee = DecimalMath.mulFloor(input, _MT_FEE_RATE_MODEL_.getFeeRate(to));
|
||||
_transferQuoteOut(_MAINTAINER_, mtFee);
|
||||
_mintShares(to, input.sub(mtFee));
|
||||
_sync();
|
||||
}
|
||||
|
||||
function cancel(address assetTo, uint256 amount) external phaseBidOrCalm preventReentrant {
|
||||
require(_SHARES_[msg.sender] >= amount, "SHARES_NOT_ENOUGH");
|
||||
_burnShares(msg.sender, amount);
|
||||
_transferQuoteOut(assetTo, amount);
|
||||
_sync();
|
||||
}
|
||||
|
||||
function _mintShares(address to, uint256 amount) internal {
|
||||
_SHARES_[to] = _SHARES_[to].add(amount);
|
||||
_TOTAL_SHARES_ = _TOTAL_SHARES_.add(amount);
|
||||
}
|
||||
|
||||
function _burnShares(address from, uint256 amount) internal {
|
||||
_SHARES_[from] = _SHARES_[from].sub(amount);
|
||||
_TOTAL_SHARES_ = _TOTAL_SHARES_.sub(amount);
|
||||
}
|
||||
|
||||
// ============ SETTLEMENT ============
|
||||
|
||||
function settle() external phaseSettlement preventReentrant {
|
||||
require(!_SETTLED_, "ALREADY_SETTLED");
|
||||
_SETTLED_ = true;
|
||||
|
||||
(uint256 poolBase, uint256 poolQuote, uint256 ownerQuote) = getSettleResult();
|
||||
_UNUSED_QUOTE_ = _QUOTE_TOKEN_.balanceOf(address(this)).sub(poolQuote).sub(ownerQuote);
|
||||
_UNUSED_BASE_ = _BASE_TOKEN_.balanceOf(address(this)).sub(poolBase);
|
||||
uint256 avgPrice = DecimalMath.divCeil(poolQuote.add(ownerQuote), _UNUSED_BASE_);
|
||||
|
||||
// 这里的目的是让开盘价尽量等于avgPrice
|
||||
// 我们统一设定k=1,如果quote和base不平衡,就必然要截断一边
|
||||
// DVM截断了quote,所以如果进入池子的quote很多,就要把quote设置成DVM的base
|
||||
// m = avgPrice
|
||||
// i = m (1-quote/(m*base))
|
||||
// if quote = m*base i = 1
|
||||
// if quote > m*base reverse
|
||||
uint256 baseDepth = DecimalMath.mulFloor(avgPrice, poolBase);
|
||||
if (poolQuote == baseDepth) {
|
||||
_POOL_ = IDVMFactory(_POOL_FACTORY_).createDODOVendingMachine(
|
||||
address(this),
|
||||
address(_BASE_TOKEN_),
|
||||
address(_QUOTE_TOKEN_),
|
||||
3e15,
|
||||
0,
|
||||
1,
|
||||
DecimalMath.ONE
|
||||
);
|
||||
} else if (poolQuote < baseDepth) {
|
||||
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divFloor(poolQuote, baseDepth));
|
||||
_POOL_ = IDVMFactory(_POOL_FACTORY_).createDODOVendingMachine(
|
||||
address(this),
|
||||
address(_BASE_TOKEN_),
|
||||
address(_QUOTE_TOKEN_),
|
||||
3e15,
|
||||
0,
|
||||
avgPrice.mul(ratio).mul(ratio).divCeil(DecimalMath.ONE2),
|
||||
DecimalMath.ONE
|
||||
);
|
||||
} else if (poolQuote > baseDepth) {
|
||||
uint256 ratio = DecimalMath.ONE.sub(DecimalMath.divFloor(baseDepth, poolQuote));
|
||||
_POOL_ = IDVMFactory(_POOL_FACTORY_).createDODOVendingMachine(
|
||||
address(this),
|
||||
address(_QUOTE_TOKEN_),
|
||||
address(_BASE_TOKEN_),
|
||||
3e15,
|
||||
0,
|
||||
DecimalMath.reciprocalFloor(avgPrice).mul(ratio).mul(ratio).divCeil(
|
||||
DecimalMath.ONE2
|
||||
),
|
||||
DecimalMath.ONE
|
||||
);
|
||||
}
|
||||
|
||||
_transferBaseOut(_POOL_, poolBase);
|
||||
_transferQuoteOut(_POOL_, poolQuote);
|
||||
_transferQuoteOut(_OWNER_, ownerQuote);
|
||||
|
||||
IDVM(_POOL_).buyShares(address(this));
|
||||
}
|
||||
|
||||
// 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;
|
||||
_UNUSED_QUOTE_ = _QUOTE_TOKEN_.balanceOf(address(this));
|
||||
_UNUSED_BASE_ = _BASE_TOKEN_.balanceOf(address(this));
|
||||
}
|
||||
|
||||
// ============ Pricing ============
|
||||
|
||||
function getSettleResult()
|
||||
public
|
||||
view
|
||||
returns (
|
||||
uint256 poolBase,
|
||||
uint256 poolQuote,
|
||||
uint256 ownerQuote
|
||||
)
|
||||
{
|
||||
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);
|
||||
if (poolBase < _POOL_BASE_RESERVE_) {
|
||||
poolBase = _POOL_BASE_RESERVE_;
|
||||
}
|
||||
ownerQuote = DecimalMath.mulFloor(poolQuote, _OWNER_QUOTE_RATIO_);
|
||||
poolQuote = poolQuote.sub(ownerQuote);
|
||||
}
|
||||
|
||||
function _getPMMState() internal view returns (PMMPricing.PMMState memory state) {
|
||||
state.i = _I_;
|
||||
state.K = _K_;
|
||||
state.B = _TOTAL_BASE_;
|
||||
state.Q = 0;
|
||||
state.B0 = state.B;
|
||||
state.Q0 = 0;
|
||||
state.R = PMMPricing.RState.ONE;
|
||||
}
|
||||
|
||||
// ============ Asset In ============
|
||||
|
||||
function _getQuoteInput() internal view returns (uint256 input) {
|
||||
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
|
||||
}
|
||||
|
||||
// ============ Set States ============
|
||||
|
||||
function _sync() internal {
|
||||
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
|
||||
if (quoteBalance != _QUOTE_RESERVE_) {
|
||||
_QUOTE_RESERVE_ = quoteBalance;
|
||||
}
|
||||
}
|
||||
|
||||
// ============ Asset Out ============
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
105
contracts/PoolKickstarter/impl/CAStorage.sol
Normal file
105
contracts/PoolKickstarter/impl/CAStorage.sol
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
|
||||
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 {ReentrancyGuard} from "../../lib/ReentrancyGuard.sol";
|
||||
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
||||
import {IFeeRateModel} from "../../lib/FeeRateModel.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
|
||||
contract CAStorage is InitializableOwnable, ReentrancyGuard {
|
||||
using SafeMath for uint256;
|
||||
|
||||
uint256 internal constant _SETTLEMENT_EXPIRED_TIME_ = 86400 * 7;
|
||||
|
||||
// ============ Timeline ============
|
||||
|
||||
uint256 _PHASE_BID_STARTTIME_;
|
||||
uint256 _PHASE_BID_ENDTIME_;
|
||||
uint256 _PHASE_CALM_ENDTIME_;
|
||||
uint256 _FREEZE_DURATION_;
|
||||
bool _SETTLED_;
|
||||
|
||||
// ============ Core Address ============
|
||||
|
||||
IERC20 public _BASE_TOKEN_;
|
||||
IERC20 public _QUOTE_TOKEN_;
|
||||
|
||||
// ============ Distribution Parameters ============
|
||||
|
||||
uint256 _OWNER_QUOTE_RATIO_; // 抽取一部分
|
||||
uint256 _TOTAL_BASE_;
|
||||
|
||||
uint256 _POOL_QUOTE_CAP_;
|
||||
uint256 _POOL_BASE_RESERVE_;
|
||||
|
||||
// ============ Settlement ============
|
||||
|
||||
uint256 public _QUOTE_RESERVE_;
|
||||
|
||||
uint256 public _UNUSED_BASE_;
|
||||
uint256 public _UNUSED_QUOTE_;
|
||||
|
||||
uint256 public _TOTAL_SHARES_;
|
||||
mapping(address => uint256) internal _SHARES_;
|
||||
mapping(address => bool) internal _QUOTE_CLAIMED_;
|
||||
mapping(address => bool) internal _BASE_CLAIMED_;
|
||||
|
||||
address _POOL_FACTORY_;
|
||||
address _POOL_;
|
||||
|
||||
// ============ Advanced Control ============
|
||||
|
||||
address public _MAINTAINER_;
|
||||
IFeeRateModel public _MT_FEE_RATE_MODEL_;
|
||||
IPermissionManager public _BIDDER_PERMISSION_;
|
||||
|
||||
// ============ PMM Parameters ============
|
||||
|
||||
uint256 public _K_;
|
||||
uint256 public _I_;
|
||||
|
||||
// ============ Modifiers ============
|
||||
|
||||
modifier phaseBid() {
|
||||
require(
|
||||
block.timestamp > _PHASE_BID_STARTTIME_ && block.timestamp <= _PHASE_BID_ENDTIME_,
|
||||
"NOT_PHASE_BID"
|
||||
);
|
||||
_;
|
||||
}
|
||||
|
||||
modifier phaseCalm() {
|
||||
require(
|
||||
block.timestamp > _PHASE_BID_ENDTIME_ && block.timestamp <= _PHASE_CALM_ENDTIME_,
|
||||
"NOT_PHASE_CALM"
|
||||
);
|
||||
_;
|
||||
}
|
||||
|
||||
modifier phaseBidOrCalm() {
|
||||
require(
|
||||
block.timestamp > _PHASE_BID_STARTTIME_ && block.timestamp <= _PHASE_CALM_ENDTIME_,
|
||||
"NOT_PHASE_BID_OR_CALM"
|
||||
);
|
||||
_;
|
||||
}
|
||||
|
||||
modifier phaseSettlement() {
|
||||
require(block.timestamp > _PHASE_CALM_ENDTIME_, "NOT_PHASE_EXE");
|
||||
_;
|
||||
}
|
||||
|
||||
modifier phaseVesting() {
|
||||
require(_SETTLED_, "NOT_VESTING");
|
||||
_;
|
||||
}
|
||||
}
|
||||
56
contracts/PoolKickstarter/impl/CAVesting.sol
Normal file
56
contracts/PoolKickstarter/impl/CAVesting.sol
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {Ownable} from "../../lib/Ownable.sol";
|
||||
import {SafeERC20} from "../../lib/SafeERC20.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {CAFunding} from "./CAFunding.sol";
|
||||
|
||||
/**
|
||||
* @title CAVesting
|
||||
* @author DODO Breeder
|
||||
*
|
||||
* @notice Lock Token and release it linearly
|
||||
*/
|
||||
|
||||
contract CAVesting is CAFunding {
|
||||
using SafeMath for uint256;
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
modifier afterSettlement() {
|
||||
require(_SETTLED_, "NOT_SETTLED");
|
||||
_;
|
||||
}
|
||||
|
||||
modifier afterFreeze() {
|
||||
require(block.timestamp >= _PHASE_CALM_ENDTIME_.add(_FREEZE_DURATION_), "FREEZED");
|
||||
_;
|
||||
}
|
||||
|
||||
// ============ Functions ============
|
||||
|
||||
function claimBase() external afterSettlement {
|
||||
require(!_BASE_CLAIMED_[msg.sender], "BASE_CLAIMED");
|
||||
_BASE_CLAIMED_[msg.sender] = true;
|
||||
_transferBaseOut(msg.sender, _UNUSED_BASE_.mul(_SHARES_[msg.sender]).div(_TOTAL_SHARES_));
|
||||
}
|
||||
|
||||
function claimQuote() external afterSettlement {
|
||||
require(!_QUOTE_CLAIMED_[msg.sender], "QUOTE_CLAIMED");
|
||||
_QUOTE_CLAIMED_[msg.sender] = true;
|
||||
_transferQuoteOut(msg.sender, _UNUSED_QUOTE_.mul(_SHARES_[msg.sender]).div(_TOTAL_SHARES_));
|
||||
}
|
||||
|
||||
function claimLPToken() external onlyOwner afterFreeze {
|
||||
IERC20(_POOL_).safeTransfer(_OWNER_, IERC20(_POOL_).balanceOf(address(this)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user