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
95 changes: 94 additions & 1 deletion lazer/solana/Cargo.lock

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

19 changes: 19 additions & 0 deletions lazer/solana/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ edition = "2021"
name = "pyth_lazer_solana_example"
crate-type = ["cdylib", "lib"]

[[bin]]
name = "client"
required-features = ["client"]

[dependencies]
pyth-lazer-sdk = "0.1.0"

solana-program = "1.18.26"
bytemuck = { version = "1.18.0", features = ["derive"] }
num-traits = "0.2.19"
num-derive = "0.4.2"
solana-client = { version = "1.18", optional = true }
anyhow = { version = "1.0.93", optional = true }
solana-sdk = { version = "1.18.26", optional = true }
hex = { version = "0.4.3", optional = true }
pyth-lazer-solana-contract = { version = "0.1.0", optional = true }
env_logger = { version = "0.11.5", optional = true }

[dev-dependencies]
hex = "0.4.3"
Expand All @@ -23,3 +33,12 @@ tokio = { version = "1.40.0", features = ["full"] }
byteorder = "1.5.0"
pyth-lazer-solana-contract = "0.1.0"
anchor-lang = "0.30.1"

[features]
solana-client = ["dep:solana-client"]
anyhow = ["dep:anyhow"]
solana-sdk = ["dep:solana-sdk"]
hex = ["dep:hex"]
pyth-lazer-solana-contract = ["dep:pyth-lazer-solana-contract"]
env_logger = ["dep:env_logger"]
client = ["solana-client", "anyhow", "solana-sdk", "hex", "pyth-lazer-solana-contract", "env_logger"]
97 changes: 97 additions & 0 deletions lazer/solana/src/bin/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use {
anyhow::Context,
bytemuck::bytes_of,
pyth_lazer_sdk::ed25519_program_args,
pyth_lazer_solana_example::{InitializeArgs, Instruction as ExampleInstruction, UpdateArgs},
solana_client::rpc_client::RpcClient,
solana_sdk::{
instruction::{AccountMeta, Instruction},
message::Message,
pubkey::Pubkey,
signature::read_keypair_file,
signer::Signer,
system_program, sysvar,
transaction::Transaction,
},
std::env,
};

fn main() -> anyhow::Result<()> {
env_logger::init();
let client = RpcClient::new(env::var("SOLANA_RPC_URL")?);
let latest_blockhash = client.get_latest_blockhash()?;
let keypair = read_keypair_file(env::var("SOLANA_KEYPAIR_FILE")?).unwrap();
let program_id: Pubkey = env::var("EXAMPLE_PROGRAM_PUBKEY")?.parse()?;

let (data_pda_key, _) = Pubkey::find_program_address(&[b"data"], &program_id);
let cmd = env::args().nth(1).context("missing arg")?;

if cmd == "init" {
let mut init_data = vec![ExampleInstruction::Initialize as u8];
init_data.extend_from_slice(bytes_of(&InitializeArgs { price_feed_id: 2 }));

let tx = Transaction::new(
&[&keypair],
Message::new(
&[Instruction::new_with_bytes(
program_id,
&init_data,
vec![
AccountMeta::new(keypair.pubkey(), true),
AccountMeta::new(data_pda_key, false),
AccountMeta::new_readonly(system_program::ID, false),
],
)],
Some(&keypair.pubkey()),
),
latest_blockhash,
);
let signature = client.send_and_confirm_transaction(&tx)?;
println!("OK {signature:?}");
} else if cmd == "update" {
let message = hex::decode(env::var("LAZER_UPDATE_HEX")?)?;
let mut update_data = vec![ExampleInstruction::Update as u8];
update_data.extend_from_slice(bytes_of(&UpdateArgs { hello: 42 }));
update_data.extend_from_slice(&message);

// Instruction #0 will be ed25519 instruction;
// Instruction #1 will be our contract instruction.
let instruction_index = 1;
// Total offset of Pyth Lazer update within the instruction data;
// 1 byte is the instruction type.
let message_offset = (size_of::<UpdateArgs>() + 1).try_into().unwrap();
let ed25519_args =
pyth_lazer_sdk::signature_offsets(&update_data, instruction_index, message_offset);
let tx = Transaction::new(
&[&keypair],
Message::new(
&[
Instruction::new_with_bytes(
solana_program::ed25519_program::ID,
&ed25519_program_args(&[ed25519_args]),
vec![],
),
Instruction::new_with_bytes(
program_id,
&update_data,
vec![
AccountMeta::new_readonly(sysvar::instructions::ID, false),
AccountMeta::new(data_pda_key, false),
AccountMeta::new_readonly(
pyth_lazer_solana_contract::storage::ID,
false,
),
],
),
],
Some(&keypair.pubkey()),
),
latest_blockhash,
);
let signature = client.send_and_confirm_transaction(&tx)?;
println!("OK {signature:?}");
} else {
panic!("unknown cmd");
}
Ok(())
}