1

I am currently running through a tutorial and am wondering if someone can explain when I need a payable modifier? For example, I have the below code and am unsure as to exactly why I would need to payable portion included. What would happen if I did not inlcude the payable modifier?

        owner = payable (msg.sender);
    }```

2 Answers 2

0

This resource tells you when, why, and what if you don’t use the payable modifier:

https://cryptomarketpool.com/payable-modifier-in-solidity-smart-contracts/

0

As described in the solidity docs on the address type, a payable address can do everything an address can do, in addition to having access to the .transfer(...) and .send(...) functions. So you might declare a state variable as address payable instead of just address if you explicitly want to indicate that it's allowed to receive ether from the contract, and want to cast it once instead of every time you need to call those functions.

If owner is a state variable that's declared as a payable address like address payable public owner;, the solidity wouldn't compile if you attempt to directly assign an address like msg.sender to it without casting it:

TypeError: Type address is not implicitly convertible to expected type address payable

But if owner was declared as a basic address like address public owner; you could just as easily cast the address to payable if and when you need it:

payable(owner).transfer(...);

Also, since .call{value: ...}('') is currently the recommended way to send value instead of .transfer and .send and can be used with a plain address, there doesn't appear to be much value in marking an address as payable beyond perhaps for indicating intent for readability.

Note that this is a different concern from marking a function with the payable modifier like function fund() external payable, which makes it so that a function is allowed to receive ether from a user or another contract when it's called, which gets added to the balance of the contract and is visible under msg.value.

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.