0

I know there is a lot of threads on this but i did search and couldn't find the answer. Hoping someone can help me better understand Approve and transferFrom with ERC20 tokens.

If I want to approve Alice to spend my ERC20 tokens because ultimately i want my tokens to go to Bob, then the "spender address" in the approve function is Alice right? I am approving her to send my tokens to Bob so her address goes in the approve function?

Now, when i call transferFrom..... the "from" address is me(msg.sender), the "to" address is Alice (the person i approved), and then i put the amount. Still correct?

Lastly I still need to get my tokens to Bob so if I call a function that accepts my tokens and when i put the "from address" in this function is it going to be my address(msg.sender) or Alice (since she was the one who I approved)?

Or do i have some things wrong here? Thanks in advance for anyone who responds and helps.

4 Answers 4

0

As described in the ERC-20 standard:

  • The approve(address _spender, uint256 _value) method allows the spender _spender, in this case Alice, to withdraw an amount _value from your account. You would be the sender of this transaction.
  • The transferFrom(address _from, address _to, uint256 _value) method transfers an amount _value from an account _from to another account _to. In this case, from your account to Bob. This transaction would revert if the _from account hadn't previously approved the sender of this transaction. Alice would be the sender of this transaction.

Summarizing, you must send a transaction that approves Alice to spend your tokens, and then she can transfer those tokens freely from your account to Bob's.

1
  • Thank you this was a great explanation and finally understand these functions better. I was also able to get one step further in this challenge that i have been struggling with for days now that i understand the approve and transferFrom better. I have one last piece of this challenge to figure out that i am still stuck on. if you are up for a challenge and can help would be awesome. Here it is... ethereum.stackexchange.com/questions/145459/…
    – Blockpain
    Commented Feb 23, 2023 at 12:30
0

If I want to approve Alice to spend my ERC20 tokens because ultimately i want my tokens to go to Bob, then the "spender address" in the approve function is Alice right? I am approving her to send my tokens to Bob so her address goes in the approve function?

You are right.

Now, when i call transferFrom..... the "from" address is me(msg.sender), the "to" address is Alice (the person i approved), and then i put the amount. Still correct?

No, you are wrong here: transferFrom function will be called by Alice, the "from" address is you and "to" address is Bob.

The end of the process.

SUMMARY There are only 2 processes involved:

1 - Owner of tokens (you) approves the spender (Alice), to spend an amount of tokens with the function approve(spender, amount)

approve(address spender, uint256 amount) → bool

2- Spender (Alice) transfers, an amount of tokens, from you with the function transferFrom(from, to, amount)

transferFrom(address from, address to, uint256 amount) → bool
1
  • Thank you this was a great explanation and finally understand these functions better. I was also able to get one step further in this challenge that i have been struggling with for days now that i understand the approve and transferFrom better. I have one last piece of this challenge to figure out that i am still stuck on. if you are up for a challenge and can help would be awesome. Here it is... ethereum.stackexchange.com/questions/145459/…
    – Blockpain
    Commented Feb 23, 2023 at 12:25
0

You must have encountered token approve process during using defi like uniswap, pancakswap etc. Eg. When you want to exchange your USDC or USDT to ETH you first have to approve the USDT to uniswap router. This means you are giving permission to uniswap router to spend your USDC in anyway the router want to spend. Because of the approve and transferFrom function of ERC20 standard the defi exist otherwise we would not have defi systems like uniswap, pancakeswap. Approving is like issueing a check to a person.

Eg.

When you approve Alice to spend your USDC then Alice can send USDC on behalf of you to any other person it be BOB or John. In that context Alice will use transferFrom function like transferFrom(yourAddress, BobAddress, amountOfUSDC) or transferFrom(yourAddress, JohnAddress, amountOfUSDC) only if you have approved Alice to spend using approve function like approve(AliceAddress, AmountUSDC).

1
  • Thank you this was a great explanation and finally understand these functions better. I was also able to get one step further in this challenge that i have been struggling with for days now that i understand the approve & transferFrom better. I think you helped me with something else in this on this so thanks. I have one last piece of this challenge to figure out that i am still stuck on. if you are up for a challenge and can help would be awesome. Here it is... ethereum.stackexchange.com/questions/145459/…
    – Blockpain
    Commented Feb 23, 2023 at 12:28
0

Don't think it is complex!! "Approve" explains that, the token details you've incorporated in your smart contract will be verified for avoiding duplication and errors and it will be finally approved.

On the other hand, "Transfer from", is the term that explains where the tokens have to be transferred. To be simple, where the tokens should be shifted from which address to which address. Hope you got an idea!!

1
  • Thank you this was a great explanation and finally understand these functions better. I was also able to get one step further in this challenge that i have been struggling with for days now that i understand the approve and transferFrom better. I have one last piece of this challenge to figure out that i am still stuck on. if you are up for a challenge and can help would be awesome. Here it is... ethereum.stackexchange.com/questions/145459/…
    – Blockpain
    Commented Feb 23, 2023 at 12: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.