barter_instrument/asset/
mod.rs

1use crate::{
2    Keyed,
3    asset::name::{AssetNameExchange, AssetNameInternal},
4    exchange::ExchangeId,
5};
6use derive_more::{Constructor, Display};
7use serde::{Deserialize, Serialize};
8
9/// Defines the [`AssetNameInternal`] and [`AssetNameExchange`] types, used as `SmolStr`
10/// identifiers for an [`Asset`].
11pub mod name;
12
13/// Unique identifier for an [`Asset`].
14#[derive(
15    Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display,
16)]
17pub struct AssetId(pub u64);
18
19#[derive(
20    Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Constructor,
21)]
22pub struct AssetIndex(pub usize);
23
24impl AssetIndex {
25    pub fn index(&self) -> usize {
26        self.0
27    }
28}
29
30impl std::fmt::Display for AssetIndex {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        write!(f, "AssetIndex({})", self.0)
33    }
34}
35
36#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
37pub struct ExchangeAsset<Asset> {
38    pub exchange: ExchangeId,
39    pub asset: Asset,
40}
41
42impl<Asset> ExchangeAsset<Asset> {
43    pub fn new<A>(exchange: ExchangeId, asset: A) -> Self
44    where
45        A: Into<Asset>,
46    {
47        Self {
48            exchange,
49            asset: asset.into(),
50        }
51    }
52}
53
54impl<Ass, Asset, T> From<(ExchangeId, Ass, T)> for Keyed<ExchangeAsset<Asset>, T>
55where
56    Ass: Into<Asset>,
57{
58    fn from((exchange, asset, value): (ExchangeId, Ass, T)) -> Self {
59        Self {
60            key: ExchangeAsset::new(exchange, asset),
61            value,
62        }
63    }
64}
65
66#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
67pub struct Asset {
68    pub name_internal: AssetNameInternal,
69    pub name_exchange: AssetNameExchange,
70}
71
72impl<S> From<S> for Asset
73where
74    S: Into<AssetNameExchange>,
75{
76    fn from(value: S) -> Self {
77        Self::new_from_exchange(value)
78    }
79}
80
81impl Asset {
82    pub fn new<Internal, Exchange>(name_internal: Internal, name_exchange: Exchange) -> Self
83    where
84        Internal: Into<AssetNameInternal>,
85        Exchange: Into<AssetNameExchange>,
86    {
87        Self {
88            name_internal: name_internal.into(),
89            name_exchange: name_exchange.into(),
90        }
91    }
92
93    pub fn new_from_exchange<S>(name_exchange: S) -> Self
94    where
95        S: Into<AssetNameExchange>,
96    {
97        let name_exchange = name_exchange.into();
98        Self {
99            name_internal: AssetNameInternal::from(name_exchange.name().clone()),
100            name_exchange,
101        }
102    }
103}
104
105#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display)]
106#[serde(rename_all = "snake_case")]
107pub enum AssetKind {
108    Crypto,
109    Fiat,
110}
111
112impl From<Asset> for AssetNameInternal {
113    fn from(value: Asset) -> Self {
114        value.name_internal
115    }
116}
117
118/// Special type that represents a "base" [`Asset`].
119///
120/// Examples: <br>
121/// a) Instrument = btc_usdt_spot, [`BaseAsset`] => btc <br>
122/// b) Instrument = eth_btc_spot, [`BaseAsset`] => eth
123#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display)]
124pub struct BaseAsset;
125
126/// Special type that represents a "quote" [`Asset`].
127///
128/// Examples: <br>
129/// a) Instrument = btc_usdt_spot, [`QuoteAsset`] => usdt <br>
130/// b) Instrument = eth_btc_spot, [`QuoteAsset`] => btc
131#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Display)]
132pub struct QuoteAsset;