user15360967
user15360967

Reputation:

Ethereum lottery smart contract with the following conditions

pragma solidity ^0.5.0;

contract LuckyDraw 
{
    address owner;
    address[3] participants;
    
    constructor() public 
    {  
        owner =  msg.sender;
    }

    modifier onlyOwner()
    {
        require(msg.sender == owner);
        _;
    }
    
    modifier notOwner()
    {
        _;
    }
    
    function joinLottery() payable public require notOwner()
    {
        require(msg.value == 2 ether);
    }
    
    function random() private view returns(uint)
    {
        return uint(keccak256(abi.encode(block.difficulty, now, participants)));
    }
    
    function pickwinner() external onlyOwner 
    {
        uint win = random() % participants.length;
        
        participants[index].transfer(address(this).balance);
        
        participants = new address[](0);
    }
}

How do I set the no. of players to 'n'?

Upvotes: 2

Views: 1488

Answers (1)

Ahmad Gorji
Ahmad Gorji

Reputation: 424

Unfortunately I have some unknown issues with solidity 0.5.0 so I provide you a better contract than you wrote here:

pragma solidity 0.6.0;

contract LuckyDraw {
    
    uint private maxParticipantNumbers;
    uint private participantNumbers;
    uint private ticketPrice;
    address private owner;
    address payable[] participants;
    
    constructor() public {  
        owner =  msg.sender;
        maxParticipantNumbers = 3;
        ticketPrice = 2 ether;
    }

    modifier onlyOwner(){
        require(msg.sender == owner, "Access denied!");
        _;
    }
    
    modifier notOwner(){
        require(msg.sender != owner, "Access denied");
        _;
    }
    
    function setTicketPrice(uint _valueInEther) public onlyOwner{
        ticketPrice = (_valueInEther * 1000000000000000000);
    }
    
    function setMaximmNumbers(uint _maxNumbers) public onlyOwner{
        participantNumbers = _maxNumbers;
    }
    function viewTicketPrice() external view returns(uint){
        return ticketPrice;
    }
    
    function joinLottery() payable public notOwner(){
        require(msg.value == ticketPrice);
        if (participantNumbers < maxParticipantNumbers){
            participants.push(msg.sender);
            participantNumbers++;
        }
        else if (participantNumbers == maxParticipantNumbers){
            msg.sender.transfer(msg.value);
            pickwinner();
        }
    }
    
    function random() private view returns(uint){
        return uint(keccak256(abi.encode(block.difficulty, now, participants, block.number)));
    }
    
    function pickwinner() internal{
        uint win = random() % participants.length;
        
        participants[win].transfer(address(this).balance);
        
        delete participants;
        participantNumbers = 0;
    }
    
    function endGame() external onlyowner{
        uint win = random() % participants.length;
        
        participants[win].transfer(address(this).balance);
        
        delete participants;
        participantNumbers = 0;
    }
}

Here is the explain:

First of all compiler is 0.6.0!

maxParticipantNumbers is maximum number of participants.

participantNumbers counts the participants.

ticketPrice is the price of ticket.

follows are same.

setTicketPrice() is a function which only owner of contract can call it and updates the price of ticket or you can just leave it (Note that the number you enter automatically is measures as wei so I added 10^18 at the end which you just need to set the number and it is in ether amount)!

setMaximmNumbers() is a function which only owner of contract can call it and changes the maximum number of players which can participate in the lottery!

viewTicketPrice() is an external view function which returns the price of ticket (Note that it returns the value in wei).

And joinLottery() is a function which sells the ticket and when the last participant buys the ticket, automatically calls the pickwinner() function and lottery ends and does the transfer and starts again automatically!

Also I put endGame() function in case if owner wants to end the game earlier than reaching maximum number of participants!

Of course you can change it by your self but I hope you enjoy it!

Upvotes: 2

Related Questions