|
| 1 | +use std::mem::size_of; |
| 2 | + |
| 3 | +use bytemuck::{ |
| 4 | + bytes_of, |
| 5 | + Pod, |
| 6 | +}; |
| 7 | +use solana_program::hash::Hash; |
| 8 | +use solana_program::instruction::{ |
| 9 | + AccountMeta, |
| 10 | + Instruction, |
| 11 | +}; |
| 12 | +use solana_program::pubkey::Pubkey; |
| 13 | +use solana_program::rent::Rent; |
| 14 | +use solana_program::system_instruction; |
| 15 | +use solana_program_test::{ |
| 16 | + processor, |
| 17 | + BanksClient, |
| 18 | + BanksClientError, |
| 19 | + ProgramTest, |
| 20 | + ProgramTestBanksClientExt, |
| 21 | +}; |
| 22 | +use solana_sdk::account::Account; |
| 23 | +use solana_sdk::signature::{ |
| 24 | + Keypair, |
| 25 | + Signer, |
| 26 | +}; |
| 27 | +use solana_sdk::transaction::Transaction; |
| 28 | + |
| 29 | +use crate::c_oracle_header::{ |
| 30 | + cmd_add_price_t, |
| 31 | + cmd_hdr_t, |
| 32 | + command_t_e_cmd_add_price, |
| 33 | + command_t_e_cmd_add_product, |
| 34 | + command_t_e_cmd_del_price, |
| 35 | + command_t_e_cmd_init_mapping, |
| 36 | + pc_map_table_t, |
| 37 | + pc_price_t, |
| 38 | + PC_PROD_ACC_SIZE, |
| 39 | + PC_PTYPE_PRICE, |
| 40 | + PC_VERSION, |
| 41 | +}; |
| 42 | +use crate::deserialize::load; |
| 43 | +use crate::processor::process_instruction; |
| 44 | + |
| 45 | +/// Simulator for the state of the pyth program on Solana. You can run solana transactions against |
| 46 | +/// this struct to test how pyth instructions execute in the Solana runtime. |
| 47 | +pub struct PythSimulator { |
| 48 | + program_id: Pubkey, |
| 49 | + banks_client: BanksClient, |
| 50 | + payer: Keypair, |
| 51 | + /// Hash used to submit the last transaction. The hash must be advanced for each new |
| 52 | + /// transaction; otherwise, replayed transactions in different states can return stale |
| 53 | + /// results. |
| 54 | + last_blockhash: Hash, |
| 55 | +} |
| 56 | + |
| 57 | +impl PythSimulator { |
| 58 | + pub async fn new() -> PythSimulator { |
| 59 | + let program_id = Pubkey::new_unique(); |
| 60 | + let (banks_client, payer, recent_blockhash) = |
| 61 | + ProgramTest::new("pyth_oracle", program_id, processor!(process_instruction)) |
| 62 | + .start() |
| 63 | + .await; |
| 64 | + |
| 65 | + PythSimulator { |
| 66 | + program_id, |
| 67 | + banks_client, |
| 68 | + payer, |
| 69 | + last_blockhash: recent_blockhash, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// Process a transaction containing `instruction` signed by `signers`. |
| 74 | + /// The transaction is assumed to require `self.payer` to pay for and sign the transaction. |
| 75 | + async fn process_ix( |
| 76 | + &mut self, |
| 77 | + instruction: Instruction, |
| 78 | + signers: &Vec<&Keypair>, |
| 79 | + ) -> Result<(), BanksClientError> { |
| 80 | + let mut transaction = |
| 81 | + Transaction::new_with_payer(&[instruction], Some(&self.payer.pubkey())); |
| 82 | + |
| 83 | + let blockhash = self |
| 84 | + .banks_client |
| 85 | + .get_new_latest_blockhash(&self.last_blockhash) |
| 86 | + .await |
| 87 | + .unwrap(); |
| 88 | + self.last_blockhash = blockhash; |
| 89 | + |
| 90 | + transaction.partial_sign(&[&self.payer], self.last_blockhash); |
| 91 | + transaction.partial_sign(signers, self.last_blockhash); |
| 92 | + |
| 93 | + self.banks_client.process_transaction(transaction).await |
| 94 | + } |
| 95 | + |
| 96 | + /// Create an account owned by the pyth program containing `size` bytes. |
| 97 | + /// The account will be created with enough lamports to be rent-exempt. |
| 98 | + pub async fn create_pyth_account(&mut self, size: usize) -> Keypair { |
| 99 | + let keypair = Keypair::new(); |
| 100 | + let rent = Rent::minimum_balance(&Rent::default(), size); |
| 101 | + let instruction = system_instruction::create_account( |
| 102 | + &self.payer.pubkey(), |
| 103 | + &keypair.pubkey(), |
| 104 | + rent, |
| 105 | + size as u64, |
| 106 | + &self.program_id, |
| 107 | + ); |
| 108 | + |
| 109 | + self.process_ix(instruction, &vec![&keypair]).await.unwrap(); |
| 110 | + |
| 111 | + keypair |
| 112 | + } |
| 113 | + |
| 114 | + /// Initialize a mapping account (using the init_mapping instruction), returning the keypair |
| 115 | + /// associated with the newly-created account. |
| 116 | + pub async fn init_mapping(&mut self) -> Result<Keypair, BanksClientError> { |
| 117 | + let mapping_keypair = self.create_pyth_account(size_of::<pc_map_table_t>()).await; |
| 118 | + |
| 119 | + let cmd = cmd_hdr_t { |
| 120 | + ver_: PC_VERSION, |
| 121 | + cmd_: command_t_e_cmd_init_mapping as i32, |
| 122 | + }; |
| 123 | + let instruction = Instruction::new_with_bytes( |
| 124 | + self.program_id, |
| 125 | + bytes_of(&cmd), |
| 126 | + vec![ |
| 127 | + AccountMeta::new(self.payer.pubkey(), true), |
| 128 | + AccountMeta::new(mapping_keypair.pubkey(), true), |
| 129 | + ], |
| 130 | + ); |
| 131 | + |
| 132 | + self.process_ix(instruction, &vec![&mapping_keypair]) |
| 133 | + .await |
| 134 | + .map(|_| mapping_keypair) |
| 135 | + } |
| 136 | + |
| 137 | + /// Initialize a product account and add it to an existing mapping account (using the |
| 138 | + /// add_product instruction). Returns the keypair associated with the newly-created account. |
| 139 | + pub async fn add_product( |
| 140 | + &mut self, |
| 141 | + mapping_keypair: &Keypair, |
| 142 | + ) -> Result<Keypair, BanksClientError> { |
| 143 | + let product_keypair = self.create_pyth_account(PC_PROD_ACC_SIZE as usize).await; |
| 144 | + |
| 145 | + let cmd = cmd_hdr_t { |
| 146 | + ver_: PC_VERSION, |
| 147 | + cmd_: command_t_e_cmd_add_product as i32, |
| 148 | + }; |
| 149 | + let instruction = Instruction::new_with_bytes( |
| 150 | + self.program_id, |
| 151 | + bytes_of(&cmd), |
| 152 | + vec![ |
| 153 | + AccountMeta::new(self.payer.pubkey(), true), |
| 154 | + AccountMeta::new(mapping_keypair.pubkey(), true), |
| 155 | + AccountMeta::new(product_keypair.pubkey(), true), |
| 156 | + ], |
| 157 | + ); |
| 158 | + |
| 159 | + self.process_ix(instruction, &vec![&mapping_keypair, &product_keypair]) |
| 160 | + .await |
| 161 | + .map(|_| product_keypair) |
| 162 | + } |
| 163 | + |
| 164 | + /// Initialize a price account and add it to an existing product account (using the add_price |
| 165 | + /// instruction). Returns the keypair associated with the newly-created account. |
| 166 | + pub async fn add_price( |
| 167 | + &mut self, |
| 168 | + product_keypair: &Keypair, |
| 169 | + expo: i32, |
| 170 | + ) -> Result<Keypair, BanksClientError> { |
| 171 | + let price_keypair = self.create_pyth_account(size_of::<pc_price_t>()).await; |
| 172 | + |
| 173 | + let cmd = cmd_add_price_t { |
| 174 | + ver_: PC_VERSION, |
| 175 | + cmd_: command_t_e_cmd_add_price as i32, |
| 176 | + expo_: expo, |
| 177 | + ptype_: PC_PTYPE_PRICE, |
| 178 | + }; |
| 179 | + let instruction = Instruction::new_with_bytes( |
| 180 | + self.program_id, |
| 181 | + bytes_of(&cmd), |
| 182 | + vec![ |
| 183 | + AccountMeta::new(self.payer.pubkey(), true), |
| 184 | + AccountMeta::new(product_keypair.pubkey(), true), |
| 185 | + AccountMeta::new(price_keypair.pubkey(), true), |
| 186 | + ], |
| 187 | + ); |
| 188 | + |
| 189 | + self.process_ix(instruction, &vec![&product_keypair, &price_keypair]) |
| 190 | + .await |
| 191 | + .map(|_| price_keypair) |
| 192 | + } |
| 193 | + |
| 194 | + /// Delete a price account from an existing product account (using the del_price instruction). |
| 195 | + pub async fn del_price( |
| 196 | + &mut self, |
| 197 | + product_keypair: &Keypair, |
| 198 | + price_keypair: &Keypair, |
| 199 | + ) -> Result<(), BanksClientError> { |
| 200 | + let cmd = cmd_hdr_t { |
| 201 | + ver_: PC_VERSION, |
| 202 | + cmd_: command_t_e_cmd_del_price as i32, |
| 203 | + }; |
| 204 | + let instruction = Instruction::new_with_bytes( |
| 205 | + self.program_id, |
| 206 | + bytes_of(&cmd), |
| 207 | + vec![ |
| 208 | + AccountMeta::new(self.payer.pubkey(), true), |
| 209 | + AccountMeta::new(product_keypair.pubkey(), true), |
| 210 | + AccountMeta::new(price_keypair.pubkey(), true), |
| 211 | + ], |
| 212 | + ); |
| 213 | + |
| 214 | + self.process_ix(instruction, &vec![&product_keypair, &price_keypair]) |
| 215 | + .await |
| 216 | + } |
| 217 | + |
| 218 | + /// Get the account at `key`. Returns `None` if no such account exists. |
| 219 | + pub async fn get_account(&mut self, key: Pubkey) -> Option<Account> { |
| 220 | + self.banks_client.get_account(key).await.unwrap() |
| 221 | + } |
| 222 | + |
| 223 | + /// Get the content of an account as a value of type `T`. This function returns a copy of the |
| 224 | + /// account data -- you cannot mutate the result to mutate the on-chain account data. |
| 225 | + /// Returns None if the account does not exist. Panics if the account data cannot be read as a |
| 226 | + /// `T`. |
| 227 | + pub async fn get_account_data_as<T: Pod>(&mut self, key: Pubkey) -> Option<T> { |
| 228 | + self.get_account(key) |
| 229 | + .await |
| 230 | + .map(|x| load::<T>(&x.data).unwrap().clone()) |
| 231 | + } |
| 232 | +} |
0 commit comments