fix: /tickers returns last prices if stale

This commit is contained in:
dboures 2023-05-29 23:31:57 -05:00
parent 9b69e380c3
commit b1999c0061
No known key found for this signature in database
GPG Key ID: AB3790129D478852
2 changed files with 57 additions and 33 deletions

View File

@ -273,22 +273,36 @@ pub async fn fetch_top_traders_by_quote_volume_from(
pub async fn fetch_coingecko_24h_volume( pub async fn fetch_coingecko_24h_volume(
pool: &Pool, pool: &Pool,
market_address_strings: &Vec<&str>,
) -> anyhow::Result<Vec<PgCoinGecko24HourVolume>> { ) -> anyhow::Result<Vec<PgCoinGecko24HourVolume>> {
let client = pool.get().await?; let client = pool.get().await?;
let stmt = client let stmt = client
.prepare( .prepare(
r#"select market as "address!", r#"SELECT
sum(native_qty_received) as "raw_base_size!", t1.market,
sum(native_qty_paid) as "raw_quote_size!" COALESCE(t2.native_qty_received, 0) as "raw_base_size!",
from fills COALESCE(t2.native_qty_paid, 0) as "raw_quote_size!"
where "time" >= current_timestamp - interval '1 day' FROM (
and bid = true SELECT distinct on (market) *
group by market"#, FROM fills f
where bid = true
and market = any($1)
order by market, "time" desc
) t1
LEFT JOIN (
select market,
sum(native_qty_received) as "native_qty_received",
sum(native_qty_paid) as "native_qty_paid"
from fills
where "time" >= current_timestamp - interval '1 day'
and bid = true
group by market
) t2 ON t1.market = t2.market"#,
) )
.await?; .await?;
let rows = client.query(&stmt, &[]).await?; let rows = client.query(&stmt, &[&market_address_strings]).await?;
Ok(rows Ok(rows
.into_iter() .into_iter()
@ -298,39 +312,47 @@ pub async fn fetch_coingecko_24h_volume(
pub async fn fetch_coingecko_24h_high_low( pub async fn fetch_coingecko_24h_high_low(
pool: &Pool, pool: &Pool,
market_names: &Vec<&str>,
) -> anyhow::Result<Vec<PgCoinGecko24HighLow>> { ) -> anyhow::Result<Vec<PgCoinGecko24HighLow>> {
let client = pool.get().await?; let client = pool.get().await?;
let stmt = client let stmt = client
.prepare( .prepare(
r#"select r#"select
g.market_name as "market_name!", r.market_name as "market_name!",
g.high as "high!", coalesce(c.high, r.high) as "high!",
g.low as "low!", coalesce(c.low, r.low) as "low!",
c."close" as "close!" r."close" as "close!"
from
(
SELECT
market_name,
max(start_time) as "start_time",
max(high) as "high",
min(low) as "low"
from from
candles (
where SELECT *
"resolution" = '1M' from
and "start_time" >= current_timestamp - interval '1 day' candles
group by where (market_name, start_time, resolution) in (
market_name select market_name, max(start_time), resolution
) as g from candles
join candles c on g.market_name = c.market_name where "resolution" = '1M'
and g.start_time = c.start_time and market_name = any($1)
where group by market_name, resolution
c.resolution = '1M'"#, )
) as r
left join (
SELECT
market_name,
max(start_time) as "start_time",
max(high) as "high",
min(low) as "low"
from
candles
where
"resolution" = '1M'
and "start_time" >= current_timestamp - interval '1 day'
group by market_name
) c on r.market_name = c.market_name"#,
) )
.await?; .await?;
let rows = client.query(&stmt, &[]).await?; let rows = client.query(&stmt, &[&market_names]).await?;
Ok(rows Ok(rows
.into_iter() .into_iter()

View File

@ -51,10 +51,12 @@ pub async fn pairs(context: web::Data<WebContext>) -> Result<HttpResponse, Serve
pub async fn tickers(context: web::Data<WebContext>) -> Result<HttpResponse, ServerError> { pub async fn tickers(context: web::Data<WebContext>) -> Result<HttpResponse, ServerError> {
// let client = RpcClient::new(context.rpc_url.clone()); // let client = RpcClient::new(context.rpc_url.clone());
let markets = &context.markets; let markets = &context.markets;
let market_names = markets.iter().map(|x| x.name.as_str()).collect();
let market_addresses = markets.iter().map(|x| x.address.as_str()).collect();
// let bba_fut = get_best_bids_and_asks(client, markets); // let bba_fut = get_best_bids_and_asks(client, markets);
let volume_fut = fetch_coingecko_24h_volume(&context.pool); let volume_fut = fetch_coingecko_24h_volume(&context.pool, &market_addresses);
let high_low_fut = fetch_coingecko_24h_high_low(&context.pool); let high_low_fut = fetch_coingecko_24h_high_low(&context.pool, &market_names);
let (volume_query, high_low_quey) = join!(volume_fut, high_low_fut,); let (volume_query, high_low_quey) = join!(volume_fut, high_low_fut,);