Created
February 5, 2023 16:48
-
-
Save ArslanKathia/eefd1005e405c7e5d171abf98f2ab358 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.7+commit.e28d00a7.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.7.0 < 0.8.18; | |
contract Lottery{ | |
address public manager; | |
address payable[] public players; | |
constructor(){ | |
manager = msg.sender; | |
} | |
function alreadyEntered() private view returns(bool){ | |
for(uint i=0;i<players.length;i++){ | |
if(players[i]==msg.sender) | |
return true; | |
} | |
return false; | |
} | |
function entered() public payable{ | |
require(msg.sender != manager, "Manager can't participate in lottery"); | |
require(alreadyEntered()==false,"Player already participated for lottery"); | |
require(msg.value >= 1 ether,"Minimum amount must be paid to participate in lottery"); | |
players.push(payable(msg.sender)); | |
} | |
function random() public view returns(uint){ | |
return uint(sha256(abi.encodePacked(block.difficulty,block.number,players))); | |
} | |
function pickWinner() public{ | |
require(manager == msg.sender,"Only manager can picked the winner"); | |
uint index = random()%players.length; //winner index | |
address contractAddress = address(this); | |
players[index].transfer(contractAddress.balance); | |
players = new address payable[](0); | |
} | |
function getPlayers() public view returns(address payable[] memory){ | |
return players; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment