Nonces.sol 896 B

123456789101112131415161718192021222324252627282930
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.19;
  3. /**
  4. * @dev Provides tracking nonces for addresses. Nonces will only increment.
  5. */
  6. abstract contract Nonces {
  7. mapping(address => uint256) private _nonces;
  8. /**
  9. * @dev Returns an address nonce.
  10. */
  11. function nonces(address owner) public view virtual returns (uint256) {
  12. return _nonces[owner];
  13. }
  14. /**
  15. * @dev Consumes a nonce.
  16. *
  17. * Returns the current value and increments nonce.
  18. */
  19. function _useNonce(address owner) internal virtual returns (uint256) {
  20. // For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be
  21. // decremented or reset. This guarantees that the nonce never overflows.
  22. unchecked {
  23. // It is important to do x++ and not ++x here.
  24. return _nonces[owner]++;
  25. }
  26. }
  27. }