dydx 0.3.0

dYdX v4 asynchronous client.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
use crate::{
    indexer::{
        ClientId, ClobPairId, Height, OrderExecution, OrderFlags, OrderType, PerpetualMarket,
        Price, Quantity, Subaccount,
    },
    node::Address,
};
use anyhow::{anyhow as err, Error};
use bigdecimal::{num_traits::cast::ToPrimitive, BigDecimal, One};
use chrono::{DateTime, Utc};
use derive_more::From;
pub use dydx_proto::dydxprotocol::clob::{
    order::{Side as OrderSide, TimeInForce as OrderTimeInForce},
    OrderId,
};
use dydx_proto::dydxprotocol::{
    clob::{
        msg_cancel_order,
        order::{self, ConditionType},
        BuilderCodeParameters, Order, TwapParameters,
    },
    subaccounts::SubaccountId,
};

/// Maximum short-term orders lifetime.
///
/// See also [short-term vs long-term orders](https://help.dydx.trade/en/articles/166985-short-term-vs-long-term-order-types).
pub const SHORT_TERM_ORDER_MAXIMUM_LIFETIME: u32 = 20;

/// Value used to identify the Rust client.
pub const DEFAULT_RUST_CLIENT_METADATA: u32 = 4;

/// Order [expirations](https://docs.dydx.xyz/concepts/trading/orders#comparison).
#[derive(From, Clone, Debug)]
pub enum OrderGoodUntil {
    /// Block expiratin is used for short-term orders.
    Block(Height),
    /// Time expiratin is used for long-term orders.
    Time(DateTime<Utc>),
}

impl TryFrom<OrderGoodUntil> for order::GoodTilOneof {
    type Error = Error;
    fn try_from(until: OrderGoodUntil) -> Result<Self, Error> {
        match until {
            OrderGoodUntil::Block(height) => Ok(Self::GoodTilBlock(height.0)),
            OrderGoodUntil::Time(time) => Ok(Self::GoodTilBlockTime(time.timestamp().try_into()?)),
        }
    }
}

impl TryFrom<OrderGoodUntil> for msg_cancel_order::GoodTilOneof {
    type Error = Error;
    fn try_from(until: OrderGoodUntil) -> Result<Self, Error> {
        match until {
            OrderGoodUntil::Block(height) => Ok(Self::GoodTilBlock(height.0)),
            OrderGoodUntil::Time(time) => Ok(Self::GoodTilBlockTime(time.timestamp().try_into()?)),
        }
    }
}

/// Market parameters required to perform price and size quantizations.
/// These quantizations are required for `Order` placement.
///
/// See also [how to interpret block data for trades](https://docs.dydx.exchange/api_integration-guides/how_to_interpret_block_data_for_trades).
#[derive(Clone, Debug)]
pub struct OrderMarketParams {
    /// Atomic resolution
    pub atomic_resolution: i32,
    /// Clob pair id.
    pub clob_pair_id: ClobPairId,
    /// Oracle price.
    pub oracle_price: Option<Price>,
    /// Quantum conversion exponent.
    pub quantum_conversion_exponent: i32,
    /// Step base quantums.
    pub step_base_quantums: u64,
    /// Subticks per tick.
    pub subticks_per_tick: u32,
}

impl OrderMarketParams {
    /// Convert price into subticks.
    pub fn quantize_price(&self, price: impl Into<Price>) -> BigDecimal {
        const QUOTE_QUANTUMS_ATOMIC_RESOLUTION: i32 = -6;
        let scale = -(self.atomic_resolution
            - self.quantum_conversion_exponent
            - QUOTE_QUANTUMS_ATOMIC_RESOLUTION);
        let factor = BigDecimal::new(One::one(), scale.into());
        let raw_subticks = price.into().0 * factor;
        let subticks_per_tick = BigDecimal::from(self.subticks_per_tick);
        let quantums = quantize(&raw_subticks, &subticks_per_tick);
        quantums.max(subticks_per_tick)
    }

