I was wondering how if(currentAllowance != type(uint256).max)
worked in the following function from OpenZeppelin's ERC-20 template. I understand the purpose of this line -- to make sure there's no overflow from an allowance that exceeds the size of the number contained within uint256
. I also read that type
is a built-in solidity function that returns the unique ID of the interface but still have a few questions:
- Why is
max
being called on the unique ID of the interface? - Where is the
max
function coming from -- I can't find it in either the solidity or ethers documentation. - Why is this check needed? I thought after Solidity version 0.8.0 the compiler now checks for under/overflow?
- Why is type is being called on
uint256
-- isn't this a variable type not an interface?
Thanks.
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}