0

I've been checking out the OpenZeppelin ERC20 documentation and I noticed that there are two ways to transfer ERC20 tokens:

  1. using transfer(address recipient, uint256 amount) → bool
  2. combining approve(address spender, uint256 amount) → bool with transferFrom(address sender, address recipient, uint256 amount) → bool

What's the difference? Why use one method vs another?

I'm building a dapp that will let users invest some of their tokens into a de-fi yield protocol and I noticed that they implemented Method 2 instead and I wasn't sure of their rationale.

Disclaimer: I'm a noob to a smart contract development so am trying to improve my understanding.

1 Answer 1

1

If you need to transfer tokens from your address to another, use transfer(), if you need to transfer tokens from an address (that is not you) to another use transferFrom. For example, if your function needed to take amount of tokens from the caller you'd do

IERC20(token).transferFrom(msg.sender, address(this), amount);

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.