    /// Convert decimal into quantums.
    pub fn quantize_quantity(&self, quantity: impl Into<Quantity>) -> BigDecimal {
        let factor = BigDecimal::new(One::one(), self.atomic_resolution.into());
        let raw_quantums = quantity.into().0 * factor;
        let step_base_quantums = BigDecimal::from(self.step_base_quantums);
        let quantums = quantize(&raw_quantums, &step_base_quantums);
        quantums.max(step_base_quantums)
    }

    /// Convert subticks into decimal.
    pub fn dequantize_subticks(&self, subticks: impl Into<BigDecimal>) -> BigDecimal {
        const QUOTE_QUANTUMS_ATOMIC_RESOLUTION: i32 = -6;
        let scale = -(self.atomic_resolution
            - self.quantum_conversion_exponent
            - QUOTE_QUANTUMS_ATOMIC_RESOLUTION);
        let factor = BigDecimal::new(One::one(), scale.into());
        subticks.into() / factor
    }

    /// Convert quantums into decimal.
    pub fn dequantize_quantums(&self, quantums: impl Into<BigDecimal>) -> BigDecimal {
        let factor = BigDecimal::new(One::one(), self.atomic_resolution.into());
        quantums.into() / factor
    }

    /// Get orderbook pair id.
    pub fn clob_pair_id(&self) -> &ClobPairId {
        &self.clob_pair_id
    }
}

/// A `round`-line function that quantize a `value` to the `fraction`.
fn quantize(value: &BigDecimal, fraction: &BigDecimal) -> BigDecimal {
    (value / fraction).round(0) * fraction
}

impl From<PerpetualMarket> for OrderMarketParams {
    fn from(market: PerpetualMarket) -> Self {
        Self {
            atomic_resolution: market.atomic_resolution,
            clob_pair_id: market.clob_pair_id,
            oracle_price: market.oracle_price,
            quantum_conversion_exponent: market.quantum_conversion_exponent,
            step_base_quantums: market.step_base_quantums,
            subticks_per_tick: market.subticks_per_tick,
        }
    }
}

/// [`Order`] builder.
///
/// Note that the price input to the `OrderBuilder` is in the "common" units of the perpetual/currency, not the quantized/atomic value.
///
/// Two main classes of orders in dYdX from persistence perspective are
/// [short-term and long-term (stateful) orders](https://docs.dydx.xyz/concepts/trading/orders#short-term-vs-long-term).
///
/// For different types of orders
/// see also [Stop-Limit Versus Stop-Loss](https://dydx.exchange/crypto-learning/stop-limit-versus-stop-loss) and
/// [Perpetual order types on dYdX Chain](https://help.dydx.trade/en/articles/166981-perpetual-order-types-on-dydx-chain).
#[derive(Clone, Debug)]
pub struct OrderBuilder {
    market_params: OrderMarketParams,
    subaccount_id: SubaccountId,
    flags: OrderFlags,
    side: Option<OrderSide>,
    ty: Option<OrderType>,
    size: Option<Quantity>,
    price: Option<Price>,
    time_in_force: Option<OrderTimeInForce>,
    reduce_only: Option<bool>,
    until: Option<OrderGoodUntil>,
    post_only: Option<bool>,
    execution: Option<OrderExecution>,
    trigger_price: Option<Price>,
    slippage: BigDecimal,
    builder_code_parameters: Option<BuilderCodeParameters>,
    twap_parameters: Option<TwapParameters>,
    order_router_address: Address,
}

impl OrderBuilder {
    /// Create a new [`Order`] builder.
    pub fn new(market_for: impl Into<OrderMarketParams>, subaccount: Subaccount) -> Self {
        Self {
            market_params: market_for.into(),
            subaccount_id: subaccount.into(),
            flags: OrderFlags::ShortTerm,
            side: Some(OrderSide::Buy),
            ty: Some(OrderType::Market),
            size: None,
            price: None,
            time_in_force: None,
            reduce_only: None,
            until: None,
            post_only: None,
            execution: None,
            trigger_price: None,
            slippage: BigDecimal::new(5.into(), 2),
            builder_code_parameters: None,
            twap_parameters: None,
            order_router_address: Address::default(),
        }
    }

