-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMOTTournament.sol
More file actions
214 lines (173 loc) · 8.32 KB
/
MOTTournament.sol
File metadata and controls
214 lines (173 loc) · 8.32 KB
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MOTTournament is ReentrancyGuard, Ownable {
struct Season {
bool bettingOpen;
bool resolved;
uint256 championId;
uint256 runnerUpId;
uint256 totalPool;
}
struct Prediction {
uint256 predictedFirst;
uint256 predictedSecond;
uint256 amount;
}
// State
mapping(uint256 => Season) public seasons;
mapping(uint256 => mapping(address => Prediction[])) public predictions; // seasonId => bettor => predictions
mapping(uint256 => address[]) public seasonBettors; // seasonId => list of bettors
mapping(uint256 => mapping(address => bool)) public hasBet; // seasonId => bettor => has placed at least one bet
mapping(uint256 => mapping(address => bool)) public claimed; // seasonId => bettor => claimed
uint256 public constant PLATFORM_FEE_PERCENT = 5;
uint256 public constant EXACT_POOL_PERCENT = 70; // 70% to exact predictors
uint256 public constant PARTIAL_POOL_PERCENT = 30; // 30% to partial predictors
address public feeRecipient;
// Events
event SeasonOpened(uint256 indexed seasonId);
event PredictionPlaced(uint256 indexed seasonId, address indexed bettor, uint256 predictedFirst, uint256 predictedSecond, uint256 amount);
event BettingClosed(uint256 indexed seasonId);
event SeasonResolved(uint256 indexed seasonId, uint256 championId, uint256 runnerUpId);
event WinningsClaimed(uint256 indexed seasonId, address indexed winner, uint256 amount);
constructor() Ownable(msg.sender) {
feeRecipient = msg.sender;
}
function openSeason(uint256 seasonId) external onlyOwner {
require(!seasons[seasonId].bettingOpen && !seasons[seasonId].resolved, "Season already exists");
seasons[seasonId].bettingOpen = true;
emit SeasonOpened(seasonId);
}
function placePrediction(uint256 seasonId, uint256 predictedFirst, uint256 predictedSecond) external payable {
require(seasons[seasonId].bettingOpen, "Betting not open");
require(msg.value > 0, "Bet must be > 0");
require(predictedFirst != predictedSecond, "Must predict different agents");
predictions[seasonId][msg.sender].push(Prediction({
predictedFirst: predictedFirst,
predictedSecond: predictedSecond,
amount: msg.value
}));
if (!hasBet[seasonId][msg.sender]) {
seasonBettors[seasonId].push(msg.sender);
hasBet[seasonId][msg.sender] = true;
}
seasons[seasonId].totalPool += msg.value;
emit PredictionPlaced(seasonId, msg.sender, predictedFirst, predictedSecond, msg.value);
}
function closeBetting(uint256 seasonId) external onlyOwner {
require(seasons[seasonId].bettingOpen, "Betting not open");
seasons[seasonId].bettingOpen = false;
emit BettingClosed(seasonId);
}
function resolveSeason(uint256 seasonId, uint256 championId, uint256 runnerUpId) external onlyOwner {
require(!seasons[seasonId].bettingOpen, "Close betting first");
require(!seasons[seasonId].resolved, "Already resolved");
seasons[seasonId].resolved = true;
seasons[seasonId].championId = championId;
seasons[seasonId].runnerUpId = runnerUpId;
// Transfer platform fee
uint256 platformFee = (seasons[seasonId].totalPool * PLATFORM_FEE_PERCENT) / 100;
if (platformFee > 0) {
(bool feeSuccess, ) = payable(feeRecipient).call{value: platformFee}("");
require(feeSuccess, "Fee transfer failed");
}
emit SeasonResolved(seasonId, championId, runnerUpId);
}
function claimWinnings(uint256 seasonId) external nonReentrant {
Season storage season = seasons[seasonId];
require(season.resolved, "Season not resolved");
require(!claimed[seasonId][msg.sender], "Already claimed");
Prediction[] storage userPreds = predictions[seasonId][msg.sender];
require(userPreds.length > 0, "No predictions");
// Calculate user's exact and partial winning amounts
uint256 exactAmount;
uint256 partialAmount;
for (uint256 i = 0; i < userPreds.length; i++) {
bool firstCorrect = userPreds[i].predictedFirst == season.championId;
bool secondCorrect = userPreds[i].predictedSecond == season.runnerUpId;
if (firstCorrect && secondCorrect) {
exactAmount += userPreds[i].amount;
} else if (firstCorrect || secondCorrect) {
partialAmount += userPreds[i].amount;
}
}
require(exactAmount > 0 || partialAmount > 0, "No winning predictions");
// Calculate pool totals
(uint256 totalExactPool, uint256 totalPartialPool) = _calculatePools(seasonId, season.championId, season.runnerUpId);
uint256 payoutPool = season.totalPool - (season.totalPool * PLATFORM_FEE_PERCENT) / 100;
uint256 exactPayoutPool = (payoutPool * EXACT_POOL_PERCENT) / 100;
uint256 partialPayoutPool = (payoutPool * PARTIAL_POOL_PERCENT) / 100;
// If one pool has no claimants, redirect to the other
if (totalExactPool == 0 && totalPartialPool > 0) {
partialPayoutPool = payoutPool;
exactPayoutPool = 0;
} else if (totalPartialPool == 0 && totalExactPool > 0) {
exactPayoutPool = payoutPool;
partialPayoutPool = 0;
}
uint256 winnings;
if (exactAmount > 0 && totalExactPool > 0) {
winnings += (exactPayoutPool * exactAmount) / totalExactPool;
}
if (partialAmount > 0 && totalPartialPool > 0) {
winnings += (partialPayoutPool * partialAmount) / totalPartialPool;
}
require(winnings > 0, "No winnings");
claimed[seasonId][msg.sender] = true;
(bool success, ) = payable(msg.sender).call{value: winnings}("");
require(success, "Transfer failed");
emit WinningsClaimed(seasonId, msg.sender, winnings);
}
// View functions
function getSeasonInfo(uint256 seasonId) external view returns (
bool bettingOpen,
bool resolved,
uint256 championId,
uint256 runnerUpId,
uint256 totalPool
) {
Season storage s = seasons[seasonId];
return (s.bettingOpen, s.resolved, s.championId, s.runnerUpId, s.totalPool);
}
function getUserPredictions(uint256 seasonId, address user) external view returns (
uint256[] memory predFirsts,
uint256[] memory predSeconds,
uint256[] memory predAmounts
) {
Prediction[] storage preds = predictions[seasonId][user];
predFirsts = new uint256[](preds.length);
predSeconds = new uint256[](preds.length);
predAmounts = new uint256[](preds.length);
for (uint256 i = 0; i < preds.length; i++) {
predFirsts[i] = preds[i].predictedFirst;
predSeconds[i] = preds[i].predictedSecond;
predAmounts[i] = preds[i].amount;
}
}
function getSeasonBettorCount(uint256 seasonId) external view returns (uint256) {
return seasonBettors[seasonId].length;
}
function withdrawFees() external onlyOwner {
uint256 balance = address(this).balance;
require(balance > 0, "No fees to withdraw");
(bool success, ) = payable(feeRecipient).call{value: balance}("");
require(success, "Transfer failed");
}
// Internal
function _calculatePools(uint256 seasonId, uint256 championId, uint256 runnerUpId) internal view returns (uint256 exactPool, uint256 partialPool) {
address[] storage bettors = seasonBettors[seasonId];
for (uint256 i = 0; i < bettors.length; i++) {
Prediction[] storage preds = predictions[seasonId][bettors[i]];
for (uint256 j = 0; j < preds.length; j++) {
bool firstCorrect = preds[j].predictedFirst == championId;
bool secondCorrect = preds[j].predictedSecond == runnerUpId;
if (firstCorrect && secondCorrect) {
exactPool += preds[j].amount;
} else if (firstCorrect || secondCorrect) {
partialPool += preds[j].amount;
}
}
}
}
}