0

Writing my first contract using YouTube courses all around. On the step to create a withdraw function get receive the money back.

Here is the code below. Not sure where I went wrong.

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

//Type error send and transfer are only available for objects of type address payable

0

1 Answer 1

0

You get this error because msg.sender is of type address by default and not address payable (since solidity 0.8.0).

A type address doesn't have access to those functions (send / transfer) , but a type of address payable does.

Try like this :

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

Btw, I removed the payable modifier to the withdraw function since it makes little sense to send money as you'd get it back instantly.

1
  • Your awesome! worked perfectly! Commented Nov 10, 2021 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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