use anchor_lang::prelude::{borsh::BorshSchema, *};
#[derive(AnchorSerialize, AnchorDeserialize, Copy, Clone, PartialEq, BorshSchema, Debug)]
pub enum VerificationLevel {
Partial {
#[allow(unused)]
num_signatures: u8,
},
Full,
}
impl VerificationLevel {
pub fn gte(&self, other: VerificationLevel) -> bool {
match self {
VerificationLevel::Full => true,
VerificationLevel::Partial { num_signatures } => match other {
VerificationLevel::Full => false,
VerificationLevel::Partial {
num_signatures: other_num_signatures,
} => *num_signatures >= other_num_signatures,
},
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, AnchorSerialize, AnchorDeserialize, BorshSchema)]
pub struct PriceFeedMessage {
pub feed_id: [u8; 32],
pub price: i64,
pub conf: u64,
pub exponent: i32,
pub publish_time: i64,
pub prev_publish_time: i64,
pub ema_price: i64,
pub ema_conf: u64,
}
#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, BorshSchema)]
pub struct PriceUpdateV2 {
pub write_authority: Pubkey,
pub verification_level: VerificationLevel,
pub price_message: PriceFeedMessage,
pub posted_slot: u64,
}
const PRICE_UPDATE_V2_DISCRIMINATOR: [u8; 8] = [34, 241, 35, 99, 157, 126, 244, 205];
impl AccountDeserialize for PriceUpdateV2 {
fn try_deserialize(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
if buf.len() < 8 {
return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorNotFound.into());
}
let given_disc = &buf[..8];
if given_disc != PRICE_UPDATE_V2_DISCRIMINATOR {
return Err(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch.into());
}
Self::try_deserialize_unchecked(buf)
}
fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
let mut data: &[u8] = &buf[8..];
Ok(AnchorDeserialize::deserialize(&mut data)?)
}
}
impl PriceUpdateV2 {
pub const LEN: usize = 8 + 32 + 2 + 32 + 8 + 8 + 4 + 8 + 8 + 8 + 8 + 8;
}