templar_common/oracle/
mod.rs

1use near_sdk::{near, AccountId};
2use pyth::PriceIdentifier;
3
4pub mod price_transformer;
5pub mod proxy;
6pub mod pyth;
7pub mod redstone;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[near(serializers = [json, borsh])]
11pub enum OracleRequest {
12    Pyth(PythRequest),
13    RedStone(RedStoneRequest),
14}
15
16impl OracleRequest {
17    pub fn oracle_id(&self) -> &near_sdk::AccountId {
18        match self {
19            OracleRequest::Pyth(id) => &id.oracle_id,
20            OracleRequest::RedStone(id) => &id.oracle_id,
21        }
22    }
23
24    pub fn pyth(oracle_id: AccountId, price_id: PriceIdentifier) -> Self {
25        Self::Pyth(PythRequest {
26            oracle_id,
27            price_id,
28        })
29    }
30
31    pub fn redstone(
32        oracle_id: AccountId,
33        price_id: impl Into<crate::oracle::redstone::FeedId>,
34    ) -> Self {
35        Self::RedStone(RedStoneRequest {
36            oracle_id,
37            price_id: price_id.into(),
38        })
39    }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
43#[near(serializers = [json, borsh])]
44pub struct PythRequest {
45    pub oracle_id: near_sdk::AccountId,
46    pub price_id: PriceIdentifier,
47}
48
49impl From<PythRequest> for OracleRequest {
50    fn from(id: PythRequest) -> Self {
51        Self::Pyth(id)
52    }
53}
54
55#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
56#[near(serializers = [json, borsh])]
57pub struct RedStoneRequest {
58    pub oracle_id: near_sdk::AccountId,
59    pub price_id: crate::oracle::redstone::FeedId,
60}
61
62impl From<RedStoneRequest> for OracleRequest {
63    fn from(id: RedStoneRequest) -> Self {
64        Self::RedStone(id)
65    }
66}