|
| 1 | +use { |
| 2 | + anyhow::Context, |
| 3 | + bytemuck::bytes_of, |
| 4 | + pyth_lazer_sdk::ed25519_program_args, |
| 5 | + pyth_lazer_solana_example::{InitializeArgs, Instruction as ExampleInstruction, UpdateArgs}, |
| 6 | + solana_client::rpc_client::RpcClient, |
| 7 | + solana_sdk::{ |
| 8 | + instruction::{AccountMeta, Instruction}, |
| 9 | + message::Message, |
| 10 | + pubkey::Pubkey, |
| 11 | + signature::read_keypair_file, |
| 12 | + signer::Signer, |
| 13 | + system_program, sysvar, |
| 14 | + transaction::Transaction, |
| 15 | + }, |
| 16 | + std::env, |
| 17 | +}; |
| 18 | + |
| 19 | +fn main() -> anyhow::Result<()> { |
| 20 | + env_logger::init(); |
| 21 | + let client = RpcClient::new(env::var("SOLANA_RPC_URL")?); |
| 22 | + let latest_blockhash = client.get_latest_blockhash()?; |
| 23 | + let keypair = read_keypair_file(env::var("SOLANA_KEYPAIR_FILE")?).unwrap(); |
| 24 | + let program_id: Pubkey = env::var("EXAMPLE_PROGRAM_PUBKEY")?.parse()?; |
| 25 | + |
| 26 | + let (data_pda_key, _) = Pubkey::find_program_address(&[b"data"], &program_id); |
| 27 | + let cmd = env::args().nth(1).context("missing arg")?; |
| 28 | + |
| 29 | + if cmd == "init" { |
| 30 | + let mut init_data = vec![ExampleInstruction::Initialize as u8]; |
| 31 | + init_data.extend_from_slice(bytes_of(&InitializeArgs { price_feed_id: 2 })); |
| 32 | + |
| 33 | + let tx = Transaction::new( |
| 34 | + &[&keypair], |
| 35 | + Message::new( |
| 36 | + &[Instruction::new_with_bytes( |
| 37 | + program_id, |
| 38 | + &init_data, |
| 39 | + vec![ |
| 40 | + AccountMeta::new(keypair.pubkey(), true), |
| 41 | + AccountMeta::new(data_pda_key, false), |
| 42 | + AccountMeta::new_readonly(system_program::ID, false), |
| 43 | + ], |
| 44 | + )], |
| 45 | + Some(&keypair.pubkey()), |
| 46 | + ), |
| 47 | + latest_blockhash, |
| 48 | + ); |
| 49 | + let signature = client.send_and_confirm_transaction(&tx)?; |
| 50 | + println!("OK {signature:?}"); |
| 51 | + } else if cmd == "update" { |
| 52 | + let message = hex::decode(env::var("LAZER_UPDATE_HEX")?)?; |
| 53 | + let mut update_data = vec![ExampleInstruction::Update as u8]; |
| 54 | + update_data.extend_from_slice(bytes_of(&UpdateArgs { hello: 42 })); |
| 55 | + update_data.extend_from_slice(&message); |
| 56 | + |
| 57 | + // Instruction #0 will be ed25519 instruction; |
| 58 | + // Instruction #1 will be our contract instruction. |
| 59 | + let instruction_index = 1; |
| 60 | + // Total offset of Pyth Lazer update within the instruction data; |
| 61 | + // 1 byte is the instruction type. |
| 62 | + let message_offset = (size_of::<UpdateArgs>() + 1).try_into().unwrap(); |
| 63 | + let ed25519_args = |
| 64 | + pyth_lazer_sdk::signature_offsets(&update_data, instruction_index, message_offset); |
| 65 | + let tx = Transaction::new( |
| 66 | + &[&keypair], |
| 67 | + Message::new( |
| 68 | + &[ |
| 69 | + Instruction::new_with_bytes( |
| 70 | + solana_program::ed25519_program::ID, |
| 71 | + &ed25519_program_args(&[ed25519_args]), |
| 72 | + vec![], |
| 73 | + ), |
| 74 | + Instruction::new_with_bytes( |
| 75 | + program_id, |
| 76 | + &update_data, |
| 77 | + vec![ |
| 78 | + AccountMeta::new_readonly(sysvar::instructions::ID, false), |
| 79 | + AccountMeta::new(data_pda_key, false), |
| 80 | + AccountMeta::new_readonly( |
| 81 | + pyth_lazer_solana_contract::storage::ID, |
| 82 | + false, |
| 83 | + ), |
| 84 | + ], |
| 85 | + ), |
| 86 | + ], |
| 87 | + Some(&keypair.pubkey()), |
| 88 | + ), |
| 89 | + latest_blockhash, |
| 90 | + ); |
| 91 | + let signature = client.send_and_confirm_transaction(&tx)?; |
| 92 | + println!("OK {signature:?}"); |
| 93 | + } else { |
| 94 | + panic!("unknown cmd"); |
| 95 | + } |
| 96 | + Ok(()) |
| 97 | +} |
0 commit comments