Skip to content

Commit

Permalink
add PriceTickerKey
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Dec 9, 2024
1 parent 0f652e3 commit baac91c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
32 changes: 13 additions & 19 deletions lib/ain-ocean/src/indexer/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
2 changes: 2 additions & 0 deletions lib/ain-ocean/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub struct OracleHistoryService {

pub struct PriceTickerService {
by_id: PriceTicker,
by_key: PriceTickerKey,
}

pub struct ScriptActivityService {
Expand Down Expand Up @@ -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)),
Expand Down
1 change: 1 addition & 0 deletions lib/ain-ocean/src/model/price_ticker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
11 changes: 10 additions & 1 deletion lib/ain-ocean/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -508,7 +516,7 @@ define_table! {
}
}

pub const COLUMN_NAMES: [&str; 27] = [
pub const COLUMN_NAMES: [&str; 28] = [
Block::NAME,
BlockByHeight::NAME,
MasternodeStats::NAME,
Expand All @@ -525,6 +533,7 @@ pub const COLUMN_NAMES: [&str; 27] = [
PoolSwapAggregatedKey::NAME,
PoolSwap::NAME,
PriceTicker::NAME,
PriceTickerKey::NAME,
RawBlock::NAME,
ScriptActivity::NAME,
ScriptAggregation::NAME,
Expand Down

0 comments on commit baac91c

Please sign in to comment.