Skip to content

Commit

Permalink
fix lazer oracle cpi
Browse files Browse the repository at this point in the history
  • Loading branch information
NourAlharithi committed Dec 18, 2024
1 parent 0957fa7 commit 90a35de
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 52 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion deps/configs/pyth_lazer_storage.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"account": {
"lamports": 1461600,
"data": [
"0XX/ucSvRAkL/td28gTUmmjn6CkzKyvYXJOMcup4pEKu3cXcP7cvDAH2UhC+5Pz1sc7h5Tf6vP2VAQKXZTuUrwTUVPxHPpSDT+g2BnoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
"0XX/ucSvRAkL/td28gTUmmjn6CkzKyvYXJOMcup4pEKu3cXcP7cvDAv+13byBNSaaOfoKTMrK9hck4xy6nikQq7dxdw/ty8MAQAAAAAAAAAB9lIQvuT89bHO4eU3+rz9lQECl2U7lK8E1FT8Rz6Ug0/oNgZ6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"base64"
],
"owner": "pytd2yyk641x7ak7mkaasSJVXh6YYZnC7wTmtgAyxPt",
Expand Down
2 changes: 1 addition & 1 deletion programs/drift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ anchor-lang = "0.29.0"
solana-program = "1.16"
anchor-spl = "0.29.0"
pyth-client = "0.2.2"
pyth-lazer-solana-contract = { git = "https://github.com/drift-labs/pyth-crosschain", rev = "1f8d9d4f5322fa96c51420e3252c3e9a995d5df0"}
pyth-lazer-solana-contract = { git = "https://github.com/drift-labs/pyth-crosschain", rev = "57ed54b2e0b1ce74a6d99c95302fec8752d7108f", features = ["no-entrypoint"]}
pythnet-sdk = { git = "https://github.com/drift-labs/pyth-crosschain", rev = "3e8a24ecd0bcf22b787313e2020f4186bb22c729"}
pyth-solana-receiver-sdk = { git = "https://github.com/drift-labs/pyth-crosschain", rev = "3e8a24ecd0bcf22b787313e2020f4186bb22c729"}
bytemuck = { version = "1.4.0" }
Expand Down
48 changes: 15 additions & 33 deletions programs/drift/src/instructions/pyth_lazer_oracle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Borrow;

use crate::error::ErrorCode;
use crate::math::casting::Cast;
use crate::math::safe_math::SafeMath;
Expand All @@ -6,13 +8,11 @@ use crate::state::pyth_lazer_oracle::{
};
use crate::validate;
use anchor_lang::prelude::*;
use anchor_lang::InstructionData;
use pyth_lazer_solana_contract::instruction::VerifyMessage;
use pyth_lazer_solana_contract::protocol::message::SolanaMessage;
use pyth_lazer_solana_contract::protocol::payload::{PayloadData, PayloadPropertyValue};
use pyth_lazer_solana_contract::protocol::router::Price;
use pyth_lazer_solana_contract::Storage;
use solana_program::sysvar::instructions::load_current_index_checked;
use solana_program::{instruction::Instruction as ProgramInstruction, program::invoke};

pub fn handle_update_pyth_lazer_oracle<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, UpdatePythLazerOracle>,
Expand All @@ -26,40 +26,25 @@ pub fn handle_update_pyth_lazer_oracle<'c: 'info, 'info>(
"instruction index must be greater than 0 to include the sig verify ix"
)?;

