Skip to main content

percli_program/
state.rs

1use anchor_lang::prelude::*;
2use percli_core::RiskEngine;
3
4/// Market account header — stored at the beginning of the account data.
5/// The RiskEngine state follows immediately after, accessed via raw pointer
6/// because RiskEngine is ~1.165 MB and doesn't derive Copy.
7#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy)]
8pub struct MarketHeader {
9    /// Authority that created this market (can update params).
10    pub authority: Pubkey,
11    /// SPL token mint for this market's collateral.
12    pub mint: Pubkey,
13    /// Bump seed for the Market PDA.
14    pub bump: u8,
15    /// Bump seed for the vault token account PDA.
16    pub vault_bump: u8,
17    /// Padding for 8-byte alignment.
18    pub _padding: [u8; 6],
19}
20
21impl MarketHeader {
22    pub const SIZE: usize = 32 + 32 + 1 + 1 + 6; // 72 bytes
23}
24
25/// Total account size: discriminator + header + engine
26pub const MARKET_ACCOUNT_SIZE: usize = 8 + MarketHeader::SIZE + std::mem::size_of::<RiskEngine>();
27
28/// Market PDA signer seeds: [b"market", authority_key, &[bump]]
29pub fn market_signer_seeds<'a>(
30    authority: &'a Pubkey,
31    bump: &'a [u8; 1],
32) -> [&'a [u8]; 3] {
33    [b"market", authority.as_ref(), bump.as_ref()]
34}
35
36/// Helper to get a mutable reference to the RiskEngine from raw account data.
37///
38/// SAFETY: The caller must ensure the account data is at least
39/// `8 + MarketHeader::SIZE + size_of::<RiskEngine>()` bytes and properly
40/// initialized. The RiskEngine is #[repr(C)] with all-valid bit patterns.
41pub fn engine_from_account_data(data: &mut [u8]) -> &mut RiskEngine {
42    let offset = 8 + MarketHeader::SIZE;
43    let engine_bytes = &mut data[offset..offset + std::mem::size_of::<RiskEngine>()];
44    unsafe { &mut *(engine_bytes.as_mut_ptr() as *mut RiskEngine) }
45}
46
47pub fn engine_from_account_data_ref(data: &[u8]) -> &RiskEngine {
48    let offset = 8 + MarketHeader::SIZE;
49    let engine_bytes = &data[offset..offset + std::mem::size_of::<RiskEngine>()];
50    unsafe { &*(engine_bytes.as_ptr() as *const RiskEngine) }
51}
52
53pub fn header_from_account_data(data: &[u8]) -> std::result::Result<MarketHeader, anchor_lang::error::Error> {
54    let header_bytes = &data[8..8 + MarketHeader::SIZE];
55    MarketHeader::try_from_slice(header_bytes)
56        .map_err(|_| anchor_lang::error!(crate::error::PercolatorError::CorruptState))
57}
58
59pub fn write_header(data: &mut [u8], header: &MarketHeader) {
60    let mut cursor = &mut data[8..8 + MarketHeader::SIZE];
61    header.serialize(&mut cursor).unwrap();
62}