-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainBetv1.sol
157 lines (116 loc) · 4.49 KB
/
ChainBetv1.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.26;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract ChainBet {
struct Prediction {
address addresss;
int price;
uint blockTime;
}
uint public poolBalance;
uint public poolReward;
mapping(uint => Prediction) public prediction;
mapping(uint => address) public winners;
uint private count;
uint internal winnersCount;
address public owner;
uint internal startPrediction;
uint internal endPredictionTime;
AggregatorV3Interface internal priceFeed;
// EVENTS
event UserPrediction(address indexed _user, int _price, uint _blockTime);
event PredictionDelete(uint _predictionDelete);
event winner(address indexed _winner);
event withdraw(address indexed _withdrawAddress, uint _amount);
event Deposit(uint _amount);
// MODIFIERS
modifier onlyOwner() {
require(msg.sender == owner, "Caller is not the owner");
_;
}
constructor(uint _poolReward) {
/* BTC / USD *** Deviation 1% *** Heartbeat 3600s */
priceFeed = AggregatorV3Interface(0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43);
owner = msg.sender;
startPrediction = block.timestamp;
endPredictionTime = block.timestamp + 60 minutes;
poolReward = _poolReward;
}
function getChainlinkDataFeed() public view returns(int) {
(
/* uint80 roundId,*/ ,
int price,
/* uint startedAt, */,
/* uint timeStamp, */,
/* uint80 answeredInRound */
) = priceFeed.latestRoundData();
return price / 10**8;
}
function pricePrediction(int _price) external {
require(checkBlockTime(), "When the round starts, you have the right to prediction for the first 15 minutes, please wait for the next round.");
prediction[count] = Prediction(msg.sender, _price, block.timestamp);
unchecked {
count += 1;
}
emit UserPrediction(msg.sender, _price, block.timestamp);
}
function depositPool() external payable onlyOwner {
require(msg.value > 0);
unchecked {
poolBalance += msg.value;
}
emit Deposit(msg.value);
}
function withdrawPool(address _to, uint _amount) external onlyOwner {
require(address(this).balance > 0, "There is no money in the pool");
unchecked {
poolBalance -= _amount;
}
payable(_to).transfer(_amount);
emit withdraw(_to, _amount);
}
function checkWinner() external onlyOwner {
require(count > 0, "No predictions, winner not announced");
int lastPrice = getChainlinkDataFeed();
int priceRange = lastPrice / 100; // range 1%
int priceRangeUp = lastPrice + priceRange;
int priceRangeDown = lastPrice - priceRange;
for (uint i = 0; i < count; i++) {
if (prediction[i].price >= priceRangeDown && prediction[i].price <= priceRangeUp) {
winners[winnersCount] = prediction[i].addresss;
unchecked {
winnersCount += 1;
poolBalance -= poolReward;
}
payable(prediction[i].addresss).transfer(poolReward);
emit winner(prediction[i].addresss);
}
}
}
function setPoolReward(uint _reward) external onlyOwner returns(uint) {
return poolReward = _reward;
}
function predictionDel() external onlyOwner {
startPrediction = block.timestamp;
endPredictionTime = block.timestamp + 60 minutes;
for(uint i = 0; i < count; i++) {
prediction[i].addresss = address(0);
prediction[i].price = 0;
prediction[i].blockTime = 0;
}
count = 0;
emit PredictionDelete(count);
}
function checkBlockTime() internal view returns(bool) {
if (block.timestamp <= startPrediction + 15 minutes) {
return true;
}
return false;
}
receive() external payable {
revert("Only owner can deposit money into this contract. (deposit function)");
}
fallback() external payable {
revert("Only owner can deposit money into this contract. (deposit function)");
}
}