Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions target_chains/solana/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions target_chains/solana/program_simulator/Cargo.toml
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"
96 changes: 96 additions & 0 deletions target_chains/solana/program_simulator/src/lib.rs
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,
},
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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..])?)
}
}
3 changes: 3 additions & 0 deletions target_chains/solana/programs/pyth-solana-receiver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ bincode = "1.3.3"
libsecp256k1 = "0.7.1"
rand = "0.8.5"
lazy_static = "1.4.0"
program-simulator = { path = "../../program_simulator" }
wormhole-sdk = { git = "https://github.com/wormhole-foundation/wormhole", tag = "v2.17.1" }
serde_wormhole = { git = "https://github.com/wormhole-foundation/wormhole", tag = "v2.17.1"}
69 changes: 2 additions & 67 deletions target_chains/solana/programs/pyth-solana-receiver/src/lib.rs
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,
Expand Down Expand Up @@ -47,6 +44,7 @@ use {
};

pub mod error;
pub mod sdk;
pub mod state;

declare_id!("rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ");
Expand Down Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These get moved to sdk

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<'_>,
Expand Down
Loading