Skip to content

Commit

Permalink
calculate oracle price added
Browse files Browse the repository at this point in the history
  • Loading branch information
lthiery committed Jul 19, 2023
1 parent f7fb496 commit dbb3ac7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
28 changes: 28 additions & 0 deletions src/price_oracle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub use ::price_oracle::*;

const SECONDS_PER_DAY: i64 = 24 * 60 * 60;
pub fn calculate_current_price(oracles: &Vec<OracleV0>, curr_ts: i64) -> Option<u64> {
let mut prices: Vec<u64> = 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)
}

0 comments on commit dbb3ac7

Please sign in to comment.