0

Working on this Udemy course for Ethereum and i keep getting the error mentioned in the title of the question.

For reference i have checked out the answers on questions: 1, 2 and 3. But the error persists.

The contract goes like this:

pragma solidity >=0.4.22 <0.6.0;

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(msg.sender, now, players)));
    }

    function pickWinner() public {
        uint index = random() % players.length;
        players[index].transfer(address(this).balance); // Where the error happens
    }
}

Using Remix Solidity compiler.

I am pretty sure this is some old version code error since the Solidity version used in the course is ^0.4. Can anyone guide me what am I missing here?

1

The error is because your players address needs to be payable, like that:

address payable[] public players;

If you update your code to Solidity version 0.6.0, you can convert from address to address payable via payable(x), like that:

payable(players[index]).transfer(address(this).balance);

From the docs:

Conversions from address to address payable are now possible via payable(x), where x must be of type address.

Your Answer

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

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