-
Notifications
You must be signed in to change notification settings - Fork 119
Command to delete price accounts #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f0eb327
deleting price accounts
84945c4
ok
58c3ac0
Merge branch 'main' into del_price
d4ff572
Merge branch 'main' into del_price
541a9bc
fix build
680e020
rut test
ea91529
Merge branch 'main' into del_price
7e7207a
fix
759481d
trying to write a test
c3c997e
trying to write tests
299921a
trying to write tests
ce84057
it works
30d14f9
it works
f89520c
better comments
a832748
integration tests
80b1b25
jeez
40ab15c
fix this
da4b44c
doc
0fe84e5
ok weird that this name matters
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| use std::mem::size_of; | ||
|
|
||
| use bytemuck::{ | ||
| bytes_of, | ||
| Pod, | ||
| }; | ||
| use solana_program::hash::Hash; | ||
| use solana_program::instruction::{ | ||
| AccountMeta, | ||
| Instruction, | ||
| }; | ||
| use solana_program::pubkey::Pubkey; | ||
| use solana_program::rent::Rent; | ||
| use solana_program::system_instruction; | ||
| use solana_program_test::{ | ||
| processor, | ||
| BanksClient, | ||
| BanksClientError, | ||
| ProgramTest, | ||
| ProgramTestBanksClientExt, | ||
| }; | ||
| use solana_sdk::account::Account; | ||
| use solana_sdk::signature::{ | ||
| Keypair, | ||
| Signer, | ||
| }; | ||
| use solana_sdk::transaction::Transaction; | ||
|
|
||
| use crate::c_oracle_header::{ | ||
| cmd_add_price_t, | ||
| cmd_hdr_t, | ||
| command_t_e_cmd_add_price, | ||
| command_t_e_cmd_add_product, | ||
| command_t_e_cmd_del_price, | ||
| command_t_e_cmd_init_mapping, | ||
| pc_map_table_t, | ||
| pc_price_t, | ||
| PC_PROD_ACC_SIZE, | ||
| PC_PTYPE_PRICE, | ||
| PC_VERSION, | ||
| }; | ||
| use crate::deserialize::load; | ||
| use crate::processor::process_instruction; | ||
|
|
||
| /// Simulator for the state of the pyth program on Solana. You can run solana transactions against | ||
| /// this struct to test how pyth instructions execute in the Solana runtime. | ||
| pub struct PythSimulator { | ||
| program_id: Pubkey, | ||
| banks_client: BanksClient, | ||
| payer: Keypair, | ||
| /// Hash used to submit the last transaction. The hash must be advanced for each new | ||
| /// transaction; otherwise, replayed transactions in different states can return stale | ||
| /// results. | ||
| last_blockhash: Hash, | ||
| } | ||
|
|
||
| impl PythSimulator { | ||
| pub async fn new() -> PythSimulator { | ||
| let program_id = Pubkey::new_unique(); | ||
| let (banks_client, payer, recent_blockhash) = | ||
| ProgramTest::new("pyth_oracle", program_id, processor!(process_instruction)) | ||
| .start() | ||
| .await; | ||
|
|
||
| PythSimulator { | ||
| program_id, | ||
| banks_client, | ||
| payer, | ||
| last_blockhash: recent_blockhash, | ||
| } | ||
| } | ||
|
|
||
| /// Process a transaction containing `instruction` signed by `signers`. | ||
| /// The transaction is assumed to require `self.payer` to pay for and sign the transaction. | ||
| async fn process_ix( | ||
| &mut self, | ||
| instruction: Instruction, | ||
| signers: &Vec<&Keypair>, | ||
| ) -> Result<(), BanksClientError> { | ||
| let mut transaction = | ||
| Transaction::new_with_payer(&[instruction], Some(&self.payer.pubkey())); | ||
|
|
||
| let blockhash = self | ||
| .banks_client | ||
| .get_new_latest_blockhash(&self.last_blockhash) | ||
| .await | ||
| .unwrap(); | ||
| self.last_blockhash = blockhash; | ||
|
|
||
| transaction.partial_sign(&[&self.payer], self.last_blockhash); | ||
| transaction.partial_sign(signers, self.last_blockhash); | ||
|
|
||
| self.banks_client.process_transaction(transaction).await | ||
| } | ||
|
|
||
| /// Create an account owned by the pyth program containing `size` bytes. | ||
| /// The account will be created with enough lamports to be rent-exempt. | ||
| pub async fn create_pyth_account(&mut self, size: usize) -> Keypair { | ||
| let keypair = Keypair::new(); | ||
| let rent = Rent::minimum_balance(&Rent::default(), size); | ||
| let instruction = system_instruction::create_account( | ||
| &self.payer.pubkey(), | ||
| &keypair.pubkey(), | ||
| rent, | ||
| size as u64, | ||
| &self.program_id, | ||
| ); | ||
|
|
||
| self.process_ix(instruction, &vec![&keypair]).await.unwrap(); | ||
|
|
||
| keypair | ||
| } | ||
|
|
||
| /// Initialize a mapping account (using the init_mapping instruction), returning the keypair | ||
| /// associated with the newly-created account. | ||
| pub async fn init_mapping(&mut self) -> Result<Keypair, BanksClientError> { | ||
| let mapping_keypair = self.create_pyth_account(size_of::<pc_map_table_t>()).await; | ||
|
|
||
| let cmd = cmd_hdr_t { | ||
| ver_: PC_VERSION, | ||
| cmd_: command_t_e_cmd_init_mapping as i32, | ||
| }; | ||
| let instruction = Instruction::new_with_bytes( | ||
| self.program_id, | ||
| bytes_of(&cmd), | ||
| vec![ | ||
| AccountMeta::new(self.payer.pubkey(), true), | ||
| AccountMeta::new(mapping_keypair.pubkey(), true), | ||
| ], | ||
| ); | ||
|
|
||
| self.process_ix(instruction, &vec![&mapping_keypair]) | ||
| .await | ||
| .map(|_| mapping_keypair) | ||
| } | ||
|
|
||
| /// Initialize a product account and add it to an existing mapping account (using the | ||
| /// add_product instruction). Returns the keypair associated with the newly-created account. | ||
| pub async fn add_product( | ||
| &mut self, | ||
| mapping_keypair: &Keypair, | ||
| ) -> Result<Keypair, BanksClientError> { | ||
| let product_keypair = self.create_pyth_account(PC_PROD_ACC_SIZE as usize).await; | ||
|
|
||
| let cmd = cmd_hdr_t { | ||
| ver_: PC_VERSION, | ||
| cmd_: command_t_e_cmd_add_product as i32, | ||
| }; | ||
| let instruction = Instruction::new_with_bytes( | ||
| self.program_id, | ||
| bytes_of(&cmd), | ||
| vec![ | ||
| AccountMeta::new(self.payer.pubkey(), true), | ||
| AccountMeta::new(mapping_keypair.pubkey(), true), | ||
| AccountMeta::new(product_keypair.pubkey(), true), | ||
| ], | ||
| ); | ||
|
|
||
| self.process_ix(instruction, &vec![&mapping_keypair, &product_keypair]) | ||
| .await | ||
| .map(|_| product_keypair) | ||
| } | ||
|
|
||
| /// Initialize a price account and add it to an existing product account (using the add_price | ||
| /// instruction). Returns the keypair associated with the newly-created account. | ||
| pub async fn add_price( | ||
| &mut self, | ||
| product_keypair: &Keypair, | ||
| expo: i32, | ||
| ) -> Result<Keypair, BanksClientError> { | ||
| let price_keypair = self.create_pyth_account(size_of::<pc_price_t>()).await; | ||
|
|
||
| let cmd = cmd_add_price_t { | ||
| ver_: PC_VERSION, | ||
| cmd_: command_t_e_cmd_add_price as i32, | ||
| expo_: expo, | ||
| ptype_: PC_PTYPE_PRICE, | ||
| }; | ||
| let instruction = Instruction::new_with_bytes( | ||
| self.program_id, | ||
| bytes_of(&cmd), | ||
| vec![ | ||
| AccountMeta::new(self.payer.pubkey(), true), | ||
| AccountMeta::new(product_keypair.pubkey(), true), | ||
| AccountMeta::new(price_keypair.pubkey(), true), | ||
| ], | ||
| ); | ||
|
|
||
| self.process_ix(instruction, &vec![&product_keypair, &price_keypair]) | ||
| .await | ||
| .map(|_| price_keypair) | ||
| } | ||
|
|
||
| /// Delete a price account from an existing product account (using the del_price instruction). | ||
| pub async fn del_price( | ||
| &mut self, | ||
| product_keypair: &Keypair, | ||
| price_keypair: &Keypair, | ||
| ) -> Result<(), BanksClientError> { | ||
| let cmd = cmd_hdr_t { | ||
| ver_: PC_VERSION, | ||
| cmd_: command_t_e_cmd_del_price as i32, | ||
| }; | ||
| let instruction = Instruction::new_with_bytes( | ||
| self.program_id, | ||
| bytes_of(&cmd), | ||
| vec![ | ||
| AccountMeta::new(self.payer.pubkey(), true), | ||
| AccountMeta::new(product_keypair.pubkey(), true), | ||
| AccountMeta::new(price_keypair.pubkey(), true), | ||
| ], | ||
| ); | ||
|
|
||
| self.process_ix(instruction, &vec![&product_keypair, &price_keypair]) | ||
| .await | ||
| } | ||
|
|
||
| /// Get the account at `key`. Returns `None` if no such account exists. | ||
| pub async fn get_account(&mut self, key: Pubkey) -> Option<Account> { | ||
| self.banks_client.get_account(key).await.unwrap() | ||
| } | ||
|
|
||
| /// Get the content of an account as a value of type `T`. This function returns a copy of the | ||
| /// account data -- you cannot mutate the result to mutate the on-chain account data. | ||
| /// Returns None if the account does not exist. Panics if the account data cannot be read as a | ||
| /// `T`. | ||
| pub async fn get_account_data_as<T: Pod>(&mut self, key: Pubkey) -> Option<T> { | ||
| self.get_account(key) | ||
| .await | ||
| .map(|x| load::<T>(&x.data).unwrap().clone()) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tuples over fixed length array literals + explicit return instead of wrapping/unwrapping an unnecessary
Result.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm yeah that does seem better. we've been doing it this way in a bunch of places already though, so i'm not going to change this right now.