When a method is payable
, it's possible to send ether to the contract while calling it, and the function can check the amount sent through msg.value
.
How can I make a function receive something other than ether, such as an ERC20 token?
erc-20payable
When a method is payable
, it's possible to send ether to the contract while calling it, and the function can check the amount sent through msg.value
.
How can I make a function receive something other than ether, such as an ERC20 token?
Best Answer
An ERC20 token is just another contract. The ERC20 standard gives you two functions that work together to help pay a contract:
approve()
andtransferFrom()
.If the token contract is called "token" and the contract you're trying to pay is called "store", the process looks like this:
token.approve(store, amount);
This givesstore
permission to transferamount
of the user's tokens.store.buy();
, which callstoken.transferFrom()
to perform the actual transfer.The
buy()
function might look like this: