Created
September 2, 2023 12:09
-
-
Save krishi-agrawal/324ddbb88c9203ae2d36c102caf79c06 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.5.0 <0.9.0; | |
contract Lottery{ | |
address payable[] public players; | |
address public manager; | |
constructor(){ | |
manager = msg.sender; | |
} | |
receive () payable external{ | |
require(msg.value == 1 ether); | |
players.push(payable(msg.sender)); | |
} | |
function getBalance() public view returns(uint){ | |
require(msg.sender == manager); | |
return address(this).balance; | |
} | |
function random() internal view returns(uint){ | |
return uint(keccak256(abi.encodePacked(block.prevrandao, block.timestamp, players.length))); | |
} | |
function pickWinner() public{ | |
require(msg.sender == manager); | |
require (players.length >= 3); | |
uint r = random(); | |
address payable winner; | |
uint index = r % players.length; | |
winner = players[index]; | |
winner.transfer(getBalance()); | |
players = new address payable[](0); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment