Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
if value greater than zero check in ERC20TemporaryApproval
  • Loading branch information
Amxx authored and cairoeth committed Jul 16, 2024
commit 5bb76626d5380af3eb69dbfc24b7d1d4d4cb5bec
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