Skip to main content

OrderMessageBuilder

Struct OrderMessageBuilder 

Source
pub struct OrderMessageBuilder { /* private fields */ }
Expand description

Builds dYdX proto messages from Nautilus orders.

§Responsibilities

  • Convert Nautilus order types to dYdX protocol messages
  • Determine short-term vs long-term routing via OrderLifetime
  • Handle price/quantity quantization via OrderMarketParams
  • Use dynamic block time estimation from BlockTimeMonitor

§Does NOT Handle

  • Sequence management (handled by TransactionManager)
  • Transaction signing (handled by TransactionManager)
  • Broadcasting (handled by TxBroadcaster)

Implementations§

Source§

impl OrderMessageBuilder

Source

pub fn new( http_client: DydxHttpClient, wallet_address: String, subaccount_number: u32, block_time_monitor: Arc<BlockTimeMonitor>, ) -> Self

Creates a new order message builder.

Source

pub fn max_short_term_secs(&self) -> f64

Returns the maximum duration (in seconds) for short-term orders.

Computed as: SHORT_TERM_ORDER_MAXIMUM_LIFETIME (20 blocks) × seconds_per_block

Uses dynamic block time from BlockTimeMonitor when available, falling back to 500ms/block when insufficient samples.

Source

pub fn get_order_lifetime(&self, params: &LimitOrderParams) -> OrderLifetime

Determines the order lifetime for given parameters.

Uses dynamic block time from BlockTimeMonitor to determine if an order fits within the short-term window (20 blocks × seconds_per_block).

§Important for Batching

dYdX protocol restriction: Short-term orders cannot be batched - each must be submitted in its own transaction. Only long-term orders can be batched. Use this method to check before attempting to batch multiple orders.

Source

pub fn is_short_term_order(&self, params: &LimitOrderParams) -> bool

Checks if an order will be submitted as short-term.

Short-term orders have protocol restrictions:

  • Cannot be batched (one MsgPlaceOrder per transaction)
  • Lower latency and fees
  • Expire by block height (max 20 blocks)
Source

pub fn is_short_term_cancel( &self, time_in_force: TimeInForce, expire_time_ns: Option<UnixNanos>, ) -> bool

Checks if a cancellation will be short-term based on the order’s properties.

Short-term cancellations have the same protocol restrictions as short-term placements:

  • Cannot be batched (one MsgCancelOrder per transaction)

The cancel must use the same lifetime as the original order placement.

Source

pub fn build_market_order( &self, instrument_id: InstrumentId, client_order_id: u32, client_metadata: u32, side: OrderSide, quantity: Quantity, block_height: u32, ) -> Result<Any, DydxError>

Builds a MsgPlaceOrder for a market order.

Market orders are always short-term and execute immediately at the best available price.

§Errors

Returns an error if market parameters cannot be retrieved or order building fails.

Source

pub fn build_limit_order( &self, instrument_id: InstrumentId, client_order_id: u32, client_metadata: u32, side: OrderSide, price: Price, quantity: Quantity, time_in_force: TimeInForce, post_only: bool, reduce_only: bool, block_height: u32, expire_time: Option<i64>, ) -> Result<Any, DydxError>

Builds a MsgPlaceOrder for a limit order.

Automatically routes to short-term or long-term based on time_in_force and expire_time.

§Errors

Returns an error if market parameters cannot be retrieved or order building fails.

Source

pub fn build_limit_order_from_params( &self, params: &LimitOrderParams, block_height: u32, ) -> Result<Any, DydxError>

Builds a MsgPlaceOrder for a limit order from LimitOrderParams.

§Errors

Returns an error if market parameters cannot be retrieved or order building fails.

Source

pub fn build_limit_orders_batch( &self, orders: &[LimitOrderParams], block_height: u32, ) -> Result<Vec<Any>, DydxError>

Builds a batch of MsgPlaceOrder messages for limit orders.

§Errors

Returns an error if any order fails to build.

Source

pub fn build_cancel_order( &self, instrument_id: InstrumentId, client_order_id: u32, time_in_force: TimeInForce, expire_time_ns: Option<UnixNanos>, block_height: u32, ) -> Result<Any, DydxError>

Builds a MsgCancelOrder message.

Automatically routes to short-term or long-term cancellation based on the order’s lifetime. Accepts raw nanoseconds and applies default_short_term_expiry_secs if configured.

§Errors

Returns an error if market parameters cannot be retrieved or order building fails.

Source

pub fn build_cancel_order_with_flags( &self, instrument_id: InstrumentId, client_order_id: u32, order_flags: u32, block_height: u32, ) -> Result<Any, DydxError>

Builds a MsgCancelOrder message with explicit order_flags.

Use this method when you have the original order_flags stored (e.g., from OrderContext). This avoids re-deriving the order type which can be incorrect for expired orders.

§Errors

