Skip to content

Commit

Permalink
only store latest data in db
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Dec 6, 2024
1 parent 746fdb7 commit 0f652e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
17 changes: 5 additions & 12 deletions lib/ain-ocean/src/api/prices.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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::<Result<Vec<_>>>()?;

Expand Down
18 changes: 17 additions & 1 deletion lib/ain-ocean/src/indexer/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0f652e3

Please sign in to comment.