I have a couple of questions relating ERC20...

  1. why is allowance needed ? It would be the same as if instead of approving my 10 tokens to some address to spend, I could directly transfer 10 tokens to that same address. Hence, no allowance necessary and the same result.

  2. I am looking at the following contract... https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol . I don't seem to figure out why transferFrom is written like this. That way, anyone can call the function and pass other 2 addresses, which means that If I call it, I can make a transfer from userA's to userB, even though I am userC and I shouldn't have the permission to do so.

  3. Where can I find the actual ERC20 compliant contract with all the functions(including mint) and with the correct implementation and not the one i linked above ?

  • The general purpose is for the whole token-exchange deal (tokens transferred from your account into the contract, and tokens or ether transferred from the contract to your account) to take place within the same (i.e., atomic) transaction. If you first transfer tokens from your account to the contract (i.e., call a function of the token contract) and then call a function of the contract itself, then the entire exchange deal will span over several transactions, and possibly over several blocks. Dec 5 '20 at 19:10
  • It is only possible to do this exchange in a single transaction, when you transfer ether to the contract (by passing it in msg.value to the contract function). But when transferring tokens, you actually need to call a function in another contract (the token contract). So instead, you call function approve (in the token contract), and then call a function in the contract itself, which performs the token transfer (by calling tranferFrom) on your behalf. Dec 5 '20 at 19:13
  1. The approve/transferFrom business can be very useful when a contract needs to transfer users' funds on their behalf.

  2. This is not correct.

The transferFrom function is :

    function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
    _transfer(sender, recipient, amount);
    _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
    return true;

If the caller is not approved by sender, this line will revert the transaction :

_allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance")

In fact, the sub function throws an error when the result of the substraction is negative.

  1. ERC20 compliant contracts must respect the ERC20 interface by implementing all of the required functions and events. The standard can be found here : https://eips.ethereum.org/EIPS/eip-20. The OpenZeppelin implementation is correct and respects this standard.
  • 2. I am not sure what you said. _transfer function will still be called successfully. I understand that _approve will throw an exception, and everything will revert back to what it was before calling transferForm. Is this what you meant ? Dec 5 '20 at 19:48
  • Yes, when an error is met during the execution of a transaction, the modifications made by the contract are cancelled.
    – clement
    Dec 5 '20 at 19:59

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.