Returns an error if market parameters cannot be retrieved.

Source

pub fn build_cancel_orders_batch( &self, orders: &[(InstrumentId, u32, TimeInForce, Option<UnixNanos>)], block_height: u32, ) -> Result<Vec<Any>, DydxError>

Builds a batch of MsgCancelOrder messages.

Each tuple contains: (instrument_id, client_order_id, time_in_force, expire_time_ns)

§Errors

Returns an error if any cancellation fails to build.

Source

pub fn build_cancel_orders_batch_with_flags( &self, orders: &[(InstrumentId, u32, u32)], block_height: u32, ) -> Result<Vec<Any>, DydxError>

Builds a batch of MsgCancelOrder messages with explicit order_flags.

Each tuple contains: (instrument_id, client_order_id, order_flags) Use this method when you have stored order_flags from OrderContext.

§Errors

Returns an error if any cancellation fails to build.

Source

pub fn build_cancel_and_replace( &self, instrument_id: InstrumentId, old_client_order_id: u32, _new_client_order_id: u32, old_time_in_force: TimeInForce, old_expire_time_ns: Option<UnixNanos>, new_params: &LimitOrderParams, block_height: u32, ) -> Result<Vec<Any>, DydxError>

Builds a cancel-and-replace batch for order modification.

Returns [MsgCancelOrder, MsgPlaceOrder] as a single atomic transaction. This eliminates race conditions when modifying orders by combining both operations into one transaction with a single sequence number.

Accepts raw nanoseconds for expire times and applies default_short_term_expiry_secs if configured (consistent with placement routing).

§Arguments
  • instrument_id - The instrument for both cancel and new order
  • old_client_order_id - Client ID of the order to cancel
  • new_client_order_id - Client ID for the replacement order
  • old_time_in_force - TimeInForce of the original order (for cancel routing)
  • old_expire_time_ns - Expire time of the original order in nanoseconds (for cancel routing)
  • new_params - Parameters for the replacement limit order
  • block_height - Current block height for short-term orders
§Errors

Returns an error if cancellation or replacement order fails to build.

Source

pub fn build_cancel_and_replace_with_flags( &self, instrument_id: InstrumentId, old_client_order_id: u32, old_order_flags: u32, new_params: &LimitOrderParams, block_height: u32, ) -> Result<Vec<Any>, DydxError>

Builds a cancel-and-replace batch with explicit order_flags for cancellation.

Use this method when you have stored order_flags from OrderContext.

§Errors

Returns an error if cancellation or replacement order fails to build.

Source

pub fn build_conditional_order( &self, instrument_id: InstrumentId, client_order_id: u32, client_metadata: u32, order_type: ConditionalOrderType, side: OrderSide, trigger_price: Price, limit_price: Option<Price>, quantity: Quantity, time_in_force: Option<TimeInForce>, post_only: bool, reduce_only: bool, expire_time: Option<i64>, ) -> Result<Any, DydxError>

Builds a MsgPlaceOrder for a conditional order (stop or take-profit).

Conditional orders are always stored on-chain (long-term/stateful).

§Errors

Returns an error if market parameters cannot be retrieved or order building fails.

Source

pub fn build_stop_market_order( &self, instrument_id: InstrumentId, client_order_id: u32, client_metadata: u32, side: OrderSide, trigger_price: Price, quantity: Quantity, reduce_only: bool, expire_time: Option<i64>, ) -> Result<Any, DydxError>

Builds a stop market order.

§Errors

Returns an error if the conditional order fails to build.

Source

pub fn build_stop_limit_order( &self, instrument_id: InstrumentId, client_order_id: u32, client_metadata: u32, side: OrderSide, trigger_price: Price, limit_price: Price, quantity: Quantity, time_in_force: TimeInForce, post_only: bool, reduce_only: bool, expire_time: Option<i64>, ) -> Result<Any, DydxError>

Builds a stop limit order.

§Errors

Returns an error if the conditional order fails to build.

Source

pub fn build_take_profit_market_order( &self, instrument_id: InstrumentId, client_order_id: u32, client_metadata: u32, side: OrderSide, trigger_price: Price, quantity: Quantity, reduce_only: bool, expire_time: Option<i64>, ) -> Result<Any, DydxError>

Builds a take profit market order.

§Errors

Returns an error if the conditional order fails to build.

Source

pub fn build_take_profit_limit_order( &self, instrument_id: InstrumentId, client_order_id: u32, client_metadata: u32, side: OrderSide, trigger_price: Price, limit_price: Price, quantity: Quantity, time_in_force: TimeInForce, post_only: bool, reduce_only: bool, expire_time: Option<i64>, ) -> Result<Any, DydxError>

Builds a take profit limit order.

§Errors

Returns an error if the conditional order fails to build.

Trait Implementations§

Source§

impl Debug for OrderMessageBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Ungil for T
where T: Send,