赞
踩
/**
* @notice 看跌
* @param epoch: epoch 轮数
* @param amount: 代币竞猜数量 wei
*/
function betBear(uint256 epoch, uint256 amount) external whenNotPaused nonReentrant notContract {
require(epoch == currentEpoch, "Bet is too early/late");
require(_bettable(epoch), "Round not bettable");
require(amount >= minBetAmount, "Bet amount must be greater than minBetAmount");
require(ledger[epoch][msg.sender].amount == 0, "Can only bet once per round");
//对面无竞猜 不可下注
if(roundsNumber[epoch][Position.Bear] > 0 && (roundsNumber[epoch][Position.Bull] == 0)){
return;
}
//代币转入合约中
tokens.safeTransferFrom(msg.sender, address(this), amount);
//更新竞猜对象内容
Round storage round = rounds[epoch];
round.totalAmount = round.totalAmount + amount;
round.bearAmount = round.bearAmount + amount;
// 更新用户数据
BetInfo storage betInfo = ledger[epoch][msg.sender];
betInfo.position = Position.Bear;
betInfo.amount = amount;
userRounds[msg.sender].push(epoch);
player[msg.sender] = true;
roundsNumber[epoch][Position.Bear] += 1;
//记录日志
emit BetBear(msg.sender, epoch, amount);
}
/**
* @notice 看涨
* @param epoch: 轮数
* @param amount: 代币竞猜数量 wei
*/
function betBull(uint256 epoch, uint256 amount) external payable whenNotPaused nonReentrant notContract {
require(epoch == currentEpoch, "Bet is too early/late");
require(_bettable(epoch), "Round not bettable");
require(amount >= minBetAmount, "Bet amount must be greater than minBetAmount");
require(ledger[epoch][msg.sender].amount == 0, "Can only bet once per round");
if(roundsNumber[epoch][Position.Bull] > 0 && (roundsNumber[epoch][Position.Bear] == 0)){
return;
}
tokens.safeTransferFrom(msg.sender, address(this), amount);
Round storage round = rounds[epoch];
round.totalAmount = round.totalAmount + amount;
round.bullAmount = round.bullAmount + amount;
// Update user data
BetInfo storage betInfo = ledger[epoch][msg.sender];
betInfo.position = Position.Bull;
betInfo.amount = amount;
userRounds[msg.sender].push(epoch);
player[msg.sender] = true;
roundsNumber[epoch][Position.Bull] += 1;
emit BetBull(msg.sender, epoch, amount);
}
/**
* @notice 开始下一轮n,锁定第n-1轮的价格,结束第n-2轮
* @dev 只有管理员账户可以操作
*/
function executeRound() external whenNotPaused onlyOperator {
require(
genesisStartOnce && genesisLockOnce,
"Can only run after genesisStartRound and genesisLockRound is triggered"
);
(uint80 currentRoundId, int256 currentPrice) = _getPriceFromOracle();
oracleLatestRoundId = uint256(currentRoundId);
// CurrentEpoch refers to previous round (n-1)
_safeLockRound(currentEpoch, currentRoundId, currentPrice);
_safeEndRound(currentEpoch - 1, currentRoundId, currentPrice);
_calculateRewards(currentEpoch - 1);
// Increment currentEpoch to current round (n)
currentEpoch = currentEpoch + 1;
_safeStartRound(currentEpoch);
}
/**
* @notice 开始回合
* @dev Callable by admin or operator
*/
function genesisStartRound() external whenNotPaused onlyOperator {
require(!genesisStartOnce, "Can only run genesisStartRound once");
currentEpoch = currentEpoch + 1;
_startRound(currentEpoch);
genesisStartOnce = true;
}
/**
* @notice 领取全部奖励
* @param epochs: 有奖励轮数数组
*/
function claim(uint256[] calldata epochs) external nonReentrant notContract {
uint256 reward; // Initializes reward
uint256 rcm1Amt;
uint256 rcm2Amt;
uint256 rcm3Amt;
uint256 marketAmt;
for (uint256 i = 0; i < epochs.length; i++) {
require(rounds[epochs[i]].times.startTimestamp != 0, "Round has not started");
require(block.timestamp > rounds[epochs[i]].times.closeTimestamp, "Round has not ended");
uint256 addedReward = 0;
// 竞猜有效
if (rounds[epochs[i]].oracleCalled) {
// 猜对
if(claimable(epochs[i], msg.sender)){
Round memory round = rounds[epochs[i]];
//轮空
if(round.isBet){
require(refundable(epochs[i], msg.sender), "Not eligible for refund");
addedReward = ledger[epochs[i]][msg.sender].amount;
recommendOtherAmt[msg.sender] = ledger[epochs[i]][msg.sender].amount * failureFee / 10000;
}else{
addedReward = (ledger[epochs[i]][msg.sender].amount * round.rewardAmount) / round.rewardBaseCalAmount;
marketAmt = addedReward / (10000 - totalFee) * marketFee / 10000000;
rcm1Amt = addedReward / (10000 - totalFee) * recommend1Fee / 10000000;
rcm2Amt = addedReward / (10000 - totalFee) * recommend2Fee / 10000000;
rcm3Amt = addedReward / (10000 - totalFee) * recommend3Fee / 10000000;
distributionProfit(rcm1Amt, rcm2Amt, rcm3Amt, marketAmt, msg.sender);
}
//猜错
}else if(isLotteryFailure(epochs[i], msg.sender)){
recommendOtherAmt[msg.sender] = ledger[epochs[i]][msg.sender].amount * failureFee / 10000;
}
}
// 竞猜无效 退还金额
else {
require(refundable(epochs[i], msg.sender), "Not eligible for refund");
addedReward = ledger[epochs[i]][msg.sender].amount;
}
ledger[epochs[i]][msg.sender].claimed = true;
reward += addedReward;
emit Claim(msg.sender, epochs[i], addedReward);
}
//发放收益
if (reward > 0) {
_safeTransfeTokens(tokens, address(msg.sender), reward);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。