draft-ERC20TemporaryApproval.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0-rc.1) (token/ERC20/extensions/draft-ERC20TemporaryApproval.sol)
  3. pragma solidity ^0.8.24;
  4. import {IERC20, ERC20} from "../ERC20.sol";
  5. import {IERC7674} from "../../../interfaces/draft-IERC7674.sol";
  6. import {Math} from "../../../utils/math/Math.sol";
  7. import {SlotDerivation} from "../../../utils/SlotDerivation.sol";
  8. import {TransientSlot} from "../../../utils/TransientSlot.sol";
  9. /**
  10. * @dev Extension of {ERC20} that adds support for temporary allowances following ERC-7674.
  11. *
  12. * WARNING: This is a draft contract. The corresponding ERC is still subject to changes.
  13. *
  14. * _Available since v5.1._
  15. */
  16. abstract contract ERC20TemporaryApproval is ERC20, IERC7674 {
  17. using SlotDerivation for bytes32;
  18. using TransientSlot for bytes32;
  19. using TransientSlot for TransientSlot.Uint256Slot;
  20. // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ERC20_TEMPORARY_APPROVAL_STORAGE")) - 1)) & ~bytes32(uint256(0xff))
  21. bytes32 private constant ERC20_TEMPORARY_APPROVAL_STORAGE =
  22. 0xea2d0e77a01400d0111492b1321103eed560d8fe44b9a7c2410407714583c400;
  23. /**
  24. * @dev {allowance} override that includes the temporary allowance when looking up the current allowance. If
  25. * adding up the persistent and the temporary allowances result in an overflow, type(uint256).max is returned.
  26. */
  27. function allowance(address owner, address spender) public view virtual override(IERC20, ERC20) returns (uint256) {
  28. (bool success, uint256 amount) = Math.tryAdd(
  29. super.allowance(owner, spender),
  30. _temporaryAllowance(owner, spender)
  31. );
  32. return success ? amount : type(uint256).max;
  33. }
  34. /**
  35. * @dev Internal getter for the current temporary allowance that `spender` has over `owner` tokens.
  36. */
  37. function _temporaryAllowance(address owner, address spender) internal view virtual returns (uint256) {
  38. return _temporaryAllowanceSlot(owner, spender).tload();
  39. }
  40. /**
  41. * @dev Alternative to {approve} that sets a `value` amount of tokens as the temporary allowance of `spender` over
  42. * the caller's tokens.
  43. *
  44. * Returns a boolean value indicating whether the operation succeeded.
  45. *
  46. * Requirements:
  47. * - `spender` cannot be the zero address.
  48. *
  49. * Does NOT emit an {Approval} event.
  50. */
  51. function temporaryApprove(address spender, uint256 value) public virtual returns (bool) {
  52. _temporaryApprove(_msgSender(), spender, value);
  53. return true;
  54. }
  55. /**
  56. * @dev Sets `value` as the temporary allowance of `spender` over the `owner` s tokens.
  57. *
  58. * This internal function is equivalent to `temporaryApprove`, and can be used to e.g. set automatic allowances
  59. * for certain subsystems, etc.
  60. *
  61. * Requirements:
  62. * - `owner` cannot be the zero address.
  63. * - `spender` cannot be the zero address.
  64. *
  65. * Does NOT emit an {Approval} event.
  66. */
  67. function _temporaryApprove(address owner, address spender, uint256 value) internal virtual {
  68. if (owner == address(0)) {
  69. revert ERC20InvalidApprover(address(0));
  70. }
  71. if (spender == address(0)) {
  72. revert ERC20InvalidSpender(address(0));
  73. }
  74. _temporaryAllowanceSlot(owner, spender).tstore(value);
  75. }
  76. /**
  77. * @dev {_spendAllowance} override that consumes the temporary allowance (if any) before eventually falling back
  78. * to consuming the persistent allowance.
  79. * NOTE: This function skips calling `super._spendAllowance` if the temporary allowance
  80. * is enough to cover the spending.
  81. */
  82. function _spendAllowance(address owner, address spender, uint256 value) internal virtual override {
  83. // load transient allowance
  84. uint256 currentTemporaryAllowance = _temporaryAllowance(owner, spender);
  85. // Check and update (if needed) the temporary allowance + set remaining value
  86. if (currentTemporaryAllowance > 0) {
  87. // All value is covered by the infinite allowance. nothing left to spend, we can return early
  88. if (currentTemporaryAllowance == type(uint256).max) {
  89. return;
  90. }
  91. // check how much of the value is covered by the transient allowance
  92. uint256 spendTemporaryAllowance = Math.min(currentTemporaryAllowance, value);
  93. unchecked {
  94. // decrease transient allowance accordingly
  95. _temporaryApprove(owner, spender, currentTemporaryAllowance - spendTemporaryAllowance);
  96. // update value necessary
  97. value -= spendTemporaryAllowance;
  98. }
  99. }
  100. // reduce any remaining value from the persistent allowance
  101. if (value > 0) {
  102. super._spendAllowance(owner, spender, value);
  103. }
  104. }
  105. function _temporaryAllowanceSlot(address owner, address spender) private pure returns (TransientSlot.Uint256Slot) {
  106. return ERC20_TEMPORARY_APPROVAL_STORAGE.deriveMapping(owner).deriveMapping(spender).asUint256();
  107. }
  108. }