draft-ERC20TemporaryApproval.sol 4.9 KB

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