PiotrMerce
PiotrMerce

Reputation: 1

Solidity TypeError in lottery contract

I'm writing a simple Lottery contract in Solidity, and in the last line I get this error:

TypeError: Return argument type address payable[] storage ref is not
implicitly convertible to expected type (type of first return
variable) address[] memory.
contracts/Lottery.sol:40:16: return players;
                                    ^^^^^^^

I'm compiling on 8.7 with this code:

    //SPDX-License-Identifier: GPL-3.0
    
pragma solidity >=0.4.17 <0.9.0;
//pragma solidity ^0.4.17;
    
contract Lottery {
    address public manager;
    address payable[] public players;

    constructor() {
        manager = msg.sender; 
    }

    function enter() public restricted payable {
        require(msg.value >= .01 ether);    //min. amount to enter the lottery is 0.01 ether in wei
        players.push(payable(msg.sender));
    }
    
    function random() private view restricted returns(uint) {  //picking random number, we use block difficulty, current block time and amount of players to hash with sha3 algorithm
        return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp , players)));          //keccak256 == sha3
    
    }

    function pickWinner() public {
        require(msg.sender == manager);             //only manager can pick winner
            
        uint index = random() % players.length;     //pick winner by using random number and % modulo from it, as player in array
                                                        //players[index].transfer(this.balance);
        players[index].transfer(address(this).balance);                 //player with index number in players array is winner, return address
                                                                        //transfer(amount of lottery reward);
        players = new address payable[](0);        //reseting lottery; '0' means size of dynamic array
    }
    
    modifier restricted() {                 //modifier for repeated functions
        require(msg.sender == manager);
        _;              // _ means run the rest of the code inside this function
    }
    
    function getPlayers() public view returns(address[] memory) {
        return players; 
    }
    
}

What is wrong? Thanks for help!

Upvotes: 0

Views: 286

Answers (1)

Antonio Carito
Antonio Carito

Reputation: 1387

You must only change return type into getPlayers() function signature instead:

function getPlayers() public view returns(address[] memory) {
    ...
}

change it with this:

function getPlayers() public view returns(address payable[] memory) {
        ...
}

Upvotes: 2

Related Questions