1

I have the following code for a simple gambling game. The code works fine except for getPlayers function

pragma solidity >=0.4.22 <0.6.0;

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

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

    require(msg.value > 0.01 ether); //condition
    players.push(msg.sender);
}

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

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

}

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

function getPlayers() public payable returns (address[] memory) {
    return players;
    // Error in the above line saying
    // "Return argument type address payable[] storage ref is not implicitly convertible to expected type(type of first return variable)  address[] memory."

}
}
  • 1
    "The code works fine" - impossible. This is a compilation error, which means that you couldn't have possibly tested the code as of yet. – goodvibration Dec 15 '18 at 9:13
  • You can fix this error by removing the memory keyword that you've placed in the declaration of function getPlayers. Side note: since players is a public state variable, the compiler automatically creates a getter function for this variable, which you can invoke from the off-chain via players(). So this function seems redundant, and you may as well get rid of it altogether. – goodvibration Dec 15 '18 at 9:16
  • By the way, I gotta admit I've never seen the use of payable in the declaration of variables (as you do at the beginning of your contract). – goodvibration Dec 15 '18 at 9:17
  • @goodvibration Yes, the recent Solidity versions have added the address payable type to the language. – Jesse Busman Dec 15 '18 at 9:52
  • @Anukool Srivastava Please accept my answer if it fixed your problem :) – Jesse Busman Dec 15 '18 at 12:07
0

As of version 0.5.0 Solidity has two address types: address and address payable.

The problem is that they cannot be implicitly converted when in memory array form, but you are trying to assign a address payable storage[] to a address memory[].

You can solve this by changing the return type of getPlayers to address payable memory[]

  • i tried your suggestion but im getting an ParserError on Remix. Error says " Expected ',' but got '[' " Its not accepting a payable return type – Anukool Srivastava Dec 17 '18 at 6:01
  • Here is the changed function definition /* function getPlayers() public payable returns (address[] payable memory) { return players; } */ – Anukool Srivastava Dec 17 '18 at 6:03
  • @AnukoolSrivastava The correct declaration should be function getPlayers() public view returns (address payable[] memory) { – Ismael Dec 18 '18 at 20:40
  • tried that, but in my local editor 'visual studio code' im getting an error with this address payable public manager;* it gives me an error saying expected identifier but got 'payable' the code is running perfectly fine on remix but when im trying to plug it with reactjs project, im getting the above error – Anukool Srivastava Jan 15 at 13:26
  • @AnukoolSrivastava Then you are compiling with a Solidity version lower than 0.5.0. To use address payable you need at least version 0.5.0 – Jesse Busman Jan 15 at 14:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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