    /// Set as Market order.
    ///
    ///  An instruction to immediately buy or sell an asset at the best available price when the order is placed.
    pub fn market(mut self, side: impl Into<OrderSide>, size: impl Into<Quantity>) -> Self {
        self.ty = Some(OrderType::Market);
        self.side = Some(side.into());
        self.size = Some(size.into());
        self
    }

    /// Set as Limit order.
    ///
    /// With a limit order, a trader specifies the price at which they’re willing to buy or sell an asset.
    /// Unlike market orders, limit orders don’t go into effect until the market price hits a trader’s “limit price.”
    pub fn limit(
        mut self,
        side: impl Into<OrderSide>,
        price: impl Into<Price>,
        size: impl Into<Quantity>,
    ) -> Self {
        self.ty = Some(OrderType::Limit);
        self.price = Some(price.into());
        self.side = Some(side.into());
        self.size = Some(size.into());
        self
    }

    /// Set as Stop Limit order
    ///
    /// Stop-limit orders use a stop `trigger_price` and a limit `price` to give investors greater control over their trades.
    /// When setting up a stop-limit order, traders set a `trigger_price` when their order enters the market
    /// and a limit `price` when they want the order to execute.
    pub fn stop_limit(
        mut self,
        side: impl Into<OrderSide>,
        price: impl Into<Price>,
        trigger_price: impl Into<Price>,
        size: impl Into<Quantity>,
    ) -> Self {
        self.ty = Some(OrderType::StopLimit);
        self.price = Some(price.into());
        self.trigger_price = Some(trigger_price.into());
        self.side = Some(side.into());
        self.size = Some(size.into());
        self.conditional()
    }

    /// Set as Stop Market order.
    ///
    /// When using a stop order, the trader sets a `trigger_price` to trigger a buy or sell order on their exchange.
    /// The moment that condition is met, it triggers a market order executed at the current market price.
    /// This means that, unlike limit orders, the execution price of a stop order may be different from the price set by the trader.
    pub fn stop_market(
        mut self,
        side: impl Into<OrderSide>,
        trigger_price: impl Into<Price>,
        size: impl Into<Quantity>,
    ) -> Self {
        self.ty = Some(OrderType::StopMarket);
        self.trigger_price = Some(trigger_price.into());
        self.side = Some(side.into());
        self.size = Some(size.into());
        self.conditional()
    }

    /// Set as Take Profit Limit order.
    ///
    /// The order enters in force if the price reaches `trigger_price` and is executed at `price` after that.
    pub fn take_profit_limit(
        mut self,
        side: impl Into<OrderSide>,
        price: impl Into<Price>,
        trigger_price: impl Into<Price>,
        size: impl Into<Quantity>,
    ) -> Self {
        self.ty = Some(OrderType::TakeProfit);
        self.price = Some(price.into());
        self.trigger_price = Some(trigger_price.into());
        self.side = Some(side.into());
        self.size = Some(size.into());
        self.conditional()
    }

    /// Set as Take Profit Market order.
    ///
    /// The order enters in force if the price reaches `trigger_price` and converts to an ordinary market order,
    /// i.e. it is executed at the best available market price.
    pub fn take_profit_market(
        mut self,
        side: impl Into<OrderSide>,
        trigger_price: impl Into<Price>,
        size: impl Into<Quantity>,
    ) -> Self {
        self.ty = Some(OrderType::TakeProfitMarket);
        self.trigger_price = Some(trigger_price.into());
        self.side = Some(side.into());
        self.size = Some(size.into());
        self.conditional()
    }

    /// Set order as a long-term.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/place_order_long_term.rs).
    pub fn long_term(mut self) -> Self {
        self.flags = OrderFlags::LongTerm;
        self
    }

    /// Set order as a short-term.
    ///
    /// Check [the example](https://github.com/dydxprotocol/v4-clients/blob/main/v4-client-rs/client/examples/place_order_short_term.rs).
    pub fn short_term(mut self) -> Self {
        self.flags = OrderFlags::ShortTerm;
        self
    }

