Can a payable function in a solidity contract only receive the native 'protocol' token or coin of the chain?
So for example, a payable
function on an ERC-721 contract can only receive ether? The same for a BEP-721 contract with a payable
method – it can only receive BNB?
Like a few others, I'm trying to understand payable
, and whether it is needed in the case of a marketplace that is using another token as its 'unit of currency'.
I believe that in the case of an ERC-20 or BEP-20 token as the 'unit of value or currency' for the market, then payable
isn't required.
Instead we follow an approve and transfer process similar to the one described here…
Make function payable for a specific ERC20 token?
And here…
How to create a Payable function that only accepts a customer token?
Have I understood this correctly?
Best Answer
Yes, the
payable
keyword makes the function capable of receiving the blockchain's native asset - such as Eth or BNB. If you try to send the native asset to the contract and there is nopayable
function to receive it, the transaction will revert.All other asset types (ERC20 tokens, ERC721, ...) are perhaps, technically, not even assets at all. They are just smart contracts which keep record of who owns how many of that contract's assets - simple ledgers. Various other smart contracts then utilize those (non-)assets in various ways, but all of that functionality has to be explicitly written as contract code.
Same goes for moving the assets: the native asset has a data slot in transactions (
value
) where it can be natively moved around. All other asset transfers are just transactions to various smart contracts, asking the contracts to change their ledger values.