1#![forbid(unsafe_code)]
2#![warn(
3 unused,
4 clippy::cognitive_complexity,
5 unused_crate_dependencies,
6 unused_extern_crates,
7 clippy::unused_self,
8 clippy::useless_let_if_seq,
9 missing_debug_implementations,
10 rust_2018_idioms,
11 rust_2024_compatibility
12)]
13#![allow(clippy::type_complexity, clippy::too_many_arguments, type_alias_bounds)]
14
15use crate::{
27 balance::AssetBalance,
28 order::{Order, OrderSnapshot, request::OrderResponseCancel},
29 trade::Trade,
30};
31use barter_instrument::{
32 asset::{AssetIndex, QuoteAsset, name::AssetNameExchange},
33 exchange::{ExchangeId, ExchangeIndex},
34 instrument::{InstrumentIndex, name::InstrumentNameExchange},
35};
36use barter_integration::snapshot::Snapshot;
37use derive_more::{Constructor, From};
38use order::state::OrderState;
39use serde::{Deserialize, Serialize};
40
41pub mod balance;
42pub mod client;
43pub mod error;
44pub mod exchange;
45pub mod indexer;
46pub mod map;
47pub mod order;
48pub mod trade;
49
50pub type UnindexedAccountEvent =
53 AccountEvent<ExchangeId, AssetNameExchange, InstrumentNameExchange>;
54
55pub type UnindexedAccountSnapshot =
58 AccountSnapshot<ExchangeId, AssetNameExchange, InstrumentNameExchange>;
59
60#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
61pub struct AccountEvent<
62 ExchangeKey = ExchangeIndex,
63 AssetKey = AssetIndex,
64 InstrumentKey = InstrumentIndex,
65> {
66 pub exchange: ExchangeKey,
67 pub kind: AccountEventKind<ExchangeKey, AssetKey, InstrumentKey>,
68}
69
70impl<ExchangeKey, AssetKey, InstrumentKey> AccountEvent<ExchangeKey, AssetKey, InstrumentKey> {
71 pub fn new<K>(exchange: ExchangeKey, kind: K) -> Self
72 where
73 K: Into<AccountEventKind<ExchangeKey, AssetKey, InstrumentKey>>,
74 {
75 Self {
76 exchange,
77 kind: kind.into(),
78 }
79 }
80}
81
82#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, From)]
83pub enum AccountEventKind<ExchangeKey, AssetKey, InstrumentKey> {
84 Snapshot(AccountSnapshot<ExchangeKey, AssetKey, InstrumentKey>),
86
87 BalanceSnapshot(Snapshot<AssetBalance<AssetKey>>),
89
90 OrderSnapshot(Snapshot<Order<ExchangeKey, InstrumentKey, OrderState<AssetKey, InstrumentKey>>>),
94
95 OrderCancelled(OrderResponseCancel<ExchangeKey, AssetKey, InstrumentKey>),
97
98 Trade(Trade<QuoteAsset, InstrumentKey>),
100}
101
102impl<ExchangeKey, AssetKey, InstrumentKey> AccountEvent<ExchangeKey, AssetKey, InstrumentKey>
103where
104 AssetKey: Eq,
105 InstrumentKey: Eq,
106{
107 pub fn snapshot(self) -> Option<AccountSnapshot<ExchangeKey, AssetKey, InstrumentKey>> {
108 match self.kind {
109 AccountEventKind::Snapshot(snapshot) => Some(snapshot),
110 _ => None,
111 }
112 }
113}
114
115#[derive(
116 Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize, Constructor,
117)]
118pub struct AccountSnapshot<
119 ExchangeKey = ExchangeIndex,
120 AssetKey = AssetIndex,
121 InstrumentKey = InstrumentIndex,
122> {
123 pub exchange: ExchangeKey,
124 pub balances: Vec<AssetBalance<AssetKey>>,
125 pub instruments: Vec<InstrumentAccountSnapshot<ExchangeKey, AssetKey, InstrumentKey>>,
126}
127
128#[derive(
129 Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize, Constructor,
130)]
131pub struct InstrumentAccountSnapshot<
132 ExchangeKey = ExchangeIndex,
133 AssetKey = AssetIndex,
134 InstrumentKey = InstrumentIndex,
135> {
136 pub instrument: InstrumentKey,
137 #[serde(default = "Vec::new")]
138 pub orders: Vec<OrderSnapshot<ExchangeKey, AssetKey, InstrumentKey>>,
139}
140
141impl<ExchangeKey, AssetKey, InstrumentKey> AccountSnapshot<ExchangeKey, AssetKey, InstrumentKey> {
142 pub fn assets(&self) -> impl Iterator<Item = &AssetKey> {
143 self.balances.iter().map(|balance| &balance.asset)
144 }
145
146 pub fn instruments(&self) -> impl Iterator<Item = &InstrumentKey> {
147 self.instruments.iter().map(|snapshot| &snapshot.instrument)
148 }
149}