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
impl OrderMessageBuilder
Sourcepub fn new(
http_client: DydxHttpClient,
wallet_address: String,
subaccount_number: u32,
block_time_monitor: Arc<BlockTimeMonitor>,
) -> Self
pub fn new( http_client: DydxHttpClient, wallet_address: String, subaccount_number: u32, block_time_monitor: Arc<BlockTimeMonitor>, ) -> Self
Creates a new order message builder.
Sourcepub fn max_short_term_secs(&self) -> f64
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.
Sourcepub fn get_order_lifetime(&self, params: &LimitOrderParams) -> OrderLifetime
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.
Sourcepub fn is_short_term_order(&self, params: &LimitOrderParams) -> bool
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)
Sourcepub fn is_short_term_cancel(
&self,
time_in_force: TimeInForce,
expire_time_ns: Option<UnixNanos>,
) -> bool
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.
Sourcepub 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>
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.
Sourcepub 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>
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.
Sourcepub fn build_limit_order_from_params(
&self,
params: &LimitOrderParams,
block_height: u32,
) -> Result<Any, DydxError>
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.
Sourcepub fn build_limit_orders_batch(
&self,
orders: &[LimitOrderParams],
block_height: u32,
) -> Result<Vec<Any>, DydxError>
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.
Sourcepub 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>
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.
Sourcepub fn build_cancel_order_with_flags(
&self,
instrument_id: InstrumentId,
client_order_id: u32,
order_flags: u32,
block_height: u32,
) -> Result<Any, DydxError>
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.
Sourcepub fn build_cancel_orders_batch(
&self,
orders: &[(InstrumentId, u32, TimeInForce, Option<UnixNanos>)],
block_height: u32,
) -> Result<Vec<Any>, DydxError>
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.
Sourcepub fn build_cancel_orders_batch_with_flags(
&self,
orders: &[(InstrumentId, u32, u32)],
block_height: u32,
) -> Result<Vec<Any>, DydxError>
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.
Sourcepub 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>
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 orderold_client_order_id- Client ID of the order to cancelnew_client_order_id- Client ID for the replacement orderold_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 orderblock_height- Current block height for short-term orders
§Errors
Returns an error if cancellation or replacement order fails to build.
Sourcepub 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>
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.
Sourcepub 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>
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.
Sourcepub 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>
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>
Sourcepub 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>
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>
Sourcepub 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>
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.
Sourcepub 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>
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>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for OrderMessageBuilder
impl !RefUnwindSafe for OrderMessageBuilder
impl Send for OrderMessageBuilder
impl Sync for OrderMessageBuilder
impl Unpin for OrderMessageBuilder
impl UnsafeUnpin for OrderMessageBuilder
impl !UnwindSafe for OrderMessageBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].