diff --git a/programs/drift/src/instructions/admin.rs b/programs/drift/src/instructions/admin.rs index 104cae4fc..fb72a1972 100644 --- a/programs/drift/src/instructions/admin.rs +++ b/programs/drift/src/instructions/admin.rs @@ -5023,6 +5023,11 @@ pub fn handle_update_mm_oracle_native(accounts: &[AccountInfo], data: &[u8]) -> let perp_market_sequence_id = u64::from_le_bytes(perp_market[936..944].try_into().unwrap()); let incoming_sequence_id = u64::from_le_bytes(data[8..16].try_into().unwrap()); + if &data[0..8] == &[0u8; 8] { + msg!("MM oracle price is zero, not updating"); + return Err(ErrorCode::DefaultError.into()); + } + if incoming_sequence_id > perp_market_sequence_id { let clock_account = &accounts[2]; let clock_data = clock_account.data.borrow(); diff --git a/programs/drift/src/instructions/pyth_lazer_oracle.rs b/programs/drift/src/instructions/pyth_lazer_oracle.rs index 827ffaab1..992e21cd1 100644 --- a/programs/drift/src/instructions/pyth_lazer_oracle.rs +++ b/programs/drift/src/instructions/pyth_lazer_oracle.rs @@ -88,22 +88,28 @@ pub fn handle_update_pyth_lazer_oracle<'c: 'info, 'info>( } } + let price = price.0.get(); + if price == 0 { + msg!("Pyth lazer price is zero, not enough publishers"); + return Err(ErrorCode::InvalidPythLazerMessage.into()); + } + let exponent = exponent.ok_or(ErrorCode::InvalidPythLazerMessage)?; // Default to 20bps of the price for conf if bid > ask or one-sided market - let mut conf: i64 = price.0.get().safe_div(500)?; + let mut conf: i64 = price.safe_div(500)?; if let (Some(bid), Some(ask)) = (best_bid_price, best_ask_price) { if bid.0.get() < ask.0.get() { conf = ask.0.get() - bid.0.get(); } } - pyth_lazer_oracle.price = price.0.get(); + pyth_lazer_oracle.price = price; pyth_lazer_oracle.posted_slot = Clock::get()?.slot; pyth_lazer_oracle.publish_time = next_timestamp; pyth_lazer_oracle.exponent = exponent.cast::()?; pyth_lazer_oracle.conf = conf.cast::()?; - msg!("Price updated to {}", price.0.get()); + msg!("Price updated to {}", price); msg!( "Posting new lazer update. current ts {} < next ts {}", diff --git a/tests/admin.ts b/tests/admin.ts index 110b66c77..d259fc657 100644 --- a/tests/admin.ts +++ b/tests/admin.ts @@ -433,6 +433,15 @@ describe('admin', () => { await driftClient.updateMmOracleNative(0, oraclePrice.addn(1), oracleTS); assert(perpMarket.amm.mmOraclePrice.eq(oraclePrice)); + // Errors if we try and update it with price of zero + try { + await driftClient.updateMmOracleNative(0, new BN(0), oracleTS); + assert.fail('Should have thrown'); + } catch (e) { + console.log(e.message); + assert(e.message.includes('custom program error')); + } + // Doesnt update if we flip the admin switch await driftClient.updateFeatureBitFlagsMMOracle(false); try {