5

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?

1 Answer 1

4

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. Commented May 28, 2021 at 19:05
  • @LauriPeltonen It is a little odd but is isn't surprising because solidity developers are usually cautious about optimizations.
    – Ismael
    Commented May 28, 2021 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.