Created
March 18, 2023 09:08
-
-
Save karandhumal2023/1616d2f56fbbda7737dc2f35bdfb41f0 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 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 public manager; | |
address payable[] public participants; | |
constructor (){ | |
manager=msg.sender; // glob_var | |
} | |
receive() external payable | |
{ | |
require(msg.value==1 ether); // only once we can use this func it is always external or payable | |
participants.push(payable(msg.sender)); | |
} | |
function getbalance() public view returns(uint) | |
{ | |
require(msg.sender==manager); | |
return address(this).balance; | |
} | |
function random() public view returns(uint) | |
{ | |
// keccak256() creates random no in string so we can convert into uint | |
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,participants.length))); | |
} | |
function selectwinner() public | |
{ | |
require(msg.sender==manager); | |
require(participants.length>=3); | |
uint r=random(); | |
address payable winner; | |
uint index=r%participants.length; // it converts big no into participents.length | |
winner=participants[index]; // random participents | |
winner.transfer(getbalance()); | |
participants=new address payable[](0); // clear data from participents array | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment