charli3-oracle-core 0.1.0-alpha.2

Core oracle types, aggregation algorithms, and price providers for Charli3
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use super::statistics::{quantile, Rational};
use sp_std::vec::Vec;

/// It will check for underflow/overflow and list size limitations (>0) and return None in that cases.
pub fn calculate_median(mut prices: Vec<u64>) -> Option<u64> {
    match prices.len() {
        0 => None,
        1 => Some(prices[0]),
        _ => {
            prices.sort_unstable();
            quantile(prices, Rational::new(1, 2))
                .and_then(|v| v.round().to_integer().try_into().ok())
        }
    }
}