1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use crate::anchor_traits::*;
use crate::cfg_client;
#[allow(unused_imports)]
use crate::impl_account_deserialize;
use crate::OnDemandError;
use crate::Quote;
use crate::get_sb_program_id;
use solana_program::account_info::AccountInfo;
use solana_program::pubkey::Pubkey;
use solana_program::sysvar::clock::Clock;
use std::cell::Ref;
cfg_client! {
    use crate::address_lookup_table;
    use crate::find_lut_of;
    use solana_sdk::address_lookup_table::AddressLookupTableAccount;
}

pub const ORACLE_FEED_STATS_SEED: &[u8; 15] = b"OracleFeedStats";

#[repr(u8)]
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
pub enum VerificationStatus {
    #[default]
    None = 0,
    VerificationPending = 1 << 0,
    VerificationFailure = 1 << 1,
    VerificationSuccess = 1 << 2,
    VerificationOverride = 1 << 3,
}
impl From<VerificationStatus> for u8 {
    fn from(value: VerificationStatus) -> Self {
        match value {
            VerificationStatus::VerificationPending => 1 << 0,
            VerificationStatus::VerificationFailure => 1 << 1,
            VerificationStatus::VerificationSuccess => 1 << 2,
            VerificationStatus::VerificationOverride => 1 << 3,
            _ => 0,
        }
    }
}
impl From<u8> for VerificationStatus {
    fn from(value: u8) -> Self {
        match value {
            1 => VerificationStatus::VerificationPending,
            2 => VerificationStatus::VerificationFailure,
            4 => VerificationStatus::VerificationSuccess,
            8 => VerificationStatus::VerificationOverride,
            _ => VerificationStatus::default(),
        }
    }
}

#[repr(C)]
#[derive(bytemuck::Zeroable, bytemuck::Pod, Debug, Copy, Clone)]
pub struct OracleAccountData {
    /// Represents the state of the quote verifiers enclave.
    pub enclave: Quote,

    // Accounts Config
    /// The authority of the EnclaveAccount which is permitted to make account changes.
    pub authority: Pubkey,
    /// Queue used for attestation to verify a MRENCLAVE measurement.
    pub queue: Pubkey,

    // Metadata Config
    /// The unix timestamp when the quote was created.
    pub created_at: i64,

    /// The last time the quote heartbeated on-chain.
    pub last_heartbeat: i64,

    pub secp_authority: [u8; 64],

    /// URI location of the verifier's gateway.
    pub gateway_uri: [u8; 64],
    pub permissions: u64,
    /// Whether the quote is located on the AttestationQueues buffer.
    pub is_on_queue: u8,
    _padding1: [u8; 7],
    pub lut_slot: u64,
    pub last_reward_epoch: u64,

    _ebuf4: [u8; 16],
    _ebuf3: [u8; 32],
    _ebuf2: [u8; 64],
    _ebuf1: [u8; 1024],
}

cfg_client! {
    impl_account_deserialize!(OracleAccountData);
}

impl Discriminator for OracleAccountData {
    const DISCRIMINATOR: [u8; 8] = [128, 30, 16, 241, 170, 73, 55, 54];
}

impl Owner for OracleAccountData {
    fn owner() -> Pubkey {
        let cluster = std::env::var("CLUSTER").unwrap_or("mainnet".to_string());
        get_sb_program_id(&cluster)
    }
}

impl OracleAccountData {
    pub fn size() -> usize {
        8 + std::mem::size_of::<OracleAccountData>()
    }

