SHARE
TWEET

Untitled

a guest Feb 27th, 2020 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^0.6.1;
  2.  
  3. contract Lottery{
  4.    
  5.     address public manager;
  6.    
  7.     address payable[] public players;
  8.    
  9.     uint public playersCounter;
  10.    
  11.     uint public maxPlayersNumbers;
  12.    
  13.     event PlayerJoined(address player);
  14.     event PlayerWon(address player, uint amount);
  15.    
  16.     constructor(uint playersNumber) public {
  17.         maxPlayersNumbers = playersNumber;
  18.         manager = msg.sender;
  19.     }
  20.    
  21.     function join() payable public {
  22.         require(msg.value >= .1 ether);
  23.        
  24.         players.push(msg.sender);
  25.         playersCounter++;
  26.        
  27.         emit PlayerJoined(msg.sender);
  28.        
  29.         if (playersCounter >= maxPlayersNumbers) {
  30.             drawWinner();
  31.         }
  32.     }
  33.    
  34.     function getPlayers() public view returns (address payable[] memory){
  35.         return players;
  36.     }
  37.    
  38.     function forceDrawing() public onlyManager{
  39.         require(msg.sender == manager);
  40.        
  41.         drawWinner();
  42.     }
  43.    
  44.     modifier onlyManager {
  45.         require(msg.sender == manager);
  46.         _;
  47.     }
  48.    
  49.     function drawWinner() private {
  50.         uint index = random() % players.length;
  51.         uint amount = address(this).balance;
  52.        
  53.         address payable winner = players[index];
  54.         winner.transfer(amount);
  55.        
  56.         players = new address payable[](0);
  57.         playersCounter = 0;
  58.        
  59.         emit PlayerWon(winner, amount);
  60.        
  61.     }
  62.    
  63.     function random() private view returns (uint) {
  64.         return uint(keccak256(
  65.             abi.encodePacked(block.difficulty, now, players)));
  66.        
  67.     }
  68.    
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top