draft-ERC20Permit.sol 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/extensions/draft-ERC20Permit.sol)
  3. pragma solidity ^0.8.0;
  4. import "./draft-IERC20Permit.sol";
  5. import "../ERC20.sol";
  6. import "../../../utils/cryptography/ECDSA.sol";
  7. import "../../../utils/cryptography/EIP712.sol";
  8. import "../../../utils/Counters.sol";
  9. /**
  10. * @dev Implementation of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
  11. * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
  12. *
  13. * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
  14. * presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
  15. * need to send a transaction, and thus is not required to hold Ether at all.
  16. *
  17. * _Available since v3.4._
  18. */
  19. abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 {
  20. using Counters for Counters.Counter;
  21. mapping(address => Counters.Counter) private _nonces;
  22. // solhint-disable-next-line var-name-mixedcase
  23. bytes32 private constant _PERMIT_TYPEHASH =
  24. keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
  25. /**
  26. * @dev In previous versions `_PERMIT_TYPEHASH` was declared as `immutable`.
  27. * However, to ensure consistency with the upgradeable transpiler, we will continue
  28. * to reserve a slot.
  29. * @custom:oz-renamed-from _PERMIT_TYPEHASH
  30. */
  31. // solhint-disable-next-line var-name-mixedcase
  32. bytes32 private _PERMIT_TYPEHASH_DEPRECATED_SLOT;
  33. /**
  34. * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
  35. *
  36. * It's a good idea to use the same `name` that is defined as the ERC20 token name.
  37. */
  38. constructor(string memory name) EIP712(name, "1") {}
  39. /**
  40. * @dev See {IERC20Permit-permit}.
  41. */
  42. function permit(
  43. address owner,
  44. address spender,
  45. uint256 value,
  46. uint256 deadline,
  47. uint8 v,
  48. bytes32 r,
  49. bytes32 s
  50. ) public virtual override {
  51. require(block.timestamp <= deadline, "ERC20Permit: expired deadline");
  52. bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
  53. bytes32 hash = _hashTypedDataV4(structHash);
  54. address signer = ECDSA.recover(hash, v, r, s);
  55. require(signer == owner, "ERC20Permit: invalid signature");
  56. _approve(owner, spender, value);
  57. }
  58. /**
  59. * @dev See {IERC20Permit-nonces}.
  60. */
  61. function nonces(address owner) public view virtual override returns (uint256) {
  62. return _nonces[owner].current();
  63. }
  64. /**
  65. * @dev See {IERC20Permit-DOMAIN_SEPARATOR}.
  66. */
  67. // solhint-disable-next-line func-name-mixedcase
  68. function DOMAIN_SEPARATOR() external view override returns (bytes32) {
  69. return _domainSeparatorV4();
  70. }
  71. /**
  72. * @dev "Consume a nonce": return the current value and increment.
  73. *
  74. * _Available since v4.1._
  75. */
  76. function _useNonce(address owner) internal virtual returns (uint256 current) {
  77. Counters.Counter storage nonce = _nonces[owner];
  78. current = nonce.current();
  79. nonce.increment();
  80. }
  81. }