Solidity TypeError – TypeError: Type int_const 0 is not Implicitly Convertible to Address Payable

contract-developmentsolidity

Hi I'm trying to update this code from solidity 0.4.X to 0.5.X and am currently stuck on this bug

    struct JediBet {
    uint guess;
    address payable addr;
    BetStatusEnum status;
    uint betAmount;
}

//the 'game' structure
struct Game {
    uint pot;
    uint outcome;
    GameStatusEnum status;
    JediBet originator;
    JediBet taker;
}

//bet status event
event BetStatus (
    GameStatusEnum gameStatus,
    BetStatusEnum originatorStatus,
    address originatorAddress,
    uint originatorGuess,
    address takerAddress,
    BetStatusEnum takerStatus,
    uint takerGuess,
    uint betAmount,
    uint actualNumber,
    uint pot
);

//the game
Game game;

//fallback function
function() external payable {}

function resetGame() private {
    //reset game
    game.status = GameStatusEnum.STATUS_NOT_STARTED;
    game.outcome = 0;
    game.pot = 0;

    game.originator.guess = 0;
    game.originator.betAmount = 0;
    game.originator.addr = 0;
    game.originator.status = BetStatusEnum.STATUS_UNKNOWN;

    game.taker.guess = 0;
    game.taker.addr = 0;
    game.taker.status = BetStatusEnum.STATUS_UNKNOWN;
}

Error Message:

    Compiling your contracts...
===========================
> Compiling .\contracts\Bet.sol
> Compiling .\contracts\Migrations.sol

/C/Users/Michael/ethereum/p60/contracts/Bet.sol:53:32: TypeError: Type int_const 0 is not implicitly convertible to expected type address payable.
        game.originator.addr = 0;
                               ^
,/C/Users/Michael/ethereum/p60/contracts/Bet.sol:57:27: TypeError: Type int_const 0 is not implicitly convertible to expected type address payable.
        game.taker.addr = 0;
                          ^
,/C/Users/Michael/ethereum/p60/contracts/Bet.sol:106:13: TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address".
            origAddress.transfer(origPot/2);

Best Answer

Error 1 & 2: Explicit type cast required


The number 0 is an (unsigned) integer. If you want to assign it to an variable of the address type, you have to cast it explicitly to an address type:

game.originator.addr = address(0);
game.taker.addr = address(0);

note: Obviously you don't want to transfer funds to the address 0, unless you want to deploy and initialize a contract. Before you execute .transfer(...) on those addresses, check if they were set:

require(game.originator.addr != address(0), 'game.originator uses address(0)')
game.originator.addr.transfer(...);

respectively for game.taker.addr or rather any address that funds are send to.

Error 3: Addresses that receive funds have to be payable


You try to execute the .send or .transfer function on a variable of the address type. This is not possible anymore, if this address is ought to receive funds, you have to declare it as payable:

address payable origAddress;
Related Topic