Skip to content

Commit

Permalink
fix oracle_price_active is not indexed
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Oct 22, 2024
1 parent f3db7e6 commit c46c3d9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
54 changes: 22 additions & 32 deletions lib/ain-ocean/src/indexer/loan_token.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{str::FromStr, sync::Arc};

use ain_dftx::loans::SetLoanToken;
use ain_dftx::{loans::SetLoanToken, Currency, Token};
use log::trace;
use rust_decimal::{prelude::Zero, Decimal};
use rust_decimal_macros::dec;
Expand Down Expand Up @@ -104,18 +104,22 @@ fn map_active_price(
block: &BlockContext,
ticker_id: (String, String),
aggregated_price: OraclePriceAggregated,
prev_price: OraclePriceActive,
prev_price: Option<OraclePriceActive>,
) -> OraclePriceActive {
let next_price = if is_aggregate_valid(&aggregated_price, block) {
Some(aggregated_price.aggregated)
} else {
None
};

let active_price = if let Some(next) = prev_price.next {
Some(next)
let active_price = if let Some(prev_price) = prev_price {
if let Some(next) = prev_price.next {
Some(next)
} else {
prev_price.active
}
} else {
prev_price.active
None
};

OraclePriceActive {
Expand Down Expand Up @@ -158,51 +162,37 @@ pub fn invalidate_active_price(services: &Arc<Services>, block: &BlockContext) -

pub fn perform_active_price_tick(
services: &Arc<Services>,
ticker_id: (String, String),
ticker_id: (Token, Currency),
block: &BlockContext,
) -> Result<()> {
let repo = &services.oracle_price_aggregated;
let prev_keys = repo
let prev_key = repo
.by_key
.list(Some(ticker_id.clone()), SortOrder::Descending)?
.take(1)
.flatten() // return empty vec if none
.collect::<Vec<_>>();

if prev_keys.is_empty() {
return Ok(());
}
.next()
.transpose()?;

let Some((_, prev_id)) = prev_keys.first() else {
let Some((_, prev_id)) = prev_key else {
return Ok(());
};

let aggregated_price = repo.by_id.get(prev_id)?;
let aggregated_price = repo.by_id.get(&prev_id)?;

let Some(aggregated_price) = aggregated_price else {
return Ok(());
};

let repo = &services.oracle_price_active;
let prev_keys = repo
let prev_key = repo
.by_key
.list(Some(ticker_id.clone()), SortOrder::Descending)?
.take(1)
.flatten()
.collect::<Vec<_>>();

if prev_keys.is_empty() {
return Ok(());
}

let Some((_, prev_id)) = prev_keys.first() else {
return Ok(());
};
.next()
.transpose()?;

let prev_price = repo.by_id.get(prev_id)?;

let Some(prev_price) = prev_price else {
return Ok(());
let prev_price = if let Some((_, prev_id)) = prev_key {
repo.by_id.get(&prev_id)?
} else {
None
};

let active_price = map_active_price(block, ticker_id, aggregated_price, prev_price);
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
ScriptAggregationScript, ScriptAggregationStatistic, ScriptUnspent, ScriptUnspentScript,
ScriptUnspentVout, TransactionVout, TransactionVoutScript,
},
storage::{RepositoryOps, SecondaryIndex, SortOrder},
storage::{RepositoryOps, SortOrder},
Result, Services,
};

Expand Down

0 comments on commit c46c3d9

Please sign in to comment.