Skip to content

Commit

Permalink
if value greater than zero check in ERC20TemporaryApproval
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Jun 13, 2024
1 parent b2d4966 commit 17833c7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
16 changes: 7 additions & 9 deletions contracts/token/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,13 @@ abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
* Does not emit an {Approval} event.
*/
function _spendAllowance(address owner, address spender, uint256 value) internal virtual {
if (value > 0) {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
if (currentAllowance < value) {
revert ERC20InsufficientAllowance(spender, currentAllowance, value);
}
unchecked {
_approve(owner, spender, currentAllowance - value, false);
}
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
if (currentAllowance < value) {
revert ERC20InsufficientAllowance(spender, currentAllowance, value);
}
unchecked {
_approve(owner, spender, currentAllowance - value, false);
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions contracts/token/ERC20/extensions/draft-ERC20TemporaryApproval.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,23 @@ abstract contract ERC20TemporaryApproval is ERC20, IERC7674 {

// Check and update (if needed) the temporary allowance + set remaining value
if (currentTemporaryAllowance > 0) {
// All value is covered by the infinite allowance. nothing left to spend, we can return early
if (currentTemporaryAllowance == type(uint256).max) {
// all value is covered by the infinite allowance. nothing left to spend.
value = 0;
} else {
// check how much of the value is covered by the transient allowance
uint256 spendTemporaryAllowance = Math.min(currentTemporaryAllowance, value);
unchecked {
// decrease transient allowance accordingly
_temporaryApprove(owner, spender, currentTemporaryAllowance - spendTemporaryAllowance);
// update value necessary
value -= spendTemporaryAllowance;
}
return;
}
// check how much of the value is covered by the transient allowance
uint256 spendTemporaryAllowance = Math.min(currentTemporaryAllowance, value);
unchecked {
// decrease transient allowance accordingly
_temporaryApprove(owner, spender, currentTemporaryAllowance - spendTemporaryAllowance);
// update value necessary
value -= spendTemporaryAllowance;
}
}
// reduce any remaining value from the persistent allowance
super._spendAllowance(owner, spender, value);
if (value > 0) {
super._spendAllowance(owner, spender, value);
}
}

function _temporaryAllowanceSlot(
Expand Down

0 comments on commit 17833c7

Please sign in to comment.