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
use crate::{
    orderflow::{AberrantFill, Fill},
    symbology::*,
    AccountId, HalfOpenRange,
};
use chrono::{DateTime, Utc};
use derive::FromValue;
use netidx_derive::Pack;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, sync::Arc};

pub static SCHEMA: &'static str = include_str!("schema.sql");

#[derive(Debug, Clone, Pack, FromValue, Serialize, Deserialize)]
pub enum FolioMessage {
    GetFillsQuery(MarketFilter, HalfOpenRange<Option<DateTime<Utc>>>),
    GetFillsQueryResponse(
        MarketFilter,
        HalfOpenRange<Option<DateTime<Utc>>>,
        Arc<Vec<Result<Fill, AberrantFill>>>,
    ),
    GetFills(CptyId, HalfOpenRange<Option<DateTime<Utc>>>),
    Fills(
        CptyId,
        HalfOpenRange<Option<DateTime<Utc>>>,
        Arc<Vec<Result<Fill, AberrantFill>>>,
    ),
    RealtimeFill(Result<Fill, AberrantFill>),
    GetAllBalances,
    AllBalances(Vec<(CptyId, Arc<Balances>)>),
    GetBalances(CptyId),
    Balances(CptyId, Option<Arc<Balances>>),
    /// Control message to folio to update balances
    UpdateBalances,
    /// Control messages to folio to sync fills
    SyncFillsForward,
    SyncFillsBackward(CptyId),
    InvalidateSyncBefore(CptyId, DateTime<Utc>),
    InvalidateSyncAfter(CptyId, DateTime<Utc>),
    /// Account advertising
    AdvertiseAccounts(CptyId, Arc<Vec<AccountId>>),
}

#[derive(Copy, Debug, Clone, Pack, FromValue, Serialize, Deserialize, PartialEq, Eq)]
pub struct MarketFilter {
    pub venue: Option<VenueId>,
    pub route: Option<RouteId>,
    pub base: Option<ProductId>,
    pub quote: Option<ProductId>,
}

#[derive(Debug, Clone, Pack, FromValue, Serialize, Deserialize)]
pub struct Balances {
    pub snapshot_ts: DateTime<Utc>,
    pub by_account: BTreeMap<AccountId, BTreeMap<ProductId, Decimal>>,
}