Compilation error :- The address is not implicitly convertible to expected type address payable

Dear Sir,

I found a compilation error in self-destructed code available in the below link. I did google but did not able to find the solution compilation errors.
https://github.com/filipmartinsson/solidity-0.7.5/blob/main/inheritance-assignment/Destroyable.sol

pragma solidity 0.8.7;

import "./Ownable.sol";
contract Destroyable is Ownable {

  function destroy() public onlyOwner  {
    address payable receiver = msg.sender; // Compilation error :- The address is not implicitly convertible to expected type address payable.
    selfdestruct(receiver);
  }
}
1 Like

Hi @Gaurav_Sahoo,

The solution code is correct for Solidity v0.7. Notice…

pragma solidity 0.7.5;

// but you are using a Solidity v0.8 compiler...
pragma solidity 0.8.7;

The course is based on Solidity v0.7, which was the latest version when it was recorded. Out of the syntax changes introduced with Solidity v0.8, the only one that affects this 101 course is the fact that msg.sender is no longer a payable address by default…

In Solidity v0.7, msg.sender is a payable address type by default, and so there is no need to explicitly convert it whenever we need it to be payable, e.g. in selfdestruct() . However, in Solidity v0.8, msg.sender is now non-payable by default, so you would have to explicity convert it by making the following modification to the code …

address payable receiver = payable(msg.sender);
selfdestruct(receiver);

But instead of converting msg.sender to a payable address via a separate variable, we can perform the conversion within the selfdestruct() function call itself…

selfdestruct(payable(msg.sender));

You should now be able to complete the assignment and post your contracts here.

But do let me know if anything is still unclear, or if you have any further questions :slight_smile:

1 Like

Note, this is the same syntax issue that you have encountered when calling the transfer address member/method on msg.sender (in the withdraw function), as discussed in your discussion topic here.

Hey sir , Just want to know what is reserved keyword immutable, in my code the error is

Expected identifier but got reserved keyword immutable

Address public immutable cardano = address (" ftugrerfhgfrrwsdt");

In this line it is showing error

Hi @Sudhanshu_Srivastava,

For a new question, always start a new topic, please. This helps to keep the forum organised, and enables your question, and the answers it receives, to be more easily found by others :slight_smile:

Here are some links to the relevant sections in the Solidity documentation which explain about the keyword immutable

https://docs.soliditylang.org/en/latest/contracts.html#constant-and-immutable-state-variables
https://docs.soliditylang.org/en/latest/contracts.html#immutable

The keyword immutable was only introduced from Solidity v0.6.5, so if you are using an earlier version, that will be why the compiler is generating the error message …

Note, you will also get compiler errors, unless you correct the following in your line of code…

  • address must start with a lower case a
  • You cannot convert a string to an address

e.g. the following will compile

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.11;

contract Test {

    address public immutable owner;

    constructor() {
        owner = msg.sender;
    }

}

yeah i m using 0.6.2 actually

but when i change the compiler version in code and in remix then new error comes

error; typeerror data location must be storage or memory for return parameter in function but call data was given …

but same code is deployed on bscscan with verification ?

how its possibile

   function _msgData() internal view virtual returns (bytes calldata) {
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
        return msg.data;
    }

Hi Sudhanshu,

When used in this way, it seems that data location calldata is only recognised by the Solidity compiler from v0.6.8 and above (although the Solidity documentation seems to suggest only from v0.6.9).

Use a more up-to-date compiler version and it compiles no problem. In Remix you now have up to v0.8.11 available.

Very nice explanation

1 Like