0

Below is the contact code. This is basically a lottery contract.

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.10;

contract Lottery {
    address public manager;
    address[] public players;

    constructor() public {
        manager = msg.sender;
    }

    function enter() public payable {
        require(msg.value > .01 ether);

        players.push(msg.sender);
    }

    function random() private view returns (uint) {
        return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,players)));

    }

    function pickWinner() public restricted {
        uint index = random() % players.length;
        payable(players[index]).transfer(address(this).balance);
        players = new address[](0);
    }

    modifier restricted() {
        require(msg.sender == manager);
        _;
    }

    function getPlayers() public view returns (address payable[] memory) {
        return players;
    }
}

On the last line return players, I am getting this ERROR: Return argument type address[] storage ref is not implicitly convertible to expected type (type of first return variable) address payable[] memory.

What am I missing ??

1 Answer 1

0

You could change your getPlayers() function to the following:

    function getPlayers() public view returns (address[] memory) {
        return players;
    }

From solidity 0.8.0 onwards you dont need to explicitly define things as payable. You can just wrap addresses in payable when you need it. For example:

    function test() public payable {
        address player1 = getPlayers()[0];
        payable(player1).transfer(msg.value);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.