When i tried to use the transfer() i get the error revert ERC20: transfer amount exceeds balance but when i use transferFrom() with little changes, i don’t get the error. What could be the issue, please?
Attached is a screenshot to show both implementations.
When an ERC20 token holder interacts with another contract using the token, two transactions are required:
The token holder calls approve to set an allowance of tokens that the contract can use. (assuming an OpenZeppelin ERC20 implementation can use increaseAllowance)
The token holder calls the contract to perform an action and the contract can transferFrom an amount of tokens within the set allowance.
This is why the transferFrom case works.
transfer moves tokens from the caller's account.
This is why this fails as your contract token balance is less than what you are trying to transfer, as the caller's account is your contract and not the token holder (end user account initiating the transaction).
@abcoathup Aha, i see my silly mistake.
Thanks for the detailed explanation.
One more thing:
Now that both operations are working, is/are there any edge(s) that transferFrom has over transfer and vice versa looking at it from a security perspective or in any other perspective you might think?
As far as security, smart contracts should be appropriately tested and audited. For testing, see the following guide: Test smart contracts like a rockstar