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 orderclient_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 valueclient_metadata:DEFAULT_RUST_CLIENT_METADATA(4) - legacy marker
For non-standard formats:
- Falls back to sequential allocation with in-memory reverse mapping
Structs§
- Client
Order IdEncoder - Manages bidirectional mapping of ClientOrderId ↔ (client_id, client_metadata) for dYdX.
- Encoded
Client Order Id - Encoded client order ID pair for dYdX.
Enums§
- Encoder
Error - 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.