1

I have an error "TypeError: "send" and "transfer" are only available for objects of type "address payable", not "address"" on

function withdraw() payable public {
    msg.sender.transfer(address(this).balance);
}

in the line "msg.sender.transfer"

0

2 Answers 2

0

address and payable address both store 160-bit length address, but If you want to send ethers to a particular address you need to specify them as payable address.

ways to define payable address:

address addr1; address payable addr2 = payable(addr1);

or

address payable addr; payable(addr).call("")...

As much I know you need to specify payable two times. once while declaring and once while using.

hope it helps...

0

this means that your type "address" (msg.sender) has no access to the send, transfer and call functions, it cannot send eth, in order to do so you need to use another type : address payable

*Documentation:*https://docs.soliditylang.org/en/v0.8.18/types.html#address

address payable: Same as address, but with the additional members transfer and send.

In order to do the latter you need to:

  • use a type modifier : "payable" when you initialize the address variable (you create a container for storing addresses that can pay and receive eth)
  • typecast the "msg.sender" address type during the declaration of the payable address variable (you fill the container with an address).

Documentation:

conversions from address to address payable must be explicit via payable().

it will give you the following :

  address payable owner;
  owner = payable(msg.sender);

and from now you can use send and transfer.

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