use anchor_lang::prelude::*;
#[account]
pub struct DataPrices {
pub authority: Pubkey,
pub last_updated: i64,
pub current_index: u16,
pub prices: [f32; 10], pub timestamps: [i64; 10], pub is_full: bool, pub bump: u8,
}
impl DataPrices {
pub const LEN: usize = 8 + 32 + 8 + 2 + 4 * 10 + 8 * 10 + 1 + 1;
pub fn add_price(&mut self, price: f32, timestamp: i64) {
self.prices[self.current_index as usize] = price;
self.timestamps[self.current_index as usize] = timestamp;
self.last_updated = timestamp;
self.current_index = (self.current_index + 1) % 10;
if self.current_index == 0 && !self.is_full {
self.is_full = true;
}
}
pub fn get_recent_prices(&self, count: usize) -> &[f32] {
let available_count = if self.is_full { 10 } else { self.current_index as usize };
let take_count = count.min(available_count);
&self.prices[..take_count]
}
}