diff --git a/lib/ain-ocean/src/api/oracle.rs b/lib/ain-ocean/src/api/oracle.rs index f1f42dd06c..e9b7c2e6a1 100644 --- a/lib/ain-ocean/src/api/oracle.rs +++ b/lib/ain-ocean/src/api/oracle.rs @@ -103,15 +103,15 @@ async fn get_feed( .list(None, SortOrder::Descending)? .paginate(&query) .flatten() - .filter(|((token, currency, oracle_id, _), _)| { + .filter(|((token, currency, oracle_id, _, _), _)| { key.0.eq(token) && key.1.eq(currency) && key.2.eq(oracle_id) }) - .map(|((token, currency, oracle_id, txid), feed)| { + .map(|((token, currency, oracle_id, height, txid), feed)| { let amount = Decimal::from(feed.amount) / Decimal::from(COIN); OraclePriceFeedResponse { id: format!("{}-{}-{}-{}", token, currency, oracle_id, txid), key: format!("{}-{}-{}", token, currency, oracle_id), - sort: hex::encode(feed.block.height.to_string() + &txid.to_string()), + sort: hex::encode(height.to_string() + &txid.to_string()), token, currency, oracle_id, diff --git a/lib/ain-ocean/src/api/prices.rs b/lib/ain-ocean/src/api/prices.rs index a0635ea185..04952df6ef 100644 --- a/lib/ain-ocean/src/api/prices.rs +++ b/lib/ain-ocean/src/api/prices.rs @@ -191,7 +191,7 @@ async fn get_feed( let (token, currency) = parse_token_currency(&key)?; let repo = &ctx.services.oracle_price_aggregated; - let id = (token.to_string(), currency.to_string(), u32::MAX); + let id = (token.to_string(), currency.to_string(), i64::MAX, u32::MAX); let oracle_aggregated = repo .by_id .list(Some(id), SortOrder::Descending)? @@ -467,6 +467,7 @@ async fn list_price_oracles( token.clone(), currency.clone(), oracle_id, + u32::MAX, Txid::from_byte_array([0xffu8; 32]), )), SortOrder::Descending, @@ -490,11 +491,12 @@ async fn list_price_oracles( let token = id.0; let currency = id.1; let oracle_id = id.2; - let txid = id.3; + let height = id.3; + let txid = id.4; OraclePriceFeedResponse { id: format!("{}-{}-{}-{}", token, currency, oracle_id, txid), key: format!("{}-{}-{}", token, currency, oracle_id), - sort: hex::encode(f.block.height.to_string() + &txid.to_string()), + sort: hex::encode(height.to_string() + &txid.to_string()), token: token.clone(), currency: currency.clone(), oracle_id, diff --git a/lib/ain-ocean/src/indexer/loan_token.rs b/lib/ain-ocean/src/indexer/loan_token.rs index 3ae78b1e21..afbafd8db0 100644 --- a/lib/ain-ocean/src/indexer/loan_token.rs +++ b/lib/ain-ocean/src/indexer/loan_token.rs @@ -169,7 +169,7 @@ pub fn perform_active_price_tick( ticker_id: (Token, Currency), block: &BlockContext, ) -> Result<()> { - let id = (ticker_id.0, ticker_id.1, u32::MAX); + let id = (ticker_id.0.clone(), ticker_id.1.clone(), i64::MAX, u32::MAX); let prev = services .oracle_price_aggregated @@ -182,6 +182,7 @@ pub fn perform_active_price_tick( return Ok(()); }; + let id = (ticker_id.0, ticker_id.1, u32::MAX); let repo = &services.oracle_price_active; let prev = repo .by_id diff --git a/lib/ain-ocean/src/indexer/oracle.rs b/lib/ain-ocean/src/indexer/oracle.rs index 461fc8c97e..447a58ca40 100644 --- a/lib/ain-ocean/src/indexer/oracle.rs +++ b/lib/ain-ocean/src/indexer/oracle.rs @@ -301,7 +301,7 @@ fn map_price_aggregated( let feed = services .oracle_price_feed .by_id - .list(Some((id.0, id.1, id.2, base_id)), SortOrder::Descending)? + .list(Some((id.0, id.1, id.2, u32::MAX, base_id)), SortOrder::Descending)? .next() .transpose()?; @@ -372,6 +372,7 @@ fn index_set_oracle_data( let id = ( token.clone(), currency.clone(), + price_aggregated.block.median_time, price_aggregated.block.height, ); oracle_repo.by_id.put(&id, &price_aggregated)?; @@ -402,6 +403,7 @@ fn index_set_oracle_data_interval( let aggregated = services.oracle_price_aggregated.by_id.get(&( token.clone(), currency.clone(), + context.block.median_time, context.block.height, ))?; @@ -447,8 +449,8 @@ impl Index for SetOracleData { let feeds = map_price_feeds(self, context); - for ((token, currency, _, _), _) in feeds.iter().rev() { - let id = (token.clone(), currency.clone(), context.block.height); + for ((token, currency, _, _, _), _) in feeds.iter().rev() { + let id = (token.clone(), currency.clone(), context.block.median_time, context.block.height); let aggregated = oracle_repo.by_id.get(&id)?; @@ -485,6 +487,7 @@ fn map_price_feeds( token_price.token.clone(), token_amount.currency.clone(), data.oracle_id, + ctx.block.height, ctx.tx.txid, ); diff --git a/lib/ain-ocean/src/model/oracle_price_aggregated.rs b/lib/ain-ocean/src/model/oracle_price_aggregated.rs index 0ee554ed8f..65832b95ba 100644 --- a/lib/ain-ocean/src/model/oracle_price_aggregated.rs +++ b/lib/ain-ocean/src/model/oracle_price_aggregated.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use super::{BlockContext, OraclePriceActiveNext}; -pub type OraclePriceAggregatedId = (String, String, u32); //token-currency-height +pub type OraclePriceAggregatedId = (String, String, i64, u32); //token-currency-mediantime-height #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] diff --git a/lib/ain-ocean/src/model/oracle_price_feed.rs b/lib/ain-ocean/src/model/oracle_price_feed.rs index 0afcc65614..4a2ff073a3 100644 --- a/lib/ain-ocean/src/model/oracle_price_feed.rs +++ b/lib/ain-ocean/src/model/oracle_price_feed.rs @@ -2,7 +2,7 @@ use bitcoin::Txid; use serde::{Deserialize, Serialize}; use super::BlockContext; -pub type OraclePriceFeedId = (String, String, Txid, Txid); // token-currency-oracle_id-txid +pub type OraclePriceFeedId = (String, String, Txid, u32, Txid); // token-currency-oracle_id-height-txid #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")]