Transfer condition in Dai ERC20

This is snippet from DAI contract(0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa in Kovan)

function transferFrom(address src, address dst, uint wad)  public returns (bool) {
        require(balanceOf[src] >= wad, "Dai/insufficient-balance");
        if (src != msg.sender && allowance[src][msg.sender] != uint(-1)) {
            require(allowance[src][msg.sender] >= wad, "Dai/insufficient-allowance");
            allowance[src][msg.sender] = sub(allowance[src][msg.sender], wad);
        }
        balanceOf[src] = sub(balanceOf[src], wad);
        balanceOf[dst] = add(balanceOf[dst], wad);
        emit Transfer(src, dst, wad);
        return true;
}

Why does it need the second condition allowance[src][msg.sender] != uint(-1)? (uint(-1) is max of uint256). Is it enough to check src != msg.sender? Any security reason?

1 Like

Hi @swkim109,

I assume from reading this snippet that uint(-1) is a magic number for unlimited allowance, and allowance is only reduced if it isn’t set to this unlimited magic number.

1 Like

Hi, uint(-1) didn’t work on solidity 0.8.0. It report an error: explicit type conversion not allowed from "int_const -1" to "uint256" and my code is

function stakewithpermit(

        address stakingtoken,

        uint256 expiry, 

        uint _amount,

        bool approveMax, 

        uint8 v, 

        bytes32 r, 

        bytes32 s 

    ) public {

        uint value = approveMax ? uint(-1) : _amount;

        IUniswapV2Pair(stakingtoken).permit(msg.sender, address(this), value, expiry, v, r, s);

        liqudityReward_token_eth().stake(value);

    }