From 0f652e3d2f19354c5097473e6c982ac274903ffa Mon Sep 17 00:00:00 2001 From: canonbrother Date: Fri, 6 Dec 2024 09:45:14 +0800 Subject: [PATCH] only store latest data in db --- lib/ain-ocean/src/api/prices.rs | 17 +++++------------ lib/ain-ocean/src/indexer/oracle.rs | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/ain-ocean/src/api/prices.rs b/lib/ain-ocean/src/api/prices.rs index 4e371f1af0..31bb6eb8f2 100644 --- a/lib/ain-ocean/src/api/prices.rs +++ b/lib/ain-ocean/src/api/prices.rs @@ -1,4 +1,4 @@ -use std::{collections::HashSet, str::FromStr, sync::Arc}; +use std::{str::FromStr, sync::Arc}; use ain_dftx::{Currency, Token, Weightage, COIN}; use ain_macros::ocean_endpoint; @@ -127,21 +127,14 @@ async fn list_prices( }) .transpose()?; - let mut seen = HashSet::new(); let prices = ctx .services .price_ticker .by_id - .list(next.clone(), SortOrder::Descending)? - .take(query.size + usize::from(next.clone().is_some())) - .skip(usize::from(next.is_some())) - .filter_map(|item| { - let ((_, _, token, currency), v) = item.ok()?; - if seen.contains(&(token.clone(), currency.clone())) { - return None; - } - seen.insert((token.clone(), currency.clone())); - Some(Ok(PriceTickerResponse::from(((token, currency), v)))) + .list(next, SortOrder::Descending)? + .map(|item| { + let ((_, _, token, currency), v) = item?; + Ok(PriceTickerResponse::from(((token, currency), v))) }) .collect::>>()?; diff --git a/lib/ain-ocean/src/indexer/oracle.rs b/lib/ain-ocean/src/indexer/oracle.rs index 8fc2eccfed..a62ce614c7 100644 --- a/lib/ain-ocean/src/indexer/oracle.rs +++ b/lib/ain-ocean/src/indexer/oracle.rs @@ -379,13 +379,29 @@ fn index_set_oracle_data( .by_id .put(&id, &price_aggregated)?; + let price_repo = &services.price_ticker.by_id; + let prev = price_repo + .list( + Some(([0xffu8; 4], [0xffu8; 4], token.clone(), currency.clone())), + SortOrder::Descending, + )? + .find(|item| match item { + Ok((k, _)) => k.2 == token.clone() && k.3 == currency.clone(), + _ => true, + }) + .transpose()?; + + if let Some((k, _)) = prev { + price_repo.delete(&k)?; + } + let id = ( price_aggregated.aggregated.oracles.total.to_be_bytes(), price_aggregated.block.height.to_be_bytes(), token, currency, ); - services.price_ticker.by_id.put( + price_repo.put( &id, &PriceTicker { price: price_aggregated,