0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Order Nonces And Duplicate Swap Execution Explained

0
Posted at

An order nonce is a value that lets a contract accept a signed swap once and reject later attempts to execute that same authorization again. If you are comparing nonce designs, the key question is where the contract records that an authorization has already been used. That choice affects parallel orders, cancellation, and gas costs.

What does an order nonce protect?

A signed order is a message a contract can execute later; it is not itself an on-chain transaction. If a solver or relayer can submit the same valid signature repeatedly, the contract needs persistent state to tell whether the authorization is fresh. A nonce supplies an order identifier or sequence number, while the contract’s state makes that identifier single-use.

That is separate from an Ethereum account transaction nonce. The transaction nonce orders transactions sent by an account and prevents the same transaction from being accepted twice. With a relayed swap, the solver may submit the on-chain settlement, so the solver’s transaction nonce says nothing about whether the user’s signed order was already consumed.

For replay protection to work, the order’s nonce must be covered by the signature digest, typically through the signed typed data. The contract then checks the nonce against storage and marks it used as part of execution. If the nonce is omitted from the signed fields, changing it does not create a distinct authorization; if the contract fails to update its used state, signing it does not prevent replay.

How do sequential and unordered nonces differ?

A sequential scheme stores a current counter per owner. An order signed with counter n is valid only when the stored counter is n; execution increments it. A basic wallet starts at 0 and advances by 1 for each accepted order. This is compact and easy to audit, but it serializes orders: if order 12 executes before order 11, order 11 may become invalid even if the user wanted both.

An unordered scheme accepts any unused nonce and records it individually or in a bitmap. A bitmap groups 256 nonce positions into one storage word; a contract can check and set the bit for a given nonce. This permits independent orders to execute in any order and can make invalidation efficient when several nonces share a word. The trade-off is more indexing and state-handling logic, and potentially a storage write for each newly used word.

For example, suppose you sign two limit orders while away. A sequential counter can make the first order stale if the second executes first. With unordered nonces, both remain eligible until each fills, expires, or is cancelled. If your contract must support concurrent orders, unordered nonces are usually the more useful design; if strict order is part of your workflow, sequential counters are simpler.

What happens when an order executes twice?

For a non-partial order, the settlement contract should check that its order identifier is unused, then mark it consumed in the same transaction that performs the swap. Because the state update and token transfers are atomic, a failed settlement reverts both; a second successful attempt sees the consumed marker and reverts. The check must happen before any external call that could re-enter the execution path.

Partial fills need a different state rule. Instead of a simple used flag, the contract tracks cumulative fill against the signed sell or buy amount and rejects any fill that would exceed the order’s limit. Fees must also be prorated consistently. A replayed partial-fill instruction can then only consume the remaining permitted amount; it cannot take the full amount again.

Cancellation is another state transition, not merely a request to stop showing an order in an interface. An off-chain service can remove an order from its book, but an already signed authorization may still be submitted on-chain until its expiry or on-chain invalidation. On-chain cancellation gives stronger finality after it confirms, but costs a transaction and can race with settlement: whichever valid transaction lands first determines the result.

How does this apply when trading through CoW Swap?

CoW Protocol’s signed orders do not use a user-supplied sequential nonce in the familiar wallet-counter sense. The settlement contract derives an order UID from the order digest, owner, and expiry, and records filled amounts against that UID. For a fully filled order, later execution fails; for a partially fillable order, cumulative execution is bounded by the signed amount. The validTo field is a Unix timestamp in seconds, so expiry also limits how long the signature can be used.

If you use cowswap, this distinction matters when comparing a nonce-based order contract with the protocol’s UID and fill accounting: both prevent duplicate value extraction, but they organize order state differently. An API cancellation can update order availability off-chain; on-chain invalidation is the relevant mechanism when you need the signed order itself disabled on-chain. Check the order status and expiry before assuming a cancellation request has already made execution impossible.

One edge case deserves special care in custom contracts: zero sell or buy amounts can break the arithmetic that assumes a meaningful fill boundary. Reject zero-amount orders at creation, and ensure every signed field that changes execution rights—including the nonce, chain/domain context, and expiry—is authenticated. A replay-safe nonce does not repair a signature that can be reused across the wrong wallet or domain.

When choosing an implementation, use this sequence:

  • Use a sequential counter when orders must execute in order and simplicity matters.
  • Use unordered nonces or a bitmap when separate orders must remain valid concurrently.
  • Bind each nonce to the signature, consume it atomically, and track partial fills against signed limits.
  • Use expiry for bounded validity and an on-chain cancellation path for immediate invalidation.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?