    /// Returns the deserialized Switchboard Quote account
    ///
    /// # Arguments
    ///
    /// * `quote_account_info` - A Solana AccountInfo referencing an existing Switchboard QuoteAccount
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use switchboard_on_demand::OracleAccountData;
    ///
    /// let quote_account = OracleAccountData::new(quote_account_info)?;
    /// ```
    pub fn new<'info>(
        quote_account_info: &'info AccountInfo<'info>,
    ) -> Result<Ref<'info, OracleAccountData>, OnDemandError> {
        let data = quote_account_info
            .try_borrow_data()
            .map_err(|_| OnDemandError::AccountBorrowError)?;
        if data.len() < OracleAccountData::discriminator().len() {
            return Err(OnDemandError::InvalidDiscriminator);
        }

        let mut disc_bytes = [0u8; 8];
        disc_bytes.copy_from_slice(&data[..8]);
        if disc_bytes != OracleAccountData::discriminator() {
            return Err(OnDemandError::InvalidDiscriminator);
        }

        Ok(Ref::map(data, |data| {
            bytemuck::from_bytes(&data[8..std::mem::size_of::<OracleAccountData>() + 8])
        }))
    }

    /// Returns the deserialized Switchboard Quote account
    ///
    /// # Arguments
    ///
    /// * `data` - A Solana AccountInfo's data buffer
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use switchboard_on_demand::OracleAccountData;
    ///
    /// let quote_account = OracleAccountData::new(quote_account_info.try_borrow_data()?)?;
    /// ```
    pub fn new_from_bytes(data: &[u8]) -> Result<&OracleAccountData, OnDemandError> {
        if data.len() < OracleAccountData::discriminator().len() {
            return Err(OnDemandError::InvalidDiscriminator);
        }

        let mut disc_bytes = [0u8; 8];
        disc_bytes.copy_from_slice(&data[..8]);
        if disc_bytes != OracleAccountData::discriminator() {
            return Err(OnDemandError::InvalidDiscriminator);
        }

        Ok(bytemuck::from_bytes(
            &data[8..std::mem::size_of::<OracleAccountData>() + 8],
        ))
    }

    pub fn signer(&self) -> Pubkey {
        self.enclave.enclave_signer
    }

    pub fn is_stale(&self, clock: &Clock) -> bool {
        let staleness_minutes = (clock.unix_timestamp - self.last_heartbeat) / 60;
        staleness_minutes > 30
    }

    pub fn is_verified(&self, clock: &Clock) -> bool {
        match self.enclave.verification_status.into() {
            VerificationStatus::VerificationOverride => true,
            VerificationStatus::VerificationSuccess => {
                self.enclave.valid_until > clock.unix_timestamp
            }
            _ => false,
        }
    }

    pub fn verify(&self, clock: &Clock) -> std::result::Result<(), OnDemandError> {
        if !self.is_verified(clock) {
            return Err(OnDemandError::InvalidQuote);
        }

        Ok(())
    }

    pub fn gateway_uri(&self) -> Option<String> {
        let uri = self.gateway_uri;
        let uri = String::from_utf8_lossy(&uri);
        let uri = uri
            .split_at(uri.find('\0').unwrap_or(uri.len()))
            .0
            .to_string();
        if uri.is_empty() {
            return None;
        }
        Some(uri)
    }

    pub fn ed25519_signer(&self) -> Option<Pubkey> {
        let key = self.enclave.enclave_signer;
        if key == Pubkey::default() {
            return None;
        }
        Some(key)
    }


    pub fn secp_authority(&self) -> Option<[u8; 64]> {
        let key = self.secp_authority;
        if key == [0u8; 64] {
            return None;
        }
        Some(key)
    }

    pub fn secp256k1_signer(&self) -> Option<[u8; 64]> {
        let key = self.enclave.secp256k1_signer;
        if key == [0u8; 64] {
            return None;
        }
        Some(key)
    }

    pub fn libsecp256k1_signer(&self) -> Option<libsecp256k1::PublicKey> {
        let bytes = self.secp256k1_signer()?;
        let tag_full_pubkey: Vec<u8> = vec![4u8];
        let bytes = [tag_full_pubkey, bytes.into()].concat().try_into().ok()?;
        libsecp256k1::PublicKey::parse(&bytes).ok()
    }

    pub fn stats_key(key: &Pubkey) -> Pubkey {
        let pid = OracleAccountData::owner();
        let oracle_stats_seed = b"OracleStats";
        let (key, _) = Pubkey::find_program_address(
            &[
                &oracle_stats_seed.as_slice(),
                &key.to_bytes(),
            ],
            &pid,
        );
        key
    }

    pub fn feed_stats_key(feed: &Pubkey, oracle: &Pubkey) -> (Pubkey, u8) {
        let pid = OracleAccountData::owner();
        Pubkey::find_program_address(
            &Self::feed_stats_seed(&feed.to_bytes(), &oracle.to_bytes(), &[]),
            &pid,
        )
    }

    pub fn feed_stats_seed<'a>(feed: &'a [u8], oracle: &'a [u8], bump: &'a [u8]) -> [&'a [u8]; 4] {
        [
            &ORACLE_FEED_STATS_SEED.as_slice(),
            feed,
            oracle,
            bump,
        ]
    }

    cfg_client! {

        pub async fn fetch_async(
            client: &solana_client::nonblocking::rpc_client::RpcClient,
            pubkey: Pubkey,
        ) -> std::result::Result<Self, crate::OnDemandError> {
            crate::client::fetch_zerocopy_account_async(client, pubkey).await
        }

        pub async fn fetch_many(
            client: &solana_client::nonblocking::rpc_client::RpcClient,
            oracles: &[Pubkey],
        ) -> std::result::Result<Vec<OracleAccountData>, crate::OnDemandError> {
            Ok(client
                .get_multiple_accounts(&oracles)
                .await
                .map_err(|_e| crate::OnDemandError::NetworkError)?
                .into_iter()
                .filter_map(|x| x)
                .map(|x| x.data.clone())
                .collect::<Vec<_>>()
                .iter()
                .map(|x| OracleAccountData::new_from_bytes(x))
                .filter_map(|x| x.ok())
                .map(|x| x.clone())
                .collect())
        }

        pub async fn fetch_lut(
            &self,
            oracle_pubkey: &Pubkey,
            client: &solana_client::nonblocking::rpc_client::RpcClient,
        ) -> std::result::Result<AddressLookupTableAccount, crate::OnDemandError> {
            let oracle = Self::fetch_async(client, *oracle_pubkey).await?;
            let lut_slot = oracle.lut_slot;
            let lut = find_lut_of(oracle_pubkey, lut_slot);
            Ok(address_lookup_table::fetch(client, &lut).await?)
        }
    }

}