Fallback functions only work when a contract receives ether. Is there a way to write a fallback-style function that is automatically called when a contract receives an ERC20 token?
1 Answer
There is a reference implementation in EIP 677 and also in ERC223
This function must transfer tokens and invoke the function tokenFallback (address, uint256, bytes
) in _to
, if _to
is a contract.
The token fallback function, which will be called at the receiver contract, must be named tokenFallback and take the parameters:(address, uint256, bytes). It’s an analogue of the fallback function for ETH transactions and should be used to handle incoming transactions.