-
Notifications
You must be signed in to change notification settings - Fork 308
[Solana] tests #1226
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
[Solana] tests #1226
Changes from all commits
e93eb7b
f7a4b2a
e229ed3
f91dfef
fd0bf69
95c8971
746cc39
d9adf9f
51ffbbe
ab10e65
8754d79
e28695e
d599cba
328ed42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "program-simulator" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [lib] | ||
| crate-type = ["lib"] | ||
| name = "program_simulator" | ||
|
|
||
|
|
||
| [dependencies] | ||
| solana-sdk = "1.16.20" | ||
| solana-client = "1.16.20" | ||
| solana-program-test = "1.16.20" | ||
| solana-program = "1.16.20" | ||
| bincode = "1.3.3" | ||
| borsh = "0.10.3" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| use { | ||
| borsh::BorshDeserialize, | ||
| solana_program::{ | ||
| hash::Hash, | ||
| instruction::Instruction, | ||
| native_token::LAMPORTS_PER_SOL, | ||
| pubkey::Pubkey, | ||
| system_instruction, | ||
| }, | ||
| solana_program_test::{ | ||
| BanksClient, | ||
| BanksClientError, | ||
| ProgramTest, | ||
| ProgramTestBanksClientExt, | ||
| }, | ||
| solana_sdk::{ | ||
| signature::{ | ||
| Keypair, | ||
| Signer, | ||
| }, | ||
| transaction::Transaction, | ||
| }, | ||
| }; | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dropped the logic to add a program as upgradable because it doesn't seem to work with 1.16.20, and I don't think we'll need this for the Solana Receiver |
||
| pub struct ProgramSimulator { | ||
| banks_client: BanksClient, | ||
| /// 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, | ||
| genesis_keypair: Keypair, | ||
| } | ||
|
|
||
| impl ProgramSimulator { | ||
| pub async fn start_from_program_test(program_test: ProgramTest) -> ProgramSimulator { | ||
| let (banks_client, genesis_keypair, recent_blockhash) = program_test.start().await; | ||
| ProgramSimulator { | ||
| banks_client, | ||
| genesis_keypair, | ||
| last_blockhash: recent_blockhash, | ||
| } | ||
| } | ||
|
|
||
| /// Process a transaction containing `instruction` signed by `signers`. | ||
| /// `payer` is used to pay for and sign the transaction. | ||
| pub async fn process_ix( | ||
| &mut self, | ||
| instruction: Instruction, | ||
| signers: &Vec<&Keypair>, | ||
| payer: Option<&Keypair>, | ||
| ) -> Result<(), BanksClientError> { | ||
| let actual_payer = payer.unwrap_or(&self.genesis_keypair); | ||
| let mut transaction = | ||
| Transaction::new_with_payer(&[instruction], Some(&actual_payer.pubkey())); | ||
|
|
||
| let blockhash = self | ||
| .banks_client | ||
| .get_new_latest_blockhash(&self.last_blockhash) | ||
| .await | ||
| .unwrap(); | ||
| self.last_blockhash = blockhash; | ||
|
|
||
| transaction.partial_sign(&[actual_payer], self.last_blockhash); | ||
| transaction.partial_sign(signers, self.last_blockhash); | ||
|
|
||
| self.banks_client.process_transaction(transaction).await | ||
| } | ||
|
|
||
| /// Send `lamports` worth of SOL to the pubkey `to`. | ||
| pub async fn airdrop(&mut self, to: &Pubkey, lamports: u64) -> Result<(), BanksClientError> { | ||
| let instruction = | ||
| system_instruction::transfer(&self.genesis_keypair.pubkey(), to, lamports); | ||
|
|
||
| self.process_ix(instruction, &vec![], None).await | ||
| } | ||
|
|
||
| pub async fn get_funded_keypair(&mut self) -> Result<Keypair, BanksClientError> { | ||
| let keypair = Keypair::new(); | ||
| self.airdrop(&keypair.pubkey(), LAMPORTS_PER_SOL).await?; | ||
| Ok(keypair) | ||
| } | ||
|
|
||
| pub async fn get_anchor_account_data<T: BorshDeserialize>( | ||
| &mut self, | ||
| pubkey: Pubkey, | ||
| ) -> Result<T, BanksClientError> { | ||
| let account = self | ||
| .banks_client | ||
| .get_account(pubkey) | ||
| .await | ||
| .unwrap() | ||
| .unwrap(); | ||
|
|
||
| Ok(T::deserialize(&mut &account.data[8..])?) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,6 @@ | ||
| use { | ||
| crate::error::ReceiverError, | ||
| anchor_lang::{ | ||
| prelude::*, | ||
| system_program, | ||
| }, | ||
| anchor_lang::prelude::*, | ||
| pythnet_sdk::{ | ||
| accumulators::merkle::MerkleRoot, | ||
| hashers::keccak256_160::Keccak160, | ||
|
|
@@ -47,6 +44,7 @@ use { | |
| }; | ||
|
|
||
| pub mod error; | ||
| pub mod sdk; | ||
| pub mod state; | ||
|
|
||
| declare_id!("rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ"); | ||
|
|
@@ -303,69 +301,6 @@ pub struct PostUpdatesAtomicParams { | |
| pub merkle_price_update: MerklePriceUpdate, | ||
| } | ||
|
|
||
| impl crate::accounts::Initialize { | ||
| pub fn populate(payer: &Pubkey) -> Self { | ||
| let config = Pubkey::find_program_address(&[CONFIG_SEED.as_ref()], &crate::ID).0; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These get moved to |
||
| crate::accounts::Initialize { | ||
| payer: *payer, | ||
| config, | ||
| system_program: system_program::ID, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl crate::accounts::PostUpdatesAtomic { | ||
| pub fn populate( | ||
| payer: Pubkey, | ||
| price_update_account: Pubkey, | ||
| wormhole_address: Pubkey, | ||
| guardian_set_index: u32, | ||
| ) -> Self { | ||
| let config = Pubkey::find_program_address(&[CONFIG_SEED.as_ref()], &crate::ID).0; | ||
| let treasury = Pubkey::find_program_address(&[TREASURY_SEED.as_ref()], &crate::ID).0; | ||
|
|
||
| let guardian_set = Pubkey::find_program_address( | ||
| &[ | ||
| GuardianSet::SEED_PREFIX, | ||
| guardian_set_index.to_be_bytes().as_ref(), | ||
| ], | ||
| &wormhole_address, | ||
| ) | ||
| .0; | ||
|
|
||
| crate::accounts::PostUpdatesAtomic { | ||
| payer, | ||
| guardian_set, | ||
| config, | ||
| treasury, | ||
| price_update_account, | ||
| system_program: system_program::ID, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl crate::accounts::PostUpdates { | ||
| pub fn populate(payer: Pubkey, encoded_vaa: Pubkey, price_update_account: Pubkey) -> Self { | ||
| let config = Pubkey::find_program_address(&[CONFIG_SEED.as_ref()], &crate::ID).0; | ||
| let treasury = Pubkey::find_program_address(&[TREASURY_SEED.as_ref()], &crate::ID).0; | ||
|
|
||
| crate::accounts::PostUpdates { | ||
| payer, | ||
| encoded_vaa, | ||
| config, | ||
| treasury, | ||
| price_update_account, | ||
| system_program: system_program::ID, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl crate::accounts::Governance { | ||
| pub fn populate(payer: Pubkey) -> Self { | ||
| let config = Pubkey::find_program_address(&[CONFIG_SEED.as_ref()], &crate::ID).0; | ||
| crate::accounts::Governance { payer, config } | ||
| } | ||
| } | ||
|
|
||
| fn deserialize_guardian_set_checked( | ||
| account_info: &AccountInfo<'_>, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.