-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLottery.sol
More file actions
99 lines (73 loc) · 2.73 KB
/
Copy pathLottery.sol
File metadata and controls
99 lines (73 loc) · 2.73 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "witnet-solidity-bridge/contracts/interfaces/IWitnetRandomness.sol";
contract Lottery {
//Address of the witnet randomness contract in Celo Alfajores testnet
address witnetAddress = 0xbD804467270bCD832b4948242453CA66972860F5;
IWitnetRandomness public witnet = IWitnetRandomness(witnetAddress);
// The price to enter the lottery
uint256 public entryAmount;
uint256 public lastWinnerAmount;
uint256 public lotteryId;
uint256 public latestRandomizingBlock;
address payable public lastWinner;
address[] public players;
address public owner;
bool public open;
constructor () {
owner = msg.sender;
}
modifier onlyOwner{
require(msg.sender == owner, "not owner");
_;
}
//Checks if there is a current active lottery
modifier onlyIfOpen{
require(open, "Not Open");
_;
}
event Started(uint lotteryId, uint entryAmount);
event Ended(uint lotteryId, uint winningAmount, address winner);
error reEntry();
function start(uint32 _entryAmount) external onlyOwner{
//Check if there is a current active lottery
require(!open, "running");
// Convert the default wei input to celo
entryAmount = _entryAmount * 1 ether;
open = true;
// Deleting the previous arrays of players
delete players;
emit Started(lotteryId, _entryAmount);
lotteryId++;
}
function join() external payable onlyIfOpen{
require(msg.value == entryAmount, "Insufficient Funds");
//Check if user is already a player
for(uint i=0; i < players.length; i++){
if (msg.sender == players[i]){
revert reEntry();
}
}
players.push(msg.sender);
}
function requestRandomness() external onlyOwner onlyIfOpen{
latestRandomizingBlock = block.number;
//Setting the fee to 1 celo
uint feeValue = 1 ether;
witnet.randomize{ value: feeValue }();
}
function pickWinner() external onlyOwner onlyIfOpen{
// Check if the requestRandomness was called to generate the randomness
assert(latestRandomizingBlock > 0);
uint32 range = uint32(players.length);
uint winnerIndex = witnet.random(range, 0, latestRandomizingBlock);
lastWinner = payable(players[winnerIndex]);
lastWinnerAmount = address(this).balance;
(bool sent,) = lastWinner.call{value: lastWinnerAmount}("");
require(sent, "Failed to send reward");
open = false;
latestRandomizingBlock = 0;
emit Ended(lotteryId, lastWinnerAmount, lastWinner);
}
receive () external payable {}
}