    /// Set order as a conditional, triggered using `trigger_price`.
    pub fn conditional(mut self) -> Self {
        self.flags = OrderFlags::Conditional;
        self
    }

    /* Single setters */
    /// Set the limit price for Limit orders (and related types),
    /// or the up-to allowed price for Market orders (and related types).
    pub fn price(mut self, price: impl Into<Price>) -> Self {
        self.price = Some(price.into());
        self
    }

    /// [Position size](https://dydx.exchange/crypto-learning/glossary?#total-order-size).
    pub fn size(mut self, size: impl Into<Quantity>) -> Self {
        self.size = Some(size.into());
        self
    }

    /// Set [time execution options](https://docs.dydx.xyz/types/time_in_force#time-in-force).
    ///
    /// Basically, it places of the order in the range between being
    /// a Taker order and a Maker order.
    ///
    /// `IOC` (Taker) <---> `Unspecified` (Taker/Maker) <---> `Post` (Maker).
    ///
    /// See also [Market Makers vs Market Takers](https://dydx.exchange/crypto-learning/market-makers-vs-market-takers).
    pub fn time_in_force(mut self, tif: impl Into<OrderTimeInForce>) -> Self {
        self.time_in_force = Some(tif.into());
        self
    }

    /// Set an order as [reduce-only](https://docs.dydx.xyz/concepts/trading/orders#types).
    pub fn reduce_only(mut self, reduce: impl Into<bool>) -> Self {
        self.reduce_only = Some(reduce.into());
        self
    }

    /// Set order's expiration.
    pub fn until(mut self, gtof: impl Into<OrderGoodUntil>) -> Self {
        self.until = Some(gtof.into());
        self
    }

    /// Time execution pattern.
    ///
    /// See also [`OrderBuilder::time_in_force`].
    pub fn execution(mut self, execution: impl Into<OrderExecution>) -> Self {
        self.execution = Some(execution.into());
        self
    }

    /// Allowed slippage (%) for long term and conditional Market orders.
    ///
    /// Allowed price slippage is calculated based on the provided PerpetualMarket oracle price,
    /// or, a user-provided `price()` taking precedence.
    ///
    /// See also [What is Slippage in Crypto?](https://dydx.exchange/crypto-learning/what-is-slippage-in-crypto).
    pub fn allowed_slippage(mut self, slippage_percent: impl Into<BigDecimal>) -> Self {
        self.slippage = slippage_percent.into() / 100.0;
        self
    }

    /// Set builder code parameters.
    pub fn builder_code_parameters(
        mut self,
        builder_code_parameters: impl Into<BuilderCodeParameters>,
    ) -> Self {
        self.builder_code_parameters = Some(builder_code_parameters.into());
        self
    }

    /// Set TWAP parameters.
    pub fn twap_parameters(mut self, twap_parameters: impl Into<TwapParameters>) -> Self {
        self.twap_parameters = Some(twap_parameters.into());
        self
    }

    /// Set order router address.
    pub fn order_router_address(mut self, order_router_address: impl Into<Address>) -> Self {
        self.order_router_address = order_router_address.into();
        self
    }

    /// Update the generator's market.
    ///
    /// Note that at the moment dYdX [doesn't support](https://dydx.exchange/faq) spot trading.
    pub fn update_market(&mut self, market_for: impl Into<OrderMarketParams>) {
        self.market_params = market_for.into();
    }

    /// Update the generator's market oracle price.
    pub fn update_market_price(&mut self, price: impl Into<Price>) {
        self.market_params.oracle_price = Some(price.into());
    }

