2023-05-14 00:15:10 -07:00
|
|
|
use deadpool_postgres::Pool;
|
2023-05-31 06:32:34 -07:00
|
|
|
use log::debug;
|
2023-03-07 22:45:37 -08:00
|
|
|
use std::{
|
2023-06-07 23:13:41 -07:00
|
|
|
collections::{HashMap}
|
2023-03-07 22:45:37 -08:00
|
|
|
};
|
2023-03-27 12:01:48 -07:00
|
|
|
use tokio::sync::mpsc::{error::TryRecvError, Receiver};
|
2023-03-05 22:52:42 -08:00
|
|
|
|
2023-03-13 09:51:30 -07:00
|
|
|
use crate::{
|
2023-06-07 21:03:54 -07:00
|
|
|
structs::{candle::Candle, openbook::OpenBookFillEvent, transaction::PgTransaction},
|
2023-05-19 12:50:56 -07:00
|
|
|
utils::{to_timestampz, AnyhowWrap},
|
2023-03-13 09:51:30 -07:00
|
|
|
};
|
2023-03-11 12:15:36 -08:00
|
|
|
|
2023-06-07 21:03:54 -07:00
|
|
|
pub async fn add_fills_atomically(
|
|
|
|
pool: &Pool,
|
|
|
|
worker_id: i32,
|
|
|
|
fills: Vec<OpenBookFillEvent>,
|
|
|
|
signatures: Vec<String>,
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let mut client = pool.get().await?;
|
|
|
|
|
|
|
|
let db_txn = client.build_transaction().start().await?;
|
|
|
|
|
|
|
|
// 1. Insert fills
|
|
|
|
if fills.len() > 0 {
|
|
|
|
let fills_statement = build_fills_upsert_statement_not_crazy(fills);
|
|
|
|
db_txn
|
|
|
|
.execute(&fills_statement, &[])
|
|
|
|
.await
|
|
|
|
.map_err_anyhow()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Update txns table as processed
|
|
|
|
let transactions_statement =
|
|
|
|
build_transactions_processed_update_statement(worker_id, signatures);
|
|
|
|
db_txn
|
|
|
|
.execute(&transactions_statement, &[])
|
|
|
|
.await
|
|
|
|
.map_err_anyhow()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
db_txn.commit().await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-12 00:13:57 -08:00
|
|
|
pub async fn persist_fill_events(
|
2023-05-14 00:15:10 -07:00
|
|
|
pool: &Pool,
|
2023-05-19 12:50:56 -07:00
|
|
|
fill_receiver: &mut Receiver<OpenBookFillEvent>,
|
2023-03-27 10:11:58 -07:00
|
|
|
) -> anyhow::Result<()> {
|
2023-03-05 22:52:42 -08:00
|
|
|
loop {
|
2023-03-12 16:32:12 -07:00
|
|
|
let mut write_batch = HashMap::new();
|
2023-03-13 23:21:15 -07:00
|
|
|
while write_batch.len() < 10 {
|
2023-03-07 22:45:37 -08:00
|
|
|
match fill_receiver.try_recv() {
|
2023-03-05 22:52:42 -08:00
|
|
|
Ok(event) => {
|
2023-05-19 17:16:37 -07:00
|
|
|
write_batch.entry(event).or_insert(0);
|
2023-03-05 22:52:42 -08:00
|
|
|
}
|
2023-03-13 23:21:15 -07:00
|
|
|
Err(TryRecvError::Empty) => {
|
2023-05-19 17:16:37 -07:00
|
|
|
if !write_batch.is_empty() {
|
2023-03-13 23:21:15 -07:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-03-05 22:52:42 -08:00
|
|
|
Err(TryRecvError::Disconnected) => {
|
2023-03-12 00:13:57 -08:00
|
|
|
panic!("Fills sender must stay alive")
|
2023-03-05 22:52:42 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:16:37 -07:00
|
|
|
if !write_batch.is_empty() {
|
2023-05-31 06:32:34 -07:00
|
|
|
debug!("writing: {:?} events to DB\n", write_batch.len());
|
2023-05-14 00:15:10 -07:00
|
|
|
let upsert_statement = build_fills_upsert_statement(write_batch);
|
2023-06-03 10:22:10 -07:00
|
|
|
let client = pool.get().await?;
|
2023-05-14 00:15:10 -07:00
|
|
|
client
|
|
|
|
.execute(&upsert_statement, &[])
|
|
|
|
.await
|
|
|
|
.map_err_anyhow()
|
|
|
|
.unwrap();
|
2023-03-05 22:52:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 12:50:56 -07:00
|
|
|
#[allow(deprecated)]
|
|
|
|
fn build_fills_upsert_statement(events: HashMap<OpenBookFillEvent, u8>) -> String {
|
2023-06-07 23:13:41 -07:00
|
|
|
let mut stmt = String::from("INSERT INTO fills (signature, time, market, open_orders, open_orders_owner, bid, maker, native_qty_paid, native_qty_received, native_fee_or_rebate, fee_tier, order_id, log_index) VALUES");
|
2023-03-12 16:32:12 -07:00
|
|
|
for (idx, event) in events.keys().enumerate() {
|
2023-03-05 22:52:42 -08:00
|
|
|
let val_str = format!(
|
2023-06-07 21:03:54 -07:00
|
|
|
"({}, \'{}\', \'{}\', \'{}\', \'{}\', {}, {}, {}, {}, {}, {}, {}, {})",
|
2023-06-07 23:13:41 -07:00
|
|
|
event.signature,
|
2023-05-19 12:50:56 -07:00
|
|
|
to_timestampz(event.block_time as u64).to_rfc3339(),
|
2023-03-05 22:52:42 -08:00
|
|
|
event.market,
|
|
|
|
event.open_orders,
|
|
|
|
event.open_orders_owner,
|
|
|
|
event.bid,
|
|
|
|
event.maker,
|
|
|
|
event.native_qty_paid,
|
|
|
|
event.native_qty_received,
|
|
|
|
event.native_fee_or_rebate,
|
|
|
|
event.fee_tier,
|
|
|
|
event.order_id,
|
2023-06-07 21:03:54 -07:00
|
|
|
event.log_index,
|
|
|
|
);
|
|
|
|
|
|
|
|
if idx == 0 {
|
|
|
|
stmt = format!("{} {}", &stmt, val_str);
|
|
|
|
} else {
|
|
|
|
stmt = format!("{}, {}", &stmt, val_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let handle_conflict = "ON CONFLICT DO NOTHING";
|
|
|
|
|
|
|
|
stmt = format!("{} {}", stmt, handle_conflict);
|
|
|
|
stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_fills_upsert_statement_not_crazy(fills: Vec<OpenBookFillEvent>) -> String {
|
2023-06-07 23:13:41 -07:00
|
|
|
let mut stmt = String::from("INSERT INTO fills (signature, time, market, open_orders, open_orders_owner, bid, maker, native_qty_paid, native_qty_received, native_fee_or_rebate, fee_tier, order_id, log_index) VALUES");
|
2023-06-07 21:03:54 -07:00
|
|
|
for (idx, fill) in fills.iter().enumerate() {
|
|
|
|
let val_str = format!(
|
2023-06-07 23:13:41 -07:00
|
|
|
"(\'{}\', \'{}\', \'{}\', \'{}\', \'{}\', {}, {}, {}, {}, {}, {}, {}, {})",
|
|
|
|
fill.signature,
|
2023-06-07 21:03:54 -07:00
|
|
|
to_timestampz(fill.block_time as u64).to_rfc3339(),
|
|
|
|
fill.market,
|
|
|
|
fill.open_orders,
|
|
|
|
fill.open_orders_owner,
|
|
|
|
fill.bid,
|
|
|
|
fill.maker,
|
|
|
|
fill.native_qty_paid,
|
|
|
|
fill.native_qty_received,
|
|
|
|
fill.native_fee_or_rebate,
|
|
|
|
fill.fee_tier,
|
|
|
|
fill.order_id,
|
|
|
|
fill.log_index,
|
2023-03-05 22:52:42 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
if idx == 0 {
|
|
|
|
stmt = format!("{} {}", &stmt, val_str);
|
|
|
|
} else {
|
|
|
|
stmt = format!("{}, {}", &stmt, val_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-07 21:03:54 -07:00
|
|
|
let handle_conflict = "ON CONFLICT DO NOTHING";
|
2023-03-05 22:52:42 -08:00
|
|
|
|
|
|
|
stmt = format!("{} {}", stmt, handle_conflict);
|
|
|
|
stmt
|
|
|
|
}
|
|
|
|
|
2023-06-03 10:22:10 -07:00
|
|
|
pub fn build_candles_upsert_statement(candles: &Vec<Candle>) -> String {
|
2023-03-12 18:03:39 -07:00
|
|
|
let mut stmt = String::from("INSERT INTO candles (market_name, start_time, end_time, resolution, open, close, high, low, volume, complete) VALUES");
|
2023-03-12 00:13:57 -08:00
|
|
|
for (idx, candle) in candles.iter().enumerate() {
|
|
|
|
let val_str = format!(
|
|
|
|
"(\'{}\', \'{}\', \'{}\', \'{}\', {}, {}, {}, {}, {}, {})",
|
2023-03-12 18:03:39 -07:00
|
|
|
candle.market_name,
|
2023-03-12 00:13:57 -08:00
|
|
|
candle.start_time.to_rfc3339(),
|
|
|
|
candle.end_time.to_rfc3339(),
|
|
|
|
candle.resolution,
|
|
|
|
candle.open,
|
|
|
|
candle.close,
|
|
|
|
candle.high,
|
|
|
|
candle.low,
|
|
|
|
candle.volume,
|
|
|
|
candle.complete,
|
|
|
|
);
|
|
|
|
|
|
|
|
if idx == 0 {
|
|
|
|
stmt = format!("{} {}", &stmt, val_str);
|
|
|
|
} else {
|
|
|
|
stmt = format!("{}, {}", &stmt, val_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 18:03:39 -07:00
|
|
|
let handle_conflict = "ON CONFLICT (market_name, start_time, resolution)
|
2023-03-12 00:13:57 -08:00
|
|
|
DO UPDATE SET
|
|
|
|
open=excluded.open,
|
|
|
|
close=excluded.close,
|
|
|
|
high=excluded.high,
|
|
|
|
low=excluded.low,
|
|
|
|
volume=excluded.volume,
|
|
|
|
complete=excluded.complete
|
|
|
|
";
|
|
|
|
|
|
|
|
stmt = format!("{} {}", stmt, handle_conflict);
|
|
|
|
stmt
|
|
|
|
}
|
|
|
|
|
2023-06-07 21:03:54 -07:00
|
|
|
pub fn build_transactions_insert_statement(transactions: Vec<PgTransaction>) -> String {
|
|
|
|
let mut stmt = String::from("INSERT INTO transactions (signature, program_pk, block_datetime, slot, err, processed, worker_partition) VALUES");
|
|
|
|
for (idx, txn) in transactions.iter().enumerate() {
|
|
|
|
let val_str = format!(
|
|
|
|
"(\'{}\', \'{}\', \'{}\', \'{}\', {}, {}, {})",
|
|
|
|
txn.signature,
|
|
|
|
txn.program_pk,
|
|
|
|
txn.block_datetime.to_rfc3339(),
|
|
|
|
txn.slot,
|
|
|
|
txn.err,
|
|
|
|
txn.processed,
|
|
|
|
txn.worker_partition,
|
|
|
|
);
|
|
|
|
|
|
|
|
if idx == 0 {
|
|
|
|
stmt = format!("{} {}", &stmt, val_str);
|
|
|
|
} else {
|
|
|
|
stmt = format!("{}, {}", &stmt, val_str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let handle_conflict = "ON CONFLICT DO NOTHING";
|
|
|
|
|
|
|
|
stmt = format!("{} {}", stmt, handle_conflict);
|
|
|
|
stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build_transactions_processed_update_statement(
|
|
|
|
worker_id: i32,
|
|
|
|
processed_signatures: Vec<String>,
|
|
|
|
) -> String {
|
|
|
|
let mut stmt = String::from(
|
|
|
|
"UPDATE transactions
|
|
|
|
SET processed = true
|
|
|
|
WHERE transactions.signature IN (",
|
|
|
|
);
|
|
|
|
for (idx, sig) in processed_signatures.iter().enumerate() {
|
|
|
|
let val_str = if idx == processed_signatures.len() - 1 {
|
|
|
|
format!("\'{}\'", sig,)
|
|
|
|
} else {
|
|
|
|
format!("\'{}\',", sig,)
|
|
|
|
};
|
|
|
|
stmt = format!("{} {}", &stmt, val_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
let worker_stmt = format!(") AND worker_partition = {} ", worker_id);
|
|
|
|
|
|
|
|
stmt = format!("{} {}", stmt, worker_stmt);
|
|
|
|
stmt
|
|
|
|
}
|