diff --git a/src/lib.rs b/src/lib.rs index 8ceddf0..8d55247 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,7 @@ pub mod helium_sub_daos; pub use lazy_distributor; pub use lazy_transactions; pub use mobile_entity_manager; -pub use price_oracle; +pub mod price_oracle; pub use rewards_oracle; pub use treasury_management; pub mod voter_stake_registry; diff --git a/src/price_oracle.rs b/src/price_oracle.rs new file mode 100644 index 0000000..235f2c8 --- /dev/null +++ b/src/price_oracle.rs @@ -0,0 +1,28 @@ +pub use ::price_oracle::*; + +const SECONDS_PER_DAY: i64 = 24 * 60 * 60; +pub fn calculate_current_price(oracles: &Vec, curr_ts: i64) -> Option { + let mut prices: Vec = oracles + .iter() + .filter(|oracle| { + oracle.last_submitted_price.is_some() + && oracle.last_submitted_timestamp.is_some() + && curr_ts - oracle.last_submitted_timestamp.unwrap() <= SECONDS_PER_DAY + }) + .filter_map(|oracle| oracle.last_submitted_price) + .collect(); + + if prices.len() < oracles.len() / 2 + 1 { + return None; + } + + prices.sort(); + let n = prices.len(); + let median = if n % 2 == 0 { + ((prices[n / 2 - 1] + prices[n / 2]) as f64 / 2.0).round() as u64 + } else { + prices[n / 2] + }; + + Some(median) +}