Files
dodo-contractV2/contracts/DODOStarter/impl/FairFunding.sol

401 lines
14 KiB
Solidity
Raw Normal View History

2021-11-21 19:50:04 +08:00
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
import {IQuota} from "../../DODOFee/UserQuota.sol";
import {SafeMath} from "../../lib/SafeMath.sol";
import {DecimalMath} from "../../lib/DecimalMath.sol";
import {IERC20} from "../../intf/IERC20.sol";
import {SafeERC20} from "../../lib/SafeERC20.sol";
2021-12-02 16:15:22 +08:00
import {Vesting} from "./Vesting.sol";
2021-11-21 19:50:04 +08:00
2021-12-02 16:15:22 +08:00
contract FairFunding is Vesting {
2021-11-21 19:50:04 +08:00
using SafeMath for uint256;
using SafeERC20 for IERC20;
2022-02-15 09:31:46 +08:00
uint256 internal constant _SETTEL_FUND_ = 2e17;
2021-11-21 19:50:04 +08:00
// ============ Fair Mode ============
uint256 public _COOLING_DURATION_;
mapping(address => uint256) _FUNDS_DEPOSITED_;
mapping(address => bool) _FUNDS_CLAIMED_;
uint256 public _USED_FUND_RATIO_;
uint256 public _FINAL_PRICE_;
uint256 public _LOWER_LIMIT_PRICE_;
uint256 public _UPPER_LIMIT_PRICE_;
2021-12-23 18:13:04 +08:00
bool public _IS_OVERCAP_STOP = false;
2021-12-19 12:33:17 +08:00
receive() external payable {
require(_INITIALIZED_ == false, "WE_NOT_SAVE_ETH_AFTER_INIT");
}
2022-02-08 22:23:52 +08:00
// ============ Events ============
event Settle(address indexed account);
event DepositFund(address indexed account, uint256 fundAmount);
event WithdrawFund(address indexed caller, address indexed to, uint256 fundAmount, bool isSettled);
event ClaimToken(address indexed caller, address indexed to, uint256 tokenAmount, uint256 fundAmount);
event WithdrawUnallocatedToken(address indexed to, uint256 tokenAmount);
event InitializeLiquidity(address pool, uint256 tokenAmount);
event ClaimFund(address indexed to, uint256 fundAmount);
2021-12-02 16:15:22 +08:00
// ============ Init ============
function init(
address[] calldata addressList,
uint256[] calldata timeLine,
2021-12-23 18:13:04 +08:00
uint256[] calldata valueList,
bool isOverCapStop
2021-12-02 16:15:22 +08:00
) external {
/*
Address List
0. owner
1. sellToken
2. fundToken
3. quotaManager
4. poolFactory
*/
require(addressList.length == 5, "ADDR_LENGTH_WRONG");
initOwner(addressList[0]);
_TOKEN_ADDRESS_ = addressList[1];
_FUNDS_ADDRESS_ = addressList[2];
_QUOTA_ = addressList[3];
_POOL_FACTORY_ = addressList[4];
/*
Time Line
0. starttime
1. bid duration
2. calm duration
3. token vesting starttime
4. token vesting duration
5. fund vesting starttime
6. fund vesting duration
7. lp vesting starttime
8. lp vesting duration
*/
require(timeLine.length == 9, "TIME_LENGTH_WRONG");
_START_TIME_ = timeLine[0];
_BIDDING_DURATION_ = timeLine[1];
_COOLING_DURATION_ = timeLine[2];
_TOKEN_VESTING_START_ = timeLine[3];
_TOKEN_VESTING_DURATION_ = timeLine[4];
_FUNDS_VESTING_START_ = timeLine[5];
_FUNDS_VESTING_DURATION_ = timeLine[6];
_LP_VESTING_START_ = timeLine[7];
_LP_VESTING_DURATION_ = timeLine[8];
require(block.timestamp <= _START_TIME_, "START_TIME_WRONG");
require(_START_TIME_.add(_BIDDING_DURATION_).add(_COOLING_DURATION_) <= _TOKEN_VESTING_START_, "TOKEN_VESTING_TIME_WRONG");
require(_START_TIME_.add(_BIDDING_DURATION_).add(_COOLING_DURATION_) <= _FUNDS_VESTING_START_, "FUND_VESTING_TIME_WRONG");
2021-12-10 09:51:02 +08:00
require(_START_TIME_.add(_BIDDING_DURATION_).add(_COOLING_DURATION_) <= _LP_VESTING_START_, "LP_VESTING_TIME_WRONG");
2021-12-02 16:15:22 +08:00
/*
Value List
0. lower price
1. upper price
2. token cliffRate
3. fund cliffRate
4. lp cliffRate
5. initial liquidity
*/
require(valueList.length == 6, "VALUE_LENGTH_WRONG");
_LOWER_LIMIT_PRICE_ = valueList[0];
_UPPER_LIMIT_PRICE_ = valueList[1];
_TOKEN_CLIFF_RATE_ = valueList[2];
_FUNDS_CLIFF_RATE_ = valueList[3];
_LP_CLIFF_RATE_ = valueList[4];
_INITIAL_FUND_LIQUIDITY_ = valueList[5];
2021-12-10 09:51:02 +08:00
require(_LOWER_LIMIT_PRICE_ > 0, "LOWER_PRICE_WRONG");
2021-12-02 16:15:22 +08:00
require(_LOWER_LIMIT_PRICE_ <= _UPPER_LIMIT_PRICE_, "PRICE_WRONG");
require(_TOKEN_CLIFF_RATE_ <= 1e18, "TOKEN_CLIFF_RATE_WRONG");
require(_FUNDS_CLIFF_RATE_ <= 1e18, "FUND_CLIFF_RATE_WRONG");
require(_LP_CLIFF_RATE_ <= 1e18, "LP_CLIFF_RATE_WRONG");
2021-12-23 18:13:04 +08:00
_IS_OVERCAP_STOP = isOverCapStop;
2021-12-02 16:15:22 +08:00
_TOTAL_TOKEN_AMOUNT_ = IERC20(_TOKEN_ADDRESS_).balanceOf(address(this));
2021-12-10 09:51:02 +08:00
require(_TOTAL_TOKEN_AMOUNT_ > 0, "NO_TOKEN_TRANSFERED");
2021-12-19 12:33:17 +08:00
require(address(this).balance == _SETTEL_FUND_, "SETTLE_FUND_NOT_MATCH");
2021-12-02 16:15:22 +08:00
}
2021-11-21 19:50:04 +08:00
// ============ View Functions ============
function getCurrentPrice() public view returns (uint256) {
return getPrice(_TOTAL_RAISED_FUNDS_);
}
function getPrice(uint256 fundAmount) public view returns (uint256 price) {
2022-02-15 09:31:46 +08:00
if(_FINAL_PRICE_ != 0)
price = _FINAL_PRICE_;
else {
price = DecimalMath.divFloor(fundAmount, _TOTAL_TOKEN_AMOUNT_);
if (price < _LOWER_LIMIT_PRICE_) {
price = _LOWER_LIMIT_PRICE_;
}
if (price > _UPPER_LIMIT_PRICE_) {
price = _UPPER_LIMIT_PRICE_;
}
2021-11-21 19:50:04 +08:00
}
}
function getUserTokenAllocation(address user) public view returns (uint256) {
if (_FINAL_PRICE_ == 0) {
return 0;
} else {
return
DecimalMath.divFloor(
DecimalMath.mulFloor(_FUNDS_DEPOSITED_[user], _USED_FUND_RATIO_),
_FINAL_PRICE_
);
}
}
function getUserFundsUnused(address user) public view returns (uint256) {
return
DecimalMath.mulFloor(_FUNDS_DEPOSITED_[user], DecimalMath.ONE.sub(_USED_FUND_RATIO_));
}
function getUserFundsUsed(address user) public view returns (uint256) {
return DecimalMath.mulFloor(_FUNDS_DEPOSITED_[user], _USED_FUND_RATIO_);
}
// ============ Settle Functions ============
2021-12-27 21:40:32 +08:00
function settle() public isNotForceStop preventReentrant {
2021-11-21 19:50:04 +08:00
require(_FINAL_PRICE_ == 0 && isFundingEnd(), "CAN_NOT_SETTLE");
_FINAL_PRICE_ = getCurrentPrice();
2021-12-10 09:51:02 +08:00
if(_TOTAL_RAISED_FUNDS_ == 0) {
return;
}
2021-11-21 19:50:04 +08:00
_USED_FUND_RATIO_ = DecimalMath.divFloor(
DecimalMath.mulFloor(_TOTAL_TOKEN_AMOUNT_, _FINAL_PRICE_),
_TOTAL_RAISED_FUNDS_
);
if (_USED_FUND_RATIO_ > DecimalMath.ONE) {
_USED_FUND_RATIO_ = DecimalMath.ONE;
}
2021-12-19 12:33:17 +08:00
msg.sender.transfer(_SETTEL_FUND_);
2022-02-08 22:23:52 +08:00
emit Settle(msg.sender);
2021-11-21 19:50:04 +08:00
}
// ============ Funding Functions ============
2021-12-27 21:40:32 +08:00
function depositFunds(address to) external preventReentrant isNotForceStop returns(uint256 inputFund) {
2021-11-21 19:50:04 +08:00
require(isDepositOpen(), "DEPOSIT_NOT_OPEN");
2021-12-23 18:13:04 +08:00
uint256 currentFundBalance = IERC20(_FUNDS_ADDRESS_).balanceOf(address(this));
if(_IS_OVERCAP_STOP) {
require(currentFundBalance <= DecimalMath.mulFloor(_TOTAL_TOKEN_AMOUNT_, _UPPER_LIMIT_PRICE_), "ALREADY_OVER_CAP");
}
2021-11-21 19:50:04 +08:00
// input fund check
2021-12-23 18:13:04 +08:00
inputFund = currentFundBalance.sub(_FUNDS_RESERVE_);
2021-11-21 19:50:04 +08:00
if (_QUOTA_ != address(0)) {
require(
inputFund.add(_FUNDS_DEPOSITED_[to]) <= uint256(IQuota(_QUOTA_).getUserQuota(to)),
"QUOTA_EXCEED"
);
}
2022-02-15 09:31:46 +08:00
_FUNDS_RESERVE_ = _FUNDS_RESERVE_.add(inputFund);
2021-11-21 19:50:04 +08:00
_FUNDS_DEPOSITED_[to] = _FUNDS_DEPOSITED_[to].add(inputFund);
_TOTAL_RAISED_FUNDS_ = _TOTAL_RAISED_FUNDS_.add(inputFund);
2022-02-08 22:23:52 +08:00
emit DepositFund(to, inputFund);
2021-11-21 19:50:04 +08:00
}
function withdrawFunds(address to, uint256 amount) external preventReentrant {
2022-02-08 22:23:52 +08:00
uint256 fundAmount;
bool isSettled = isSettled();
if (!isSettled) {
2021-11-21 19:50:04 +08:00
require(_FUNDS_DEPOSITED_[msg.sender] >= amount, "WITHDRAW_TOO_MUCH");
_FUNDS_DEPOSITED_[msg.sender] = _FUNDS_DEPOSITED_[msg.sender].sub(amount);
_TOTAL_RAISED_FUNDS_ = _TOTAL_RAISED_FUNDS_.sub(amount);
2021-12-10 09:51:02 +08:00
_FUNDS_RESERVE_ = _FUNDS_RESERVE_.sub(amount);
2022-02-08 22:23:52 +08:00
fundAmount = amount;
2021-11-21 19:50:04 +08:00
IERC20(_FUNDS_ADDRESS_).safeTransfer(to, amount);
} else {
require(!_FUNDS_CLAIMED_[msg.sender], "ALREADY_CLAIMED");
_FUNDS_CLAIMED_[msg.sender] = true;
2022-02-08 22:23:52 +08:00
fundAmount = getUserFundsUnused(msg.sender);
IERC20(_FUNDS_ADDRESS_).safeTransfer(to, fundAmount);
2021-11-21 19:50:04 +08:00
}
2022-02-08 22:23:52 +08:00
emit WithdrawFund(msg.sender, to, fundAmount, isSettled);
2021-11-21 19:50:04 +08:00
}
2022-02-15 09:31:46 +08:00
function claimToken(address to) external preventReentrant {
2021-12-20 17:43:23 +08:00
require(isSettled(), "NOT_SETTLED");
uint256 totalAllocation = getUserTokenAllocation(msg.sender);
2022-02-08 22:23:52 +08:00
uint256 claimableTokenAmount = _claimToken(to, totalAllocation);
2021-12-20 17:43:23 +08:00
2022-02-08 22:23:52 +08:00
uint256 fundAmount = 0;
2021-12-23 18:13:04 +08:00
if(!_FUNDS_CLAIMED_[msg.sender]) {
_FUNDS_CLAIMED_[msg.sender] = true;
2022-02-08 22:23:52 +08:00
fundAmount = getUserFundsUnused(msg.sender);
IERC20(_FUNDS_ADDRESS_).safeTransfer(to, fundAmount);
2021-12-23 18:13:04 +08:00
}
2022-02-08 22:23:52 +08:00
emit ClaimToken(msg.sender, to, claimableTokenAmount, fundAmount);
2021-12-23 18:13:04 +08:00
}
2021-12-20 17:43:23 +08:00
// ============ Ownable Functions ============
2021-11-21 19:50:04 +08:00
function withdrawUnallocatedToken(address to) external preventReentrant onlyOwner {
2021-12-19 12:33:17 +08:00
require(isSettled(), "NOT_SETTLED");
2021-11-21 19:50:04 +08:00
require(_FINAL_PRICE_ == _LOWER_LIMIT_PRICE_, "NO_TOKEN_LEFT");
uint256 allocatedToken = DecimalMath.divCeil(_TOTAL_RAISED_FUNDS_, _FINAL_PRICE_);
2022-02-08 22:23:52 +08:00
uint256 unallocatedAmount = _TOTAL_TOKEN_AMOUNT_.sub(allocatedToken);
IERC20(_TOKEN_ADDRESS_).safeTransfer(to, unallocatedAmount);
2021-11-21 19:50:04 +08:00
_TOTAL_TOKEN_AMOUNT_ = allocatedToken;
2022-02-08 22:23:52 +08:00
emit WithdrawUnallocatedToken(to, unallocatedAmount);
2021-11-21 19:50:04 +08:00
}
2021-12-10 09:51:02 +08:00
function initializeLiquidity(uint256 initialTokenAmount, uint256 lpFeeRate, bool isOpenTWAP) external preventReentrant onlyOwner {
require(isSettled(), "NOT_SETTLED");
2021-12-19 12:33:17 +08:00
uint256 totalUsedRaiseFunds = DecimalMath.mulFloor(_TOTAL_RAISED_FUNDS_, _USED_FUND_RATIO_);
_initializeLiquidity(initialTokenAmount, totalUsedRaiseFunds, lpFeeRate, isOpenTWAP);
2022-02-08 22:23:52 +08:00
emit InitializeLiquidity(_INITIAL_POOL_, initialTokenAmount);
2021-12-10 09:51:02 +08:00
}
2021-12-19 12:33:17 +08:00
function claimFund(address to) external preventReentrant onlyOwner {
require(isSettled(), "NOT_SETTLED");
uint256 totalUsedRaiseFunds = DecimalMath.mulFloor(_TOTAL_RAISED_FUNDS_, _USED_FUND_RATIO_);
2022-02-08 22:23:52 +08:00
uint256 claimableFund = _claimFunds(to,totalUsedRaiseFunds);
emit ClaimFund(to, claimableFund);
2021-12-19 12:33:17 +08:00
}
2021-11-21 19:50:04 +08:00
// ============ Timeline Control Functions ============
2021-11-29 14:51:43 +08:00
function isDepositOpen() public view returns (bool) {
2021-11-21 19:50:04 +08:00
return
block.timestamp >= _START_TIME_ &&
block.timestamp < _START_TIME_.add(_BIDDING_DURATION_);
}
2021-11-29 14:51:43 +08:00
function isFundingEnd() public view returns (bool) {
2021-11-21 19:50:04 +08:00
return block.timestamp > _START_TIME_.add(_BIDDING_DURATION_).add(_COOLING_DURATION_);
}
2021-11-29 14:51:43 +08:00
function isSettled() public view returns (bool) {
2021-11-21 19:50:04 +08:00
return _FINAL_PRICE_ != 0;
}
2022-02-07 17:16:13 +08:00
// ============ Version Control ============
2022-02-15 09:31:46 +08:00
function version() virtual public pure returns (string memory) {
2022-02-07 17:16:13 +08:00
return "FairFunding 1.0.0";
}
2022-02-15 09:31:46 +08:00
// ============ View Helper ==============
function getCurrentFundingInfo(address user) external view returns(
uint256 raiseFundAmount,
uint256 userFundAmount,
uint256 currentPrice,
uint256 soldTokenAmount,
uint256 claimableTokenAmount,
bool isHaveCap,
uint256 userQuota,
uint256 userCurrentQuota
) {
raiseFundAmount =_TOTAL_RAISED_FUNDS_;
userFundAmount = _FUNDS_DEPOSITED_[user];
currentPrice = getCurrentPrice();
uint256 tmpSoldTokenAmount = DecimalMath.divCeil(_TOTAL_RAISED_FUNDS_, currentPrice);
soldTokenAmount = tmpSoldTokenAmount > _TOTAL_TOKEN_AMOUNT_ ? _TOTAL_TOKEN_AMOUNT_ : tmpSoldTokenAmount;
if(block.timestamp > _TOKEN_VESTING_START_) {
uint256 totalAllocation = getUserTokenAllocation(user);
uint256 remainingToken = DecimalMath.mulFloor(
getRemainingRatio(block.timestamp,0),
totalAllocation
);
claimableTokenAmount = totalAllocation.sub(remainingToken).sub(_CLAIMED_TOKEN_[user]);
}else {
claimableTokenAmount = 0;
}
if(_QUOTA_ == address(0)) {
isHaveCap = false;
userQuota = uint256(-1);
userCurrentQuota = uint256(-1);
} else {
isHaveCap = true;
userQuota = uint256(IQuota(_QUOTA_).getUserQuota(user));
if(userQuota > userFundAmount) {
userCurrentQuota = userQuota - userFundAmount;
} else {
userCurrentQuota = 0;
}
}
}
function getBaseFundInfo() external view returns(
address tokenAddress,
address fundAddress,
uint256 totalTokenAmount,
uint256 price0, //_LOWER_LIMIT_PRICE_
uint256 price1, //_UPPER_LIMIT_PRICE_
string memory versionType,
uint256 startTime,
uint256 bidDuration,
uint256 tokenVestingStart,
uint256 tokenVestingDuration
) {
tokenAddress = _TOKEN_ADDRESS_;
fundAddress = _FUNDS_ADDRESS_;
totalTokenAmount = _TOTAL_TOKEN_AMOUNT_;
price0 = _LOWER_LIMIT_PRICE_;
price1 = _UPPER_LIMIT_PRICE_;
versionType = version();
startTime = _START_TIME_;
bidDuration = _BIDDING_DURATION_;
tokenVestingStart = _TOKEN_VESTING_START_;
tokenVestingDuration = _TOKEN_VESTING_DURATION_;
}
function getFairFundInfo(address user) external view returns(
bool isOverCapStop,
uint256 finalPrice,
uint256 userUnusedFundAmount,
uint256 coolDuration
) {
isOverCapStop = _IS_OVERCAP_STOP;
finalPrice = _FINAL_PRICE_;
userUnusedFundAmount = getUserFundsUnused(user);
coolDuration = _COOLING_DURATION_;
}
}