Files
openzeppelin-contracts/contracts/utils/Nonces.sol
Hadrien Croubois 2ee1da12c4 Remove utils/Counters.sol (#4289)
Co-authored-by: Francisco Giordano <fg@frang.io>
2023-05-31 11:40:28 -03:00

31 lines
896 B
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
/**
* @dev Provides tracking nonces for addresses. Nonces will only increment.
*/
abstract contract Nonces {
mapping(address => uint256) private _nonces;
/**
* @dev Returns an address nonce.
*/
function nonces(address owner) public view virtual returns (uint256) {
return _nonces[owner];
}
/**
* @dev Consumes a nonce.
*
* Returns the current value and increments nonce.
*/
function _useNonce(address owner) internal virtual returns (uint256) {
// For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be
// decremented or reset. This guarantees that the nonce never overflows.
unchecked {
// It is important to do x++ and not ++x here.
return _nonces[owner]++;
}
}
}