    /* Builder */
    /// Build an [`Order`] and a corresponding [`OrderId`].
    ///
    /// `client_id` [impacts](https://docs.dydx.xyz/concepts/trading/limit-orderbook#replacements) an order id.
    /// So it is important to provide its uniqueness as otherwise some orders may overwrite others.
    pub fn build(self, client_id: impl Into<ClientId>) -> Result<(OrderId, Order), Error> {
        let side = self
            .side
            .ok_or_else(|| err!("Missing Order side (Buy/Sell)"))?;
        let size = self
            .size
            .as_ref()
            .ok_or_else(|| err!("Missing Order size"))?;
        let ty = self.ty.as_ref().ok_or_else(|| err!("Missing Order type"))?;
        let post_only = self.post_only.as_ref().unwrap_or(&false);
        let execution = self.execution.as_ref().unwrap_or(&OrderExecution::Default);
        let time_in_force = ty.time_in_force(
            &self.time_in_force.unwrap_or(OrderTimeInForce::Unspecified),
            *post_only,
            execution,
        )?;
        let reduce_only = *self.reduce_only.as_ref().unwrap_or(&false);
        let until = self
            .until
            .as_ref()
            .ok_or_else(|| err!("Missing Order until (good-til-oneof)"))?;
        let quantums = self
            .market_params
            .quantize_quantity(size.clone())
            .to_u64()
            .ok_or_else(|| err!("Failed converting BigDecimal size into u64"))?;
        let conditional_order_trigger_subticks = match ty {
            OrderType::StopLimit
            | OrderType::StopMarket
            | OrderType::TakeProfit
            | OrderType::TakeProfitMarket => self
                .market_params
                .quantize_price(
                    self.trigger_price
                        .clone()
                        .ok_or_else(|| err!("Missing Order trigger price"))?,
                )
                .to_u64()
                .ok_or_else(|| err!("Failed converting BigDecimal trigger-price into u64"))?,
            _ => 0,
        };

        let clob_pair_id = self.market_params.clob_pair_id().0;

        let order_id = OrderId {
            subaccount_id: Some(self.subaccount_id.clone()),
            client_id: client_id.into().0,
            order_flags: self.flags.clone() as u32,
            clob_pair_id,
        };

        let order = Order {
            order_id: Some(order_id.clone()),
            side: side.into(),
            quantums,
            subticks: self.calculate_subticks()?,
            time_in_force: time_in_force.into(),
            reduce_only,
            client_metadata: DEFAULT_RUST_CLIENT_METADATA,
            condition_type: ty.condition_type()?.into(),
            conditional_order_trigger_subticks,
            good_til_oneof: Some(until.clone().try_into()?),
            builder_code_parameters: self.builder_code_parameters.clone(),
            twap_parameters: self.twap_parameters,
            order_router_address: self.order_router_address.to_string(),
        };

        Ok((order_id, order))
    }

    /* Helpers */
    fn calculate_subticks(&self) -> Result<u64, Error> {
        let ty = self.ty.as_ref().ok_or_else(|| err!("Missing Order type"))?;
        let price = match ty {
            OrderType::Market | OrderType::StopMarket | OrderType::TakeProfitMarket => {
                // Use user-provided slippage price
                if let Some(price) = self.price.clone() {
                    price
                // Calculate slippage price based on oracle price
                } else if let Some(oracle_price) = self.market_params.oracle_price.clone() {
                    let side = self
                        .side
                        .as_ref()
                        .ok_or_else(|| err!("Missing Order side"))?;
                    let one = <BigDecimal as One>::one();
                    match side {
                        OrderSide::Buy => oracle_price * (one + &self.slippage),
                        OrderSide::Sell => oracle_price * (one - &self.slippage),
                        _ => return Err(err!("Order side {side:?} not supported")),
                    }
                } else {
                    return Err(err!("Failed to calculate Market order slippage price"));
                }
            }
            _ => self
                .price
                .clone()
                .ok_or_else(|| err!("Missing Order price"))?,
        };

        self.market_params
            .quantize_price(price)
            .to_u64()
            .ok_or_else(|| err!("Failed converting BigDecimal price into u64"))
    }
}

