0
  1. Why Transfer function doesn't check for allowance comparing to TransferFrom?
  2. Do I need to call increaseAllowance each time before calling TransferFrom?

Ex implementation of Transfer without allowance check

    function transfer(address receiver, uint256 numTokens) public override returns (bool) {
        require(numTokens <= balances[msg.sender]);
        balances[msg.sender] = balances[msg.sender].sub(numTokens);
        balances[receiver] = balances[receiver].add(numTokens);
        emit Transfer(msg.sender, receiver, numTokens);
        return true;
    }

1 Answer 1

3
  1. Using account A in order to execute transferFrom(source, target, amount) requires both:
    • source holds at least amount of the given token
    • A has been approved by source to transfer from it at least amount of the given token
  2. Using account A in order to execute transfer(target, amount) requires only:
    • A holds at least amount of the given token
  3. Your question is essentially, why doesn't it also require:
    • A has been approved by A to transfer from it at least amount of the given token?
  4. And the answer is obviously:
    • Because it is pointless to approve yourself to do something (anything really)

This is true regardless of whether A is an externally-owned account or a smart-contract account.

3
  • If I understand you correctly, transferFrom is called from another contract and instead of calling a.approve(sender, amount) I need to call sender.approve(a, amount) Dec 17, 2020 at 13:03
  • 1
    @DmitryDyachkov: Precisely. And with sender typically being an externally-owned account, sender.approve needs to be executed from the off-chain (e.g., in a web3.js script). Dec 17, 2020 at 13:30
  • Adding onto #4 – even if you own the tokens, if you call transferFrom without explicitly approving yourself first, the transaction will fail and revert. It is always better to use transfer in this case.
    – P A S H
    Sep 7, 2022 at 23:54

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.