137 lines
4.7 KiB
Rust
137 lines
4.7 KiB
Rust
use crate::{assets, validation::identifier};
|
|
use anyhow::{ensure, Result};
|
|
use rusqlite::{params, Connection};
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::{json, Value};
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
#[serde(rename_all = "UPPERCASE")]
|
|
enum Kind {
|
|
Trade,
|
|
Split,
|
|
Merge,
|
|
Conversion,
|
|
Redeem,
|
|
Deposit,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(rename_all = "UPPERCASE")]
|
|
enum Direction {
|
|
Asc,
|
|
Desc,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub(crate) struct Filter {
|
|
account: String,
|
|
market: Option<String>,
|
|
types: Option<Vec<Kind>>,
|
|
#[serde(default)]
|
|
include_deposits: bool,
|
|
from_ms: Option<i64>,
|
|
to_ms: Option<i64>,
|
|
direction: Option<Direction>,
|
|
after: Option<Cursor>,
|
|
limit: Option<u32>,
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Copy)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Cursor {
|
|
asc: bool,
|
|
key: (i64, i64, i64, i64),
|
|
}
|
|
|
|
impl Filter {
|
|
pub(crate) fn read(self, db: &Connection) -> Result<Value> {
|
|
identifier(&self.account)?;
|
|
if let Some(m) = &self.market {
|
|
identifier(m)?;
|
|
}
|
|
let limit = self.limit.unwrap_or(100);
|
|
ensure!((1..=1000).contains(&limit), "invalid page size");
|
|
ensure!(
|
|
self.from_ms.unwrap_or(0) >= 0
|
|
&& self.to_ms.unwrap_or(i64::MAX) >= self.from_ms.unwrap_or(0),
|
|
"invalid time range"
|
|
);
|
|
let asc = match self.direction {
|
|
Some(Direction::Asc) => true,
|
|
Some(Direction::Desc) => false,
|
|
None => self.after.is_some_and(|c| c.asc),
|
|
};
|
|
let bound = if asc { i64::MIN } else { i64::MAX };
|
|
let cursor = self.after.unwrap_or(Cursor {
|
|
asc,
|
|
key: (bound, bound, bound, bound),
|
|
});
|
|
ensure!(cursor.asc == asc, "direction conflicts with cursor");
|
|
let sql = include_str!("activity.sql")
|
|
.replace("/*seek*/", if asc { ">" } else { "<" })
|
|
.replace("/*dir*/", if asc { "ASC" } else { "DESC" });
|
|
let types = self.types.as_ref().map(serde_json::to_string).transpose()?;
|
|
let (time, source, seq, leg) = cursor.key;
|
|
let mut stmt = db.prepare(&sql)?;
|
|
let mut rows = stmt.query(params![
|
|
self.account,
|
|
self.market,
|
|
types,
|
|
self.include_deposits,
|
|
self.from_ms,
|
|
self.to_ms,
|
|
time,
|
|
source,
|
|
seq,
|
|
leg,
|
|
limit + 1
|
|
])?;
|
|
let mut items = Vec::new();
|
|
let mut last = None;
|
|
let mut next = None;
|
|
while let Some(r) = rows.next()? {
|
|
if items.len() == limit as usize {
|
|
next = last;
|
|
break;
|
|
}
|
|
let source: i64 = r.get(1)?;
|
|
let seq: i64 = r.get(2)?;
|
|
let leg: i64 = r.get(3)?;
|
|
let market: Option<String> = r.get(6)?;
|
|
let outcome: Option<String> = r.get(11)?;
|
|
let asset = market
|
|
.as_deref()
|
|
.zip(outcome.as_deref())
|
|
.map(|(m, o)| assets::id(m, o));
|
|
let fee: i64 = r.get(14)?;
|
|
let cash: i64 = r.get(9)?;
|
|
let precise_cash: Option<i64> = r.get(15)?;
|
|
let gross = precise_cash.map_or(i128::from(cash) * 10000, i128::from);
|
|
let shares = match r.get::<_, Option<i64>>(16)? {
|
|
Some(u) => Some(crate::inventory::decimal(u.into(), crate::inventory::UNIT)),
|
|
None => r.get::<_, Option<i64>>(8)?.map(Into::into),
|
|
};
|
|
let cash_cents = if r.get::<_, String>(5)? == "DEPOSIT" {
|
|
cash.into()
|
|
} else {
|
|
crate::inventory::decimal(gross, 10000)
|
|
};
|
|
let mut item = json!({"type":r.get::<_,String>(5)?,"account":self.account,"at_ms":r.get::<_,Option<i64>>(4)?,
|
|
"market":market,"group":r.get::<_,Option<String>>(7)?,"asset_id":asset,"outcome":outcome,
|
|
"shares":shares,"cash_delta_cents":cash_cents,"fee_micros":fee,"net_cash_delta_micros":gross-i128::from(fee),
|
|
"side":r.get::<_,Option<String>>(12)?,"price_micros":r.get::<_,Option<i64>>(13)?.map(|p|p*100),
|
|
"request_id":r.get::<_,Option<String>>(10)?,"execution":(source==1).then_some(seq),"order":(source==1).then_some(leg)});
|
|
if let Some(raw) = r.get::<_, Option<String>>(17)? {
|
|
item["amounts_micros"] = serde_json::from_str(&raw)?;
|
|
}
|
|
items.push(item);
|
|
last = Some(Cursor {
|
|
asc,
|
|
key: (r.get(0)?, source, seq, leg),
|
|
});
|
|
}
|
|
Ok(json!({"activity":items,"next_after":next}))
|
|
}
|
|
}
|