1#![allow(dead_code)]
18#![allow(unused_variables)]
19
20use std::str::FromStr;
21
22use nautilus_core::{Params, UnixNanos};
23use nautilus_model::{
24 enums::{AssetClass, OptionKind},
25 identifiers::{InstrumentId, Symbol},
26 instruments::{
27 BettingInstrument, BinaryOption, Cfd, Commodity, CryptoFuture, CryptoFuturesSpread,
28 CryptoOption, CryptoOptionSpread, CryptoPerpetual, CurrencyPair, Equity, FuturesContract,
29 FuturesSpread, IndexInstrument, InstrumentAny, OptionContract, OptionSpread,
30 PerpetualContract, TokenizedAsset,
31 },
32 types::{Currency, Price, Quantity},
33};
34use rust_decimal::Decimal;
35use sqlx::{FromRow, Row, error::BoxDynError, postgres::PgRow, types::Json};
36use ustr::Ustr;
37
38use crate::sql::models::{decode_money, enums::AssetClassPg, read_u8, read_u64};
39
40#[derive(Debug)]
41pub struct InstrumentAnyRow(pub InstrumentAny);
42
43#[derive(Debug)]
44pub struct BettingInstrumentRow(pub BettingInstrument);
45
46#[derive(Debug)]
47pub struct BinaryOptionRow(pub BinaryOption);
48
49#[derive(Debug)]
50pub struct CryptoFutureRow(pub CryptoFuture);
51
52#[derive(Debug)]
53pub struct CryptoOptionRow(pub CryptoOption);
54
55#[derive(Debug)]
56pub struct CryptoPerpetualRow(pub CryptoPerpetual);
57
58#[derive(Debug)]
59pub struct CurrencyPairRow(pub CurrencyPair);
60
61#[derive(Debug)]
62pub struct EquityRow(pub Equity);
63
64#[derive(Debug)]
65pub struct FuturesContractRow(pub FuturesContract);
66
67#[derive(Debug)]
68pub struct FuturesSpreadRow(pub FuturesSpread);
69
70#[derive(Debug)]
71pub struct OptionContractRow(pub OptionContract);
72
73#[derive(Debug)]
74pub struct CommodityRow(pub Commodity);
75
76#[derive(Debug)]
77pub struct IndexInstrumentRow(pub IndexInstrument);
78
79#[derive(Debug)]
80pub struct CfdRow(pub Cfd);
81
82#[derive(Debug)]
83pub struct PerpetualContractRow(pub PerpetualContract);
84
85#[derive(Debug)]
86pub struct OptionSpreadRow(pub OptionSpread);
87
88#[derive(Debug)]
89pub struct CryptoFuturesSpreadRow(pub CryptoFuturesSpread);
90
91#[derive(Debug)]
92pub struct CryptoOptionSpreadRow(pub CryptoOptionSpread);
93
94#[derive(Debug)]
95pub struct TokenizedAssetRow(pub TokenizedAsset);
96
97impl<'r> FromRow<'r, PgRow> for InstrumentAnyRow {
98 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
99 let kind = row.try_get::<String, _>("kind")?;
100 if kind == "BETTING" {
101 Ok(Self(InstrumentAny::Betting(
102 BettingInstrumentRow::from_row(row).unwrap().0,
103 )))
104 } else if kind == "BINARY_OPTION" {
105 Ok(Self(InstrumentAny::BinaryOption(
106 BinaryOptionRow::from_row(row).unwrap().0,
107 )))
108 } else if kind == "CRYPTO_FUTURE" {
109 Ok(Self(InstrumentAny::CryptoFuture(
110 CryptoFutureRow::from_row(row).unwrap().0,
111 )))
112 } else if kind == "CRYPTO_FUTURES_SPREAD" {
113 Ok(Self(InstrumentAny::CryptoFuturesSpread(
114 CryptoFuturesSpreadRow::from_row(row).unwrap().0,
115 )))
116 } else if kind == "CRYPTO_OPTION" {
117 Ok(Self(InstrumentAny::CryptoOption(
118 CryptoOptionRow::from_row(row)?.0,
119 )))
120 } else if kind == "CRYPTO_OPTION_SPREAD" {
121 Ok(Self(InstrumentAny::CryptoOptionSpread(
122 CryptoOptionSpreadRow::from_row(row).unwrap().0,
123 )))
124 } else if kind == "CRYPTO_PERPETUAL" {
125 Ok(Self(InstrumentAny::CryptoPerpetual(
126 CryptoPerpetualRow::from_row(row).unwrap().0,
127 )))
128 } else if kind == "CURRENCY_PAIR" {
129 Ok(Self(InstrumentAny::CurrencyPair(
130 CurrencyPairRow::from_row(row)?.0,
131 )))
132 } else if kind == "EQUITY" {
133 Ok(Self(InstrumentAny::Equity(
134 EquityRow::from_row(row).unwrap().0,
135 )))
136 } else if kind == "FUTURES_CONTRACT" {
137 Ok(Self(InstrumentAny::FuturesContract(
138 FuturesContractRow::from_row(row).unwrap().0,
139 )))
140 } else if kind == "FUTURES_SPREAD" {
141 Ok(Self(InstrumentAny::FuturesSpread(
142 FuturesSpreadRow::from_row(row).unwrap().0,
143 )))
144 } else if kind == "OPTION_CONTRACT" {
145 Ok(Self(InstrumentAny::OptionContract(
146 OptionContractRow::from_row(row)?.0,
147 )))
148 } else if kind == "COMMODITY" {
149 Ok(Self(InstrumentAny::Commodity(
150 CommodityRow::from_row(row).unwrap().0,
151 )))
152 } else if kind == "INDEX_INSTRUMENT" {
153 Ok(Self(InstrumentAny::IndexInstrument(
154 IndexInstrumentRow::from_row(row).unwrap().0,
155 )))
156 } else if kind == "CFD" {
157 Ok(Self(InstrumentAny::Cfd(CfdRow::from_row(row).unwrap().0)))
158 } else if kind == "OPTION_SPREAD" {
159 Ok(Self(InstrumentAny::OptionSpread(
160 OptionSpreadRow::from_row(row)?.0,
161 )))
162 } else if kind == "PERPETUAL_CONTRACT" {
163 Ok(Self(InstrumentAny::PerpetualContract(
164 PerpetualContractRow::from_row(row).unwrap().0,
165 )))
166 } else if kind == "TOKENIZED_ASSET" {
167 Ok(Self(InstrumentAny::TokenizedAsset(
168 TokenizedAssetRow::from_row(row).unwrap().0,
169 )))
170 } else {
171 Err(sqlx::Error::Decode(
172 format!("Unknown instrument type: {kind}").into(),
173 ))
174 }
175 }
176}
177
178impl<'r> FromRow<'r, PgRow> for BettingInstrumentRow {
180 #[expect(
181 clippy::too_many_lines,
182 reason = "SQL row mapping mirrors the full betting instrument constructor"
183 )]
184 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
185 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
186 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
187 let event_type_id = read_u64(row, "event_type_id")?;
188 let event_type_name = row
189 .try_get::<String, _>("event_type_name")
190 .map(|res| Ustr::from(res.as_str()))?;
191 let competition_id = read_u64(row, "competition_id")?;
192 let competition_name = row
193 .try_get::<String, _>("competition_name")
194 .map(|res| Ustr::from(res.as_str()))?;
195 let event_id = read_u64(row, "event_id")?;
196 let event_name = row
197 .try_get::<String, _>("event_name")
198 .map(|res| Ustr::from(res.as_str()))?;
199 let event_country_code = row
200 .try_get::<String, _>("event_country_code")
201 .map(|res| Ustr::from(res.as_str()))?;
202 let event_open_date = row
203 .try_get::<String, _>("event_open_date")
204 .map(UnixNanos::from)?;
205 let betting_type = row
206 .try_get::<String, _>("betting_type")
207 .map(|res| Ustr::from(res.as_str()))?;
208 let market_id = row
209 .try_get::<String, _>("market_id")
210 .map(|res| Ustr::from(res.as_str()))?;
211 let market_name = row
212 .try_get::<String, _>("market_name")
213 .map(|res| Ustr::from(res.as_str()))?;
214 let market_type = row
215 .try_get::<String, _>("market_type")
216 .map(|res| Ustr::from(res.as_str()))?;
217 let market_start_time = row
218 .try_get::<String, _>("market_start_time")
219 .map(UnixNanos::from)?;
220 let selection_id = read_u64(row, "selection_id")?;
221 let selection_name = row
222 .try_get::<String, _>("selection_name")
223 .map(|res| Ustr::from(res.as_str()))?;
224 let selection_handicap = row.try_get::<f64, _>("selection_handicap")?;
225 let currency = row
226 .try_get::<String, _>("quote_currency")
227 .map(Currency::from)?;
228 let price_precision = read_u8(row, "price_precision")?;
229 let size_precision = read_u8(row, "size_precision")?;
230 let price_increment = row
231 .try_get::<String, _>("price_increment")
232 .map(Price::from)?;
233 let size_increment = row
234 .try_get::<String, _>("size_increment")
235 .map(Quantity::from)?;
236 let max_quantity = row
237 .try_get::<Option<String>, _>("max_quantity")
238 .ok()
239 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
240 let min_quantity = row
241 .try_get::<Option<String>, _>("min_quantity")
242 .ok()
243 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
244 let max_notional = row
245 .try_get::<Option<String>, _>("max_notional")
246 .ok()
247 .flatten()
248 .map(|value| decode_money(&value, "max_notional"))
249 .transpose()?;
250 let min_notional = row
251 .try_get::<Option<String>, _>("min_notional")
252 .ok()
253 .flatten()
254 .map(|value| decode_money(&value, "min_notional"))
255 .transpose()?;
256 let max_price = row
257 .try_get::<Option<String>, _>("max_price")
258 .ok()
259 .and_then(|res| res.map(|value| Price::from(value.as_str())));
260 let min_price = row
261 .try_get::<Option<String>, _>("min_price")
262 .ok()
263 .and_then(|res| res.map(|value| Price::from(value.as_str())));
264 let margin_init = row
265 .try_get::<String, _>("margin_init")
266 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
267 let margin_maint = row
268 .try_get::<String, _>("margin_maint")
269 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
270 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
271 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
272
273 let info: Option<Json<Params>> = row.try_get("info")?;
274
275 let inst = BettingInstrument::builder()
276 .instrument_id(id)
277 .raw_symbol(raw_symbol)
278 .event_type_id(event_type_id)
279 .event_type_name(event_type_name)
280 .competition_id(competition_id)
281 .competition_name(competition_name)
282 .event_id(event_id)
283 .event_name(event_name)
284 .event_country_code(event_country_code)
285 .event_open_date(event_open_date)
286 .betting_type(betting_type)
287 .market_id(market_id)
288 .market_name(market_name)
289 .market_type(market_type)
290 .market_start_time(market_start_time)
291 .selection_id(selection_id)
292 .selection_name(selection_name)
293 .selection_handicap(selection_handicap)
294 .currency(currency)
295 .price_precision(price_precision)
296 .size_precision(size_precision)
297 .price_increment(price_increment)
298 .size_increment(size_increment)
299 .maybe_max_quantity(max_quantity)
300 .maybe_min_quantity(min_quantity)
301 .maybe_max_notional(max_notional)
302 .maybe_min_notional(min_notional)
303 .maybe_max_price(max_price)
304 .maybe_min_price(min_price)
305 .maybe_margin_init(margin_init)
306 .maybe_margin_maint(margin_maint)
307 .maybe_info(info.map(|info| info.0))
308 .ts_event(ts_event)
309 .ts_init(ts_init)
310 .build()
311 .unwrap();
312 Ok(Self(inst))
313 }
314}
315
316impl<'r> FromRow<'r, PgRow> for BinaryOptionRow {
317 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
318 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
319 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
320 let asset_class = row
321 .try_get::<AssetClassPg, _>("asset_class")
322 .map(|res| res.0)?;
323 let currency = row
324 .try_get::<String, _>("quote_currency")
325 .map(Currency::from)?;
326 let activation_ns = row
327 .try_get::<String, _>("activation_ns")
328 .map(UnixNanos::from)?;
329 let expiration_ns = row
330 .try_get::<String, _>("expiration_ns")
331 .map(UnixNanos::from)?;
332 let price_precision = read_u8(row, "price_precision")?;
333 let size_precision = read_u8(row, "size_precision")?;
334 let price_increment = row
335 .try_get::<String, _>("price_increment")
336 .map(|res| Price::from_str(res.as_str()).unwrap())?;
337 let size_increment = row
338 .try_get::<String, _>("size_increment")
339 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
340 let outcome = row
341 .try_get::<Option<String>, _>("outcome")
342 .ok()
343 .and_then(|res| res.map(|value| Ustr::from(value.as_str())));
344 let description = row
345 .try_get::<Option<String>, _>("description")
346 .ok()
347 .and_then(|res| res.map(|value| Ustr::from(value.as_str())));
348 let max_quantity = row
349 .try_get::<Option<String>, _>("max_quantity")
350 .ok()
351 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
352 let min_quantity = row
353 .try_get::<Option<String>, _>("min_quantity")
354 .ok()
355 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
356 let max_notional = row
357 .try_get::<Option<String>, _>("max_notional")
358 .ok()
359 .flatten()
360 .map(|value| decode_money(&value, "max_notional"))
361 .transpose()?;
362 let min_notional = row
363 .try_get::<Option<String>, _>("min_notional")
364 .ok()
365 .flatten()
366 .map(|value| decode_money(&value, "min_notional"))
367 .transpose()?;
368 let max_price = row
369 .try_get::<Option<String>, _>("max_price")
370 .ok()
371 .and_then(|res| res.map(|value| Price::from(value.as_str())));
372 let min_price = row
373 .try_get::<Option<String>, _>("min_price")
374 .ok()
375 .and_then(|res| res.map(|value| Price::from(value.as_str())));
376 let margin_init = row
377 .try_get::<String, _>("margin_init")
378 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
379 let margin_maint = row
380 .try_get::<String, _>("margin_maint")
381 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
382 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
383 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
384
385 let info: Option<Json<Params>> = row.try_get("info")?;
386
387 let inst = BinaryOption::builder()
388 .instrument_id(id)
389 .raw_symbol(raw_symbol)
390 .asset_class(asset_class)
391 .currency(currency)
392 .activation_ns(activation_ns)
393 .expiration_ns(expiration_ns)
394 .price_precision(price_precision)
395 .size_precision(size_precision)
396 .price_increment(price_increment)
397 .size_increment(size_increment)
398 .maybe_outcome(outcome)
399 .maybe_description(description)
400 .maybe_max_quantity(max_quantity)
401 .maybe_min_quantity(min_quantity)
402 .maybe_max_notional(max_notional)
403 .maybe_min_notional(min_notional)
404 .maybe_max_price(max_price)
405 .maybe_min_price(min_price)
406 .maybe_margin_init(margin_init)
407 .maybe_margin_maint(margin_maint)
408 .maybe_info(info.map(|info| info.0))
409 .ts_event(ts_event)
410 .ts_init(ts_init)
411 .build()
412 .unwrap();
413 Ok(Self(inst))
414 }
415}
416
417impl<'r> FromRow<'r, PgRow> for CryptoFutureRow {
418 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
419 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
420 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
421 let underlying = row.try_get::<String, _>("underlying").map(Currency::from)?;
422 let quote_currency = row
423 .try_get::<String, _>("quote_currency")
424 .map(Currency::from)?;
425 let settlement_currency = row
426 .try_get::<String, _>("settlement_currency")
427 .map(Currency::from)?;
428 let is_inverse = row.try_get::<bool, _>("is_inverse")?;
429 let activation_ns = row
430 .try_get::<String, _>("activation_ns")
431 .map(UnixNanos::from)?;
432 let expiration_ns = row
433 .try_get::<String, _>("expiration_ns")
434 .map(UnixNanos::from)?;
435 let price_precision = read_u8(row, "price_precision")?;
436 let size_precision = read_u8(row, "size_precision")?;
437 let price_increment = row
438 .try_get::<String, _>("price_increment")
439 .map(|res| Price::from_str(res.as_str()).unwrap())?;
440 let size_increment = row
441 .try_get::<String, _>("size_increment")
442 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
443 let multiplier = row
444 .try_get::<String, _>("multiplier")
445 .map(|res| Quantity::from(res.as_str()))?;
446 let lot_size = row
447 .try_get::<String, _>("lot_size")
448 .map(|res| Quantity::from(res.as_str()))?;
449 let max_quantity = row
450 .try_get::<Option<String>, _>("max_quantity")
451 .ok()
452 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
453 let min_quantity = row
454 .try_get::<Option<String>, _>("min_quantity")
455 .ok()
456 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
457 let max_notional = row
458 .try_get::<Option<String>, _>("max_notional")
459 .ok()
460 .flatten()
461 .map(|value| decode_money(&value, "max_notional"))
462 .transpose()?;
463 let min_notional = row
464 .try_get::<Option<String>, _>("min_notional")
465 .ok()
466 .flatten()
467 .map(|value| decode_money(&value, "min_notional"))
468 .transpose()?;
469 let max_price = row
470 .try_get::<Option<String>, _>("max_price")
471 .ok()
472 .and_then(|res| res.map(|value| Price::from(value.as_str())));
473 let min_price = row
474 .try_get::<Option<String>, _>("min_price")
475 .ok()
476 .and_then(|res| res.map(|value| Price::from(value.as_str())));
477 let margin_init = row
478 .try_get::<String, _>("margin_init")
479 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
480 let margin_maint = row
481 .try_get::<String, _>("margin_maint")
482 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
483 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
484 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
485
486 let info: Option<Json<Params>> = row.try_get("info")?;
487
488 let inst = CryptoFuture::builder()
489 .instrument_id(id)
490 .raw_symbol(raw_symbol)
491 .underlying(underlying)
492 .quote_currency(quote_currency)
493 .settlement_currency(settlement_currency)
494 .is_inverse(is_inverse)
495 .activation_ns(activation_ns)
496 .expiration_ns(expiration_ns)
497 .price_precision(price_precision)
498 .size_precision(size_precision)
499 .price_increment(price_increment)
500 .size_increment(size_increment)
501 .multiplier(multiplier)
502 .lot_size(lot_size)
503 .maybe_max_quantity(max_quantity)
504 .maybe_min_quantity(min_quantity)
505 .maybe_max_notional(max_notional)
506 .maybe_min_notional(min_notional)
507 .maybe_max_price(max_price)
508 .maybe_min_price(min_price)
509 .maybe_margin_init(margin_init)
510 .maybe_margin_maint(margin_maint)
511 .maybe_info(info.map(|info| info.0))
512 .ts_event(ts_event)
513 .ts_init(ts_init)
514 .build()
515 .unwrap();
516 Ok(Self(inst))
517 }
518}
519
520impl<'r> FromRow<'r, PgRow> for CryptoOptionRow {
521 #[expect(
522 clippy::too_many_lines,
523 reason = "SQL row mapping mirrors the full crypto option constructor"
524 )]
525 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
526 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
527 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
528 let underlying = row.try_get::<String, _>("underlying").map(Currency::from)?;
529 let quote_currency = row
530 .try_get::<String, _>("quote_currency")
531 .map(Currency::from)?;
532 let settlement_currency = row
533 .try_get::<String, _>("settlement_currency")
534 .map(Currency::from)?;
535 let is_inverse = row.try_get::<bool, _>("is_inverse")?;
536 let option_kind = row
537 .try_get::<String, _>("option_kind")
538 .map(|res| OptionKind::from_str(res.as_str()).unwrap())?;
539 let strike_price = row
540 .try_get::<String, _>("strike_price")
541 .map(|res| Price::from_str(res.as_str()).unwrap())?;
542 let activation_ns = row
543 .try_get::<String, _>("activation_ns")
544 .map(UnixNanos::from)?;
545 let expiration_ns = row
546 .try_get::<String, _>("expiration_ns")
547 .map(UnixNanos::from)?;
548 let price_precision = read_u8(row, "price_precision")?;
549 let size_precision = read_u8(row, "size_precision")?;
550 let price_increment = row
551 .try_get::<String, _>("price_increment")
552 .map(|res| Price::from_str(res.as_str()).unwrap())?;
553 let size_increment = row
554 .try_get::<String, _>("size_increment")
555 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
556 let multiplier = row
557 .try_get::<String, _>("multiplier")
558 .map(|res| Quantity::from(res.as_str()))?;
559 let lot_size = row
560 .try_get::<String, _>("lot_size")
561 .map(|res| Quantity::from(res.as_str()))?;
562 let max_quantity = row
563 .try_get::<Option<String>, _>("max_quantity")
564 .ok()
565 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
566 let min_quantity = row
567 .try_get::<Option<String>, _>("min_quantity")
568 .ok()
569 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
570 let max_notional = row
571 .try_get::<Option<String>, _>("max_notional")
572 .ok()
573 .flatten()
574 .map(|value| decode_money(&value, "max_notional"))
575 .transpose()?;
576 let min_notional = row
577 .try_get::<Option<String>, _>("min_notional")
578 .ok()
579 .flatten()
580 .map(|value| decode_money(&value, "min_notional"))
581 .transpose()?;
582 let max_price = row
583 .try_get::<Option<String>, _>("max_price")
584 .ok()
585 .and_then(|res| res.map(|value| Price::from(value.as_str())));
586 let min_price = row
587 .try_get::<Option<String>, _>("min_price")
588 .ok()
589 .and_then(|res| res.map(|value| Price::from(value.as_str())));
590 let margin_init = row
591 .try_get::<String, _>("margin_init")
592 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
593 let margin_maint = row
594 .try_get::<String, _>("margin_maint")
595 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
596 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
597 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
598
599 let info: Option<Json<Params>> = row.try_get("info")?;
600
601 let underlying_instrument_id = read_optional_parsed(row, "underlying_instrument_id")?;
602 let exercise_style = read_optional_parsed(row, "exercise_style")?;
603 let settlement_type = read_optional_parsed(row, "settlement_type")?;
604
605 let inst = CryptoOption::builder()
606 .instrument_id(id)
607 .raw_symbol(raw_symbol)
608 .underlying(underlying)
609 .quote_currency(quote_currency)
610 .settlement_currency(settlement_currency)
611 .is_inverse(is_inverse)
612 .option_kind(option_kind)
613 .maybe_underlying_instrument_id(underlying_instrument_id)
614 .maybe_exercise_style(exercise_style)
615 .maybe_settlement_type(settlement_type)
616 .strike_price(strike_price)
617 .activation_ns(activation_ns)
618 .expiration_ns(expiration_ns)
619 .price_precision(price_precision)
620 .size_precision(size_precision)
621 .price_increment(price_increment)
622 .size_increment(size_increment)
623 .multiplier(multiplier)
624 .lot_size(lot_size)
625 .maybe_max_quantity(max_quantity)
626 .maybe_min_quantity(min_quantity)
627 .maybe_max_notional(max_notional)
628 .maybe_min_notional(min_notional)
629 .maybe_max_price(max_price)
630 .maybe_min_price(min_price)
631 .maybe_margin_init(margin_init)
632 .maybe_margin_maint(margin_maint)
633 .maybe_info(info.map(|info| info.0))
634 .ts_event(ts_event)
635 .ts_init(ts_init)
636 .build()
637 .unwrap();
638 Ok(Self(inst))
639 }
640}
641
642impl<'r> FromRow<'r, PgRow> for CryptoPerpetualRow {
643 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
644 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
645 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
646 let base_currency = row
647 .try_get::<String, _>("base_currency")
648 .map(Currency::from)?;
649 let quote_currency = row
650 .try_get::<String, _>("quote_currency")
651 .map(Currency::from)?;
652 let settlement_currency = row
653 .try_get::<String, _>("settlement_currency")
654 .map(Currency::from)?;
655 let is_inverse = row.try_get::<bool, _>("is_inverse")?;
656 let price_precision = read_u8(row, "price_precision")?;
657 let size_precision = read_u8(row, "size_precision")?;
658 let price_increment = row
659 .try_get::<String, _>("price_increment")
660 .map(|res| Price::from_str(res.as_str()).unwrap())?;
661 let size_increment = row
662 .try_get::<String, _>("size_increment")
663 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
664 let multiplier = row
665 .try_get::<String, _>("multiplier")
666 .map(|res| Quantity::from(res.as_str()))?;
667 let lot_size = row
668 .try_get::<String, _>("lot_size")
669 .map(|res| Quantity::from(res.as_str()))?;
670 let max_quantity = row
671 .try_get::<Option<String>, _>("max_quantity")
672 .ok()
673 .and_then(|res| res.map(|res| Quantity::from(res.as_str())));
674 let min_quantity = row
675 .try_get::<Option<String>, _>("min_quantity")
676 .ok()
677 .and_then(|res| res.map(|res| Quantity::from(res.as_str())));
678 let max_notional = row
679 .try_get::<Option<String>, _>("max_notional")
680 .ok()
681 .flatten()
682 .map(|value| decode_money(&value, "max_notional"))
683 .transpose()?;
684 let min_notional = row
685 .try_get::<Option<String>, _>("min_notional")
686 .ok()
687 .flatten()
688 .map(|value| decode_money(&value, "min_notional"))
689 .transpose()?;
690 let max_price = row
691 .try_get::<Option<String>, _>("max_price")
692 .ok()
693 .and_then(|res| res.map(|res| Price::from(res.as_str())));
694 let min_price = row
695 .try_get::<Option<String>, _>("min_price")
696 .ok()
697 .and_then(|res| res.map(|res| Price::from(res.as_str())));
698 let margin_init = row
699 .try_get::<String, _>("margin_init")
700 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
701 let margin_maint = row
702 .try_get::<String, _>("margin_maint")
703 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
704 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
705 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
706 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
707
708 let info: Option<Json<Params>> = row.try_get("info")?;
709
710 let inst = CryptoPerpetual::builder()
711 .instrument_id(id)
712 .raw_symbol(raw_symbol)
713 .base_currency(base_currency)
714 .quote_currency(quote_currency)
715 .settlement_currency(settlement_currency)
716 .is_inverse(is_inverse)
717 .price_precision(price_precision)
718 .size_precision(size_precision)
719 .price_increment(price_increment)
720 .size_increment(size_increment)
721 .multiplier(multiplier)
722 .lot_size(lot_size)
723 .maybe_max_quantity(max_quantity)
724 .maybe_min_quantity(min_quantity)
725 .maybe_max_notional(max_notional)
726 .maybe_min_notional(min_notional)
727 .maybe_max_price(max_price)
728 .maybe_min_price(min_price)
729 .maybe_margin_init(margin_init)
730 .maybe_margin_maint(margin_maint)
731 .maybe_info(info.map(|info| info.0))
732 .ts_event(ts_event)
733 .ts_init(ts_init)
734 .build()
735 .unwrap();
736 Ok(Self(inst))
737 }
738}
739
740impl<'r> FromRow<'r, PgRow> for CurrencyPairRow {
741 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
742 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
743 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
744 let base_currency = row
745 .try_get::<String, _>("base_currency")
746 .map(Currency::from)?;
747 let quote_currency = row
748 .try_get::<String, _>("quote_currency")
749 .map(Currency::from)?;
750 let price_precision = read_u8(row, "price_precision")?;
751 let size_precision = read_u8(row, "size_precision")?;
752 let price_increment = row
753 .try_get::<String, _>("price_increment")
754 .map(|res| Price::from(res.as_str()))?;
755 let size_increment = row
756 .try_get::<String, _>("size_increment")
757 .map(|res| Quantity::from(res.as_str()))?;
758 let multiplier = row
759 .try_get::<Option<String>, _>("multiplier")
760 .ok()
761 .and_then(|res| res.map(|res| Quantity::from(res.as_str())));
762 let lot_size = row
763 .try_get::<Option<String>, _>("lot_size")
764 .ok()
765 .and_then(|res| res.map(|res| Quantity::from(res.as_str())));
766 let max_quantity = row
767 .try_get::<Option<String>, _>("max_quantity")
768 .ok()
769 .and_then(|res| res.map(|res| Quantity::from(res.as_str())));
770 let min_quantity = row
771 .try_get::<Option<String>, _>("min_quantity")
772 .ok()
773 .and_then(|res| res.map(|res| Quantity::from(res.as_str())));
774 let max_notional = row
775 .try_get::<Option<String>, _>("max_notional")
776 .ok()
777 .flatten()
778 .map(|value| decode_money(&value, "max_notional"))
779 .transpose()?;
780 let min_notional = row
781 .try_get::<Option<String>, _>("min_notional")
782 .ok()
783 .flatten()
784 .map(|value| decode_money(&value, "min_notional"))
785 .transpose()?;
786 let max_price = row
787 .try_get::<Option<String>, _>("max_price")
788 .ok()
789 .and_then(|res| res.map(|res| Price::from(res.as_str())));
790 let min_price = row
791 .try_get::<Option<String>, _>("min_price")
792 .ok()
793 .and_then(|res| res.map(|res| Price::from(res.as_str())));
794 let margin_init = row
795 .try_get::<String, _>("margin_init")
796 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
797 let margin_maint = row
798 .try_get::<String, _>("margin_maint")
799 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
800 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
801 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
802
803 let info: Option<Json<Params>> = row.try_get("info")?;
804
805 let inst = CurrencyPair::builder()
806 .instrument_id(id)
807 .raw_symbol(raw_symbol)
808 .base_currency(base_currency)
809 .quote_currency(quote_currency)
810 .price_precision(price_precision)
811 .size_precision(size_precision)
812 .price_increment(price_increment)
813 .size_increment(size_increment)
814 .maybe_multiplier(multiplier)
815 .maybe_lot_size(lot_size)
816 .maybe_max_quantity(max_quantity)
817 .maybe_min_quantity(min_quantity)
818 .maybe_max_notional(max_notional)
819 .maybe_min_notional(min_notional)
820 .maybe_max_price(max_price)
821 .maybe_min_price(min_price)
822 .maybe_margin_init(margin_init)
823 .maybe_margin_maint(margin_maint)
824 .maybe_info(info.map(|info| info.0))
825 .ts_event(ts_event)
826 .ts_init(ts_init)
827 .build()
828 .unwrap();
829 Ok(Self(inst))
830 }
831}
832
833impl<'r> FromRow<'r, PgRow> for EquityRow {
834 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
835 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
836 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
837 let isin = row
838 .try_get::<Option<String>, _>("isin")
839 .map(|res| res.map(|s| Ustr::from(s.as_str())))?;
840 let currency = row
841 .try_get::<String, _>("quote_currency")
842 .map(Currency::from)?;
843 let price_precision = read_u8(row, "price_precision")?;
844 let price_increment = row
845 .try_get::<String, _>("price_increment")
846 .map(|res| Price::from_str(res.as_str()).unwrap())?;
847 let lot_size = row
848 .try_get::<Option<String>, _>("lot_size")
849 .map(|res| res.map(|s| Quantity::from_str(s.as_str()).unwrap()))?;
850 let max_quantity = row
851 .try_get::<Option<String>, _>("max_quantity")
852 .ok()
853 .and_then(|res| res.map(|s| Quantity::from_str(s.as_str()).unwrap()));
854 let min_quantity = row
855 .try_get::<Option<String>, _>("min_quantity")
856 .ok()
857 .and_then(|res| res.map(|s| Quantity::from_str(s.as_str()).unwrap()));
858 let max_price = row
859 .try_get::<Option<String>, _>("max_price")
860 .ok()
861 .and_then(|res| res.map(|s| Price::from(s.as_str())));
862 let min_price = row
863 .try_get::<Option<String>, _>("min_price")
864 .ok()
865 .and_then(|res| res.map(|s| Price::from(s.as_str())));
866 let margin_init = row
867 .try_get::<String, _>("margin_init")
868 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
869 let margin_maint = row
870 .try_get::<String, _>("margin_maint")
871 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
872 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
873 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
874
875 let info: Option<Json<Params>> = row.try_get("info")?;
876
877 let inst = Equity::builder()
878 .instrument_id(id)
879 .raw_symbol(raw_symbol)
880 .maybe_isin(isin)
881 .currency(currency)
882 .price_precision(price_precision)
883 .price_increment(price_increment)
884 .maybe_lot_size(lot_size)
885 .maybe_max_quantity(max_quantity)
886 .maybe_min_quantity(min_quantity)
887 .maybe_max_price(max_price)
888 .maybe_min_price(min_price)
889 .maybe_margin_init(margin_init)
890 .maybe_margin_maint(margin_maint)
891 .maybe_info(info.map(|info| info.0))
892 .ts_event(ts_event)
893 .ts_init(ts_init)
894 .build()
895 .unwrap();
896 Ok(Self(inst))
897 }
898}
899
900impl<'r> FromRow<'r, PgRow> for FuturesContractRow {
901 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
902 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
903 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::new)?;
904 let asset_class = row
905 .try_get::<AssetClassPg, _>("asset_class")
906 .map(|res| res.0)?;
907 let exchange = row
908 .try_get::<Option<String>, _>("exchange")
909 .map(|res| res.map(|s| Ustr::from(s.as_str())))?;
910 let underlying = row
911 .try_get::<String, _>("underlying")
912 .map(|res| Ustr::from(res.as_str()))?;
913 let currency = row
914 .try_get::<String, _>("quote_currency")
915 .map(Currency::from)?;
916 let activation_ns = row
917 .try_get::<String, _>("activation_ns")
918 .map(UnixNanos::from)?;
919 let expiration_ns = row
920 .try_get::<String, _>("expiration_ns")
921 .map(UnixNanos::from)?;
922 let price_precision = read_u8(row, "price_precision")?;
923 let price_increment = row
924 .try_get::<String, _>("price_increment")
925 .map(|res| Price::from(res.as_str()))?;
926 let multiplier = row
927 .try_get::<String, _>("multiplier")
928 .map(|res| Quantity::from(res.as_str()))?;
929 let lot_size = row
930 .try_get::<String, _>("lot_size")
931 .map(|res| Quantity::from(res.as_str()))?;
932 let max_quantity = row
933 .try_get::<Option<String>, _>("max_quantity")
934 .ok()
935 .and_then(|res| res.map(|s| Quantity::from(s.as_str())));
936 let min_quantity = row
937 .try_get::<Option<String>, _>("min_quantity")
938 .ok()
939 .and_then(|res| res.map(|s| Quantity::from(s.as_str())));
940 let max_price = row
941 .try_get::<Option<String>, _>("max_price")
942 .ok()
943 .and_then(|res| res.map(|s| Price::from(s.as_str())));
944 let min_price = row
945 .try_get::<Option<String>, _>("min_price")
946 .ok()
947 .and_then(|res| res.map(|s| Price::from(s.as_str())));
948 let margin_init = row
949 .try_get::<String, _>("margin_init")
950 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
951 let margin_maint = row
952 .try_get::<String, _>("margin_maint")
953 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
954 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
955 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
956
957 let info: Option<Json<Params>> = row.try_get("info")?;
958
959 let inst = FuturesContract::builder()
960 .instrument_id(id)
961 .raw_symbol(raw_symbol)
962 .asset_class(asset_class)
963 .maybe_exchange(exchange)
964 .underlying(underlying)
965 .activation_ns(activation_ns)
966 .expiration_ns(expiration_ns)
967 .currency(currency)
968 .price_precision(price_precision)
969 .price_increment(price_increment)
970 .multiplier(multiplier)
971 .lot_size(lot_size)
972 .maybe_max_quantity(max_quantity)
973 .maybe_min_quantity(min_quantity)
974 .maybe_max_price(max_price)
975 .maybe_min_price(min_price)
976 .maybe_margin_init(margin_init)
977 .maybe_margin_maint(margin_maint)
978 .maybe_info(info.map(|info| info.0))
979 .ts_event(ts_event)
980 .ts_init(ts_init)
981 .build()
982 .unwrap();
983 Ok(Self(inst))
984 }
985}
986
987impl<'r> FromRow<'r, PgRow> for FuturesSpreadRow {
988 fn from_row(_row: &'r PgRow) -> Result<Self, sqlx::Error> {
989 todo!("Implement FromRow for FuturesSpread")
990 }
991}
992
993impl<'r> FromRow<'r, PgRow> for OptionContractRow {
994 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
995 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
996 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::new)?;
997 let asset_class = row
998 .try_get::<AssetClassPg, _>("asset_class")
999 .map(|res| res.0)?;
1000 let exchange = row
1001 .try_get::<Option<String>, _>("exchange")
1002 .map(|res| res.map(|s| Ustr::from(s.as_str())))?;
1003 let underlying = row
1004 .try_get::<String, _>("underlying")
1005 .map(|res| Ustr::from(res.as_str()))?;
1006 let option_kind = row
1007 .try_get::<String, _>("option_kind")
1008 .map(|res| OptionKind::from_str(res.as_str()).unwrap())?;
1009 let activation_ns = row
1010 .try_get::<String, _>("activation_ns")
1011 .map(UnixNanos::from)?;
1012 let expiration_ns = row
1013 .try_get::<String, _>("expiration_ns")
1014 .map(UnixNanos::from)?;
1015 let strike_price = row
1016 .try_get::<String, _>("strike_price")
1017 .map(|res| Price::from_str(res.as_str()).unwrap())?;
1018 let currency = row
1019 .try_get::<String, _>("quote_currency")
1020 .map(Currency::from)?;
1021 let price_precision = read_u8(row, "price_precision")?;
1022 let price_increment = row
1023 .try_get::<String, _>("price_increment")
1024 .map(|res| Price::from_str(res.as_str()).unwrap())?;
1025 let multiplier = row
1026 .try_get::<String, _>("multiplier")
1027 .map(|res| Quantity::from(res.as_str()))?;
1028 let lot_size = row
1029 .try_get::<String, _>("lot_size")
1030 .map(|res| Quantity::from(res.as_str()))
1031 .unwrap();
1032 let max_quantity = row
1033 .try_get::<Option<String>, _>("max_quantity")
1034 .ok()
1035 .and_then(|res| res.map(|s| Quantity::from(s.as_str())));
1036 let min_quantity = row
1037 .try_get::<Option<String>, _>("min_quantity")
1038 .ok()
1039 .and_then(|res| res.map(|s| Quantity::from(s.as_str())));
1040 let max_price = row
1041 .try_get::<Option<String>, _>("max_price")
1042 .ok()
1043 .and_then(|res| res.map(|s| Price::from(s.as_str())));
1044 let min_price = row
1045 .try_get::<Option<String>, _>("min_price")
1046 .ok()
1047 .and_then(|res| res.map(|s| Price::from(s.as_str())));
1048 let margin_init = row
1049 .try_get::<String, _>("margin_init")
1050 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1051 let margin_maint = row
1052 .try_get::<String, _>("margin_maint")
1053 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1054 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
1055 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
1056
1057 let info: Option<Json<Params>> = row.try_get("info")?;
1058
1059 let underlying_instrument_id = read_optional_parsed(row, "underlying_instrument_id")?;
1060 let exercise_style = read_optional_parsed(row, "exercise_style")?;
1061 let settlement_type = read_optional_parsed(row, "settlement_type")?;
1062
1063 let inst = OptionContract::builder()
1064 .instrument_id(id)
1065 .raw_symbol(raw_symbol)
1066 .asset_class(asset_class)
1067 .maybe_exchange(exchange)
1068 .underlying(underlying)
1069 .option_kind(option_kind)
1070 .maybe_underlying_instrument_id(underlying_instrument_id)
1071 .maybe_exercise_style(exercise_style)
1072 .maybe_settlement_type(settlement_type)
1073 .strike_price(strike_price)
1074 .currency(currency)
1075 .activation_ns(activation_ns)
1076 .expiration_ns(expiration_ns)
1077 .price_precision(price_precision)
1078 .price_increment(price_increment)
1079 .multiplier(multiplier)
1080 .lot_size(lot_size)
1081 .maybe_max_quantity(max_quantity)
1082 .maybe_min_quantity(min_quantity)
1083 .maybe_max_price(max_price)
1084 .maybe_min_price(min_price)
1085 .maybe_margin_init(margin_init)
1086 .maybe_margin_maint(margin_maint)
1087 .maybe_info(info.map(|info| info.0))
1088 .ts_event(ts_event)
1089 .ts_init(ts_init)
1090 .build()
1091 .unwrap();
1092 Ok(Self(inst))
1093 }
1094}
1095
1096impl<'r> FromRow<'r, PgRow> for CommodityRow {
1097 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
1098 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
1099 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
1100 let asset_class = row
1101 .try_get::<AssetClassPg, _>("asset_class")
1102 .map(|res| res.0)?;
1103 let quote_currency = row
1104 .try_get::<String, _>("quote_currency")
1105 .map(Currency::from)?;
1106 let price_precision = read_u8(row, "price_precision")?;
1107 let size_precision = read_u8(row, "size_precision")?;
1108 let price_increment = row
1109 .try_get::<String, _>("price_increment")
1110 .map(|res| Price::from_str(res.as_str()).unwrap())?;
1111 let size_increment = row
1112 .try_get::<String, _>("size_increment")
1113 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
1114 let lot_size = row
1115 .try_get::<Option<String>, _>("lot_size")
1116 .ok()
1117 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1118 let max_quantity = row
1119 .try_get::<Option<String>, _>("max_quantity")
1120 .ok()
1121 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1122 let min_quantity = row
1123 .try_get::<Option<String>, _>("min_quantity")
1124 .ok()
1125 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1126 let max_notional = row
1127 .try_get::<Option<String>, _>("max_notional")
1128 .ok()
1129 .flatten()
1130 .map(|value| decode_money(&value, "max_notional"))
1131 .transpose()?;
1132 let min_notional = row
1133 .try_get::<Option<String>, _>("min_notional")
1134 .ok()
1135 .flatten()
1136 .map(|value| decode_money(&value, "min_notional"))
1137 .transpose()?;
1138 let max_price = row
1139 .try_get::<Option<String>, _>("max_price")
1140 .ok()
1141 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1142 let min_price = row
1143 .try_get::<Option<String>, _>("min_price")
1144 .ok()
1145 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1146 let margin_init = row
1147 .try_get::<String, _>("margin_init")
1148 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1149 let margin_maint = row
1150 .try_get::<String, _>("margin_maint")
1151 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1152 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
1153 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
1154
1155 let info: Option<Json<Params>> = row.try_get("info")?;
1156
1157 let inst = Commodity::builder()
1158 .instrument_id(id)
1159 .raw_symbol(raw_symbol)
1160 .asset_class(asset_class)
1161 .quote_currency(quote_currency)
1162 .price_precision(price_precision)
1163 .size_precision(size_precision)
1164 .price_increment(price_increment)
1165 .size_increment(size_increment)
1166 .maybe_lot_size(lot_size)
1167 .maybe_max_quantity(max_quantity)
1168 .maybe_min_quantity(min_quantity)
1169 .maybe_max_notional(max_notional)
1170 .maybe_min_notional(min_notional)
1171 .maybe_max_price(max_price)
1172 .maybe_min_price(min_price)
1173 .maybe_margin_init(margin_init)
1174 .maybe_margin_maint(margin_maint)
1175 .maybe_info(info.map(|info| info.0))
1176 .ts_event(ts_event)
1177 .ts_init(ts_init)
1178 .build()
1179 .unwrap();
1180 Ok(Self(inst))
1181 }
1182}
1183
1184impl<'r> FromRow<'r, PgRow> for IndexInstrumentRow {
1185 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
1186 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
1187 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
1188 let currency = row
1189 .try_get::<String, _>("quote_currency")
1190 .map(Currency::from)?;
1191 let price_precision = read_u8(row, "price_precision")?;
1192 let size_precision = read_u8(row, "size_precision")?;
1193 let price_increment = row
1194 .try_get::<String, _>("price_increment")
1195 .map(|res| Price::from_str(res.as_str()).unwrap())?;
1196 let size_increment = row
1197 .try_get::<String, _>("size_increment")
1198 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
1199 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
1200 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
1201
1202 let info: Option<Json<Params>> = row.try_get("info")?;
1203
1204 let inst = IndexInstrument::builder()
1205 .instrument_id(id)
1206 .raw_symbol(raw_symbol)
1207 .currency(currency)
1208 .price_precision(price_precision)
1209 .size_precision(size_precision)
1210 .price_increment(price_increment)
1211 .size_increment(size_increment)
1212 .maybe_info(info.map(|info| info.0))
1213 .ts_event(ts_event)
1214 .ts_init(ts_init)
1215 .build()
1216 .unwrap();
1217 Ok(Self(inst))
1218 }
1219}
1220
1221impl<'r> FromRow<'r, PgRow> for CfdRow {
1222 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
1223 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
1224 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
1225 let asset_class = row
1226 .try_get::<AssetClassPg, _>("asset_class")
1227 .map(|res| res.0)?;
1228 let base_currency = row
1229 .try_get::<Option<String>, _>("base_currency")
1230 .ok()
1231 .and_then(|res| res.map(Currency::from));
1232 let quote_currency = row
1233 .try_get::<String, _>("quote_currency")
1234 .map(Currency::from)?;
1235 let price_precision = read_u8(row, "price_precision")?;
1236 let size_precision = read_u8(row, "size_precision")?;
1237 let price_increment = row
1238 .try_get::<String, _>("price_increment")
1239 .map(|res| Price::from_str(res.as_str()).unwrap())?;
1240 let size_increment = row
1241 .try_get::<String, _>("size_increment")
1242 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
1243 let lot_size = row
1244 .try_get::<Option<String>, _>("lot_size")
1245 .ok()
1246 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1247 let max_quantity = row
1248 .try_get::<Option<String>, _>("max_quantity")
1249 .ok()
1250 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1251 let min_quantity = row
1252 .try_get::<Option<String>, _>("min_quantity")
1253 .ok()
1254 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1255 let max_notional = row
1256 .try_get::<Option<String>, _>("max_notional")
1257 .ok()
1258 .flatten()
1259 .map(|value| decode_money(&value, "max_notional"))
1260 .transpose()?;
1261 let min_notional = row
1262 .try_get::<Option<String>, _>("min_notional")
1263 .ok()
1264 .flatten()
1265 .map(|value| decode_money(&value, "min_notional"))
1266 .transpose()?;
1267 let max_price = row
1268 .try_get::<Option<String>, _>("max_price")
1269 .ok()
1270 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1271 let min_price = row
1272 .try_get::<Option<String>, _>("min_price")
1273 .ok()
1274 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1275 let margin_init = row
1276 .try_get::<String, _>("margin_init")
1277 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1278 let margin_maint = row
1279 .try_get::<String, _>("margin_maint")
1280 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1281 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
1282 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
1283
1284 let info: Option<Json<Params>> = row.try_get("info")?;
1285
1286 let inst = Cfd::builder()
1287 .instrument_id(id)
1288 .raw_symbol(raw_symbol)
1289 .asset_class(asset_class)
1290 .maybe_base_currency(base_currency)
1291 .quote_currency(quote_currency)
1292 .price_precision(price_precision)
1293 .size_precision(size_precision)
1294 .price_increment(price_increment)
1295 .size_increment(size_increment)
1296 .maybe_lot_size(lot_size)
1297 .maybe_max_quantity(max_quantity)
1298 .maybe_min_quantity(min_quantity)
1299 .maybe_max_notional(max_notional)
1300 .maybe_min_notional(min_notional)
1301 .maybe_max_price(max_price)
1302 .maybe_min_price(min_price)
1303 .maybe_margin_init(margin_init)
1304 .maybe_margin_maint(margin_maint)
1305 .maybe_info(info.map(|info| info.0))
1306 .ts_event(ts_event)
1307 .ts_init(ts_init)
1308 .build()
1309 .unwrap();
1310 Ok(Self(inst))
1311 }
1312}
1313
1314impl<'r> FromRow<'r, PgRow> for PerpetualContractRow {
1315 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
1316 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
1317 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
1318 let underlying = row
1319 .try_get::<String, _>("underlying")
1320 .map(|res| Ustr::from(res.as_str()))?;
1321 let asset_class = row
1322 .try_get::<AssetClassPg, _>("asset_class")
1323 .map(|res| res.0)?;
1324 let base_currency = row
1325 .try_get::<Option<String>, _>("base_currency")
1326 .ok()
1327 .and_then(|res| res.map(Currency::from));
1328 let quote_currency = row
1329 .try_get::<String, _>("quote_currency")
1330 .map(Currency::from)?;
1331 let settlement_currency = row
1332 .try_get::<String, _>("settlement_currency")
1333 .map(Currency::from)?;
1334 let is_inverse = row.try_get::<bool, _>("is_inverse")?;
1335 let price_precision = read_u8(row, "price_precision")?;
1336 let size_precision = read_u8(row, "size_precision")?;
1337 let price_increment = row
1338 .try_get::<String, _>("price_increment")
1339 .map(|res| Price::from_str(res.as_str()).unwrap())?;
1340 let size_increment = row
1341 .try_get::<String, _>("size_increment")
1342 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
1343 let multiplier = row
1344 .try_get::<String, _>("multiplier")
1345 .map(|res| Quantity::from(res.as_str()))?;
1346 let lot_size = row
1347 .try_get::<String, _>("lot_size")
1348 .map(|res| Quantity::from(res.as_str()))?;
1349 let max_quantity = row
1350 .try_get::<Option<String>, _>("max_quantity")
1351 .ok()
1352 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1353 let min_quantity = row
1354 .try_get::<Option<String>, _>("min_quantity")
1355 .ok()
1356 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1357 let max_notional = row
1358 .try_get::<Option<String>, _>("max_notional")
1359 .ok()
1360 .flatten()
1361 .map(|value| decode_money(&value, "max_notional"))
1362 .transpose()?;
1363 let min_notional = row
1364 .try_get::<Option<String>, _>("min_notional")
1365 .ok()
1366 .flatten()
1367 .map(|value| decode_money(&value, "min_notional"))
1368 .transpose()?;
1369 let max_price = row
1370 .try_get::<Option<String>, _>("max_price")
1371 .ok()
1372 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1373 let min_price = row
1374 .try_get::<Option<String>, _>("min_price")
1375 .ok()
1376 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1377 let margin_init = row
1378 .try_get::<String, _>("margin_init")
1379 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1380 let margin_maint = row
1381 .try_get::<String, _>("margin_maint")
1382 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1383 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
1384 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
1385
1386 let info: Option<Json<Params>> = row.try_get("info")?;
1387
1388 let inst = PerpetualContract::builder()
1389 .instrument_id(id)
1390 .raw_symbol(raw_symbol)
1391 .underlying(underlying)
1392 .asset_class(asset_class)
1393 .maybe_base_currency(base_currency)
1394 .quote_currency(quote_currency)
1395 .settlement_currency(settlement_currency)
1396 .is_inverse(is_inverse)
1397 .price_precision(price_precision)
1398 .size_precision(size_precision)
1399 .price_increment(price_increment)
1400 .size_increment(size_increment)
1401 .multiplier(multiplier)
1402 .lot_size(lot_size)
1403 .maybe_max_quantity(max_quantity)
1404 .maybe_min_quantity(min_quantity)
1405 .maybe_max_notional(max_notional)
1406 .maybe_min_notional(min_notional)
1407 .maybe_max_price(max_price)
1408 .maybe_min_price(min_price)
1409 .maybe_margin_init(margin_init)
1410 .maybe_margin_maint(margin_maint)
1411 .maybe_info(info.map(|info| info.0))
1412 .ts_event(ts_event)
1413 .ts_init(ts_init)
1414 .build()
1415 .unwrap();
1416 Ok(Self(inst))
1417 }
1418}
1419
1420impl<'r> FromRow<'r, PgRow> for OptionSpreadRow {
1421 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
1422 let result = (|| {
1423 let id = read_parsed(row, "id")?;
1424 let raw_symbol: String = row.try_get("raw_symbol")?;
1425
1426 let raw_symbol =
1427 Symbol::new_checked(raw_symbol).map_err(|e| sqlx::Error::ColumnDecode {
1428 index: "raw_symbol".to_string(),
1429 source: e.to_string().into(),
1430 })?;
1431
1432 let asset_class = row.try_get::<AssetClassPg, _>("asset_class")?.0;
1433 let exchange = row
1434 .try_get::<Option<String>, _>("exchange")?
1435 .as_deref()
1436 .map(Ustr::from);
1437 let underlying = row
1438 .try_get::<String, _>("underlying")
1439 .map(|value| Ustr::from(&value))?;
1440 let strategy_type = row
1441 .try_get::<String, _>("strategy_type")
1442 .map(|value| Ustr::from(&value))?;
1443 let activation_ns = read_parsed(row, "activation_ns")?;
1444 let expiration_ns = read_parsed(row, "expiration_ns")?;
1445 let currency = read_parsed(row, "quote_currency")?;
1446 let price_precision = read_u8(row, "price_precision")?;
1447 let price_increment = read_parsed(row, "price_increment")?;
1448 let size_precision = read_u8(row, "size_precision")?;
1449 let size_increment: Quantity = read_parsed(row, "size_increment")?;
1450 let multiplier = read_parsed(row, "multiplier")?;
1451 let lot_size = read_parsed(row, "lot_size")?;
1452 let max_quantity = read_optional_parsed(row, "max_quantity")?;
1453 let min_quantity = read_optional_parsed(row, "min_quantity")?;
1454 let max_price = read_optional_parsed(row, "max_price")?;
1455 let min_price = read_optional_parsed(row, "min_price")?;
1456 let margin_init = read_parsed(row, "margin_init")?;
1457 let margin_maint = read_parsed(row, "margin_maint")?;
1458 let tick_scheme = row
1459 .try_get::<Option<String>, _>("tick_scheme")?
1460 .as_deref()
1461 .map(Ustr::from);
1462 let info: Option<Json<Params>> = row.try_get("info")?;
1463 let ts_event = read_parsed(row, "ts_event")?;
1464 let ts_init = read_parsed(row, "ts_init")?;
1465
1466 let mut inst = OptionSpread::builder()
1467 .instrument_id(id)
1468 .raw_symbol(raw_symbol)
1469 .asset_class(asset_class)
1470 .maybe_exchange(exchange)
1471 .underlying(underlying)
1472 .strategy_type(strategy_type)
1473 .activation_ns(activation_ns)
1474 .expiration_ns(expiration_ns)
1475 .currency(currency)
1476 .price_precision(price_precision)
1477 .price_increment(price_increment)
1478 .multiplier(multiplier)
1479 .lot_size(lot_size)
1480 .maybe_max_quantity(max_quantity)
1481 .maybe_min_quantity(min_quantity)
1482 .maybe_max_price(max_price)
1483 .maybe_min_price(min_price)
1484 .margin_init(margin_init)
1485 .margin_maint(margin_maint)
1486 .maybe_tick_scheme(tick_scheme)
1487 .maybe_info(info.map(|info| info.0))
1488 .ts_event(ts_event)
1489 .ts_init(ts_init)
1490 .build()
1491 .map_err(|e| sqlx::Error::Decode(e.to_string().into()))?;
1492
1493 inst.min_quantity = min_quantity;
1495 if size_precision != inst.size_precision
1496 || size_increment != inst.size_increment
1497 || size_increment.precision != inst.size_increment.precision
1498 {
1499 return Err(sqlx::Error::Decode(
1500 "Invalid size_precision or size_increment for OptionSpread".into(),
1501 ));
1502 }
1503
1504 Ok(Self(inst))
1505 })();
1506
1507 result.map_err(|e: sqlx::Error| {
1508 let id = row.try_get::<String, _>("id").ok();
1509 sqlx::Error::Decode(format!("Invalid OptionSpread row {id:?}: {e}").into())
1510 })
1511 }
1512}
1513
1514impl<'r> FromRow<'r, PgRow> for CryptoFuturesSpreadRow {
1515 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
1516 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
1517 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
1518 let underlying = row.try_get::<String, _>("underlying").map(Currency::from)?;
1519 let quote_currency = row
1520 .try_get::<String, _>("quote_currency")
1521 .map(Currency::from)?;
1522 let settlement_currency = row
1523 .try_get::<String, _>("settlement_currency")
1524 .map(Currency::from)?;
1525 let is_inverse = row.try_get::<bool, _>("is_inverse")?;
1526 let strategy_type = row
1527 .try_get::<String, _>("strategy_type")
1528 .map(|res| Ustr::from(res.as_str()))?;
1529 let activation_ns = row
1530 .try_get::<String, _>("activation_ns")
1531 .map(UnixNanos::from)?;
1532 let expiration_ns = row
1533 .try_get::<String, _>("expiration_ns")
1534 .map(UnixNanos::from)?;
1535 let price_precision = read_u8(row, "price_precision")?;
1536 let size_precision = read_u8(row, "size_precision")?;
1537 let price_increment = row
1538 .try_get::<String, _>("price_increment")
1539 .map(|res| Price::from_str(res.as_str()).unwrap())?;
1540 let size_increment = row
1541 .try_get::<String, _>("size_increment")
1542 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
1543 let multiplier = row
1544 .try_get::<String, _>("multiplier")
1545 .map(|res| Quantity::from(res.as_str()))?;
1546 let lot_size = row
1547 .try_get::<String, _>("lot_size")
1548 .map(|res| Quantity::from(res.as_str()))?;
1549 let max_quantity = row
1550 .try_get::<Option<String>, _>("max_quantity")
1551 .ok()
1552 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1553 let min_quantity = row
1554 .try_get::<Option<String>, _>("min_quantity")
1555 .ok()
1556 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1557 let max_notional = row
1558 .try_get::<Option<String>, _>("max_notional")
1559 .ok()
1560 .flatten()
1561 .map(|value| decode_money(&value, "max_notional"))
1562 .transpose()?;
1563 let min_notional = row
1564 .try_get::<Option<String>, _>("min_notional")
1565 .ok()
1566 .flatten()
1567 .map(|value| decode_money(&value, "min_notional"))
1568 .transpose()?;
1569 let max_price = row
1570 .try_get::<Option<String>, _>("max_price")
1571 .ok()
1572 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1573 let min_price = row
1574 .try_get::<Option<String>, _>("min_price")
1575 .ok()
1576 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1577 let margin_init = row
1578 .try_get::<String, _>("margin_init")
1579 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1580 let margin_maint = row
1581 .try_get::<String, _>("margin_maint")
1582 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1583 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
1584 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
1585
1586 let info: Option<Json<Params>> = row.try_get("info")?;
1587
1588 let inst = CryptoFuturesSpread::builder()
1589 .instrument_id(id)
1590 .raw_symbol(raw_symbol)
1591 .underlying(underlying)
1592 .quote_currency(quote_currency)
1593 .settlement_currency(settlement_currency)
1594 .is_inverse(is_inverse)
1595 .strategy_type(strategy_type)
1596 .activation_ns(activation_ns)
1597 .expiration_ns(expiration_ns)
1598 .price_precision(price_precision)
1599 .size_precision(size_precision)
1600 .price_increment(price_increment)
1601 .size_increment(size_increment)
1602 .multiplier(multiplier)
1603 .lot_size(lot_size)
1604 .maybe_max_quantity(max_quantity)
1605 .maybe_min_quantity(min_quantity)
1606 .maybe_max_notional(max_notional)
1607 .maybe_min_notional(min_notional)
1608 .maybe_max_price(max_price)
1609 .maybe_min_price(min_price)
1610 .maybe_margin_init(margin_init)
1611 .maybe_margin_maint(margin_maint)
1612 .maybe_info(info.map(|info| info.0))
1613 .ts_event(ts_event)
1614 .ts_init(ts_init)
1615 .build()
1616 .unwrap();
1617 Ok(Self(inst))
1618 }
1619}
1620
1621impl<'r> FromRow<'r, PgRow> for CryptoOptionSpreadRow {
1622 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
1623 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
1624 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
1625 let underlying = row.try_get::<String, _>("underlying").map(Currency::from)?;
1626 let quote_currency = row
1627 .try_get::<String, _>("quote_currency")
1628 .map(Currency::from)?;
1629 let settlement_currency = row
1630 .try_get::<String, _>("settlement_currency")
1631 .map(Currency::from)?;
1632 let is_inverse = row.try_get::<bool, _>("is_inverse")?;
1633 let strategy_type = row
1634 .try_get::<String, _>("strategy_type")
1635 .map(|res| Ustr::from(res.as_str()))?;
1636 let activation_ns = row
1637 .try_get::<String, _>("activation_ns")
1638 .map(UnixNanos::from)?;
1639 let expiration_ns = row
1640 .try_get::<String, _>("expiration_ns")
1641 .map(UnixNanos::from)?;
1642 let price_precision = read_u8(row, "price_precision")?;
1643 let size_precision = read_u8(row, "size_precision")?;
1644 let price_increment = row
1645 .try_get::<String, _>("price_increment")
1646 .map(|res| Price::from_str(res.as_str()).unwrap())?;
1647 let size_increment = row
1648 .try_get::<String, _>("size_increment")
1649 .map(|res| Quantity::from_str(res.as_str()).unwrap())?;
1650 let multiplier = row
1651 .try_get::<String, _>("multiplier")
1652 .map(|res| Quantity::from(res.as_str()))?;
1653 let lot_size = row
1654 .try_get::<String, _>("lot_size")
1655 .map(|res| Quantity::from(res.as_str()))?;
1656 let max_quantity = row
1657 .try_get::<Option<String>, _>("max_quantity")
1658 .ok()
1659 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1660 let min_quantity = row
1661 .try_get::<Option<String>, _>("min_quantity")
1662 .ok()
1663 .and_then(|res| res.map(|value| Quantity::from(value.as_str())));
1664 let max_notional = row
1665 .try_get::<Option<String>, _>("max_notional")
1666 .ok()
1667 .flatten()
1668 .map(|value| decode_money(&value, "max_notional"))
1669 .transpose()?;
1670 let min_notional = row
1671 .try_get::<Option<String>, _>("min_notional")
1672 .ok()
1673 .flatten()
1674 .map(|value| decode_money(&value, "min_notional"))
1675 .transpose()?;
1676 let max_price = row
1677 .try_get::<Option<String>, _>("max_price")
1678 .ok()
1679 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1680 let min_price = row
1681 .try_get::<Option<String>, _>("min_price")
1682 .ok()
1683 .and_then(|res| res.map(|value| Price::from(value.as_str())));
1684 let margin_init = row
1685 .try_get::<String, _>("margin_init")
1686 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1687 let margin_maint = row
1688 .try_get::<String, _>("margin_maint")
1689 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1690 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
1691 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
1692
1693 let info: Option<Json<Params>> = row.try_get("info")?;
1694
1695 let inst = CryptoOptionSpread::builder()
1696 .instrument_id(id)
1697 .raw_symbol(raw_symbol)
1698 .underlying(underlying)
1699 .quote_currency(quote_currency)
1700 .settlement_currency(settlement_currency)
1701 .is_inverse(is_inverse)
1702 .strategy_type(strategy_type)
1703 .activation_ns(activation_ns)
1704 .expiration_ns(expiration_ns)
1705 .price_precision(price_precision)
1706 .size_precision(size_precision)
1707 .price_increment(price_increment)
1708 .size_increment(size_increment)
1709 .multiplier(multiplier)
1710 .lot_size(lot_size)
1711 .maybe_max_quantity(max_quantity)
1712 .maybe_min_quantity(min_quantity)
1713 .maybe_max_notional(max_notional)
1714 .maybe_min_notional(min_notional)
1715 .maybe_max_price(max_price)
1716 .maybe_min_price(min_price)
1717 .maybe_margin_init(margin_init)
1718 .maybe_margin_maint(margin_maint)
1719 .maybe_info(info.map(|info| info.0))
1720 .ts_event(ts_event)
1721 .ts_init(ts_init)
1722 .build()
1723 .unwrap();
1724 Ok(Self(inst))
1725 }
1726}
1727
1728impl<'r> FromRow<'r, PgRow> for TokenizedAssetRow {
1729 fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
1730 let id = row.try_get::<String, _>("id").map(InstrumentId::from)?;
1731 let raw_symbol = row.try_get::<String, _>("raw_symbol").map(Symbol::from)?;
1732 let asset_class = row.try_get::<String, _>("asset_class").and_then(|res| {
1733 AssetClass::from_str(res.as_str()).map_err(|e| {
1734 sqlx::Error::Decode(format!("Invalid asset class '{res}': {e}").into())
1735 })
1736 })?;
1737 let base_currency = row
1740 .try_get::<String, _>("base_currency")
1741 .map(|code| Currency::get_or_create_crypto(&code))?;
1742 let quote_currency = row
1743 .try_get::<String, _>("quote_currency")
1744 .map(Currency::from)?;
1745 let isin = row
1746 .try_get::<Option<String>, _>("isin")
1747 .ok()
1748 .and_then(|res| res.map(|s| Ustr::from(s.as_str())));
1749 let price_precision = read_u8(row, "price_precision")?;
1750 let size_precision = read_u8(row, "size_precision")?;
1751 let price_increment = row
1752 .try_get::<String, _>("price_increment")
1753 .map(|res| Price::from(res.as_str()))?;
1754 let size_increment = row
1755 .try_get::<String, _>("size_increment")
1756 .map(|res| Quantity::from(res.as_str()))?;
1757 let multiplier = read_optional_quantity(row, "multiplier");
1758 let lot_size = read_optional_quantity(row, "lot_size");
1759 let max_quantity = read_optional_quantity(row, "max_quantity");
1760 let min_quantity = read_optional_quantity(row, "min_quantity");
1761 let max_notional = row
1762 .try_get::<Option<String>, _>("max_notional")
1763 .ok()
1764 .flatten()
1765 .map(|value| decode_money(&value, "max_notional"))
1766 .transpose()?;
1767 let min_notional = row
1768 .try_get::<Option<String>, _>("min_notional")
1769 .ok()
1770 .flatten()
1771 .map(|value| decode_money(&value, "min_notional"))
1772 .transpose()?;
1773 let max_price = row
1774 .try_get::<Option<String>, _>("max_price")
1775 .ok()
1776 .and_then(|res| res.map(|res| Price::from(res.as_str())));
1777 let min_price = row
1778 .try_get::<Option<String>, _>("min_price")
1779 .ok()
1780 .and_then(|res| res.map(|res| Price::from(res.as_str())));
1781 let margin_init = row
1782 .try_get::<String, _>("margin_init")
1783 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1784 let margin_maint = row
1785 .try_get::<String, _>("margin_maint")
1786 .map(|res| Some(Decimal::from_str(res.as_str()).unwrap()))?;
1787 let ts_event = row.try_get::<String, _>("ts_event").map(UnixNanos::from)?;
1788 let ts_init = row.try_get::<String, _>("ts_init").map(UnixNanos::from)?;
1789
1790 let info: Option<Json<Params>> = row.try_get("info")?;
1791
1792 let inst = TokenizedAsset::builder()
1793 .instrument_id(id)
1794 .raw_symbol(raw_symbol)
1795 .asset_class(asset_class)
1796 .base_currency(base_currency)
1797 .quote_currency(quote_currency)
1798 .maybe_isin(isin)
1799 .price_precision(price_precision)
1800 .size_precision(size_precision)
1801 .price_increment(price_increment)
1802 .size_increment(size_increment)
1803 .maybe_multiplier(multiplier)
1804 .maybe_lot_size(lot_size)
1805 .maybe_max_quantity(max_quantity)
1806 .maybe_min_quantity(min_quantity)
1807 .maybe_max_notional(max_notional)
1808 .maybe_min_notional(min_notional)
1809 .maybe_max_price(max_price)
1810 .maybe_min_price(min_price)
1811 .maybe_margin_init(margin_init)
1812 .maybe_margin_maint(margin_maint)
1813 .maybe_info(info.map(|info| info.0))
1814 .ts_event(ts_event)
1815 .ts_init(ts_init)
1816 .build()
1817 .unwrap();
1818 Ok(Self(inst))
1819 }
1820}
1821
1822fn read_optional_quantity(row: &PgRow, column: &str) -> Option<Quantity> {
1823 row.try_get::<Option<String>, _>(column)
1824 .ok()
1825 .and_then(|res| res.map(|res| Quantity::from(res.as_str())))
1826}
1827
1828fn read_parsed<T>(row: &PgRow, column: &str) -> Result<T, sqlx::Error>
1829where
1830 T: FromStr,
1831 T::Err: std::fmt::Display,
1832{
1833 let value: String = row.try_get(column)?;
1834 value.parse::<T>().map_err(|e| sqlx::Error::ColumnDecode {
1835 index: column.to_string(),
1836 source: e.to_string().into(),
1837 })
1838}
1839
1840fn read_optional_parsed<T>(row: &PgRow, column: &str) -> Result<Option<T>, sqlx::Error>
1841where
1842 T: FromStr,
1843 T::Err: Into<BoxDynError>,
1844{
1845 row.try_get::<Option<String>, _>(column)?
1846 .map(|value| {
1847 value.parse::<T>().map_err(|e| sqlx::Error::ColumnDecode {
1848 index: column.to_string(),
1849 source: e.into(),
1850 })
1851 })
1852 .transpose()
1853}