templar_common/oracle/proxy/
mod.rs

1use near_sdk::near;
2
3use crate::time::Nanoseconds;
4
5use super::{price_transformer::ProxyPriceTransformer, OracleRequest};
6
7pub mod aggregator;
8use aggregator::{Aggregator, Filter};
9pub mod governance;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12#[near(serializers = [json, borsh])]
13pub struct Proxy {
14    pub aggregator: Aggregator,
15    pub entries: Vec<Entry>,
16}
17
18impl Proxy {
19    pub fn median_low(entries: impl IntoIterator<Item = Source>) -> Self {
20        Self {
21            aggregator: Aggregator::median_low(Filter {
22                max_age: Some(Nanoseconds::from_ms(60 * 1000)),
23                max_clock_drift: Some(Nanoseconds::from_ms(10 * 1000)),
24                min_sources: Some(1),
25            }),
26            entries: entries.into_iter().map(|s| Entry::new(s, 1)).collect(),
27        }
28    }
29
30    pub fn priority(entries: impl IntoIterator<Item = Source>) -> Self {
31        Self {
32            aggregator: Aggregator::priority(Filter {
33                max_age: Some(Nanoseconds::from_ms(60 * 1000)),
34                max_clock_drift: Some(Nanoseconds::from_ms(10 * 1000)),
35                min_sources: Some(1),
36            }),
37            entries: entries.into_iter().map(|s| Entry::new(s, 1)).collect(),
38        }
39    }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
43#[near(serializers = [json, borsh])]
44pub struct Entry {
45    pub source: Source,
46    pub weight: u32,
47}
48
49impl Entry {
50    pub fn new(source: impl Into<Source>, weight: u32) -> Self {
51        Self {
52            source: source.into(),
53            weight,
54        }
55    }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
59#[near(serializers = [json, borsh])]
60pub enum Source {
61    Request(OracleRequest),
62    Transformer(ProxyPriceTransformer),
63}
64
65impl From<ProxyPriceTransformer> for Source {
66    fn from(transformer: ProxyPriceTransformer) -> Self {
67        Self::Transformer(transformer)
68    }
69}
70
71impl From<OracleRequest> for Source {
72    fn from(oracle_price: OracleRequest) -> Self {
73        Self::Request(oracle_price)
74    }
75}