From f3080d6a6da35a141bb348bfd89628d98345f134 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Tue, 10 Dec 2024 22:19:21 +0800 Subject: [PATCH] fix oracle interval type --- lib/ain-ocean/src/api/prices.rs | 8 ++-- lib/ain-ocean/src/indexer/oracle.rs | 33 ++++++++------ .../model/oracle_price_aggregated_interval.rs | 43 +++++++++++++++---- 3 files changed, 58 insertions(+), 26 deletions(-) diff --git a/lib/ain-ocean/src/api/prices.rs b/lib/ain-ocean/src/api/prices.rs index 31bb6eb8f2..8c7e88fbda 100644 --- a/lib/ain-ocean/src/api/prices.rs +++ b/lib/ain-ocean/src/api/prices.rs @@ -358,12 +358,13 @@ async fn get_feed_with_interval( let (token, currency) = parse_token_currency(&key)?; let interval = interval.parse::()?; - let interval_type = match interval { + let interval = match interval { 900 => OracleIntervalSeconds::FifteenMinutes, 3600 => OracleIntervalSeconds::OneHour, 86400 => OracleIntervalSeconds::OneDay, _ => return Err(From::from("Invalid oracle interval")), }; + let interval = interval as u32; let next = query .next @@ -374,7 +375,7 @@ async fn get_feed_with_interval( .transpose()? .unwrap_or([0xffu8; 4]); - let id = (token.clone(), currency.clone(), interval_type.clone(), next); + let id = (token.clone(), currency.clone(), interval.to_string(), next); let items = ctx .services @@ -384,13 +385,14 @@ async fn get_feed_with_interval( .take(query.size) .take_while(|item| match item { Ok(((t, c, i, _), _)) => { - t == &token.clone() && c == ¤cy.clone() && i == &interval_type.clone() + t == &token.clone() && c == ¤cy.clone() && i == &interval.to_string() } _ => true, }) .flatten() .collect::>(); + let interval = interval as i64; let mut prices = Vec::new(); for (id, item) in items { let start = item.block.median_time - (item.block.median_time % interval); diff --git a/lib/ain-ocean/src/indexer/oracle.rs b/lib/ain-ocean/src/indexer/oracle.rs index 7af1e81774..75bdbaa149 100644 --- a/lib/ain-ocean/src/indexer/oracle.rs +++ b/lib/ain-ocean/src/indexer/oracle.rs @@ -27,10 +27,10 @@ use crate::{ Services, }; -pub const AGGREGATED_INTERVALS: [OracleIntervalSeconds; 3] = [ - OracleIntervalSeconds::FifteenMinutes, - OracleIntervalSeconds::OneDay, - OracleIntervalSeconds::OneHour, +pub const AGGREGATED_INTERVALS: [u32; 3] = [ + OracleIntervalSeconds::FifteenMinutes as u32, + OracleIntervalSeconds::OneDay as u32, + OracleIntervalSeconds::OneHour as u32, ]; impl Index for AppointOracle { @@ -478,7 +478,7 @@ impl Index for SetOracleData { token, currency, &aggregated, - &interval, + interval, )?; } @@ -521,9 +521,14 @@ fn start_new_bucket( token: Token, currency: Currency, aggregated: &OraclePriceAggregated, - interval: OracleIntervalSeconds, + interval: u32, ) -> Result<()> { - let id = (token, currency, interval, block.height.to_be_bytes()); + let id = ( + token, + currency, + interval.to_string(), + block.height.to_be_bytes(), + ); services.oracle_price_aggregated_interval.by_id.put( &id, &OraclePriceAggregatedInterval { @@ -549,7 +554,7 @@ pub fn index_interval_mapper( token: Token, currency: Currency, aggregated: &OraclePriceAggregated, - interval: OracleIntervalSeconds, + interval: u32, ) -> Result<()> { let repo = &services.oracle_price_aggregated_interval; let previous = repo @@ -558,14 +563,14 @@ pub fn index_interval_mapper( Some(( token.clone(), currency.clone(), - interval.clone(), + interval.to_string(), [0xffu8; 4], )), SortOrder::Descending, )? .take_while(|item| match item { Ok(((t, c, i, _), _)) => { - t == &token.clone() && c == ¤cy.clone() && i == &interval.clone() + t == &token.clone() && c == ¤cy.clone() && i == &interval.to_string() } _ => true, }) @@ -576,7 +581,7 @@ pub fn index_interval_mapper( return start_new_bucket(services, block, token, currency, aggregated, interval); }; - if block.median_time - aggregated.block.median_time > interval.clone() as i64 { + if block.median_time - aggregated.block.median_time > interval as i64 { return start_new_bucket(services, block, token, currency, aggregated, interval); }; @@ -591,7 +596,7 @@ pub fn invalidate_oracle_interval( token: &str, currency: &str, aggregated: &OraclePriceAggregated, - interval: &OracleIntervalSeconds, + interval: u32, ) -> Result<()> { let repo = &services.oracle_price_aggregated_interval; let previous = repo @@ -600,7 +605,7 @@ pub fn invalidate_oracle_interval( Some(( token.to_string(), currency.to_string(), - interval.clone(), + interval.to_string(), [0xffu8; 4], )), SortOrder::Descending, @@ -612,7 +617,7 @@ pub fn invalidate_oracle_interval( return Err(Error::NotFoundIndex { action: IndexAction::Invalidate, r#type: "Invalidate oracle price aggregated interval".to_string(), - id: format!("{}-{}-{:?}", token, currency, interval), + id: format!("{}-{}-{:?}", token, currency, interval.to_string()), }); }; diff --git a/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs b/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs index 443e4e7e5a..aaa7f70be2 100644 --- a/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs +++ b/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs @@ -3,17 +3,42 @@ use rust_decimal::Decimal; use serde::{Deserialize, Serialize}; use super::BlockContext; -pub type OraclePriceAggregatedIntervalId = (Token, Currency, OracleIntervalSeconds, [u8; 4]); //token-currency-interval-height +pub type OraclePriceAggregatedIntervalId = (Token, Currency, String, [u8; 4]); //token-currency-interval-height -pub const FIFTEEN_MINUTES: isize = 15 * 60; -pub const ONE_HOUR: isize = 60 * 60; -pub const ONE_DAY: isize = 24 * 60 * 60; - -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub enum OracleIntervalSeconds { - FifteenMinutes = FIFTEEN_MINUTES, - OneHour = ONE_HOUR, - OneDay = ONE_DAY, + FifteenMinutes = 900, + OneHour = 3600, + OneDay = 86400, +} + +impl Serialize for OracleIntervalSeconds { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + match self { + Self::FifteenMinutes => serializer.serialize_str("900"), + Self::OneHour => serializer.serialize_str("3600"), + Self::OneDay => serializer.serialize_str("86400"), + } + } +} + +impl<'a> Deserialize<'a> for OracleIntervalSeconds { + fn deserialize(deserializer: D) -> std::result::Result + where + D: serde::Deserializer<'a>, + { + let s = String::deserialize(deserializer).unwrap(); + if s == *"900" { + Ok(Self::FifteenMinutes) + } else if s == *"3600" { + Ok(Self::OneHour) + } else { + Ok(Self::OneDay) + } + } } #[derive(Serialize, Deserialize, Debug, Clone)]