Skip to content

pyth lazer CU improvements #1710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4504,6 +4504,8 @@ pub fn handle_initialize_pyth_lazer_oracle(
feed_id: u32,
) -> Result<()> {
let pubkey = ctx.accounts.lazer_oracle.to_account_info().key;
let mut account = ctx.accounts.lazer_oracle.load_init()?;
account.bump = ctx.bumps.lazer_oracle;
msg!(
"Lazer price feed initted {} with feed_id {}",
pubkey,
Expand All @@ -4512,6 +4514,20 @@ pub fn handle_initialize_pyth_lazer_oracle(
Ok(())
}

pub fn handle_update_pyth_lazer_oracle_bump(
ctx: Context<UpdatePythLazerOracleBump>,
feed_id: u32,
) -> Result<()> {
let mut account = ctx.accounts.lazer_oracle.load_mut()?;
account.bump = ctx.bumps.lazer_oracle;
msg!(
"Lazer price bump updated to {} for feed id {}",
ctx.bumps.lazer_oracle,
feed_id
);
Ok(())
}

pub fn handle_settle_expired_market<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, AdminUpdatePerpMarket<'info>>,
market_index: u16,
Expand Down Expand Up @@ -5481,6 +5497,23 @@ pub struct InitPythLazerOracle<'info> {
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
#[instruction(feed_id: u32)]
pub struct UpdatePythLazerOracleBump<'info> {
#[account(
mut,
constraint = admin.key() == admin_hot_wallet::id() || admin.key() == state.admin
)]
pub admin: Signer<'info>,
#[account(
mut,
seeds = [PYTH_LAZER_ORACLE_SEED, &feed_id.to_le_bytes()],
bump,
)]
pub lazer_oracle: AccountLoader<'info, PythLazerOracle>,
pub state: Box<Account<'info, State>>,
}

#[derive(Accounts)]
pub struct InitializeHighLeverageModeConfig<'info> {
#[account(mut)]
Expand Down
24 changes: 12 additions & 12 deletions programs/drift/src/instructions/pyth_lazer_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,21 @@ pub fn handle_update_pyth_lazer_oracle<'c: 'info, 'info>(
let feed_id = payload_data.feed_id.0;

// Verify the pda
let pda = Pubkey::find_program_address(
&[PYTH_LAZER_ORACLE_SEED, &feed_id.to_le_bytes()],
let pda = Pubkey::create_program_address(
&[
PYTH_LAZER_ORACLE_SEED,
&feed_id.to_le_bytes(),
pyth_lazer_oracle.bump.to_le_bytes().as_ref(),
],
&crate::ID,
)
.0;
);
if pda.is_err() {
return Err(ErrorCode::OracleBadRemainingAccountPublicKey.into());
}
let unwrapped_pda = pda.unwrap();
require_keys_eq!(
*account.key,
pda,
unwrapped_pda,
ErrorCode::OracleBadRemainingAccountPublicKey
);

Expand Down Expand Up @@ -103,13 +110,6 @@ pub fn handle_update_pyth_lazer_oracle<'c: 'info, 'info>(
pyth_lazer_oracle.publish_time = next_timestamp;
pyth_lazer_oracle.exponent = exponent.cast::<i32>()?;
pyth_lazer_oracle.conf = conf.cast::<u64>()?;
msg!("Price updated to {}", price.0.get());

