-
Notifications
You must be signed in to change notification settings - Fork 0
/
lottery.sol
61 lines (51 loc) · 2.13 KB
/
lottery.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
pragma solidity ^0.8;
contract lottery {
mapping(address => bool) public play;
mapping(uint256 => address payable[]) public numbers;
address public owner;
uint256 public winning_number;
uint public voting_stop_block;
uint public lottery_result_block;
bool public winner_picked;
event log(string, uint);
constructor (uint _number_of_blocks_to_play){
owner = msg.sender;
voting_stop_block = block.number + _number_of_blocks_to_play;
}
function set_number(uint256 val) external payable {
require(block.number <= voting_stop_block, "lottery picking is over !");
require(msg.sender != owner, "owner cannot participate !");
require(play[msg.sender] == false, "The participant already played !");
require(msg.value == 0.1 ether, "Send 0.1 ether to play");
play[msg.sender] = true;
numbers[val%10].push(payable(msg.sender));
}
function pick_number() external {
require(winner_picked == false);
require(block.number > voting_stop_block, "lottery picking still going on");
require(msg.sender == owner, "only owner can pick winner");
winner_picked = true;
lottery_result_block = block.number;
winning_number = block.number % 10;
}
function distribute_rewards() external {
require(winner_picked == true, "Winner not yet picked");
require(owner == msg.sender, "Only owner can distribute");
uint256 balance = address(this).balance;
emit log("balance", balance);
uint winners = numbers[winning_number].length;
if (winners > 0) {
uint share = address(this).balance / (numbers[winning_number].length + 1);
for(uint i=0; i < winners; i++) {
numbers[winning_number][i].call{value: share}("");
}
}
msg.sender.call{value: address(this).balance}("");
}
function get_block_number() external view returns(uint) {
return block.number;
}
function get_current_balance() external view returns(uint256) {
return address(this).balance;
}
}