Untitled
unknown
plain_text
2 years ago
1.8 kB
8
Indexable
pragma solidity ^0.8.0; contract Lottery { address public manager; address[] public participants; address public lastWinner; uint public numberOfParticipants; uint public poolSize; uint public requiredParticipants = 5; uint public requiredEth = 0.1 ether; constructor() { manager = msg.sender; } function enter() public payable { require(msg.value == requiredEth, "You must send 0.1 ether to enter the lottery."); require(participants.length < requiredParticipants, "The current lottery pool is full."); for (uint i = 0; i < participants.length; i++) { require(participants[i] != msg.sender, "You are already a participant."); } participants.push(msg.sender); if (participants.length == requiredParticipants) { pickWinner(); startNewLottery(); } } function pickWinner() private { require(participants.length == requiredParticipants, "The pool is not full yet."); uint index = random() % requiredParticipants; lastWinner = participants[index]; payable(lastWinner).transfer(poolSize); } function startNewLottery() private { delete participants; poolSize = 0; } function random() private view returns (uint) { return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, participants))); } function viewParticipants() public view returns (address[] memory, uint) { return (participants, participants.length); } function viewPreviousWinner() public view returns (address) { require(lastWinner != address(0), "No lottery has been completed yet."); return lastWinner; } }
Editor is loading...