ERC20Permit.sol 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.5 <0.8.0;
  3. import "../token/ERC20/ERC20.sol";
  4. import "./IERC20Permit.sol";
  5. import "../cryptography/ECDSA.sol";
  6. import "../utils/Counters.sol";
  7. import "./EIP712.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. abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 {
  17. using Counters for Counters.Counter;
  18. mapping (address => Counters.Counter) private _nonces;
  19. // solhint-disable-next-line var-name-mixedcase
  20. bytes32 private immutable _PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
  21. /**
  22. * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
  23. *
  24. * It's a good idea to use the same `name` that is defined as the ERC20 token name.
  25. */
  26. constructor(string memory name) internal EIP712(name, "1") {
  27. }
  28. /**
  29. * @dev See {IERC20Permit-permit}.
  30. */
  31. function permit(address owner, address spender, uint256 amount, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public virtual override {
  32. // solhint-disable-next-line not-rely-on-time
  33. require(block.timestamp <= deadline, "ERC20Permit: expired deadline");
  34. bytes32 structHash = keccak256(
  35. abi.encode(
  36. _PERMIT_TYPEHASH,
  37. owner,
  38. spender,
  39. amount,
  40. _nonces[owner].current(),
  41. deadline
  42. )
  43. );
  44. bytes32 hash = _hashTypedDataV4(structHash);
  45. address signer = ECDSA.recover(hash, v, r, s);
  46. require(signer == owner, "ERC20Permit: invalid signature");
  47. _nonces[owner].increment();
  48. _approve(owner, spender, amount);
  49. }
  50. /**
  51. * @dev See {IERC20Permit-nonces}.
  52. */
  53. function nonces(address owner) public view override returns (uint256) {
  54. return _nonces[owner].current();
  55. }
  56. /**
  57. * @dev See {IERC20Permit-DOMAIN_SEPARATOR}.
  58. */
  59. // solhint-disable-next-line func-name-mixedcase
  60. function DOMAIN_SEPARATOR() external view override returns (bytes32) {
  61. return _domainSeparatorV4();
  62. }
  63. }