draft-ERC20PermitUpgradeable.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/draft-ERC20Permit.sol)
  3. pragma solidity ^0.8.0;
  4. import "./draft-IERC20PermitUpgradeable.sol";
  5. import "../ERC20Upgradeable.sol";
  6. import "../../../utils/cryptography/draft-EIP712Upgradeable.sol";
  7. import "../../../utils/cryptography/ECDSAUpgradeable.sol";
  8. import "../../../utils/CountersUpgradeable.sol";
  9. import "../../../proxy/utils/Initializable.sol";
  10. /**
  11. * @dev Implementation of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
  12. * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
  13. *
  14. * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
  15. * presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
  16. * need to send a transaction, and thus is not required to hold Ether at all.
  17. *
  18. * _Available since v3.4._
  19. */
  20. abstract contract ERC20PermitUpgradeable is Initializable, ERC20Upgradeable, IERC20PermitUpgradeable, EIP712Upgradeable {
  21. using CountersUpgradeable for CountersUpgradeable.Counter;
  22. mapping(address => CountersUpgradeable.Counter) private _nonces;
  23. // solhint-disable-next-line var-name-mixedcase
  24. bytes32 private _PERMIT_TYPEHASH;
  25. /**
  26. * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
  27. *
  28. * It's a good idea to use the same `name` that is defined as the ERC20 token name.
  29. */
  30. function __ERC20Permit_init(string memory name) internal onlyInitializing {
  31. __EIP712_init_unchained(name, "1");
  32. __ERC20Permit_init_unchained(name);
  33. }
  34. function __ERC20Permit_init_unchained(string memory) internal onlyInitializing {
  35. _PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");}
  36. /**
  37. * @dev See {IERC20Permit-permit}.
  38. */
  39. function permit(
  40. address owner,
  41. address spender,
  42. uint256 value,
  43. uint256 deadline,
  44. uint8 v,
  45. bytes32 r,
  46. bytes32 s
  47. ) public virtual override {
  48. require(block.timestamp <= deadline, "ERC20Permit: expired deadline");
  49. bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
  50. bytes32 hash = _hashTypedDataV4(structHash);
  51. address signer = ECDSAUpgradeable.recover(hash, v, r, s);
  52. require(signer == owner, "ERC20Permit: invalid signature");
  53. _approve(owner, spender, value);
  54. }
  55. /**
  56. * @dev See {IERC20Permit-nonces}.
  57. */
  58. function nonces(address owner) public view virtual override returns (uint256) {
  59. return _nonces[owner].current();
  60. }
  61. /**
  62. * @dev See {IERC20Permit-DOMAIN_SEPARATOR}.
  63. */
  64. // solhint-disable-next-line func-name-mixedcase
  65. function DOMAIN_SEPARATOR() external view override returns (bytes32) {
  66. return _domainSeparatorV4();
  67. }
  68. /**
  69. * @dev "Consume a nonce": return the current value and increment.
  70. *
  71. * _Available since v4.1._
  72. */
  73. function _useNonce(address owner) internal virtual returns (uint256 current) {
  74. CountersUpgradeable.Counter storage nonce = _nonces[owner];
  75. current = nonce.current();
  76. nonce.increment();
  77. }
  78. /**
  79. * This empty reserved space is put in place to allow future versions to add new
  80. * variables without shifting down storage in the inheritance chain.
  81. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  82. */
  83. uint256[49] private __gap;
  84. }