2

There is something i do not understand in this file: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol

function transferFrom(address from, address to, uint value) external returns (bool) {
    if (allowance[from][msg.sender] != uint(-1)) {
        allowance[from][msg.sender] = allowance[from][msg.sender].sub(value);
    }
    _transfer(from, to, value);
    return true;
}

What is the goal of the first condition:

if (allowance[from][msg.sender] != uint(-1))

I did not see this condition in other ERC20 implementations and i want to understand

Thanks a lot

0

When using Uniswap you have to grant it permission to transfer tokens on your behalf. (This is part of the standard ERC20 functionality and a common feature of dapps.)

The Uniswap UI gives you two options:

  1. Grant an "infinitely" large allowance so you never have to re-approve again in the future, or;
  2. Grant a specific allowance amount, and re-approve in future when this allowance is used up.

The "infinite" allowance mentioned above isn't literally infinite, but bound by the size of a uint256 (alias: uint) - i.e. a very big number. (Elsewhere in the Uniswap codebase they use MaxUint256.)

What is the goal of the first condition:

if (allowance[from][msg.sender] != uint(-1))

uint(-1) equates to the maximum value that a uint256 integer can hold (so basically MaxUint256). (It's unsigned, so can't hold a negative number, meaning it wraps back around to the other end of the value range.)

As such, this line is checking which of the two options the user used for the initial approval. If the user has not granted an "infinite" allowance, then subtract the current transfer amount from their allowance, otherwise don't reduce their allowance.

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.