Skip to content

Commit

Permalink
filter_map to get sub_root
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Dec 4, 2024
1 parent 813cbce commit 850fa5a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
13 changes: 9 additions & 4 deletions lib/ain-ocean/src/api/prices.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{str::FromStr, sync::Arc};
use std::{collections::HashSet, str::FromStr, sync::Arc};

use ain_dftx::{Currency, Token, Weightage, COIN};
use ain_macros::ocean_endpoint;
Expand Down Expand Up @@ -127,16 +127,21 @@ 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()))
.map(|item| {
let ((_, _, token, currency), v) = item?;
Ok(PriceTickerResponse::from(((token, currency), v)))
.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))))
})
.collect::<Result<Vec<_>>>()?;

Expand Down
19 changes: 2 additions & 17 deletions lib/ain-ocean/src/indexer/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,29 +378,14 @@ fn index_set_oracle_data(
.oracle_price_aggregated
.by_id
.put(&id, &price_aggregated)?;
let price_repo = &services.price_ticker;

let id = (
price_aggregated.aggregated.oracles.total.to_be_bytes(),
price_aggregated.block.height.to_be_bytes(),
token.clone(),
currency.clone(),
);
// NOTE(canonbrother): rocksdb sort by key by default
// temp solution: clean up extra data to allow limit by `token-currency` but sort by `count-height-token-currency`
{
let prev_price = price_repo
.by_id
.list(Some(id.clone()), SortOrder::Descending)?
.find(|item| match item {
Ok(((_, _, t, c), _)) => t == &token && c == &currency,
_ => true,
})
.transpose()?;
if let Some((k, _)) = prev_price {
price_repo.by_id.delete(&k)?
}
}
price_repo.by_id.put(
services.price_ticker.by_id.put(
&id,
&PriceTicker {
price: price_aggregated,
Expand Down
3 changes: 2 additions & 1 deletion lib/ain-ocean/src/model/price_ticker.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use ain_dftx::{Currency, Token};
use serde::{Deserialize, Serialize};

use super::oracle_price_aggregated::OraclePriceAggregated;

pub type PriceTickerId = ([u8; 4], [u8; 4], String, String); // total-height-token-currency
pub type PriceTickerId = ([u8; 4], [u8; 4], Token, Currency); // total-height-token-currency

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down

0 comments on commit 850fa5a

Please sign in to comment.