From 90a35de55998a565f560cb708c2d5a31fcaa10dd Mon Sep 17 00:00:00 2001 From: Nour Alharithi Date: Wed, 18 Dec 2024 13:38:38 -0800 Subject: [PATCH] fix lazer oracle cpi --- Cargo.lock | 4 +- deps/configs/pyth_lazer_storage.json | 2 +- programs/drift/Cargo.toml | 2 +- .../src/instructions/pyth_lazer_oracle.rs | 48 ++++++------------- sdk/src/driftClient.ts | 14 ------ tests/pythLazerData.ts | 2 +- 6 files changed, 20 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef01d967b..5856f5e1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1853,7 +1853,7 @@ checksum = "44de48029c54ec1ca570786b5baeb906b0fc2409c8e0145585e287ee7a526c72" [[package]] name = "pyth-lazer-protocol" version = "0.1.2" -source = "git+https://github.com/drift-labs/pyth-crosschain?rev=1f8d9d4f5322fa96c51420e3252c3e9a995d5df0#1f8d9d4f5322fa96c51420e3252c3e9a995d5df0" +source = "git+https://github.com/drift-labs/pyth-crosschain?rev=57ed54b2e0b1ce74a6d99c95302fec8752d7108f#57ed54b2e0b1ce74a6d99c95302fec8752d7108f" dependencies = [ "anyhow", "byteorder", @@ -1866,7 +1866,7 @@ dependencies = [ [[package]] name = "pyth-lazer-solana-contract" version = "0.2.0" -source = "git+https://github.com/drift-labs/pyth-crosschain?rev=1f8d9d4f5322fa96c51420e3252c3e9a995d5df0#1f8d9d4f5322fa96c51420e3252c3e9a995d5df0" +source = "git+https://github.com/drift-labs/pyth-crosschain?rev=57ed54b2e0b1ce74a6d99c95302fec8752d7108f#57ed54b2e0b1ce74a6d99c95302fec8752d7108f" dependencies = [ "anchor-lang", "bytemuck", diff --git a/deps/configs/pyth_lazer_storage.json b/deps/configs/pyth_lazer_storage.json index 3c4a1368f..6d35d576b 100644 --- a/deps/configs/pyth_lazer_storage.json +++ b/deps/configs/pyth_lazer_storage.json @@ -3,7 +3,7 @@ "account": { "lamports": 1461600, "data": [ - "0XX/ucSvRAkL/td28gTUmmjn6CkzKyvYXJOMcup4pEKu3cXcP7cvDAH2UhC+5Pz1sc7h5Tf6vP2VAQKXZTuUrwTUVPxHPpSDT+g2BnoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==", + "0XX/ucSvRAkL/td28gTUmmjn6CkzKyvYXJOMcup4pEKu3cXcP7cvDAv+13byBNSaaOfoKTMrK9hck4xy6nikQq7dxdw/ty8MAQAAAAAAAAAB9lIQvuT89bHO4eU3+rz9lQECl2U7lK8E1FT8Rz6Ug0/oNgZ6AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "base64" ], "owner": "pytd2yyk641x7ak7mkaasSJVXh6YYZnC7wTmtgAyxPt", diff --git a/programs/drift/Cargo.toml b/programs/drift/Cargo.toml index 0bda4532c..0a739c223 100644 --- a/programs/drift/Cargo.toml +++ b/programs/drift/Cargo.toml @@ -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" } diff --git a/programs/drift/src/instructions/pyth_lazer_oracle.rs b/programs/drift/src/instructions/pyth_lazer_oracle.rs index e2468dd3e..e7f611ea5 100644 --- a/programs/drift/src/instructions/pyth_lazer_oracle.rs +++ b/programs/drift/src/instructions/pyth_lazer_oracle.rs @@ -1,3 +1,5 @@ +use std::borrow::Borrow; + use crate::error::ErrorCode; use crate::math::casting::Cast; use crate::math::safe_math::SafeMath; @@ -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>, @@ -26,33 +26,6 @@ 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!( @@ -60,6 +33,18 @@ pub fn handle_update_pyth_lazer_oracle<'c: 'info, 'info>( 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)?; @@ -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>, } diff --git a/sdk/src/driftClient.ts b/sdk/src/driftClient.ts index 2efc0443f..9fe7177fd 100644 --- a/sdk/src/driftClient.ts +++ b/sdk/src/driftClient.ts @@ -233,7 +233,6 @@ export class DriftClient { txHandler: TxHandler; receiverProgram?: Program; - pythLazerTreausryPubkey?: PublicKey; wormholeProgram?: Program; sbOnDemandProgramdId: PublicKey; sbOnDemandProgram?: Program30; @@ -8530,13 +8529,6 @@ export class DriftClient { return txSig; } - public async loadPythLazerTreasuryPubkey(): Promise { - const accountInfo = await this.connection.getAccountInfo( - PYTH_LAZER_STORAGE_ACCOUNT_KEY - ); - return new PublicKey(accountInfo!.data.slice(40, 72)); - } - public async getUpdatePythPullOracleIxs( params: { merklePriceUpdate: { @@ -8593,10 +8585,6 @@ export class DriftClient { precedingIxs: TransactionInstruction[] = [], overrideIxCount?: number ): Promise { - if (!this.pythLazerTreausryPubkey) { - this.pythLazerTreausryPubkey = await this.loadPythLazerTreasuryPubkey(); - } - const pythMessageBytes = Buffer.from(pythMessageHex, 'hex'); const verifyIx = createMinimalEd25519VerifyIx( @@ -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, } diff --git a/tests/pythLazerData.ts b/tests/pythLazerData.ts index a6065b6fa..0762cf4dd 100644 --- a/tests/pythLazerData.ts +++ b/tests/pythLazerData.ts @@ -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';