invoke(
&ProgramInstruction::new_with_bytes(
pyth_lazer_solana_contract::ID,
&VerifyMessage {
message_data: pyth_message.to_vec(),
ed25519_instruction_index: ix_idx - 1,
signature_index: 0,
message_offset: 12,
}
.data(),
vec![
AccountMeta::new(*ctx.accounts.keeper.key, true),
AccountMeta::new_readonly(*ctx.accounts.pyth_lazer_storage.key, false),
AccountMeta::new(*ctx.accounts.pyth_lazer_treasury.key, false),
AccountMeta::new_readonly(*ctx.accounts.pyth_lazer_treasury.key, false),
AccountMeta::new_readonly(*ctx.accounts.ix_sysvar.key, false),
],
),
&[
ctx.accounts.keeper.to_account_info(),
ctx.accounts.pyth_lazer_storage.clone(),
ctx.accounts.pyth_lazer_treasury.clone(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.ix_sysvar.to_account_info(),
],
)?;

// Load oracle accounts from remaining accounts
let remaining_accounts = ctx.remaining_accounts;
validate!(
remaining_accounts.len() <= 3,
ErrorCode::OracleTooManyPriceAccountUpdates
)?;

let storage_account_data = ctx.accounts.pyth_lazer_storage.try_borrow_data()?;
let pyth_storage = Storage::try_deserialize(&mut &storage_account_data[..])?;

pyth_lazer_solana_contract::verify_message_direct(
&pyth_storage,
&ctx.accounts.ix_sysvar,
&pyth_message,
ix_idx - 1,
0,
12,
)?;

let deserialized_pyth_message = SolanaMessage::deserialize_slice(&pyth_message)
.map_err(|_| ProgramError::InvalidInstructionData)?;

Expand Down Expand Up @@ -150,10 +135,7 @@ pub struct UpdatePythLazerOracle<'info> {
address = PYTH_LAZER_STORAGE_ID @ ErrorCode::InvalidPythLazerStorageOwner,
)]
pub pyth_lazer_storage: AccountInfo<'info>,
/// CHECK: this account doesn't need additional constraints.
pub pyth_lazer_treasury: AccountInfo<'info>,
/// CHECK: checked by ed25519 verify
#[account(address = solana_program::sysvar::instructions::ID)]
pub ix_sysvar: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
14 changes: 0 additions & 14 deletions sdk/src/driftClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ export class DriftClient {
txHandler: TxHandler;

receiverProgram?: Program<PythSolanaReceiver>;
pythLazerTreausryPubkey?: PublicKey;
wormholeProgram?: Program<WormholeCoreBridgeSolana>;
sbOnDemandProgramdId: PublicKey;
sbOnDemandProgram?: Program30<Idl30>;
Expand Down Expand Up @@ -8530,13 +8529,6 @@ export class DriftClient {
return txSig;
}

public async loadPythLazerTreasuryPubkey(): Promise<PublicKey> {
const accountInfo = await this.connection.getAccountInfo(
PYTH_LAZER_STORAGE_ACCOUNT_KEY
);
return new PublicKey(accountInfo!.data.slice(40, 72));
}

public async getUpdatePythPullOracleIxs(
params: {
merklePriceUpdate: {
Expand Down Expand Up @@ -8593,10 +8585,6 @@ export class DriftClient {
precedingIxs: TransactionInstruction[] = [],
overrideIxCount?: number
): Promise<TransactionInstruction[]> {
if (!this.pythLazerTreausryPubkey) {
this.pythLazerTreausryPubkey = await this.loadPythLazerTreasuryPubkey();
}

const pythMessageBytes = Buffer.from(pythMessageHex, 'hex');

const verifyIx = createMinimalEd25519VerifyIx(
Expand All @@ -8620,8 +8608,6 @@ export class DriftClient {
keeper: this.wallet.publicKey,
pythLazerStorage: PYTH_LAZER_STORAGE_ACCOUNT_KEY,
ixSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
pythLazerTreasury: this.pythLazerTreausryPubkey,
},
remainingAccounts: remainingAccountsMeta,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pythLazerData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const PYTH_STORAGE_DATA =
'0XX/ucSvRAkL/td28gTUmmjn6CkzKyvYXJOMcup4pEKu3cXcP7cvDAH2UhC+5Pz1sc7h5Tf6vP2VAQKXZTuUrwTUVPxHPpSDT+g2BnoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==';
'0XX/ucSvRAkL/td28gTUmmjn6CkzKyvYXJOMcup4pEKu3cXcP7cvDAv+13byBNSaaOfoKTMrK9hck4xy6nikQq7dxdw/ty8MAQAAAAAAAAAB9lIQvuT89bHO4eU3+rz9lQECl2U7lK8E1FT8Rz6Ug0/oNgZ6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
export const PYTH_LAZER_HEX_STRING_BTC =
'b9011a82c6e392a8837694bb546244b833da502497fe0b3a78b09295d5e9832d1d7b11fbb06ae6a93a78bb5988079259006fe79b32ab5983013464f358cd56ceb5a26a0df65210bee4fcf5b1cee1e537fabcfd95010297653b94af04d454fc473e94834f1c0075d3c79340b0d7837f2906000301010000000100677a0e79a6090000';

Expand Down

0 comments on commit 90a35de

Please sign in to comment.