Skip to content

Commit

Permalink
fix OraclePriceAggregatedId & OraclePriceFeedId sort key
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 19, 2024
1 parent f39c49c commit ac54bdd
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/ain-ocean/src/api/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions lib/ain-ocean/src/api/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lib/ain-ocean/src/indexer/loan_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/ain-ocean/src/indexer/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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,
))?;

Expand Down Expand Up @@ -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)?;

Expand Down Expand Up @@ -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,
);

Expand Down
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/model/oracle_price_aggregated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/model/oracle_price_feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit ac54bdd

Please sign in to comment.