impl OrderType {
    /// Validate time execution options.
    ///
    /// See also [`OrderBuilder::time_in_force`].
    pub fn time_in_force(
        &self,
        time_in_force: &OrderTimeInForce,
        post_only: bool,
        execution: &OrderExecution,
    ) -> Result<OrderTimeInForce, Error> {
        match self {
            OrderType::Market => Ok(OrderTimeInForce::Ioc),
            OrderType::Limit => {
                if post_only {
                    Ok(OrderTimeInForce::PostOnly)
                } else {
                    Ok(*time_in_force)
                }
            }
            OrderType::StopLimit | OrderType::TakeProfit => match execution {
                OrderExecution::Default => Ok(OrderTimeInForce::Unspecified),
                OrderExecution::PostOnly => Ok(OrderTimeInForce::PostOnly),
                OrderExecution::Fok => Ok(OrderTimeInForce::FillOrKill),
                OrderExecution::Ioc => Ok(OrderTimeInForce::Ioc),
            },
            OrderType::StopMarket | OrderType::TakeProfitMarket => match execution {
                OrderExecution::Default | OrderExecution::PostOnly => Err(err!(
                    "Execution value {execution:?} not supported for order type {self:?}"
                )),
                OrderExecution::Fok => Ok(OrderTimeInForce::FillOrKill),
                OrderExecution::Ioc => Ok(OrderTimeInForce::Ioc),
            },
            _ => Err(err!(
                "Invalid combination of order type, time in force, and execution"
            )),
        }
    }

    /// Get [the condition type](https://docs.dydx.xyz/concepts/trading/orders#types) for the order.
    pub fn condition_type(&self) -> Result<ConditionType, Error> {
        match self {
            OrderType::Limit | OrderType::Market => Ok(ConditionType::Unspecified),
            OrderType::StopLimit | OrderType::StopMarket => Ok(ConditionType::StopLoss),
            OrderType::TakeProfit | OrderType::TakeProfitMarket => Ok(ConditionType::TakeProfit),
            _ => Err(err!("Order type unsupported for condition type")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::indexer::{ClobPairId, PerpetualMarketStatus, PerpetualMarketType, Ticker};
    use std::str::FromStr;

    fn sample_market_params() -> OrderMarketParams {
        PerpetualMarket {
            ticker: Ticker::from("BTC-USD"),
            default_funding_rate_1h: Default::default(),
            atomic_resolution: -10,
            clob_pair_id: ClobPairId(0),
            market_type: PerpetualMarketType::Cross,
            quantum_conversion_exponent: -9,
            step_base_quantums: 1_000_000,
            subticks_per_tick: 100_000,

            base_open_interest: Default::default(),
            initial_margin_fraction: Default::default(),
            maintenance_margin_fraction: Default::default(),
            next_funding_rate: Default::default(),
            open_interest: Default::default(),
            open_interest_lower_cap: None,
            open_interest_upper_cap: None,
            oracle_price: Default::default(),
            price_change_24h: Default::default(),
            status: PerpetualMarketStatus::Active,
            step_size: Default::default(),
            tick_size: Default::default(),
            trades_24h: 0,
            volume_24h: Quantity(0.into()),
        }
        .into()
    }

    fn bigdecimal(val: &str) -> BigDecimal {
        BigDecimal::from_str(val).expect("Failed converting str into BigDecimal")
    }

    #[test]
    fn market_size_to_quantums() {
        let market = sample_market_params();
        let size = bigdecimal("0.01");
        let quantums = market.quantize_quantity(size);
        let expected = bigdecimal("100_000_000");
        assert_eq!(quantums, expected);
    }

    #[test]
    fn market_price_to_subticks() {
        let market = sample_market_params();
        let price = bigdecimal("50_000");
        let subticks = market.quantize_price(price);
        let expected = bigdecimal("5_000_000_000");
        assert_eq!(subticks, expected);
    }

    #[test]
    fn market_quantums_to_size() {
        let market = sample_market_params();
        let quantums = bigdecimal("100_000_000");
        let size = market.dequantize_quantums(quantums);
        let expected = bigdecimal("0.01");
        assert_eq!(size, expected);
    }

    #[test]
    fn market_subticks_to_price() {
        let market = sample_market_params();
        let subticks = bigdecimal("5_000_000_000");
        let price = market.dequantize_subticks(subticks);
        let expected = bigdecimal("50_000");
        assert_eq!(price, expected);
    }
}