daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity >=0.4.22 <0.6.0;
  2.  
  3.   contract Lottery{
  4. address payable[] public  players; //dynamic array with players
  5. addresses
  6. address payable public manager; //contract manager
  7.  
  8.  
  9. //contract constructor, runs once at contract deployment
  10. constructor() public{
  11.     //the manager is account address that deploys the contract
  12.     manager = msg.sender;
  13. }
  14.  
  15. //this fallback payable function will be automatically called when
  16.  somebody
  17. //sends ether to our contract address
  18. function () payable external{
  19.     require(msg.value >= 0.01 ether);
  20.     players.push(msg.sender); //add the address of the account that
  21.  
  22.                    //sends
  23.                               //ether to players array
  24. }
  25.  
  26. function get_balance() public view returns(uint){
  27.     require(msg.sender == manager);
  28.     return address(this).balance; //return contract balance
  29. }
  30.  
  31. //returns a very big pseodo-random integer no.
  32. function random() public view returns(uint256){
  33.      return keccak256(abi.encodePacked(block.difficulty,
  34.   block.timestamp, players.length));<--error is this line
  35.  
  36. }
  37. function selectWinner() public view returns(address) {
  38.     require(msg.sender == manager);
  39.  
  40.  
  41.     uint r = random();
  42.  
  43.     address payable winner;
  44.  
  45.     //a random index
  46.     uint index = r % players.length;
  47.     winner = players[index];
  48.  
  49.     //transfer contract balance to the winner address
  50.     winner.transfer(address(this).balance);
  51.  
  52.     players = new address payable[](0); //resetting the players
  53.          dynamic array
  54.     }
  55.  }
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