4

This is copied from their ERC20 contract from github (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol):

function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
    _approve(_msgSender(), spender, _allowances[_msgSender()] + addedValue);
    return true;
}

function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
    uint256 currentAllowance = _allowances[_msgSender()][spender];
    require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
    unchecked {
        _approve(_msgSender(), spender, currentAllowance - subtractedValue);
    }
return true;
}

Why would they not be implemented the same way?

3

In PR #2669 where those changes were made they mention Issue #2665. In that issue a comment by vladyan18 points that using unchecked reduces gas usage at deployment by 1723.

2
  • Thanks for the info. I think the other ideas that switching the position of a condition check conditions reduces gas cost are interesting. May 28 at 19:05
  • @LauriPeltonen It is a little odd but is isn't surprising because solidity developers are usually cautious about optimizations.
    – Ismael
    May 28 at 20:03

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.