From b1999c00615f2f813ce63e85b78726f0689ddf9c Mon Sep 17 00:00:00 2001 From: dboures Date: Mon, 29 May 2023 23:31:57 -0500 Subject: [PATCH] fix: /tickers returns last prices if stale --- src/database/fetch.rs | 84 ++++++++++++++++++++++++++--------------- src/server/coingecko.rs | 6 ++- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/database/fetch.rs b/src/database/fetch.rs index d0882ef..0abd820 100644 --- a/src/database/fetch.rs +++ b/src/database/fetch.rs @@ -273,22 +273,36 @@ pub async fn fetch_top_traders_by_quote_volume_from( pub async fn fetch_coingecko_24h_volume( pool: &Pool, + market_address_strings: &Vec<&str>, ) -> anyhow::Result> { let client = pool.get().await?; let stmt = client .prepare( - r#"select market as "address!", - sum(native_qty_received) as "raw_base_size!", - sum(native_qty_paid) as "raw_quote_size!" - from fills - where "time" >= current_timestamp - interval '1 day' - and bid = true - group by market"#, + r#"SELECT + t1.market, + COALESCE(t2.native_qty_received, 0) as "raw_base_size!", + COALESCE(t2.native_qty_paid, 0) as "raw_quote_size!" + FROM ( + SELECT distinct on (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?; - let rows = client.query(&stmt, &[]).await?; + let rows = client.query(&stmt, &[&market_address_strings]).await?; Ok(rows .into_iter() @@ -298,39 +312,47 @@ pub async fn fetch_coingecko_24h_volume( pub async fn fetch_coingecko_24h_high_low( pool: &Pool, + market_names: &Vec<&str>, ) -> anyhow::Result> { let client = pool.get().await?; let stmt = client .prepare( r#"select - g.market_name as "market_name!", - g.high as "high!", - g.low as "low!", - c."close" as "close!" - from - ( - SELECT - market_name, - max(start_time) as "start_time", - max(high) as "high", - min(low) as "low" + r.market_name as "market_name!", + coalesce(c.high, r.high) as "high!", + coalesce(c.low, r.low) as "low!", + r."close" as "close!" from - candles - where - "resolution" = '1M' - and "start_time" >= current_timestamp - interval '1 day' - group by - market_name - ) as g - join candles c on g.market_name = c.market_name - and g.start_time = c.start_time - where - c.resolution = '1M'"#, + ( + SELECT * + from + candles + where (market_name, start_time, resolution) in ( + select market_name, max(start_time), resolution + from candles + where "resolution" = '1M' + and market_name = any($1) + group by market_name, resolution + ) + ) 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?; - let rows = client.query(&stmt, &[]).await?; + let rows = client.query(&stmt, &[&market_names]).await?; Ok(rows .into_iter() diff --git a/src/server/coingecko.rs b/src/server/coingecko.rs index a507c7e..b639e40 100644 --- a/src/server/coingecko.rs +++ b/src/server/coingecko.rs @@ -51,10 +51,12 @@ pub async fn pairs(context: web::Data) -> Result) -> Result { // let client = RpcClient::new(context.rpc_url.clone()); 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 volume_fut = fetch_coingecko_24h_volume(&context.pool); - let high_low_fut = fetch_coingecko_24h_high_low(&context.pool); + let volume_fut = fetch_coingecko_24h_volume(&context.pool, &market_addresses); + 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,);