Skip to content

Instantly share code, notes, and snippets.

@crisleo94
Created December 14, 2021 11:29
Show Gist options
  • Save crisleo94/f70fb56156e5c1824777aaa8dcfe670e to your computer and use it in GitHub Desktop.
Save crisleo94/f70fb56156e5c1824777aaa8dcfe670e to your computer and use it in GitHub Desktop.
Lottery game with solidity, only the manager can pick a winner
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Lottery {
address public manager_address;
address[] public players;
constructor() {
manager_address = msg.sender;
}
function enter() public payable {
// if the value is less the transaction will be reverted
require(msg.value > 0.1 ether);
players.push(msg.sender);
}
function pickWinner() public restricted payable {
// generate a random index based on the pseudorandom value
uint index = random() % players.length;
payable(players[index]).transfer(address(this).balance);
delete players;
}
function random() private view returns(uint) {
// create a pseudorandom value
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players)));
}
function getPlayers() public view returns(address[] memory) {
return players;
}
modifier restricted() {
require(msg.sender == manager_address);
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment