Skip to content

Commit 9502359

Browse files
committed
add ERC20BaseInternal#_decreaseAllowance function for generic holder
1 parent fdc9012 commit 9502359

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

contracts/token/ERC20/base/ERC20BaseInternal.sol

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ abstract contract ERC20BaseInternal is IERC20BaseInternal {
6868
return true;
6969
}
7070

71+
/**
72+
* @notice decrease spend amount granted by holder to spender
73+
* @param holder address on whose behalf tokens may be spent
74+
* @param spender address whose allowance to decrease
75+
* @param amount quantity by which to decrease allowance
76+
*/
77+
function _decreaseAllowance(
78+
address holder,
79+
address spender,
80+
uint256 amount
81+
) internal {
82+
uint256 allowance = _allowance(holder, spender);
83+
84+
require(allowance >= amount, 'ERC20: insufficient allowance');
85+
86+
unchecked {
87+
_approve(holder, spender, allowance - amount);
88+
}
89+
}
90+
7191
/**
7292
* @notice mint tokens for given account
7393
* @param account recipient of minted tokens
@@ -151,16 +171,7 @@ abstract contract ERC20BaseInternal is IERC20BaseInternal {
151171
address recipient,
152172
uint256 amount
153173
) internal virtual returns (bool) {
154-
uint256 allowance = _allowance(holder, msg.sender);
155-
156-
require(
157-
allowance >= amount,
158-
'ERC20: transfer amount exceeds allowance'
159-
);
160-
161-
unchecked {
162-
_approve(holder, msg.sender, allowance - amount);
163-
}
174+
_decreaseAllowance(holder, msg.sender, amount);
164175

165176
_transfer(holder, recipient, amount);
166177

contracts/token/ERC20/extended/ERC20ExtendedInternal.sol

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ abstract contract ERC20ExtendedInternal is
4747
virtual
4848
returns (bool)
4949
{
50-
uint256 allowance = _allowance(msg.sender, spender);
51-
52-
require(amount <= allowance, 'ERC20Extended: insufficient allowance');
50+
_decreaseAllowance(msg.sender, spender, amount);
5351

54-
unchecked {
55-
return _approve(msg.sender, spender, allowance - amount);
56-
}
52+
return true;
5753
}
5854
}

0 commit comments

Comments
 (0)