1

i am creating smart contract for lottery system and i am stuck on a problem, i want to make a time based transaction like a certain amount of players are added into the array then a time stamp should run and on specific time which i will declare should send the amount to the winner, I tried a lot of things but couldn't find the answer please anyone can help here is my code

this is the part where i am stuck, i am trying to enter into the lottery from this this function and when conditions met i want to transfer the amount through the same function cause i want to automate the winner function .......... i hope you got my point

function enter() public payable{
   require(msg.value > 1 wei);
   players.push(msg.sender);

 if(players.length==10){
     start = block.timestamp;
 }

 if(block.timestamp>= start+totalTime){
    uint index = random()% players.length;
    players[index].transfer(this.balance);
    dead[index].transfer((this.balance*2)/100);
    winner = players[index];
    players = new address[](0);
 }

this is my complete code


pragma solidity ^0.4.26;

contract Lottery{
   address public manager;
   address[] public players;
   address [0x000000000000000000000000000000000000dead] private dead;
   address public winner;
   uint start;
    uint end;
   uint totalTime=50;
  
   constructor()public {
      manager = msg.sender;
   }

   function enter() public payable{
      require(msg.value > 1 wei);
      players.push(msg.sender);
   
    if(players.length==10){
        start = block.timestamp;
    }

    if(block.timestamp>= start+totalTime){
       uint index = random()% players.length;
       players[index].transfer(this.balance);
       dead[index].transfer((this.balance*2)/100);
       winner = players[index];
       players = new address[](0);
    }
   }
   function random() private view returns (uint){
       return uint(keccak256(block.difficulty,now,players));
   }
   function getBalance() public view returns(uint){
       return address(this).balance;
   }
   function getPlayers() public view returns (address[]){
       return players;
   }
   function getWinner() public view returns (address){
       return winner;
   }
    function getTime() public view returns (uint){
       return end-block.timestamp;
   }
}


6
  • So you want the SmartContract to declare the winner on its own, after some time had elapsed?
    – Sky
    Jan 13 at 14:31
  • Try to avoid using block.timestamp in a lottery contract. You're not the first one who comes up with this idea and there are a lot of projects that got hacked through this use of block.timestamp. First of all miners can exploit this by tweaking the timestamp (they can add or remove around 15 seconds from the actual time, and second block.timestamp is a value that can be predicted Jan 13 at 15:18
  • Also this function function random() private view returns (uint){ return uint(keccak256(block.difficulty,now,players)); }is absolutely not random. You contract can be easily exploited by other contracts. A use can create a new contract, wait until 9 already joined the lottery, execute the pseudo-random function and depending on the value they get, they will join the lottery and win it. Try finding another solution. Jan 13 at 15:21
  • then what do you recommend rather than using block.timestamp Jan 13 at 16:00
  • @MoeedElahi So what's the problem? the transaction reverts, run out of gas, does nothing, ... . Looking at it you send the whole balance with players[index].transfer(this.balance), then tries to transfer ` dead[index].transfer((this.balance*2)/100)` again, it doesn't make much sense since balance will be zero at that point.
    – Ismael
    Jan 13 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.