draft-ERC20Permit.sol 2.8 KB

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