rustrade_data/
instrument.rs1use rustrade_instrument::{
2 Keyed,
3 instrument::{
4 Instrument,
5 market_data::{MarketDataInstrument, kind::MarketDataInstrumentKind},
6 name::InstrumentNameExchange,
7 },
8};
9use serde::{Deserialize, Serialize};
10use std::fmt::Debug;
11
12pub trait InstrumentData
18where
19 Self: Clone + Debug + Send + Sync,
20{
21 type Key: Debug + Clone + Eq + Send + Sync;
22 fn key(&self) -> &Self::Key;
23 fn kind(&self) -> &MarketDataInstrumentKind;
24}
25
26impl<InstrumentKey> InstrumentData for Keyed<InstrumentKey, MarketDataInstrument>
27where
28 InstrumentKey: Debug + Clone + Eq + Send + Sync,
29{
30 type Key = InstrumentKey;
31
32 fn key(&self) -> &Self::Key {
33 &self.key
34 }
35
36 fn kind(&self) -> &MarketDataInstrumentKind {
37 &self.value.kind
38 }
39}
40
41impl InstrumentData for MarketDataInstrument {
42 type Key = Self;
43
44 fn key(&self) -> &Self::Key {
45 self
46 }
47
48 fn kind(&self) -> &MarketDataInstrumentKind {
49 &self.kind
50 }
51}
52
53#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
54pub struct MarketInstrumentData<InstrumentKey> {
55 pub key: InstrumentKey,
56 pub name_exchange: InstrumentNameExchange,
57 pub kind: MarketDataInstrumentKind,
58}
59
60#[cfg(feature = "hyperliquid")]
61impl<InstrumentKey> MarketInstrumentData<InstrumentKey> {
62 pub fn hyperliquid_perp(
69 key: InstrumentKey,
70 perp: &rustrade_instrument::hyperliquid::PerpCoin,
71 ) -> Self {
72 Self {
73 key,
74 name_exchange: InstrumentNameExchange::from(perp.coin()),
75 kind: MarketDataInstrumentKind::Perpetual,
76 }
77 }
78
79 pub fn hyperliquid_spot(
86 key: InstrumentKey,
87 pair: &rustrade_instrument::hyperliquid::SpotPair,
88 ) -> Self {
89 Self {
90 key,
91 name_exchange: InstrumentNameExchange::from(pair.coin()),
92 kind: MarketDataInstrumentKind::Spot,
93 }
94 }
95}
96
97impl<InstrumentKey> InstrumentData for MarketInstrumentData<InstrumentKey>
98where
99 InstrumentKey: Debug + Clone + Eq + Send + Sync,
100{
101 type Key = InstrumentKey;
102
103 fn key(&self) -> &Self::Key {
104 &self.key
105 }
106
107 fn kind(&self) -> &MarketDataInstrumentKind {
108 &self.kind
109 }
110}
111
112impl<InstrumentKey> std::fmt::Display for MarketInstrumentData<InstrumentKey>
113where
114 InstrumentKey: std::fmt::Display,
115{
116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117 write!(
118 f,
119 "{}_{}_{}",
120 self.key,
121 self.name_exchange.as_ref(),
122 self.kind
123 )
124 }
125}
126
127impl<ExchangeKey, AssetKey, InstrumentKey>
128 From<&Keyed<InstrumentKey, Instrument<ExchangeKey, AssetKey>>>
129 for MarketInstrumentData<InstrumentKey>
130where
131 InstrumentKey: Clone,
132{
133 fn from(value: &Keyed<InstrumentKey, Instrument<ExchangeKey, AssetKey>>) -> Self {
134 Self {
135 key: value.key.clone(),
136 name_exchange: value.value.data_name_exchange().clone(),
141 kind: MarketDataInstrumentKind::from(&value.value.kind),
142 }
143 }
144}