-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBet.sol
221 lines (188 loc) · 8.94 KB
/
Bet.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
contract Bet is Ownable, AccessControl {
enum Position {
None,
Home,
Away,
Draw,
Refund
}
struct Round {
Position positionWin;
uint32 timeCreatePrediction;
uint32 timeLockPrediction;
uint32 timeEndPrediction;
uint48 homeId;
uint48 awayId;
uint256 roundId;
uint256 amountHome;
uint256 amountAway;
uint256 amountDraw;
uint256 totalAmount;
}
struct Prediction {
bool isClaimed;
Position positionPredict;
uint roundId;
uint256 amount;
}
using SafeMath for uint256;
using SafeCast for uint256;
using SafeERC20 for IERC20;
IERC20 token;
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
mapping(uint => Round) public round;
mapping(uint => mapping(address => Prediction)) public userPrediction;
mapping(address => uint256[]) public userRound;
uint roundId = 0;
event Claim(address _address, uint round, uint256 reward);
event Time(uint256 timeLock, uint256 timeNow, uint256 timeEnd);
modifier minmumPrediction(uint256 amount) {
require(amount > 1000000000000000000, "Amount minmum 1 BUSD");
_;
}
modifier roundLock(uint256 _roundId) {
require(round[_roundId].timeLockPrediction > block.timestamp , "Round is Lock");
_;
}
modifier checkRound(uint256 _roundId) {
require(round[_roundId].timeCreatePrediction > 0, "Not have Round");
_;
}
modifier checkRoundResult(uint256 _roundId) {
require(round[_roundId].positionWin == Position.None, "positionWin != Position.None");
_;
}
modifier checkHasPrediction(uint256 _roundId) {
require(userPrediction[_roundId][msg.sender].positionPredict == Position.None, "You already Prediction");
_;
}
constructor(IERC20 _token) {
token = _token;
_grantRole(DEFAULT_ADMIN_ROLE , msg.sender);
}
function createRound(uint48 homeId, uint48 awayId, uint32 timeLockPrediction, uint32 timeEndPrediction) public onlyRole(ADMIN_ROLE) {
require(homeId != awayId, "Can not Add id same");
require(timeEndPrediction > block.timestamp, "Time Over");
roundId++;
uint32 timeEnd = (timeEndPrediction + (8 * 60));
Round storage roundCurrent = round[roundId];
roundCurrent.roundId = roundId;
roundCurrent.homeId = homeId;
roundCurrent.awayId = awayId;
roundCurrent.positionWin = Position.None;
roundCurrent.timeLockPrediction = timeLockPrediction;
roundCurrent.timeCreatePrediction = block.timestamp.toUint32();
roundCurrent.timeEndPrediction = timeEnd;
emit Time(timeLockPrediction, block.timestamp, timeEnd);
}
function endRoundLatest(Position position) public onlyRole(ADMIN_ROLE) checkRound(roundId) checkRoundResult(roundId) {
round[roundId].positionWin = position;
}
function endRoundByRoundId(uint256 _roundId, Position position) external onlyRole(ADMIN_ROLE) checkRound(_roundId) checkRoundResult(_roundId) {
round[_roundId].positionWin = position;
}
function predictionHome(uint256 _roundId, uint256 amount) public minmumPrediction(amount) checkHasPrediction(_roundId) roundLock(_roundId) {
Round storage roundCurrent = round[_roundId];
roundCurrent.amountHome = roundCurrent.amountHome.add(amount);
roundCurrent.totalAmount = roundCurrent.totalAmount.add(amount);
Prediction storage prediction = userPrediction[_roundId][msg.sender];
prediction.positionPredict = Position.Home;
prediction.roundId = _roundId;
prediction.amount = prediction.amount.add(amount);
prediction.isClaimed = false;
userRound[msg.sender].push(_roundId);
// send amount
token.safeTransferFrom(msg.sender, address(this), amount);
}
function predictionAway(uint256 _roundId, uint256 amount) public minmumPrediction(amount) checkHasPrediction(_roundId) roundLock(_roundId) {
Round storage roundCurrent = round[_roundId];
roundCurrent.amountAway = roundCurrent.amountAway.add(amount);
roundCurrent.totalAmount = roundCurrent.totalAmount.add(amount);
Prediction storage prediction = userPrediction[_roundId][msg.sender];
prediction.positionPredict = Position.Away;
prediction.roundId = _roundId;
prediction.amount = SafeMath.add(prediction.amount, amount);
prediction.isClaimed = false;
userRound[msg.sender].push(_roundId);
// send amount
token.safeTransferFrom(msg.sender, address(this), amount);
}
function predictionDraw(uint256 _roundId, uint256 amount) public minmumPrediction(amount) checkHasPrediction(_roundId) roundLock(_roundId) {
Round storage roundCurrent = round[_roundId];
roundCurrent.amountDraw = roundCurrent.amountDraw.add(amount);
roundCurrent.totalAmount = roundCurrent.totalAmount.add(amount);
Prediction storage prediction = userPrediction[_roundId][msg.sender];
prediction.positionPredict = Position.Draw;
prediction.roundId = _roundId;
prediction.amount = SafeMath.add(prediction.amount, amount);
prediction.isClaimed = false;
userRound[msg.sender].push(_roundId);
// send amount
token.safeTransferFrom(msg.sender, address(this), amount);
}
function claimReward(uint _roundId, address _address) public checkRound(_roundId) {
require(round[_roundId].timeEndPrediction < block.timestamp, "Match is not end");
require(round[_roundId].positionWin != Position.None, "Waiting Result");
require(userPrediction[_roundId][_address].amount > 0, "You not prediction");
require(userPrediction[_roundId][_address].positionPredict == round[roundId].positionWin || (userPrediction[_roundId][_address].positionPredict != Position.None && round[_roundId].positionWin == Position.Refund), "You Lose");
require(!userPrediction[_roundId][_address].isClaimed ,"You already claim");
if(round[_roundId].positionWin == Position.Refund) {
uint256 reward = userPrediction[_roundId][_address].amount;
token.safeTransfer(_address, reward);
userPrediction[_roundId][_address].isClaimed = true;
} else if(userPrediction[_roundId][_address].positionPredict == round[_roundId].positionWin) {
// Home Win
if(round[_roundId].positionWin == Position.Home) {
uint totalAmount = round[_roundId].totalAmount;
uint amountHome = round[_roundId].amountHome;
uint userAmount = userPrediction[_roundId][_address].amount;
uint256 reward = (userAmount * totalAmount) / amountHome;
// claim
token.safeTransfer(_address, reward);
userPrediction[_roundId][_address].isClaimed = true;
emit Claim(_address, _roundId, reward);
}
// Away Win
else if(round[_roundId].positionWin == Position.Away) {
uint totalAmount = round[_roundId].totalAmount;
uint amountAway = round[_roundId].amountAway;
uint userAmount = userPrediction[_roundId][_address].amount;
uint256 reward = (userAmount * totalAmount) / amountAway;
// claim
token.safeTransfer(_address, reward);
userPrediction[_roundId][_address].isClaimed = true;
emit Claim(_address, _roundId, reward);
}
// Draw Win
else if(round[_roundId].positionWin == Position.Draw) {
uint totalAmount = round[_roundId].totalAmount;
uint amountDraw = round[_roundId].amountDraw;
uint userAmount = userPrediction[_roundId][_address].amount;
uint256 reward = (userAmount * totalAmount) / amountDraw;
// claim
token.safeTransfer(_address, reward);
userPrediction[_roundId][_address].isClaimed = true;
emit Claim(_address, _roundId, reward);
}
}
}
function getRoundOnRun() public view returns (Round [] memory) {
Round[] memory roundIdOnRun = new Round[](roundId);
for(uint i = 0; i < roundId; i++) {
roundIdOnRun[i] = round[i + 1] ;
}
return roundIdOnRun;
}
function getUserRound(address _address) public view returns(uint256[] memory) {
return userRound[_address];
}
}