-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictionPool.sol
More file actions
573 lines (490 loc) · 21.3 KB
/
PredictionPool.sol
File metadata and controls
573 lines (490 loc) · 21.3 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/**
* @title PredictionPool - Chainlink-Based Betting Game
* @author Siegfried Bozza
* @notice A decentralized betting game where players predict if a Chainlink price feed
* will be above/below a target at round end. Winners split the pot proportionally
* to their bet weight (amount + time factor).
* @dev Features:
* - Chainlink Automation for round resolution.
* - Time-weighted bet rewards (earlier bets = higher weight).
* - Reentrancy protection for `claimReward`.
* - Admin-controlled allowed price feeds and round durations.
*/
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import {AggregatorV3Interface} from "@chainlink/interfaces/AggregatorV3Interface.sol";
import {AutomationCompatibleInterface} from "@chainlink/interfaces/AutomationCompatibleInterface.sol";
error PredictionPool_CanBetOnlyOncePerRound();
error PredictionPool_RoundIsNotActive();
error PredictionPool_InvalidValue();
error PredictionPool_InvalidFeed();
error PredictionPool_InvalidDuration();
error PredictionPool_AlreadyResolved();
error PredictionPool_TooEarlyTooResolve();
error PredictionPool_InvalidChainlinkDecimals(uint8 decimals);
error PredictionPool_NegativeChainlinkPrice();
error PredictionPool_RoundNotResolved();
error PredictionPool_AlreadyClaimed();
error PredictionPool_NotAWinner();
error PredictionPool_RewardTransferFailed();
error PredictionPool_ZeroAddressNotAllowed();
contract PredictionPool is Ownable, AutomationCompatibleInterface, ReentrancyGuard {
/**
* events
*/
event PredictionPool_RoundCreated(
uint256 indexed roundId, address indexed player, uint256 indexed value, BetSide betSide, uint256 end
);
event PredictionPool_NewBet(
uint256 indexed roundId, address indexed player, uint256 indexed value, BetSide betSide, uint256 playTime
);
event PredictionPool_NewRoundStatus(uint256 indexed roundId, RoundStatus indexed status);
event PredictionPool_RoundResolved(uint256 indexed roundId, address indexed creator, bool indexed creatorIsWinner);
event PredictionPool_PriceFeedToggled(address indexed feed, bool indexed enabled);
event PredictionPool_RewardClaimed(uint256 indexed roundId, address indexed player, uint256 indexed value);
event PredictionPool_NewMinRoundDuration(uint256 indexed duration);
/**
* constants
*/
uint256 public constant STRICT_MIN_ROUND_DURATION = 2 minutes;
/**
* storage
*/
uint256 public minRoundDuration;
uint256 public nextRoundId;
mapping(uint256 => Round) public rounds;
mapping(uint256 => mapping(address => Bet)) public roundToPlayerBet;
mapping(uint256 => address[]) roundToGtePlayers;
mapping(uint256 => address[]) roundToLtPlayers;
mapping(uint256 => mapping(address => bool)) public isRoundWinner;
mapping(address => bool) public allowedDataFeeds; // allowed chainlink pairs priceFeeds
address[] public allowedDataFeedsList;
/**
* types
*/
struct Round {
uint256 id;
address creator;
RoundStatus status;
uint256 gteTotal; // total ETH bet by gtePlayers
uint256 ltTotal; // total ETH bet by ltPlayers
address priceFeed; // address of the chainlink pair priceFeed for this round
uint256 target; // target price
uint256 start; // timestamp when round starts
uint256 end; // timestamp when round ends
}
struct Bet {
uint256 amount; // eth amount placed for this bet
uint256 time; // timestamp when user placed its bet on roundId
bool claimed;
}
enum BetSide {
Lt, // 0
Gte // 1
}
enum RoundStatus {
NotStarted,
Active,
Ended,
Resolved
}
/**
* modifiers
*/
/// @notice Ensures a round is in the `Active` state before allowing interaction.
/// @dev Mutates state: Transitions round from `Active` to `Ended` if its end time has passed.
/// @param _roundId The ID of the round to check.
modifier roundIsActive(uint256 _roundId) {
_roundIsActive(_roundId);
_;
}
function _roundIsActive(uint256 _roundId) internal {
Round storage round = rounds[_roundId];
if (round.status == RoundStatus.Active && block.timestamp >= round.end) {
round.status = RoundStatus.Ended;
emit PredictionPool_NewRoundStatus(_roundId, round.status);
}
if (round.status != RoundStatus.Active) {
revert PredictionPool_RoundIsNotActive();
}
}
/// @notice Ensures a player can only place one bet per round.
/// @dev Reverts if the player has already bet.
/// @param _roundId The ID of the round to check.
modifier betOnlyOncePerRound(uint256 _roundId) {
_betOnlyOncePerRound(_roundId);
_;
}
function _betOnlyOncePerRound(uint256 _roundId) internal view {
if (roundToPlayerBet[_roundId][msg.sender].time != 0) {
revert PredictionPool_CanBetOnlyOncePerRound();
}
}
/// @notice Validates that a price feed is allowed for round creation.
/// @dev Reverts if the feed is not in `allowedDataFeeds`.
/// @param _feed The address of the Chainlink price feed to validate.
modifier isValidFeed(address _feed) {
_isValidFeed(_feed);
_;
}
function _isValidFeed(address _feed) internal view {
if (!allowedDataFeeds[_feed]) {
revert PredictionPool_InvalidFeed();
}
}
/// @notice Ensures `msg.value` is non-zero for payable functions.
/// @dev Reverts if `msg.value == 0`.
modifier isValidValue() {
_isValidValue();
_;
}
function _isValidValue() internal view {
if (msg.value == 0) {
revert PredictionPool_InvalidValue();
}
}
/// @notice Validates that a round duration meets the minimum requirement.
/// @dev Reverts if `_duration < minRoundDuration`.
/// @param _duration The proposed duration for the round.
modifier isValidDuration(uint256 _duration) {
_isValidDuration(_duration);
_;
}
function _isValidDuration(uint256 _duration) internal view {
if (_duration < minRoundDuration) {
revert PredictionPool_InvalidDuration();
}
}
/**
* constructor
*/
/// @notice Initializes the contract with allowed price feeds and minimum round duration.
/// @dev Reverts if:
/// - Any feed in `_allowedDataFeeds` is the zero address.
/// - `_minRoundDuration < STRICT_MIN_ROUND_DURATION`.
/// @param _allowedDataFeeds Array of allowed Chainlink price feed addresses.
/// @param _minRoundDuration Minimum duration for rounds (in seconds).
constructor(address[] memory _allowedDataFeeds, uint256 _minRoundDuration) Ownable() {
if (_minRoundDuration < STRICT_MIN_ROUND_DURATION) {
revert PredictionPool_InvalidDuration();
}
for (uint256 i = 0; i < _allowedDataFeeds.length; i++) {
address _dataFeed = _allowedDataFeeds[i];
if (_dataFeed == address(0)) {
revert PredictionPool_ZeroAddressNotAllowed();
}
allowedDataFeeds[_dataFeed] = true;
allowedDataFeedsList.push(_dataFeed);
}
minRoundDuration = _minRoundDuration;
}
/**
* functions
*/
/// @notice Checks if a round is ready for resolution by Chainlink Automation.
/// @dev Returns `true` if:
/// - Round status is `Active` or `Ended`.
/// - AND Current block timestamp >= round end time.
/// @param _roundId The ID of the round to check.
/// @return bool True if the round is ready for upkeep.
function roundIsReadyForUpkeep(uint256 _roundId) public view returns (bool) {
Round storage round = rounds[_roundId];
return (round.status == RoundStatus.Active || round.status == RoundStatus.Ended) && block.timestamp >= round.end;
}
/// @notice Called by Chainlink Automation to check if any rounds need resolution.
/// @dev Returns:
/// - `upkeepNeeded`: True if any rounds are ready for resolution.
/// - `performData`: Encoded array of round IDs needing upkeep (empty if none).
/// param checkData Unused (reserved for future extensions).
/// @return upkeepNeeded Boolean indicating if upkeep is needed.
/// @return performData Encoded round IDs for `performUpkeep`.
function checkUpkeep(
bytes calldata /* checkData */
)
external
view
override
returns (bool upkeepNeeded, bytes memory performData)
{
// Create a temporary array to store candidate round IDs that might need upkeep.
// Size is 'nextRoundId' because rounds are identified from 0 up to nextRoundId - 1.
uint256[] memory rawRoundIdsToUpkeep = new uint256[](nextRoundId);
// Counter to keep track of how many rounds actually need upkeep.
uint256 count = 0;
// Iterate over all existing rounds.
for (uint256 i = 0; i < nextRoundId; ++i) {
// Check if round with id 'i' is ready to be resolved
if (roundIsReadyForUpkeep(i)) {
rawRoundIdsToUpkeep[count] = i;
count++;
}
}
// If no rounds need upkeep, return false and empty performData.
if (count == 0) {
upkeepNeeded = false;
performData = "";
} else {
// If some rounds need upkeep, create a trimmed array containing only those rounds.
uint256[] memory roundIdsToUpkeep = new uint256[](count);
for (uint256 j = 0; j < count; ++j) {
roundIdsToUpkeep[j] = rawRoundIdsToUpkeep[j];
}
// Mark upkeep as needed & encode the round IDs for performUpkeep.
upkeepNeeded = true;
performData = abi.encode(roundIdsToUpkeep);
}
}
/// @notice Resolves rounds marked as ready by `checkUpkeep`.
/// @dev Decodes `performData` (from `checkUpkeep`) and calls `_resolveRound` for each valid round ID.
/// @param performData Encoded array of round IDs to resolve.
function performUpkeep(bytes calldata performData) external override {
// Decode the array of round IDs from params
uint256[] memory roundIdsFromData = abi.decode(performData, (uint256[]));
// Check - Iterate through each round ID & check if actually requires upkeep.
for (uint256 i = 0; i < roundIdsFromData.length; ++i) {
uint256 roundId = roundIdsFromData[i];
if (roundIsReadyForUpkeep(roundId)) {
// Resolve the round
_resolveRound(roundId);
}
}
}
/// @notice Creates a new betting round and places the creator's initial bet.
/// @dev Emits:
/// - `PredictionPool_RoundCreated` (round details).
/// - `PredictionPool_NewBet` (creator's bet).
/// @param _feed Address of the Chainlink price feed for this round.
/// @param _target Target price for the round.
/// @param _betSide Creator's bet side (`Lt` or `Gte`).
/// @param _duration Duration of the round (in seconds).
function createRound(address _feed, uint256 _target, BetSide _betSide, uint256 _duration)
public
payable
isValidValue
isValidFeed(_feed)
isValidDuration(_duration)
{
// create new round
uint256 end = block.timestamp + _duration;
Round storage round = rounds[nextRoundId];
round.id = nextRoundId;
round.creator = msg.sender;
round.status = RoundStatus.Active;
round.priceFeed = _feed;
round.target = _target;
round.start = block.timestamp;
round.end = end;
if (_betSide == BetSide.Gte) {
round.gteTotal = msg.value;
roundToGtePlayers[nextRoundId].push(msg.sender);
} else {
round.ltTotal = msg.value;
roundToLtPlayers[nextRoundId].push(msg.sender);
}
// attach msg.sender to a new bet for this round
Bet storage newBet = roundToPlayerBet[nextRoundId][msg.sender];
newBet.amount = msg.value;
newBet.time = block.timestamp;
emit PredictionPool_RoundCreated(nextRoundId, msg.sender, msg.value, _betSide, end);
emit PredictionPool_NewBet(nextRoundId, msg.sender, msg.value, _betSide, block.timestamp);
nextRoundId++;
}
/// @notice Allows a player to bet on an active round.
/// @dev Reverts if:
/// - `msg.value == 0` (`isValidValue` modifier).
/// - Round is not active (`roundIsActive` modifier).
/// - Player has already bet (`betOnlyOncePerRound` modifier).
/// @param _roundId ID of the round to bet on.
/// @param _betSide Player's bet side (`Lt` or `Gte`).
function betOn(uint256 _roundId, BetSide _betSide)
public
payable
isValidValue
roundIsActive(_roundId)
betOnlyOncePerRound(_roundId)
{
Round storage round = rounds[_roundId];
if (_betSide == BetSide.Gte) {
round.gteTotal += msg.value;
roundToGtePlayers[_roundId].push(msg.sender);
} else {
round.ltTotal += msg.value;
roundToLtPlayers[_roundId].push(msg.sender);
}
Bet storage newBet = roundToPlayerBet[_roundId][msg.sender];
newBet.amount = msg.value;
// newBet.side = _betSide;
newBet.time = block.timestamp;
emit PredictionPool_NewBet(_roundId, msg.sender, msg.value, _betSide, block.timestamp);
}
/// @notice Resolves a round by fetching the current price and determining winners.
/// @dev Steps:
/// 1. Fetches normalized price from Chainlink.
/// 2. Sets winners based on `round.target <= normalizedPrice` and each player's bet.
/// 3. Emits `PredictionPool_RoundResolved` with creator's win status.
/// @param _roundId ID of the round to resolve.
function _resolveRound(uint256 _roundId) private {
Round storage round = rounds[_roundId];
round.status = RoundStatus.Resolved;
emit PredictionPool_NewRoundStatus(_roundId, round.status);
uint256 normalizedPrice = getChainlinkDataFeedNormalizedPrice(round.priceFeed);
bool isGteTarget = round.target <= normalizedPrice;
// set winners
address[] memory winners = isGteTarget ? roundToGtePlayers[_roundId] : roundToLtPlayers[_roundId];
for (uint256 i = 0; i < winners.length; ++i) {
isRoundWinner[_roundId][winners[i]] = true;
}
// check if round creator is winner
address roundCreator = round.creator;
bool roundCreatorIsWinner = isRoundWinner[_roundId][roundCreator];
emit PredictionPool_RoundResolved(_roundId, roundCreator, roundCreatorIsWinner);
}
/// @notice Allows winners to claim their reward after a round is resolved.
/// @dev Reverts if:
/// - Round is not resolved.
/// - Player already claimed their reward.
/// - Player is not a winner.
/// - Reward transfer fails.
/// @param _roundId ID of the resolved round.
function claimReward(uint256 _roundId) public nonReentrant {
Round storage round = rounds[_roundId];
Bet storage bet = roundToPlayerBet[_roundId][msg.sender];
if (round.status != RoundStatus.Resolved) {
revert PredictionPool_RoundNotResolved();
}
if (bet.claimed) {
revert PredictionPool_AlreadyClaimed();
}
// Check if winner
bool isWinner = isRoundWinner[_roundId][msg.sender];
if (!isWinner) {
revert PredictionPool_NotAWinner();
}
// Calculate payout
uint256 playerWeight = getBetWeight(_roundId, msg.sender);
uint256 totalWeight = getTotalWeight(_roundId);
uint256 rewardPool = round.gteTotal + round.ltTotal;
uint256 payout = (rewardPool * playerWeight) / totalWeight;
// Mark as claimed and send funds
bet.claimed = true;
emit PredictionPool_RewardClaimed(_roundId, msg.sender, payout);
(bool success,) = msg.sender.call{value: payout}("");
if (!success) {
revert PredictionPool_RewardTransferFailed();
}
}
/// @notice Fetches and normalizes the latest price from a Chainlink feed.
/// @dev Reverts if:
/// - Feed decimals > 18.
/// - Price <= 0.
/// @param _dataFeed Address of the Chainlink feed.
/// @return normalizedPrice Price scaled to 18 decimals.
function getChainlinkDataFeedNormalizedPrice(address _dataFeed)
public
view
isValidFeed(_dataFeed)
returns (uint256 normalizedPrice)
{
uint8 decimals = AggregatorV3Interface(_dataFeed).decimals();
if (decimals > 18) {
revert PredictionPool_InvalidChainlinkDecimals(decimals);
}
int256 price = _getChainlinkDataFeedLatestAnswer(_dataFeed);
if (price <= 0) {
revert PredictionPool_NegativeChainlinkPrice();
}
normalizedPrice = uint256(price) * (10 ** (18 - decimals));
}
/// @notice Returns the `Round` struct for a given ID.
/// @dev Returns a default `Round` if `_roundId` does not exist.
/// @param _roundId ID of the round to fetch.
/// @return Round struct for the specified ID.
function getRound(uint256 _roundId) public view returns (Round memory) {
return rounds[_roundId];
}
/// @notice Fetches the raw latest answer from a Chainlink feed.
/// @dev Uses `AggregatorV3Interface.latestRoundData()` and returns the `answer` field.
/// @param _dataFeed Address of the Chainlink feed.
/// @return int256 Raw price answer (can be negative/zero).
function _getChainlinkDataFeedLatestAnswer(address _dataFeed) private view returns (int256) {
AggregatorV3Interface dataFeed = AggregatorV3Interface(_dataFeed);
// prettier-igNore
(
/* uint80 roundId */
,
int256 answer,
/*uint256 startedAt*/
,
/*uint256 updatedAt*/
,
/*uint80 answeredInRound*/
) = dataFeed.latestRoundData();
return answer;
}
/// @notice Calculates the weight of a player's bet in a round.
/// @dev Weight is proportional to:
/// - Bet amount (`bet.amount`).
/// - Time factor: `(round.end - bet.time) / (round.end - round.start)` (earlier bets = higher weight).
/// @param _roundId ID of the round.
/// @param player Address of the player.
/// @return uint256 Computed bet weight (normalized).
function getBetWeight(uint256 _roundId, address player) public view returns (uint256) {
Bet storage bet = roundToPlayerBet[_roundId][player];
Round storage round = rounds[_roundId];
// earlier bet = higher weight; linearly scaled
uint256 timeFactor = ((round.end - bet.time) * 1e18) / (round.end - round.start); // normalized to 1e18
return (bet.amount * timeFactor) / 1e18; // weight also normalized
}
/// @notice Sums the weights of all bets in a round.
/// @dev Iterates over `gtePlayers` and `ltPlayers` arrays.
/// @param _roundId ID of the round.
/// return uint256 Total weight of all bets in the round.
function getTotalWeight(uint256 _roundId) public view returns (uint256 totalWeight) {
address[] memory gtePlayers = roundToGtePlayers[_roundId];
address[] memory ltPlayers = roundToLtPlayers[_roundId];
for (uint256 i = 0; i < gtePlayers.length; ++i) {
totalWeight += getBetWeight(_roundId, gtePlayers[i]);
}
for (uint256 i = 0; i < ltPlayers.length; ++i) {
totalWeight += getBetWeight(_roundId, ltPlayers[i]);
}
}
/// @notice Adds/removes a price feed from the allowed list.
/// @dev Emits `PredictionPool_PriceFeedToggled`.
/// Reverts if `_dataFeed` is the zero address.
/// @param _dataFeed Address of the feed to toggle.
function toggleAllowPriceFeed(address _dataFeed) public onlyOwner {
bool enabled = allowedDataFeeds[_dataFeed];
if (_dataFeed == address(0)) {
revert PredictionPool_ZeroAddressNotAllowed();
}
if (enabled) {
delete allowedDataFeeds[_dataFeed];
uint256 len = allowedDataFeedsList.length;
for (uint256 i = 0; i < len; ++i) {
if (allowedDataFeedsList[i] == _dataFeed) {
allowedDataFeedsList[i] = allowedDataFeedsList[len - 1];
allowedDataFeedsList.pop();
break;
}
}
} else {
allowedDataFeeds[_dataFeed] = true;
allowedDataFeedsList.push(_dataFeed);
}
emit PredictionPool_PriceFeedToggled(_dataFeed, !enabled);
}
/// @notice Updates the minimum round duration.
/// @dev Reverts if `_duration < STRICT_MIN_ROUND_DURATION`.
/// Emits `PredictionPool_NewMinRoundDuration`.
/// @param _duration New minimum duration (in seconds).
function setRoundDuration(uint256 _duration) public onlyOwner {
if (_duration < STRICT_MIN_ROUND_DURATION) {
revert PredictionPool_InvalidDuration();
}
minRoundDuration = _duration;
emit PredictionPool_NewMinRoundDuration(_duration);
}
}