openbook-candles/src/database/fetch.rs

159 lines
4.0 KiB
Rust
Raw Normal View History

2023-03-11 14:50:22 -08:00
use chrono::{DateTime, Utc};
use sqlx::{Pool, Postgres};
use crate::{database::PgOpenBookFill, utils::AnyhowWrap};
use super::{Candle, Resolution};
pub async fn fetch_earliest_fill(
pool: &Pool<Postgres>,
market_address_string: &str,
) -> anyhow::Result<Option<PgOpenBookFill>> {
sqlx::query_as!(
PgOpenBookFill,
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
2023-03-12 17:48:53 -07:00
and maker = true
2023-03-11 14:50:22 -08:00
ORDER BY time asc LIMIT 1"#,
market_address_string
)
.fetch_optional(pool)
.await
.map_err_anyhow()
}
pub async fn fetch_fills_from(
pool: &Pool<Postgres>,
market_address_string: &str,
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
) -> anyhow::Result<Vec<PgOpenBookFill>> {
sqlx::query_as!(
PgOpenBookFill,
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 time >= $2
and time < $3
2023-03-12 17:48:53 -07:00
and maker = true
2023-03-11 14:50:22 -08:00
ORDER BY time asc"#,
market_address_string,
start_time,
end_time
)
.fetch_all(pool)
.await
.map_err_anyhow()
}
pub async fn fetch_latest_finished_candle(
pool: &Pool<Postgres>,
market_address_string: &str,
resolution: Resolution,
) -> anyhow::Result<Option<Candle>> {
sqlx::query_as!(
Candle,
r#"SELECT
start_time as "start_time!",
end_time as "end_time!",
resolution as "resolution!",
market as "market!",
open as "open!",
close as "close!",
high as "high!",
low as "low!",
volume as "volume!",
complete as "complete!"
from candles
where market = $1
and resolution = $2
and complete = true
ORDER BY start_time desc LIMIT 1"#,
market_address_string,
resolution.to_string()
)
.fetch_optional(pool)
.await
.map_err_anyhow()
}
2023-03-12 00:13:57 -08:00
pub async fn fetch_earliest_candle(
pool: &Pool<Postgres>,
market_address_string: &str,
resolution: Resolution,
) -> anyhow::Result<Option<Candle>> {
sqlx::query_as!(
Candle,
r#"SELECT
start_time as "start_time!",
end_time as "end_time!",
resolution as "resolution!",
market as "market!",
open as "open!",
close as "close!",
high as "high!",
low as "low!",
volume as "volume!",
complete as "complete!"
from candles
where market = $1
and resolution = $2
ORDER BY start_time asc LIMIT 1"#,
market_address_string,
resolution.to_string()
)
.fetch_optional(pool)
.await
.map_err_anyhow()
}
pub async fn fetch_candles_from(
pool: &Pool<Postgres>,
market_address_string: &str,
resolution: Resolution,
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
) -> anyhow::Result<Vec<Candle>> {
sqlx::query_as!(
Candle,
r#"SELECT
start_time as "start_time!",
end_time as "end_time!",
resolution as "resolution!",
market as "market!",
open as "open!",
close as "close!",
high as "high!",
low as "low!",
volume as "volume!",
complete as "complete!"
from candles
where market = $1
and resolution = $2
and start_time >= $3
and end_time <= $4
and complete = true
ORDER BY start_time asc"#,
market_address_string,
resolution.to_string(),
start_time,
end_time
)
.fetch_all(pool)
.await
.map_err_anyhow()
}