Skip to content

Commit d06f9a9

Browse files
authored
pyth lazer zero guard (#2023)
1 parent 9a37795 commit d06f9a9

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

programs/drift/src/instructions/admin.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5023,6 +5023,11 @@ pub fn handle_update_mm_oracle_native(accounts: &[AccountInfo], data: &[u8]) ->
50235023
let perp_market_sequence_id = u64::from_le_bytes(perp_market[936..944].try_into().unwrap());
50245024
let incoming_sequence_id = u64::from_le_bytes(data[8..16].try_into().unwrap());
50255025

5026+
if &data[0..8] == &[0u8; 8] {
5027+
msg!("MM oracle price is zero, not updating");
5028+
return Err(ErrorCode::DefaultError.into());
5029+
}
5030+
50265031
if incoming_sequence_id > perp_market_sequence_id {
50275032
let clock_account = &accounts[2];
50285033
let clock_data = clock_account.data.borrow();

programs/drift/src/instructions/pyth_lazer_oracle.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,28 @@ pub fn handle_update_pyth_lazer_oracle<'c: 'info, 'info>(
8888
}
8989
}
9090

91+
let price = price.0.get();
92+
if price == 0 {
93+
msg!("Pyth lazer price is zero, not enough publishers");
94+
return Err(ErrorCode::InvalidPythLazerMessage.into());
95+
}
96+
9197
let exponent = exponent.ok_or(ErrorCode::InvalidPythLazerMessage)?;
9298

9399
// Default to 20bps of the price for conf if bid > ask or one-sided market
94-
let mut conf: i64 = price.0.get().safe_div(500)?;
100+
let mut conf: i64 = price.safe_div(500)?;
95101
if let (Some(bid), Some(ask)) = (best_bid_price, best_ask_price) {
96102
if bid.0.get() < ask.0.get() {
97103
conf = ask.0.get() - bid.0.get();
98104
}
99105
}
100106

101-
pyth_lazer_oracle.price = price.0.get();
107+
pyth_lazer_oracle.price = price;
102108
pyth_lazer_oracle.posted_slot = Clock::get()?.slot;
103109
pyth_lazer_oracle.publish_time = next_timestamp;
104110
pyth_lazer_oracle.exponent = exponent.cast::<i32>()?;
105111
pyth_lazer_oracle.conf = conf.cast::<u64>()?;
106-
msg!("Price updated to {}", price.0.get());
112+
msg!("Price updated to {}", price);
107113

108114
msg!(
109115
"Posting new lazer update. current ts {} < next ts {}",

tests/admin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,15 @@ describe('admin', () => {
433433
await driftClient.updateMmOracleNative(0, oraclePrice.addn(1), oracleTS);
434434
assert(perpMarket.amm.mmOraclePrice.eq(oraclePrice));
435435

436+
// Errors if we try and update it with price of zero
437+
try {
438+
await driftClient.updateMmOracleNative(0, new BN(0), oracleTS);
439+
assert.fail('Should have thrown');
440+
} catch (e) {
441+
console.log(e.message);
442+
assert(e.message.includes('custom program error'));
443+
}
444+
436445
// Doesnt update if we flip the admin switch
437446
await driftClient.updateFeatureBitFlagsMMOracle(false);
438447
try {

0 commit comments

Comments
 (0)