Search code examples
ethereumsoliditysmartcontracts

How can get winner address of lottery smart contract


Hello solidity developer , I am new to this language. I want to get winner address after draw winner function. here is draw function


function draw_daily(uint256 _randomness) public payable onlyOwner returns(address){
        require(Daily.time_end < block.timestamp, 'Draw period not over');
        uint256 index = generate_random(_randomness, (Daily.participants.length));
        uint256 prize = Daily.total_amount - ((Daily.total_amount * Daily.owner_share)/100);
        address lucky_winner = Daily.participants[index];
        Daily.total_amount = 0;
        delete Daily.participants;
        return lucky_winner;
    }
    

here lucky_winner is winner address, if I want to get address of lucky_winner , how can I do ? does I need to declare global variable luck_winner first? and then

function get_daily_winner() public view returns(address[] memory){
        return Daily.lucky_winner;
    }

can it work?

here are global variables


 address owner;
    
    struct lottery_draw{
        uint256 price;
        uint256 time_start;
        uint256 time_end;
        address[] participants;
       
        uint256 total_amount;
        uint256 owner_share;

         address lucky_winner 

    }
    
    lottery_draw Daily; 
    lottery_draw Weekly; 
    lottery_draw Monthly; 

please someone guide me . how can do it correctly.


Solution

  • Yes, you should go with the global variable and store it there in the "draw_daily"

    So in the method instead of

    address lucky_winner = Daily.participants[index];
    

    you should just use the global variable:

    lucky_winner = Daily.participants[index];