Skip to main content

Module encoder

Module encoder 

Source
Expand description

True bidirectional client order ID encoder for dYdX.

dYdX chain requires u32 client IDs, but Nautilus uses string-based ClientOrderId. This module provides deterministic encoding that:

  • Encodes the full ClientOrderId into (client_id, client_metadata) u32 pair
  • Decodes back to the exact original ClientOrderId string
  • Works across restarts without persisted state
  • Enables reconciliation of orders from previous sessions

§Encoding Scheme

For O-format ClientOrderIds (O-YYYYMMDD-HHMMSS-TTT-SSS-CCC):

  • client_id (32 bits): [trader:10][strategy:10][count:12] - unique per order
  • client_metadata (32 bits): Seconds since base epoch (2020-01-01 00:00:00 UTC)

IMPORTANT: dYdX uses client_id for order identity/deduplication, so the unique part (trader+strategy+count) must be in client_id, not client_metadata.

For numeric ClientOrderIds (e.g., “12345”):

  • client_id: The parsed u32 value
  • client_metadata: DEFAULT_RUST_CLIENT_METADATA (4) - legacy marker

For non-standard formats:

  • Falls back to sequential allocation with in-memory reverse mapping

Structs§

ClientOrderIdEncoder
Manages bidirectional mapping of ClientOrderId ↔ (client_id, client_metadata) for dYdX.
EncodedClientOrderId
Encoded client order ID pair for dYdX.

Enums§

EncoderError
Error type for client order ID encoding operations.

Constants§

DEFAULT_RUST_CLIENT_METADATA
Value used to identify legacy/numeric client IDs. When client_metadata == 4, the client_id is treated as a literal numeric ID.
DYDX_BASE_EPOCH
Base epoch for timestamp encoding: 2020-01-01 00:00:00 UTC. This gives us ~136 years of range with 32-bit seconds.
MAX_SAFE_CLIENT_ID
Maximum safe client order ID value before warning about overflow. Leave room for ~1000 additional orders after reaching this threshold.