From 68b18b8327c801b9ce75b52c99a4f24da11b8309 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 13:25:34 -0500 Subject: [PATCH 01/11] basic summary --- crates/sui-deepbook-indexer/src/server.rs | 81 +++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index 3977034ed652e..fb8f2a3bf5700 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -46,6 +46,7 @@ pub const GET_NET_DEPOSITS: &str = "/get_net_deposits/:asset_ids/:timestamp"; pub const TICKER_PATH: &str = "/ticker"; pub const TRADES_PATH: &str = "/trades/:pool_name"; pub const ASSETS_PATH: &str = "/assets"; +pub const SUMMARY_PATH: &str = "/summary"; pub const LEVEL2_PATH: &str = "/orderbook/:pool_name"; pub const LEVEL2_MODULE: &str = "pool"; pub const LEVEL2_FUNCTION: &str = "get_level2_ticks_from_mid"; @@ -78,6 +79,7 @@ pub(crate) fn make_router(state: PgDeepbookPersistent) -> Router { .route(TICKER_PATH, get(ticker)) .route(TRADES_PATH, get(trades)) .route(ASSETS_PATH, get(assets)) + .route(SUMMARY_PATH, get(summary)) .with_state(state) } @@ -464,6 +466,85 @@ async fn fetch_historical_volume( .await .map(|Json(volumes)| volumes) } +async fn summary( + State(state): State, +) -> Result>>, DeepBookError> { + // Call the ticker function to get volumes and last price + let ticker_data = ticker(Query(HashMap::new()), State(state.clone())).await?; + let Json(ticker_map) = ticker_data; + + let mut response = Vec::new(); + + for (pool_name, ticker_info) in &ticker_map { + // Extract data from the ticker function response + let last_price = ticker_info + .get("last_price") + .and_then(|price| price.as_f64()) + .unwrap_or(0.0); + + let base_volume = ticker_info + .get("base_volume") + .and_then(|volume| volume.as_f64()) + .unwrap_or(0.0); + + let quote_volume = ticker_info + .get("quote_volume") + .and_then(|volume| volume.as_f64()) + .unwrap_or(0.0); + + // Call the orderbook function to get lowest ask and highest bid + let orderbook_data = orderbook( + Path(pool_name.clone()), + Query(HashMap::from([("level".to_string(), "1".to_string())])), + State(state.clone()), + ) + .await + .ok() + .map(|Json(data)| data); + + let lowest_ask = orderbook_data + .as_ref() + .and_then(|data| data.get("asks")) + .and_then(|asks| asks.as_array()) + .and_then(|asks| asks.get(0)) + .and_then(|ask| ask.as_array()) + .and_then(|ask| ask.get(0)) + .and_then(|price| price.as_str()?.parse::().ok()) + .unwrap_or(0.0); + + let highest_bid = orderbook_data + .as_ref() + .and_then(|data| data.get("bids")) + .and_then(|bids| bids.as_array()) + .and_then(|bids| bids.get(0)) + .and_then(|bid| bid.as_array()) + .and_then(|bid| bid.get(0)) + .and_then(|price| price.as_str()?.parse::().ok()) + .unwrap_or(0.0); + + // Prepare the summary data + let mut summary_data = HashMap::new(); + summary_data.insert( + "trading_pairs".to_string(), + Value::String(pool_name.clone()), + ); + summary_data.insert("last_price".to_string(), Value::from(last_price)); + summary_data.insert("lowest_ask".to_string(), Value::from(lowest_ask)); + summary_data.insert("highest_bid".to_string(), Value::from(highest_bid)); + summary_data.insert("base_volume".to_string(), Value::from(base_volume)); + summary_data.insert("quote_volume".to_string(), Value::from(quote_volume)); + summary_data.insert( + "price_change_percent_24h".to_string(), + Value::from(0.0), // Hardcoded for now + ); + summary_data.insert("highest_price_24h".to_string(), Value::from(0.0)); // Hardcoded for now + summary_data.insert("lowest_price_24h".to_string(), Value::from(0.0)); // Hardcoded for now + + response.push(summary_data); + } + + Ok(Json(response)) +} async fn trades( Path(pool_name): Path, From de0036e42cce3bb26c1efba695d470a6719c429a Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 13:39:06 -0500 Subject: [PATCH 02/11] 24hr price change --- crates/sui-deepbook-indexer/src/server.rs | 77 ++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index fb8f2a3bf5700..10c0cc14b3c3f 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -466,6 +466,7 @@ async fn fetch_historical_volume( .await .map(|Json(volumes)| volumes) } + async fn summary( State(state): State, ) -> Result>>, DeepBookError> { @@ -473,6 +474,10 @@ async fn summary( let ticker_data = ticker(Query(HashMap::new()), State(state.clone())).await?; let Json(ticker_map) = ticker_data; + // Call the price_change_24h function to get price changes + let price_change_data = price_change_24h(Query(HashMap::new()), State(state.clone())).await?; + let Json(price_change_map) = price_change_data; + let mut response = Vec::new(); for (pool_name, ticker_info) in &ticker_map { @@ -522,6 +527,9 @@ async fn summary( .and_then(|price| price.as_str()?.parse::().ok()) .unwrap_or(0.0); + // Fetch the 24-hour price change percent + let price_change_percent = price_change_map.get(pool_name).copied().unwrap_or(0.0); + // Prepare the summary data let mut summary_data = HashMap::new(); summary_data.insert( @@ -535,7 +543,7 @@ async fn summary( summary_data.insert("quote_volume".to_string(), Value::from(quote_volume)); summary_data.insert( "price_change_percent_24h".to_string(), - Value::from(0.0), // Hardcoded for now + Value::from(price_change_percent), ); summary_data.insert("highest_price_24h".to_string(), Value::from(0.0)); // Hardcoded for now summary_data.insert("lowest_price_24h".to_string(), Value::from(0.0)); // Hardcoded for now @@ -546,6 +554,73 @@ async fn summary( Ok(Json(response)) } +async fn price_change_24h( + Query(params): Query>, + State(state): State, +) -> Result>, DeepBookError> { + let connection = &mut state.pool.get().await?; + + // Calculate the timestamp for 24 hours ago + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_err(|_| DeepBookError::InternalError("System time error".to_string()))? + .as_millis() as i64; + + let timestamp_24h_ago = now - (24 * 60 * 60 * 1000); // 24 hours in milliseconds + + // Fetch pools data for metadata + let pools: Json> = get_pools(State(state.clone())).await?; + let pool_map: HashMap = pools + .0 + .iter() + .map(|pool| (pool.pool_id.clone(), pool)) + .collect(); + + let mut response = HashMap::new(); + + for (pool_id, pool) in pool_map.iter() { + let pool_name = &pool.pool_name; + + // Get the latest price <= 24 hours ago + let earliest_trade_24h = schema::order_fills::table + .filter(schema::order_fills::pool_id.eq(pool_id)) + .filter(schema::order_fills::checkpoint_timestamp_ms.le(timestamp_24h_ago)) + .order_by(schema::order_fills::checkpoint_timestamp_ms.desc()) + .select(schema::order_fills::price) + .first::(connection) + .await; + + // Get the most recent price + let most_recent_trade = schema::order_fills::table + .filter(schema::order_fills::pool_id.eq(pool_id)) + .order_by(schema::order_fills::checkpoint_timestamp_ms.desc()) + .select(schema::order_fills::price) + .first::(connection) + .await; + + if let (Ok(earliest_price), Ok(most_recent_price)) = (earliest_trade_24h, most_recent_trade) + { + let price_factor = + 10u64.pow((9 - pool.base_asset_decimals + pool.quote_asset_decimals) as u32); + + // Scale the prices + let earliest_price_scaled = earliest_price as f64 / price_factor as f64; + let most_recent_price_scaled = most_recent_price as f64 / price_factor as f64; + + // Calculate price change percentage + let price_change_percent = + ((most_recent_price_scaled / earliest_price_scaled) - 1.0) * 100.0; + + response.insert(pool_name.clone(), price_change_percent); + } else { + // If there's no price data for 24 hours or recent trades, insert 0.0 as price change + response.insert(pool_name.clone(), 0.0); + } + } + + Ok(Json(response)) +} + async fn trades( Path(pool_name): Path, State(state): State, From 4349885b5879680bcc12ae964ec5fe9ccd2b1550 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 13:58:30 -0500 Subject: [PATCH 03/11] required endpoints done --- crates/sui-deepbook-indexer/src/server.rs | 174 +++++++++++++--------- 1 file changed, 107 insertions(+), 67 deletions(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index 10c0cc14b3c3f..6d45b83370e06 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -474,86 +474,126 @@ async fn summary( let ticker_data = ticker(Query(HashMap::new()), State(state.clone())).await?; let Json(ticker_map) = ticker_data; + // Prepare pool metadata (including decimals and pool_id <-> pool_name mapping) + let pools: Json> = get_pools(State(state.clone())).await?; + let pool_metadata: HashMap = pools + .0 + .into_iter() + .map(|pool| { + ( + pool.pool_name.clone(), + ( + pool.pool_id.clone(), + (pool.base_asset_decimals, pool.quote_asset_decimals), + ), + ) + }) + .collect(); + + // Prepare pool decimals for scaling + let pool_decimals: HashMap = pool_metadata + .iter() + .map(|(_, (pool_id, decimals))| (pool_id.clone(), *decimals)) + .collect(); + // Call the price_change_24h function to get price changes let price_change_data = price_change_24h(Query(HashMap::new()), State(state.clone())).await?; let Json(price_change_map) = price_change_data; + // Call the high_low_prices_24h function to get the highest and lowest prices + let high_low_map = high_low_prices_24h(&pool_decimals, State(state.clone())).await?; + let mut response = Vec::new(); for (pool_name, ticker_info) in &ticker_map { - // Extract data from the ticker function response - let last_price = ticker_info - .get("last_price") - .and_then(|price| price.as_f64()) - .unwrap_or(0.0); - - let base_volume = ticker_info - .get("base_volume") - .and_then(|volume| volume.as_f64()) - .unwrap_or(0.0); - - let quote_volume = ticker_info - .get("quote_volume") - .and_then(|volume| volume.as_f64()) - .unwrap_or(0.0); - - // Call the orderbook function to get lowest ask and highest bid - let orderbook_data = orderbook( - Path(pool_name.clone()), - Query(HashMap::from([("level".to_string(), "1".to_string())])), - State(state.clone()), - ) - .await - .ok() - .map(|Json(data)| data); - - let lowest_ask = orderbook_data - .as_ref() - .and_then(|data| data.get("asks")) - .and_then(|asks| asks.as_array()) - .and_then(|asks| asks.get(0)) - .and_then(|ask| ask.as_array()) - .and_then(|ask| ask.get(0)) - .and_then(|price| price.as_str()?.parse::().ok()) - .unwrap_or(0.0); - - let highest_bid = orderbook_data - .as_ref() - .and_then(|data| data.get("bids")) - .and_then(|bids| bids.as_array()) - .and_then(|bids| bids.get(0)) - .and_then(|bid| bid.as_array()) - .and_then(|bid| bid.get(0)) - .and_then(|price| price.as_str()?.parse::().ok()) - .unwrap_or(0.0); - - // Fetch the 24-hour price change percent - let price_change_percent = price_change_map.get(pool_name).copied().unwrap_or(0.0); - - // Prepare the summary data - let mut summary_data = HashMap::new(); - summary_data.insert( - "trading_pairs".to_string(), - Value::String(pool_name.clone()), - ); - summary_data.insert("last_price".to_string(), Value::from(last_price)); - summary_data.insert("lowest_ask".to_string(), Value::from(lowest_ask)); - summary_data.insert("highest_bid".to_string(), Value::from(highest_bid)); - summary_data.insert("base_volume".to_string(), Value::from(base_volume)); - summary_data.insert("quote_volume".to_string(), Value::from(quote_volume)); - summary_data.insert( - "price_change_percent_24h".to_string(), - Value::from(price_change_percent), - ); - summary_data.insert("highest_price_24h".to_string(), Value::from(0.0)); // Hardcoded for now - summary_data.insert("lowest_price_24h".to_string(), Value::from(0.0)); // Hardcoded for now + if let Some((pool_id, _)) = pool_metadata.get(pool_name) { + // Extract data from the ticker function response + let last_price = ticker_info + .get("last_price") + .and_then(|price| price.as_f64()) + .unwrap_or(0.0); + + let base_volume = ticker_info + .get("base_volume") + .and_then(|volume| volume.as_f64()) + .unwrap_or(0.0); + + let quote_volume = ticker_info + .get("quote_volume") + .and_then(|volume| volume.as_f64()) + .unwrap_or(0.0); + + // Fetch the 24-hour price change percent + let price_change_percent = price_change_map.get(pool_name).copied().unwrap_or(0.0); + + // Fetch the highest and lowest prices in the last 24 hours + let (highest_price, lowest_price) = + high_low_map.get(pool_id).copied().unwrap_or((0.0, 0.0)); + + // Prepare the summary data + let mut summary_data = HashMap::new(); + summary_data.insert( + "trading_pairs".to_string(), + Value::String(pool_name.clone()), + ); + summary_data.insert("last_price".to_string(), Value::from(last_price)); + summary_data.insert("base_volume".to_string(), Value::from(base_volume)); + summary_data.insert("quote_volume".to_string(), Value::from(quote_volume)); + summary_data.insert( + "price_change_percent_24h".to_string(), + Value::from(price_change_percent), + ); + summary_data.insert("highest_price_24h".to_string(), Value::from(highest_price)); + summary_data.insert("lowest_price_24h".to_string(), Value::from(lowest_price)); - response.push(summary_data); + response.push(summary_data); + } } Ok(Json(response)) } +async fn high_low_prices_24h( + pool_decimals: &HashMap, + State(state): State, +) -> Result, DeepBookError> { + // Get the current timestamp in milliseconds + let end_time = SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_err(|_| DeepBookError::InternalError("System time error".to_string()))? + .as_millis() as i64; + + // Calculate the start time for 24 hours ago + let start_time = end_time - (24 * 60 * 60 * 1000); + + let connection = &mut state.pool.get().await?; + + // Query for trades within the last 24 hours for all pools + let results: Vec<(String, i64)> = schema::order_fills::table + .select((schema::order_fills::pool_id, schema::order_fills::price)) + .filter(schema::order_fills::checkpoint_timestamp_ms.between(start_time, end_time)) + .order_by(schema::order_fills::pool_id.asc()) + .load(connection) + .await?; + + // Aggregate the highest and lowest prices for each pool + let mut price_map: HashMap = HashMap::new(); + + for (pool_id, price) in results { + if let Some((base_decimals, quote_decimals)) = pool_decimals.get(&pool_id) { + let scaling_factor = 10f64.powi((9 - base_decimals + quote_decimals) as i32); + let price_f64 = price as f64 / scaling_factor; + + let entry = price_map.entry(pool_id).or_insert((f64::MIN, f64::MAX)); + // Update the highest and lowest prices + entry.0 = entry.0.max(price_f64); // Highest price + entry.1 = entry.1.min(price_f64); // Lowest price + } + } + + Ok(price_map) +} + async fn price_change_24h( Query(params): Query>, State(state): State, From 8b888a4a5ed39dbe45eb8f8d8905ae3290de3bbd Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 14:05:49 -0500 Subject: [PATCH 04/11] finish summary endpoint --- crates/sui-deepbook-indexer/src/server.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index 6d45b83370e06..bf4a183f362ab 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -497,7 +497,7 @@ async fn summary( .collect(); // Call the price_change_24h function to get price changes - let price_change_data = price_change_24h(Query(HashMap::new()), State(state.clone())).await?; + let price_change_data = price_change_24h(State(state.clone())).await?; let Json(price_change_map) = price_change_data; // Call the high_low_prices_24h function to get the highest and lowest prices @@ -530,12 +530,17 @@ async fn summary( let (highest_price, lowest_price) = high_low_map.get(pool_id).copied().unwrap_or((0.0, 0.0)); - // Prepare the summary data let mut summary_data = HashMap::new(); summary_data.insert( "trading_pairs".to_string(), Value::String(pool_name.clone()), ); + let parts: Vec<&str> = pool_name.split('_').collect(); + let base_currency = parts.get(0).unwrap_or(&"Unknown").to_string(); + let quote_currency = parts.get(1).unwrap_or(&"Unknown").to_string(); + + summary_data.insert("base_currency".to_string(), Value::String(base_currency)); + summary_data.insert("quote_currency".to_string(), Value::String(quote_currency)); summary_data.insert("last_price".to_string(), Value::from(last_price)); summary_data.insert("base_volume".to_string(), Value::from(base_volume)); summary_data.insert("quote_volume".to_string(), Value::from(quote_volume)); @@ -595,7 +600,6 @@ async fn high_low_prices_24h( } async fn price_change_24h( - Query(params): Query>, State(state): State, ) -> Result>, DeepBookError> { let connection = &mut state.pool.get().await?; From 9c0c9f914a92e0605c06116b49268e63b9b7d484 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 14:07:48 -0500 Subject: [PATCH 05/11] volume in quote by default --- crates/sui-deepbook-indexer/src/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index bf4a183f362ab..aaea14130acaa 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -167,7 +167,7 @@ async fn historical_volume( let volume_in_base = params .get("volume_in_base") .map(|v| v == "true") - .unwrap_or(true); + .unwrap_or(false); let column_to_query = if volume_in_base { sql::("base_quantity") } else { From 8f9ecc35c7a5976cf9273d380b61ce1a9e53b6cd Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 14:10:50 -0500 Subject: [PATCH 06/11] quote asset default --- crates/sui-deepbook-indexer/src/server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index aaea14130acaa..77f52707ba041 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -244,7 +244,7 @@ async fn get_historical_volume_by_balance_manager_id( let volume_in_base = params .get("volume_in_base") .map(|v| v == "true") - .unwrap_or(true); + .unwrap_or(false); let column_to_query = if volume_in_base { sql::("base_quantity") } else { @@ -333,7 +333,7 @@ async fn get_historical_volume_by_balance_manager_id_with_interval( let volume_in_base = params .get("volume_in_base") .map(|v| v == "true") - .unwrap_or(true); + .unwrap_or(false); let column_to_query = if volume_in_base { sql::("base_quantity") } else { From 3f9e12d55a3b6eed82b90c83490614836fcbf26d Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 14:17:43 -0500 Subject: [PATCH 07/11] 24hrquery --- crates/sui-deepbook-indexer/src/server.rs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index 77f52707ba041..430480cdd2755 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -497,8 +497,7 @@ async fn summary( .collect(); // Call the price_change_24h function to get price changes - let price_change_data = price_change_24h(State(state.clone())).await?; - let Json(price_change_map) = price_change_data; + let price_change_map = price_change_24h(&pool_metadata, State(state.clone())).await?; // Call the high_low_prices_24h function to get the highest and lowest prices let high_low_map = high_low_prices_24h(&pool_decimals, State(state.clone())).await?; @@ -600,8 +599,9 @@ async fn high_low_prices_24h( } async fn price_change_24h( + pool_metadata: &HashMap, State(state): State, -) -> Result>, DeepBookError> { +) -> Result, DeepBookError> { let connection = &mut state.pool.get().await?; // Calculate the timestamp for 24 hours ago @@ -612,19 +612,9 @@ async fn price_change_24h( let timestamp_24h_ago = now - (24 * 60 * 60 * 1000); // 24 hours in milliseconds - // Fetch pools data for metadata - let pools: Json> = get_pools(State(state.clone())).await?; - let pool_map: HashMap = pools - .0 - .iter() - .map(|pool| (pool.pool_id.clone(), pool)) - .collect(); - let mut response = HashMap::new(); - for (pool_id, pool) in pool_map.iter() { - let pool_name = &pool.pool_name; - + for (pool_name, (pool_id, (base_decimals, quote_decimals))) in pool_metadata.iter() { // Get the latest price <= 24 hours ago let earliest_trade_24h = schema::order_fills::table .filter(schema::order_fills::pool_id.eq(pool_id)) @@ -644,8 +634,7 @@ async fn price_change_24h( if let (Ok(earliest_price), Ok(most_recent_price)) = (earliest_trade_24h, most_recent_trade) { - let price_factor = - 10u64.pow((9 - pool.base_asset_decimals + pool.quote_asset_decimals) as u32); + let price_factor = 10u64.pow((9 - base_decimals + quote_decimals) as u32); // Scale the prices let earliest_price_scaled = earliest_price as f64 / price_factor as f64; @@ -662,7 +651,7 @@ async fn price_change_24h( } } - Ok(Json(response)) + Ok(response) } async fn trades( From 8811d6977e1ed9ebce3293a51405dfc8a9cf352e Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 14:25:25 -0500 Subject: [PATCH 08/11] highest bid and lowest ask --- crates/sui-deepbook-indexer/src/server.rs | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index 430480cdd2755..1d31c22c5114a 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -529,6 +529,36 @@ async fn summary( let (highest_price, lowest_price) = high_low_map.get(pool_id).copied().unwrap_or((0.0, 0.0)); + // Fetch the highest bid and lowest ask from the orderbook + let orderbook_data = orderbook( + Path(pool_name.clone()), + Query(HashMap::from([("level".to_string(), "1".to_string())])), + State(state.clone()), + ) + .await + .ok() + .map(|Json(data)| data); + + let highest_bid = orderbook_data + .as_ref() + .and_then(|data| data.get("bids")) + .and_then(|bids| bids.as_array()) + .and_then(|bids| bids.get(0)) + .and_then(|bid| bid.as_array()) + .and_then(|bid| bid.get(0)) + .and_then(|price| price.as_str()?.parse::().ok()) + .unwrap_or(0.0); + + let lowest_ask = orderbook_data + .as_ref() + .and_then(|data| data.get("asks")) + .and_then(|asks| asks.as_array()) + .and_then(|asks| asks.get(0)) + .and_then(|ask| ask.as_array()) + .and_then(|ask| ask.get(0)) + .and_then(|price| price.as_str()?.parse::().ok()) + .unwrap_or(0.0); + let mut summary_data = HashMap::new(); summary_data.insert( "trading_pairs".to_string(), @@ -549,6 +579,8 @@ async fn summary( ); summary_data.insert("highest_price_24h".to_string(), Value::from(highest_price)); summary_data.insert("lowest_price_24h".to_string(), Value::from(lowest_price)); + summary_data.insert("highest_bid".to_string(), Value::from(highest_bid)); + summary_data.insert("lowest_ask".to_string(), Value::from(lowest_ask)); response.push(summary_data); } From 375a3253f9fe3a2da9db333e11b9fdccdaf86b11 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 14:25:59 -0500 Subject: [PATCH 09/11] mutable error --- crates/sui-deepbook-indexer/src/server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index 1d31c22c5114a..866d80f713fd6 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -564,8 +564,8 @@ async fn summary( "trading_pairs".to_string(), Value::String(pool_name.clone()), ); - let parts: Vec<&str> = pool_name.split('_').collect(); - let base_currency = parts.get(0).unwrap_or(&"Unknown").to_string(); + let mut parts: Vec<&str> = pool_name.split('_').collect(); + let base_currency = parts.first_mut().unwrap_or(&mut "Unknown").to_string(); let quote_currency = parts.get(1).unwrap_or(&"Unknown").to_string(); summary_data.insert("base_currency".to_string(), Value::String(base_currency)); From 41132c90bc55ed3bedfb39e02c51f2d1ac5f65ad Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 15:15:38 -0500 Subject: [PATCH 10/11] clippy --- crates/sui-deepbook-indexer/src/server.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index 866d80f713fd6..ea33931315b9f 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -454,6 +454,7 @@ async fn ticker( Ok(Json(response)) } +#[allow(clippy::get_first)] async fn fetch_historical_volume( params: &HashMap, volume_in_base: bool, @@ -564,8 +565,8 @@ async fn summary( "trading_pairs".to_string(), Value::String(pool_name.clone()), ); - let mut parts: Vec<&str> = pool_name.split('_').collect(); - let base_currency = parts.first_mut().unwrap_or(&mut "Unknown").to_string(); + let parts: Vec<&str> = pool_name.split('_').collect(); + let base_currency = parts.get(0).unwrap_or(&"Unknown").to_string(); let quote_currency = parts.get(1).unwrap_or(&"Unknown").to_string(); summary_data.insert("base_currency".to_string(), Value::String(base_currency)); From 77a278eda4d5081bb6b87ae0aa85df5fade7bfea Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 6 Dec 2024 15:35:08 -0500 Subject: [PATCH 11/11] clippy --- crates/sui-deepbook-indexer/src/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/sui-deepbook-indexer/src/server.rs b/crates/sui-deepbook-indexer/src/server.rs index ea33931315b9f..f7cb029ad97b2 100644 --- a/crates/sui-deepbook-indexer/src/server.rs +++ b/crates/sui-deepbook-indexer/src/server.rs @@ -454,7 +454,6 @@ async fn ticker( Ok(Json(response)) } -#[allow(clippy::get_first)] async fn fetch_historical_volume( params: &HashMap, volume_in_base: bool, @@ -468,6 +467,7 @@ async fn fetch_historical_volume( .map(|Json(volumes)| volumes) } +#[allow(clippy::get_first)] async fn summary( State(state): State, ) -> Result>>, DeepBookError> {