msg!(
"Posting new lazer update. current ts {} < next ts {}",
current_timestamp,
next_timestamp
);
} else {
msg!(
"Skipping new lazer update. current ts {} >= next ts {}",
Expand Down
7 changes: 7 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,13 @@ pub mod drift {
handle_initialize_pyth_lazer_oracle(ctx, feed_id)
}

pub fn update_pyth_lazer_oracle_bump(
ctx: Context<UpdatePythLazerOracleBump>,
feed_id: u32,
) -> Result<()> {
handle_update_pyth_lazer_oracle_bump(ctx, feed_id)
}

pub fn post_pyth_lazer_oracle_update<'c: 'info, 'info>(
ctx: Context<'_, '_, 'c, 'info, UpdatePythLazerOracle>,
pyth_message: Vec<u8>,
Expand Down
12 changes: 12 additions & 0 deletions programs/drift/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,15 @@ macro_rules! msg {
(solana_program::msg!(&format!($($arg)*)));
}
}

#[macro_export]
macro_rules! compute_fn {
($msg:expr=> $($tt:tt)*) => {
::solana_program::msg!(concat!($msg, " {"));
::solana_program::log::sol_log_compute_units();
let res = { $($tt)* };
::solana_program::log::sol_log_compute_units();
::solana_program::msg!(concat!(" } // ", $msg));
res
};
}
3 changes: 2 additions & 1 deletion programs/drift/src/state/pyth_lazer_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct PythLazerOracle {
pub publish_time: u64,
pub posted_slot: u64,
pub exponent: i32,
pub _padding: [u8; 4],
pub bump: u8,
pub _padding: [u8; 3],
pub conf: u64,
}
28 changes: 28 additions & 0 deletions sdk/src/adminClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4333,6 +4333,34 @@ export class AdminClient extends DriftClient {
});
}

public async updatePythLazerOracleBump(
feedId: number
): Promise<TransactionSignature> {
const initializePythPullOracleIx =
await this.getUpdatePythLazerOracleBumpeIx(feedId);
const tx = await this.buildTransaction(initializePythPullOracleIx);
const { txSig } = await this.sendTransaction(tx, [], this.opts);

return txSig;
}

public async getUpdatePythLazerOracleBumpeIx(
feedId: number
): Promise<TransactionInstruction> {
return await this.program.instruction.updatePythLazerOracleBump(feedId, {
accounts: {
admin: this.useHotWalletAdmin
? this.wallet.publicKey
: this.getStateAccount().admin,
state: await this.getStatePublicKey(),
lazerOracle: getPythLazerOraclePublicKey(
this.program.programId,
feedId
),
},
});
}

public async initializeHighLeverageModeConfig(
maxUsers: number
): Promise<TransactionSignature> {
Expand Down
32 changes: 31 additions & 1 deletion sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -7243,6 +7243,32 @@
}
]
},
{
"name": "updatePythLazerOracleBump",
"accounts": [
{
"name": "admin",
"isMut": true,
"isSigner": true
},
{
"name": "lazerOracle",
"isMut": true,
"isSigner": false
},
{
"name": "state",
"isMut": false,
"isSigner": false
}
],
"args": [
{
"name": "feedId",
"type": "u32"
}
]
},
{
"name": "postPythLazerOracleUpdate",
"accounts": [
Expand Down Expand Up @@ -8338,12 +8364,16 @@
"name": "exponent",
"type": "i32"
},
{
"name": "bump",
"type": "u8"
},
{
"name": "padding",
"type": {
"array": [
"u8",
4
3
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion test-scripts/single-anchor-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fi

export ANCHOR_WALLET=~/.config/solana/id.json

test_files=(overwritePerpAccounts.ts)
test_files=(pythLazerBankrun.ts)

for test_file in ${test_files[@]}; do
ts-mocha -t 300000 ./tests/${test_file}
Expand Down
22 changes: 21 additions & 1 deletion tests/pythLazerBankrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,28 @@ describe('pyth pull oracles', () => {
await driftClient.initializePythLazerOracle(6);
});

it('update lazer account bumps', async () => {
await driftClient.updatePythLazerOracleBump(1);
await driftClient.updatePythLazerOracleBump(2);
await driftClient.updatePythLazerOracleBump(6);
});

it('crank single', async () => {
await driftClient.postPythLazerOracleUpdate([6], PYTH_LAZER_HEX_STRING_SOL);
const tx = await driftClient.postPythLazerOracleUpdate(
[6],
PYTH_LAZER_HEX_STRING_SOL
);
const logs = (
await bankrunContextWrapper.connection.getTransaction(tx, {
commitment: 'confirmed',
})
).meta.logMessages;
const consumedLog = logs.find((log) => log.includes('consumed'));
const cus = consumedLog.split('consumed ')[1].split(' ')[0];
console.log(`Consumed cus: ${cus}`);
});

it('can update perp market feed', async () => {
await driftClient.updatePerpMarketOracle(
0,
getPythLazerOraclePublicKey(driftClient.program.programId, 6),
Expand Down
Loading