Skip to content

Instantly share code, notes, and snippets.

@gregszero
Created October 2, 2023 19:45
Show Gist options
  • Save gregszero/bf918c67219a8b72a2ff88074a07b2f7 to your computer and use it in GitHub Desktop.
Save gregszero/bf918c67219a8b72a2ff88074a07b2f7 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract Lottery {
address public manager;
address payable[] public candidates;
address payable public winner;
constructor(){
manager = msg.sender;
}
receive() external payable {
require(msg.value == 1 ether);
candidates.push(payable(msg.sender));
}
function getBalance() public view returns(uint){
require(msg.sender == manager);
return address(this).balance;
}
function getRandom() public view returns(uint){
return uint(keccak256(abi.encodePacked(block.prevrandao, block.timestamp, candidates.length)));
}
function PickWinner() public {
require(msg.sender == manager);
require(candidates.length >= 2);
uint r = getRandom();
uint index = r%candidates.length;
winner = candidates[index];
winner.transfer(getBalance());
candidates = new address payable [](0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment