2023-05-14 00:15:10 -07:00
|
|
|
use crate::structs::{candle::Candle, openbook::PgOpenBookFill, resolution::Resolution};
|
2023-03-11 14:50:22 -08:00
|
|
|
use chrono::{DateTime, Utc};
|
2023-05-14 00:15:10 -07:00
|
|
|
use deadpool_postgres::{GenericClient, Pool};
|
2023-03-11 14:50:22 -08:00
|
|
|
|
|
|
|
pub async fn fetch_earliest_fill(
|
2023-05-14 00:15:10 -07:00
|
|
|
pool: &Pool,
|
2023-03-11 14:50:22 -08:00
|
|
|
market_address_string: &str,
|
|
|
|
) -> anyhow::Result<Option<PgOpenBookFill>> {
|
2023-05-14 00:15:10 -07:00
|
|
|
let client = pool.get().await?;
|
|
|
|
|
|
|
|
let stmt = client
|
|
|
|
.prepare(
|
|
|
|
r#"SELECT
|
|
|
|
time as "time!",
|
|
|
|
bid as "bid!",
|
|
|
|
maker as "maker!",
|
|
|
|
native_qty_paid as "native_qty_paid!",
|
|
|
|
native_qty_received as "native_qty_received!",
|
|
|
|
native_fee_or_rebate as "native_fee_or_rebate!"
|
|
|
|
from fills
|
|
|
|
where market = $1
|
|
|
|
and maker = true
|
|
|
|
ORDER BY time asc LIMIT 1"#,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let row = client.query_opt(&stmt, &[&market_address_string]).await?;
|
|
|
|
|
|
|
|
match row {
|
|
|
|
Some(r) => Ok(Some(PgOpenBookFill::from_row(r))),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
2023-03-11 14:50:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch_fills_from(
|
2023-05-14 00:15:10 -07:00
|
|
|
pool: &Pool,
|
2023-03-11 14:50:22 -08:00
|
|
|
market_address_string: &str,
|
|
|
|
start_time: DateTime<Utc>,
|
|
|
|
end_time: DateTime<Utc>,
|
|
|
|
) -> anyhow::Result<Vec<PgOpenBookFill>> {
|
2023-05-14 00:15:10 -07:00
|
|
|
let client = pool.get().await?;
|
|
|
|
|
|
|
|
let stmt = client
|
|
|
|
.prepare(
|
|
|
|
r#"SELECT
|
2023-03-11 14:50:22 -08:00
|
|
|
time as "time!",
|
|
|
|
bid as "bid!",
|
|
|
|
maker as "maker!",
|
|
|
|
native_qty_paid as "native_qty_paid!",
|
|
|
|
native_qty_received as "native_qty_received!",
|
|
|
|
native_fee_or_rebate as "native_fee_or_rebate!"
|
|
|
|
from fills
|
|
|
|
where market = $1
|
2023-05-14 00:15:10 -07:00
|
|
|
and time >= $2::timestamptz
|
|
|
|
and time < $3::timestamptz
|
2023-03-12 17:48:53 -07:00
|
|
|
and maker = true
|
2023-03-11 14:50:22 -08:00
|
|
|
ORDER BY time asc"#,
|
2023-05-14 00:15:10 -07:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let rows = client
|
|
|
|
.query(&stmt, &[&market_address_string, &start_time, &end_time])
|
|
|
|
.await?;
|
|
|
|
Ok(rows
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| PgOpenBookFill::from_row(r))
|
|
|
|
.collect())
|
2023-03-11 14:50:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch_latest_finished_candle(
|
2023-05-14 00:15:10 -07:00
|
|
|
pool: &Pool,
|
2023-03-12 18:03:39 -07:00
|
|
|
market_name: &str,
|
2023-03-11 14:50:22 -08:00
|
|
|
resolution: Resolution,
|
|
|
|
) -> anyhow::Result<Option<Candle>> {
|
2023-05-14 00:15:10 -07:00
|
|
|
let client = pool.get().await?;
|
|
|
|
|
|
|
|
let stmt = client
|
|
|
|
.prepare(
|
|
|
|
r#"SELECT
|
|
|
|
market_name as "market_name!",
|
2023-03-11 14:50:22 -08:00
|
|
|
start_time as "start_time!",
|
|
|
|
end_time as "end_time!",
|
|
|
|
resolution as "resolution!",
|
|
|
|
open as "open!",
|
|
|
|
close as "close!",
|
|
|
|
high as "high!",
|
|
|
|
low as "low!",
|
|
|
|
volume as "volume!",
|
|
|
|
complete as "complete!"
|
|
|
|
from candles
|
2023-03-12 18:03:39 -07:00
|
|
|
where market_name = $1
|
2023-03-11 14:50:22 -08:00
|
|
|
and resolution = $2
|
|
|
|
and complete = true
|
|
|
|
ORDER BY start_time desc LIMIT 1"#,
|
2023-05-14 00:15:10 -07:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let row = client
|
|
|
|
.query_opt(&stmt, &[&market_name, &resolution.to_string()])
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
match row {
|
|
|
|
Some(r) => Ok(Some(Candle::from_row(r))),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
2023-03-11 14:50:22 -08:00
|
|
|
}
|
|
|
|
|
2023-03-27 12:01:48 -07:00
|
|
|
pub async fn fetch_earliest_candles(
|
2023-05-14 00:15:10 -07:00
|
|
|
pool: &Pool,
|
2023-03-12 18:03:39 -07:00
|
|
|
market_name: &str,
|
2023-03-12 00:13:57 -08:00
|
|
|
resolution: Resolution,
|
2023-03-27 12:01:48 -07:00
|
|
|
) -> anyhow::Result<Vec<Candle>> {
|
2023-05-14 00:15:10 -07:00
|
|
|
let client = pool.get().await?;
|
|
|
|
|
|
|
|
let stmt = client
|
|
|
|
.prepare(
|
|
|
|
r#"SELECT
|
|
|
|
market_name as "market_name!",
|
2023-03-12 00:13:57 -08:00
|
|
|
start_time as "start_time!",
|
|
|
|
end_time as "end_time!",
|
|
|
|
resolution as "resolution!",
|
|
|
|
open as "open!",
|
|
|
|
close as "close!",
|
|
|
|
high as "high!",
|
|
|
|
low as "low!",
|
|
|
|
volume as "volume!",
|
|
|
|
complete as "complete!"
|
|
|
|
from candles
|
2023-03-12 18:03:39 -07:00
|
|
|
where market_name = $1
|
2023-03-12 00:13:57 -08:00
|
|
|
and resolution = $2
|
2023-03-27 12:01:48 -07:00
|
|
|
ORDER BY start_time asc"#,
|
2023-05-14 00:15:10 -07:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let rows = client
|
|
|
|
.query(&stmt, &[&market_name, &resolution.to_string()])
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(rows.into_iter().map(|r| Candle::from_row(r)).collect())
|
2023-03-12 00:13:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn fetch_candles_from(
|
2023-05-14 00:15:10 -07:00
|
|
|
pool: &Pool,
|
2023-03-12 22:03:58 -07:00
|
|
|
market_name: &str,
|
2023-03-12 00:13:57 -08:00
|
|
|
resolution: Resolution,
|
|
|
|
start_time: DateTime<Utc>,
|
|
|
|
end_time: DateTime<Utc>,
|
|
|
|
) -> anyhow::Result<Vec<Candle>> {
|
2023-05-14 00:15:10 -07:00
|
|
|
let client = pool.get().await?;
|
2023-03-12 22:03:58 -07:00
|
|
|
|
2023-05-14 00:15:10 -07:00
|
|
|
let stmt = client
|
|
|
|
.prepare(
|
|
|
|
r#"SELECT
|
|
|
|
market_name as "market_name!",
|
2023-03-12 22:03:58 -07:00
|
|
|
start_time as "start_time!",
|
|
|
|
end_time as "end_time!",
|
|
|
|
resolution as "resolution!",
|
|
|
|
open as "open!",
|
|
|
|
close as "close!",
|
|
|
|
high as "high!",
|
|
|
|
low as "low!",
|
|
|
|
volume as "volume!",
|
|
|
|
complete as "complete!"
|
|
|
|
from candles
|
|
|
|
where market_name = $1
|
|
|
|
and resolution = $2
|
|
|
|
and start_time >= $3
|
|
|
|
and end_time <= $4
|
2023-05-05 18:41:17 -07:00
|
|
|
ORDER BY start_time asc"#,
|
2023-05-14 00:15:10 -07:00
|
|
|
)
|
|
|
|
.await?;
|
2023-03-13 09:51:30 -07:00
|
|
|
|
2023-05-14 00:15:10 -07:00
|
|
|
let rows = client
|
|
|
|
.query(
|
|
|
|
&stmt,
|
|
|
|
&[
|
|
|
|
&market_name,
|
|
|
|
&resolution.to_string(),
|
|
|
|
&start_time,
|
|
|
|
&end_time,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.await?;
|
2023-05-05 18:41:17 -07:00
|
|
|
|
2023-05-14 00:15:10 -07:00
|
|
|
Ok(rows.into_iter().map(|r| Candle::from_row(r)).collect())
|
2023-05-05 18:41:17 -07:00
|
|
|
}
|