Skip to content

Instantly share code, notes, and snippets.

@MasnadNehith
Created July 21, 2018 12:26
Show Gist options
  • Select an option

  • Save MasnadNehith/1c5292e8bd4c3b176d0cf57ef8d9985c to your computer and use it in GitHub Desktop.

Select an option

Save MasnadNehith/1c5292e8bd4c3b176d0cf57ef8d9985c to your computer and use it in GitHub Desktop.
Decentralized Lottery Smart Contract
pragma solidity ^0.4.0;
contract Lottery{
// varibale to store managers address
address public manager;
//we are storing the address of the participants
address[] public participants;
constructor () public {
manager = msg.sender;
}
// Function to enter the lottery, we are going to make each users
// pay a small amount to enter the lottery
function enterLottery() public payable {
require(msg.value > 0.01 ether);
participants.push(msg.sender);
}
function pickWinner() public{
// check only that the manager can call the pick winner function
require(msg.sender == manager);
// select a random participant
uint index = random() % participants.length;
// transfer the contract balance to the participants
participants[index].transfer(this.balance);
// empty the address array
participants = new address[](0);
}
function random() private view returns(uint256){
return uint(keccak256(block.difficulty, now, participants));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment