1use crate::utils::try_from_slice_checked;
2use borsh::{BorshDeserialize, BorshSerialize};
3use shank::ShankAccount;
4use solana_program::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
5pub const PREFIX: &str = "vault";
7
8#[repr(C)]
9#[derive(Clone, BorshSerialize, BorshDeserialize, PartialEq)]
10pub enum Key {
11 Uninitialized,
12 SafetyDepositBoxV1,
13 ExternalAccountKeyV1,
14 VaultV1,
15}
16
17pub const MAX_SAFETY_DEPOSIT_SIZE: usize = 1 + 32 + 32 + 32 + 1;
18pub const MAX_VAULT_SIZE: usize = 1 + 32 + 32 + 32 + 32 + 1 + 32 + 1 + 32 + 1 + 1 + 8;
19pub const MAX_EXTERNAL_ACCOUNT_SIZE: usize = 1 + 8 + 32 + 1;
20#[repr(C)]
21#[derive(Clone, BorshSerialize, BorshDeserialize, PartialEq)]
22pub enum VaultState {
23 Inactive,
24 Active,
25 Combined,
26 Deactivated,
27}
28
29#[repr(C)]
30#[derive(Clone, BorshSerialize, BorshDeserialize, ShankAccount)]
31pub struct Vault {
32 pub key: Key,
33 pub token_program: Pubkey,
35 pub fraction_mint: Pubkey,
37 pub authority: Pubkey,
39 pub fraction_treasury: Pubkey,
41 pub redeem_treasury: Pubkey,
43 pub allow_further_share_creation: bool,
45
46 pub pricing_lookup_address: Pubkey,
48 pub token_type_count: u8,
55 pub state: VaultState,
56
57 pub locked_price_per_share: u64,
60
61 _extra_byte: u8,
65}
66
67impl Vault {
68 pub fn from_account_info(a: &AccountInfo) -> Result<Vault, ProgramError> {
69 let vt: Vault = try_from_slice_checked(&a.data.borrow_mut(), Key::VaultV1, MAX_VAULT_SIZE)?;
70
71 Ok(vt)
72 }
73
74 pub fn get_token_type_count(a: &AccountInfo) -> u8 {
75 return a.data.borrow()[194];
76 }
77}
78
79#[repr(C)]
80#[derive(Clone, BorshSerialize, BorshDeserialize, ShankAccount)]
81pub struct SafetyDepositBox {
82 pub key: Key,
86 pub vault: Pubkey,
88 pub token_mint: Pubkey,
90 pub store: Pubkey,
92 pub order: u8,
94}
95
96impl SafetyDepositBox {
97 pub fn from_account_info(a: &AccountInfo) -> Result<SafetyDepositBox, ProgramError> {
98 let sd: SafetyDepositBox = try_from_slice_checked(
99 &a.data.borrow_mut(),
100 Key::SafetyDepositBoxV1,
101 MAX_SAFETY_DEPOSIT_SIZE,
102 )?;
103
104 Ok(sd)
105 }
106
107 pub fn get_order(a: &AccountInfo) -> u8 {
108 a.data.borrow()[97]
109 }
110}
111
112#[repr(C)]
113#[derive(Clone, BorshSerialize, BorshDeserialize, ShankAccount)]
114pub struct ExternalPriceAccount {
115 pub key: Key,
116 pub price_per_share: u64,
117 pub price_mint: Pubkey,
120 pub allowed_to_combine: bool,
122}
123
124impl ExternalPriceAccount {
125 pub fn from_account_info(a: &AccountInfo) -> Result<ExternalPriceAccount, ProgramError> {
126 let sd: ExternalPriceAccount = try_from_slice_checked(
127 &a.data.borrow_mut(),
128 Key::ExternalAccountKeyV1,
129 MAX_EXTERNAL_ACCOUNT_SIZE,
130 )?;
131
132 Ok(sd)
133 }
134}