Skip to main content

Overview

The NonceManager mixin maintains a mapping of account nonces used to determine order validity and enable order cancellation. An order is only valid if the nonce included in the signed order matches the signer’s current nonce value. Users can cancel orders by incrementing their nonce, which invalidates all orders signed with previous nonce values. Source: src/exchange/mixins/NonceManager.sol
Nonces can only increase. If an account sets their nonce to the maximum uint256 value, they will no longer be able to cancel orders via nonce increments.

State Variables

nonces

mapping(address => uint256) public nonces
Mapping of account addresses to their current nonce values.

Functions

incrementNonce

function incrementNonce() external override
Increments the caller’s nonce by 1. This invalidates all previously signed orders that used the old nonce value. The function uses msg.sender to determine which account’s nonce to increment.

updateNonce

function updateNonce(uint256 val) internal
Internal function that updates the caller’s nonce by adding a specific value to their current nonce.

Parameters

  • val (uint256): The value to add to the user’s current nonce
This is an internal function. Use incrementNonce() to increment by 1.

isValidNonce

function isValidNonce(address usr, uint256 nonce) public view override returns (bool)
Checks whether a specified nonce matches a user’s current nonce stored in the nonces mapping.

Parameters

  • usr (address): The account to check the nonce for
  • nonce (uint256): The nonce value to compare against the stored value

Returns

  • bool: true if the supplied nonce matches the user’s current nonce, false otherwise

Usage

When a user wants to cancel their outstanding orders, they can call incrementNonce(). This will:
  1. Increase their nonce by 1
  2. Invalidate all orders signed with the previous nonce value
  3. Require them to sign new orders with the updated nonce
During order matching, the exchange validates that the order’s nonce matches the signer’s current nonce using isValidNonce().