2

Here's the function:

   function withdrawMoney() public {
        address payable to = msg.sender;
        to.transfer(this.getBalance());
    }

And the error:

TypeError: Type address is not implicitly convertible to expected type address payable.
  --> pay.sol:16:9:
   |
16 |         address payable to = msg.sender;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

someone suggested to remove the payable keyword, but then I can't use transfer keyword on the next line.

0

3 Answers 3

2

Before Solidity version 0.8.0 it was possible to implicitly convert a type address to a type address payable.

But from Solidity version 0.8.0 and on, we need to explicitly cast an address type to a address payable like:

address payable to = payable(msg.sender);
0

explicit conversion:

address payable to = payable(msg.sender);

0
function withdrawMoney(address payable to) external {
     to = msg.sender;
     to.transfer(this.getBalance());
    }

This is a way to do it. Casting the address as payable in the function args simplifies the whole thing for you. You can also use a bool to ensure that the transaction returns true.

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