|
| 1 | +//! Account types and utilities for working with Pyth accounts. |
| 2 | +
|
| 3 | +use { |
| 4 | + crate::{ |
| 5 | + c_oracle_header::PC_MAGIC, |
| 6 | + deserialize::load_account_as_mut, |
| 7 | + error::OracleError, |
| 8 | + utils::{ |
| 9 | + check_valid_fresh_account, |
| 10 | + get_rent, |
| 11 | + pyth_assert, |
| 12 | + send_lamports, |
| 13 | + try_convert, |
| 14 | + }, |
| 15 | + }, |
| 16 | + bytemuck::{ |
| 17 | + Pod, |
| 18 | + Zeroable, |
| 19 | + }, |
| 20 | + solana_program::{ |
| 21 | + account_info::AccountInfo, |
| 22 | + program::invoke_signed, |
| 23 | + program_error::ProgramError, |
| 24 | + program_memory::sol_memset, |
| 25 | + pubkey::Pubkey, |
| 26 | + system_instruction::{ |
| 27 | + allocate, |
| 28 | + assign, |
| 29 | + }, |
| 30 | + }, |
| 31 | + std::{ |
| 32 | + borrow::BorrowMut, |
| 33 | + cell::RefMut, |
| 34 | + mem::size_of, |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +mod mapping; |
| 39 | +mod permission; |
| 40 | +mod price; |
| 41 | +mod product; |
| 42 | + |
| 43 | +pub use { |
| 44 | + mapping::MappingAccount, |
| 45 | + permission::{ |
| 46 | + PermissionAccount, |
| 47 | + PERMISSIONS_SEED, |
| 48 | + }, |
| 49 | + price::{ |
| 50 | + PriceAccount, |
| 51 | + PriceComponent, |
| 52 | + PriceEma, |
| 53 | + PriceInfo, |
| 54 | + }, |
| 55 | + product::ProductAccount, |
| 56 | +}; |
| 57 | + |
| 58 | +#[repr(C)] |
| 59 | +#[derive(Copy, Clone, Zeroable, Pod)] |
| 60 | +pub struct AccountHeader { |
| 61 | + pub magic_number: u32, |
| 62 | + pub version: u32, |
| 63 | + pub account_type: u32, |
| 64 | + pub size: u32, |
| 65 | +} |
| 66 | + |
| 67 | +/// The PythAccount trait's purpose is to attach constants to the 3 types of accounts that Pyth has |
| 68 | +/// (mapping, price, product). This allows less duplicated code, because now we can create generic |
| 69 | +/// functions to perform common checks on the accounts and to load and initialize the accounts. |
| 70 | +pub trait PythAccount: Pod { |
| 71 | + /// `ACCOUNT_TYPE` is just the account discriminator, it is different for mapping, product and |
| 72 | + /// price |
| 73 | + const ACCOUNT_TYPE: u32; |
| 74 | + |
| 75 | + /// `INITIAL_SIZE` is the value that the field `size_` will take when the account is first |
| 76 | + /// initialized this one is slightly tricky because for mapping (resp. price) `size_` won't |
| 77 | + /// include the unpopulated entries of `prod_` (resp. `comp_`). At the beginning there are 0 |
| 78 | + /// products (resp. 0 components) therefore `INITIAL_SIZE` will be equal to the offset of |
| 79 | + /// `prod_` (resp. `comp_`) Similarly the product account `INITIAL_SIZE` won't include any |
| 80 | + /// key values. |
| 81 | + const INITIAL_SIZE: u32; |
| 82 | + |
| 83 | + /// `minimum_size()` is the minimum size that the solana account holding the struct needs to |
| 84 | + /// have. `INITIAL_SIZE` <= `minimum_size()` |
| 85 | + const MINIMUM_SIZE: usize = size_of::<Self>(); |
| 86 | + |
| 87 | + /// Given an `AccountInfo`, verify it is sufficiently large and has the correct discriminator. |
| 88 | + fn initialize<'a>( |
| 89 | + account: &'a AccountInfo, |
| 90 | + version: u32, |
| 91 | + ) -> Result<RefMut<'a, Self>, ProgramError> { |
| 92 | + pyth_assert( |
| 93 | + account.data_len() >= Self::MINIMUM_SIZE, |
| 94 | + OracleError::AccountTooSmall.into(), |
| 95 | + )?; |
| 96 | + |
| 97 | + check_valid_fresh_account(account)?; |
| 98 | + clear_account(account)?; |
| 99 | + |
| 100 | + { |
| 101 | + let mut account_header = load_account_as_mut::<AccountHeader>(account)?; |
| 102 | + account_header.magic_number = PC_MAGIC; |
| 103 | + account_header.version = version; |
| 104 | + account_header.account_type = Self::ACCOUNT_TYPE; |
| 105 | + account_header.size = Self::INITIAL_SIZE; |
| 106 | + } |
| 107 | + load_account_as_mut::<Self>(account) |
| 108 | + } |
| 109 | + |
| 110 | + // Creates PDA accounts only when needed, and initializes it as one of the Pyth accounts |
| 111 | + fn initialize_pda<'a>( |
| 112 | + account: &AccountInfo<'a>, |
| 113 | + funding_account: &AccountInfo<'a>, |
| 114 | + system_program: &AccountInfo<'a>, |
| 115 | + program_id: &Pubkey, |
| 116 | + seeds: &[&[u8]], |
| 117 | + version: u32, |
| 118 | + ) -> Result<(), ProgramError> { |
| 119 | + let target_rent = get_rent()?.minimum_balance(Self::MINIMUM_SIZE); |
| 120 | + |
| 121 | + if account.lamports() < target_rent { |
| 122 | + send_lamports( |
| 123 | + funding_account, |
| 124 | + account, |
| 125 | + system_program, |
| 126 | + target_rent - account.lamports(), |
| 127 | + )?; |
| 128 | + } |
| 129 | + |
| 130 | + if account.data_len() == 0 { |
| 131 | + allocate_data(account, system_program, Self::MINIMUM_SIZE, seeds)?; |
| 132 | + assign_owner(account, program_id, system_program, seeds)?; |
| 133 | + Self::initialize(account, version)?; |
| 134 | + } |
| 135 | + Ok(()) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +/// Given an already empty `AccountInfo`, allocate the data field to the given size. This make no |
| 140 | +/// assumptions about owner. |
| 141 | +fn allocate_data<'a>( |
| 142 | + account: &AccountInfo<'a>, |
| 143 | + system_program: &AccountInfo<'a>, |
| 144 | + space: usize, |
| 145 | + seeds: &[&[u8]], |
| 146 | +) -> Result<(), ProgramError> { |
| 147 | + let allocate_instruction = allocate(account.key, try_convert(space)?); |
| 148 | + invoke_signed( |
| 149 | + &allocate_instruction, |
| 150 | + &[account.clone(), system_program.clone()], |
| 151 | + &[seeds], |
| 152 | + )?; |
| 153 | + Ok(()) |
| 154 | +} |
| 155 | + |
| 156 | +/// Given a newly created `AccountInfo`, assign the owner to the given program id. |
| 157 | +fn assign_owner<'a>( |
| 158 | + account: &AccountInfo<'a>, |
| 159 | + owner: &Pubkey, |
| 160 | + system_program: &AccountInfo<'a>, |
| 161 | + seeds: &[&[u8]], |
| 162 | +) -> Result<(), ProgramError> { |
| 163 | + let assign_instruction = assign(account.key, owner); |
| 164 | + invoke_signed( |
| 165 | + &assign_instruction, |
| 166 | + &[account.clone(), system_program.clone()], |
| 167 | + &[seeds], |
| 168 | + )?; |
| 169 | + Ok(()) |
| 170 | +} |
| 171 | + |
| 172 | +/// Sets the data of account to all-zero |
| 173 | +pub fn clear_account(account: &AccountInfo) -> Result<(), ProgramError> { |
| 174 | + let mut data = account |
| 175 | + .try_borrow_mut_data() |
| 176 | + .map_err(|_| ProgramError::InvalidArgument)?; |
| 177 | + let length = data.len(); |
| 178 | + sol_memset(data.borrow_mut(), 0, length); |
| 179 | + Ok(()) |
| 180 | +} |
0 commit comments