diff --git a/lib/ain-ocean/src/indexer/oracle.rs b/lib/ain-ocean/src/indexer/oracle.rs index a62ce614c7..a6c0cfb522 100644 --- a/lib/ain-ocean/src/indexer/oracle.rs +++ b/lib/ain-ocean/src/indexer/oracle.rs @@ -379,34 +379,28 @@ 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 price_repo = &services.price_ticker; + let sort_key = price_repo.by_key.get(&(token.clone(), currency.clone()))?; + if let Some(sort_key) = sort_key { + price_repo.by_id.delete(&sort_key)?; } - let id = ( + let new_sort_key = ( price_aggregated.aggregated.oracles.total.to_be_bytes(), price_aggregated.block.height.to_be_bytes(), - token, - currency, + token.clone(), + currency.clone(), ); - price_repo.put( - &id, + price_repo.by_id.put( + &new_sort_key, &PriceTicker { price: price_aggregated, }, )?; + price_repo.by_key.put( + &(token, currency), + &new_sort_key + )?; } Ok(()) } diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index bde84b9312..c72b6a9f67 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -93,6 +93,7 @@ pub struct OracleHistoryService { pub struct PriceTickerService { by_id: PriceTicker, + by_key: PriceTickerKey, } pub struct ScriptActivityService { @@ -195,6 +196,7 @@ impl Services { }, price_ticker: PriceTickerService { by_id: PriceTicker::new(Arc::clone(&store)), + by_key: PriceTickerKey::new(Arc::clone(&store)), }, script_activity: ScriptActivityService { by_id: ScriptActivity::new(Arc::clone(&store)), diff --git a/lib/ain-ocean/src/model/price_ticker.rs b/lib/ain-ocean/src/model/price_ticker.rs index 713c058b53..e2d7ea8c46 100644 --- a/lib/ain-ocean/src/model/price_ticker.rs +++ b/lib/ain-ocean/src/model/price_ticker.rs @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize}; use super::oracle_price_aggregated::OraclePriceAggregated; pub type PriceTickerId = ([u8; 4], [u8; 4], Token, Currency); // total-height-token-currency +pub type PriceTickerKey = (Token, Currency); #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] diff --git a/lib/ain-ocean/src/storage/mod.rs b/lib/ain-ocean/src/storage/mod.rs index 1f373022c4..2159eda832 100644 --- a/lib/ain-ocean/src/storage/mod.rs +++ b/lib/ain-ocean/src/storage/mod.rs @@ -372,6 +372,14 @@ define_table! { } } +define_table! { + #[derive(Debug)] + pub struct PriceTickerKey { + key_type = model::PriceTickerKey, + value_type = model::PriceTickerId, + } +} + define_table! { #[derive(Debug)] pub struct RawBlock { @@ -508,7 +516,7 @@ define_table! { } } -pub const COLUMN_NAMES: [&str; 27] = [ +pub const COLUMN_NAMES: [&str; 28] = [ Block::NAME, BlockByHeight::NAME, MasternodeStats::NAME, @@ -525,6 +533,7 @@ pub const COLUMN_NAMES: [&str; 27] = [ PoolSwapAggregatedKey::NAME, PoolSwap::NAME, PriceTicker::NAME, + PriceTickerKey::NAME, RawBlock::NAME, ScriptActivity::NAME, ScriptAggregation::NAME,