From what I've gathered (and I'm new to Ethereum), smart contracts can receive ETH payments and hold them through the receive/fallback blocks, but cannot directly hold ERC-20 tokens, but rather they can utilise them if a user calls the allow function on the token's contract. If the above statement is even true, why is that?

Example through a random use case: Say that there's a smart contract that converts certain ERC-20 tokens to ETH through an exchange. Users deposit their ERC-20 tokens and invoke the swap functions. Do the users have to allow the contract to manage the tokens or is there a simpler way to achieve the same effect?

  • Try reading this answer, it might give you a lot of relevant knowledge. – goodvibration Sep 29 '20 at 10:01
  • @goodvibration Excellent, thank you. – Milan Velebit Sep 29 '20 at 10:05

To add to goodvibration's answer, the problem with sending ERC-20 to any contract is that the contract is not aware of that transaction. There's no function that's called when the tokens are received, so it's not possible to assign that balance to a specific sender. This is why things like decentralised exchanges use approve and transferFrom. In this case a contract can transfer user's tokens itself, in which case it knows that the user has that number of tokens.

More recent token standards like ERC-721 (non-fungible tokens), and ERC-777 solve this problem with hooks. An ERC-777 token contract for example calls the receiving contract before the tokens are sent. If the receiving contract does not implement the token receiver hook, the transaction is reverted. This way it's possible to: 1) assign a token balance to a user on transfer and 2) prevent tokens being sent to a contract that does not support it.

I wrote a more in-depth explanation of the different token standards (including an explanation of hooks) here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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