2

I am beginner. I understood how approve+transferFrom+allowance work, but I can not understand where they are used. Can you give examples from real life?

1 Answer 1

6

A real life example you might be able to easily relate to is a Decentralized Exchange like EtherDelta.

Token contracts hold a mapping balance that associates an eth address to the amount of tokens they hold.

Say you hold 100 EXAMPLE tokens and you want to trade them on EtherDelta.

If you were to transfer those tokens to the EtherDelta contract by calling the transfer method on the token you would certainly lose them.

The EXAMPLE token would now hold a balance of 100 tokens on ED's contract, but EtherDelta doesn't know anything about who sent those tokens so they can't hold a internal balance of the tokens for your account.

The way to make a correct deposit of your tokens into EtherDelta would be for you to call a method on ED. Let's say it's called deposit. This deposit method on ED's contract which you call when attempting to deposit your tokens there will "take" the tokens from your account into their account and then it will proceed to store internally that they are holding 100 EXAMPLE tokens send by you.

Now, in order for ED's contract to be able to move your tokens from your account to their account, given that the token balance in the EXAMPLE token is made to your account, they can't just use transfer. Remember that it's their contract trying to transfer the token balance and not you.

How do they do it then? That's where approve and transferFrom come into scene.

ED needs to transfer your tokens to them on your behalf. For that their contract will execute EXAMPLE.transferFrom(yourAccount,EDAccount,100). having them call that method within their deposit method will transfer your tokens to them.

Of course, if anyone could just go about calling transferFrom to transfer other people's tokens without their permission ERC20 would not make any sense... In order for ED to be able to successfully execute transferFrom, first you must call approve on the token contract to give ED an allowance over the tokens you want them to extract from you.

Hope it makes sense. The above applies to basically any token transaction a contract needs to do with your tokens.

Utility tokens are supposed to be used as a means to interact with a protocol/platform, etc. Any smart contract that requires you to use said token to do something will inevitably require you to first approve them to use part of your balance and they will need to use transferFrom to make use of it.

1
  • Absolutely Brilliant. What a lot of examples or explanations are missing is a Use Case explained in a step by step fashion. Thanks. What about approveAndCall!
    – Cyberience
    Commented Feb 28, 2019 at 8:30

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.