1

I want to ask what is the difference between transfer and transferfrom and AND WHEN should i use them in the following examples:

1- My firend wants to give tokens to other friends. what function is he gunna call to send it to another and why(and also if transferfrom dont forget to allowance function).

2- my friends want to send tokens to me vice versa

2

transfer

If a wants to send 10 ERC-20 tokens to b, then a calls transfer(b, 10). If b now checks his balance (e.g. because a informs him that he has just sent him 10 tokens), he will find that it has increased by 10 tokens. If your friends want to send tokens to each other, transfer is the way.

transferFrom

An issue arises when b is a contract (let’s call it c), and it wants to receive the 10 tokens from a and immediately do something in response to receiving those 10 tokens, all within the same Ethereum transaction. This is a very common use case, e.g. if c is a DEX and a is converting 10$ to 9€, c will want to receive from a the 10$ and send back to a the 9€ in one atomic step.

The ERC-20 standard does not specify that a contract c should be notified when it is on the receiving end of a transfer. If a were to call transfer(c, 10), then c’s token balance would increase by 10 as expected… but because c would not actively be notified about the transfer, c would not be able react to it.

Instead, the ERC-20 way for a to transfer tokens to a contract c such that c can react to that transfer, is over two Ethereum transactions, as follows:

  1. First a calls approve(c, 10) on the token contract. This does not transfer any tokens per se, but records internally in the token contract that c is now allowed to transferFrom a to itself up to 10 tokens from a’s balance. It’s as if a writes a personal cheque for 10 tokens to c.
  2. Someone (maybe a, maybe not) calls someCustomAction(a, 10, ...) on the contract c. Inside this function:
    1. c calls transferFrom(a, c, 10) to “cash the cheque”, and then
    2. c instantly does something with those 10 tokens it has just received

More recent token standards like ERC-777 and ERC-721, as well as recent upgrades of specific ERC-20 tokens like DAI and USDC have all devised ways of squashing these two transactions into one. But for a generic ERC-20 token, this 2-step process remains the way.

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.