I've been checking out the OpenZeppelin ERC20 documentation and I noticed that there are two ways to transfer ERC20 tokens:
- using
transfer(address recipient, uint256 amount) → bool
- combining
approve(address spender, uint256 amount) → bool
withtransferFrom(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.