Skip to content

Commit 1a876ea

Browse files
committed
chore: cargo fmt
1 parent aa57067 commit 1a876ea

3 files changed

Lines changed: 60 additions & 55 deletions

File tree

src/oracle.rs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn validate_pyth_price(
108108
}
109109

110110
// --- Read the number of component publishers ---
111-
// In pyth_sdk_solana::state, `num` is at offset 20 (after
111+
// In pyth_sdk_solana::state, `num` is at offset 20 (after
112112
// magic(4), ver(4), type(4), size(4), expo(4) = 20).
113113
let num = u32::from_le_bytes(data[20..24].try_into().ok()?) as usize;
114114

@@ -131,21 +131,9 @@ pub fn validate_pyth_price(
131131
// [16..20): status (i32)
132132
// [20..24): corp_act (i32)
133133
// [24..32): pub_slot (i64)
134-
let price_raw = i64::from_le_bytes(
135-
data[agg_offset..agg_offset + 8]
136-
.try_into()
137-
.ok()?,
138-
);
139-
let confidence = u64::from_le_bytes(
140-
data[agg_offset + 8..agg_offset + 16]
141-
.try_into()
142-
.ok()?,
143-
);
144-
let publish_time = i64::from_le_bytes(
145-
data[agg_offset + 24..agg_offset + 32]
146-
.try_into()
147-
.ok()?,
148-
);
134+
let price_raw = i64::from_le_bytes(data[agg_offset..agg_offset + 8].try_into().ok()?);
135+
let confidence = u64::from_le_bytes(data[agg_offset + 8..agg_offset + 16].try_into().ok()?);
136+
let publish_time = i64::from_le_bytes(data[agg_offset + 24..agg_offset + 32].try_into().ok()?);
149137

150138
// --- Read exponent from the header (offset 16 in pyth-sdk-solana) ---
151139
let exponent = i32::from_le_bytes(data[16..20].try_into().ok()?);
@@ -220,9 +208,7 @@ pub fn normalize_price(raw_price: u64, exponent: i32) -> u64 {
220208
.unwrap_or(u64::MAX);
221209
raw_price.checked_div(divisor).unwrap_or(0)
222210
} else {
223-
let multiplier = 10u64
224-
.checked_pow(exponent as u32)
225-
.unwrap_or(u64::MAX);
211+
let multiplier = 10u64.checked_pow(exponent as u32).unwrap_or(u64::MAX);
226212
raw_price.saturating_mul(multiplier)
227213
}
228214
}
@@ -245,11 +231,11 @@ mod tests {
245231
let mut data = vec![0u8; total_size];
246232

247233
// Header
248-
data[0..4].copy_from_slice(&PYTH_MAGIC.to_le_bytes()); // magic
249-
data[4..8].copy_from_slice(&2u32.to_le_bytes()); // ver
250-
data[8] = PYTH_ACCOUNT_TYPE_PRICE; // type
234+
data[0..4].copy_from_slice(&PYTH_MAGIC.to_le_bytes()); // magic
235+
data[4..8].copy_from_slice(&2u32.to_le_bytes()); // ver
236+
data[8] = PYTH_ACCOUNT_TYPE_PRICE; // type
251237
data[12..16].copy_from_slice(&(total_size as u32).to_le_bytes()); // size
252-
data[16..20].copy_from_slice(&exponent.to_le_bytes()); // expo
238+
data[16..20].copy_from_slice(&exponent.to_le_bytes()); // expo
253239
data[20..24].copy_from_slice(&num_publishers.to_le_bytes()); // num
254240

255241
// Aggregate PriceInfo starts after header + comp array
@@ -286,16 +272,9 @@ mod tests {
286272
}
287273

288274
let price_raw = i64::from_le_bytes(data[agg_offset..agg_offset + 8].try_into().ok()?);
289-
let confidence = u64::from_le_bytes(
290-
data[agg_offset + 8..agg_offset + 16]
291-
.try_into()
292-
.ok()?,
293-
);
294-
let publish_time = i64::from_le_bytes(
295-
data[agg_offset + 24..agg_offset + 32]
296-
.try_into()
297-
.ok()?,
298-
);
275+
let confidence = u64::from_le_bytes(data[agg_offset + 8..agg_offset + 16].try_into().ok()?);
276+
let publish_time =
277+
i64::from_le_bytes(data[agg_offset + 24..agg_offset + 32].try_into().ok()?);
299278
let exponent = i32::from_le_bytes(data[16..20].try_into().ok()?);
300279

301280
let price_abs = price_raw.unsigned_abs();
@@ -326,7 +305,7 @@ mod tests {
326305
}
327306

328307
let age = clock_ts.checked_sub(publish_time).unwrap_or(i64::MAX);
329-
if !(0..=MAX_STALENESS_SECS).contains(&age) {
308+
if !(0..=MAX_STALENESS_SECS).contains(&age) {
330309
return Some(OraclePriceData {
331310
price: price_abs,
332311
exponent,

src/processor.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,10 +1419,7 @@ pub fn match_orders(
14191419
// fill price. Otherwise fall back to the CLOB limit price.
14201420
let clock = Clock::get()?;
14211421
let fill_price = if let Some(oracle_acct) = ctx.oracle_price {
1422-
let oracle_data = crate::oracle::validate_pyth_price(
1423-
oracle_acct,
1424-
clock.unix_timestamp,
1425-
);
1422+
let oracle_data = crate::oracle::validate_pyth_price(oracle_acct, clock.unix_timestamp);
14261423
match oracle_data {
14271424
Some(od) if od.is_valid => {
14281425
// If PMM is configured, compute the PMM effective price using
@@ -1462,7 +1459,8 @@ pub fn match_orders(
14621459
.checked_mul(1_000_000)
14631460
.ok_or(FluxDexError::ArithmeticOverflow)?
14641461
.checked_div(m_pmm_base_eq as i128)
1465-
.ok_or(FluxDexError::ArithmeticOverflow)? as i64)
1462+
.ok_or(FluxDexError::ArithmeticOverflow)?
1463+
as i64)
14661464
};
14671465

14681466
// Clamp skew to [-1_000_000, 1_000_000]

tests/flux_dex_tests.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,8 +2547,15 @@ fn match_orders_ix(
25472547
seller_order: &Pubkey,
25482548
) -> Instruction {
25492549
match_orders_ix_with_oracle(
2550-
crank, crank_signer, market, orderbook, base_vault, quote_vault,
2551-
buyer_order, seller_order, None,
2550+
crank,
2551+
crank_signer,
2552+
market,
2553+
orderbook,
2554+
base_vault,
2555+
quote_vault,
2556+
buyer_order,
2557+
seller_order,
2558+
None,
25522559
)
25532560
}
25542561

@@ -4210,10 +4217,8 @@ fn two_traders_delegate_undelegate_match_settle() {
42104217

42114218
/// Pyth oracle program ID on mainnet (just for owner field in test).
42124219
const PYTH_ORACLE_ID: Pubkey = Pubkey::new_from_array([
4213-
0x6d, 0xe2, 0x39, 0xa5, 0xa2, 0xb4, 0xc7, 0x43,
4214-
0x62, 0x09, 0xf5, 0xa5, 0x0c, 0x35, 0x2b, 0x98,
4215-
0x51, 0xdb, 0xe0, 0x4e, 0xae, 0x13, 0x8a, 0xd3,
4216-
0x11, 0xf8, 0x9e, 0x28, 0x85, 0xaf, 0x11, 0x00,
4220+
0x6d, 0xe2, 0x39, 0xa5, 0xa2, 0xb4, 0xc7, 0x43, 0x62, 0x09, 0xf5, 0xa5, 0x0c, 0x35, 0x2b, 0x98,
4221+
0x51, 0xdb, 0xe0, 0x4e, 0xae, 0x13, 0x8a, 0xd3, 0x11, 0xf8, 0x9e, 0x28, 0x85, 0xaf, 0x11, 0x00,
42174222
]);
42184223

42194224
/// Create a mock Pyth V2 price account and set it into the SVM.
@@ -4226,16 +4231,18 @@ fn setup_pyth_oracle(
42264231
exponent: i32,
42274232
num_publishers: u32,
42284233
) {
4229-
use flux_dex::oracle::{COMP_SIZE, HEADER_SIZE, PRICE_INFO_SIZE, PYTH_ACCOUNT_TYPE_PRICE, PYTH_MAGIC};
4234+
use flux_dex::oracle::{
4235+
COMP_SIZE, HEADER_SIZE, PRICE_INFO_SIZE, PYTH_ACCOUNT_TYPE_PRICE, PYTH_MAGIC,
4236+
};
42304237

42314238
let comp_array_size = num_publishers as usize * COMP_SIZE;
42324239
let total_size = HEADER_SIZE + comp_array_size + PRICE_INFO_SIZE;
42334240
let mut data = vec![0u8; total_size];
42344241

42354242
// Header
42364243
data[0..4].copy_from_slice(&PYTH_MAGIC.to_le_bytes());
4237-
data[4..8].copy_from_slice(&2u32.to_le_bytes()); // version
4238-
data[8] = PYTH_ACCOUNT_TYPE_PRICE; // type
4244+
data[4..8].copy_from_slice(&2u32.to_le_bytes()); // version
4245+
data[8] = PYTH_ACCOUNT_TYPE_PRICE; // type
42394246
data[12..16].copy_from_slice(&(total_size as u32).to_le_bytes());
42404247
data[16..20].copy_from_slice(&exponent.to_le_bytes());
42414248
data[20..24].copy_from_slice(&num_publishers.to_le_bytes());
@@ -4251,10 +4258,13 @@ fn setup_pyth_oracle(
42514258
// Insert into SVM
42524259
svm.airdrop(oracle_addr, 1_000_000_000)
42534260
.expect("airdrop for oracle account");
4254-
let mut account = svm.get_account(oracle_addr).expect("oracle account after airdrop");
4261+
let mut account = svm
4262+
.get_account(oracle_addr)
4263+
.expect("oracle account after airdrop");
42554264
account.data = data;
42564265
account.owner = PYTH_ORACLE_ID;
4257-
svm.set_account(*oracle_addr, account).expect("set pyth oracle account");
4266+
svm.set_account(*oracle_addr, account)
4267+
.expect("set pyth oracle account");
42584268
}
42594269

42604270
/// match_orders with a valid Pyth oracle price account.
@@ -4296,7 +4306,10 @@ fn match_orders_with_pyth_oracle() {
42964306
Some(&px),
42974307
);
42984308
let cu = send_tx(&mut fix.svm, &[ix], &fix.payer.pubkey(), &[&fix.payer]);
4299-
assert!(cu < 40_000, "match_orders with oracle CU {cu} exceeded 40k limit");
4309+
assert!(
4310+
cu < 40_000,
4311+
"match_orders with oracle CU {cu} exceeded 40k limit"
4312+
);
43004313

43014314
// Verify both orders filled.
43024315
let b_acc = fix.svm.get_account(&b_order).expect("buyer order account");
@@ -4311,7 +4324,11 @@ fn match_orders_with_pyth_oracle() {
43114324
// The raw Pyth price (100_000_000) is stored; frontend normalizes via exponent.
43124325
let mkt_acc = fix.svm.get_account(&fix.market).expect("market account");
43134326
let mkt = Market::from_account(&mkt_acc.data).expect("decode market");
4314-
assert_eq!(mkt.oracle_price(), 100_000_000, "oracle price raw (exp -6 => 100 human)");
4327+
assert_eq!(
4328+
mkt.oracle_price(),
4329+
100_000_000,
4330+
"oracle price raw (exp -6 => 100 human)"
4331+
);
43154332
assert_eq!(mkt.oracle_timestamp(), now, "oracle timestamp cached");
43164333
}
43174334

@@ -4357,7 +4374,11 @@ fn match_orders_with_stale_oracle_still_succeeds() {
43574374
// Verify orders filled.
43584375
let b_acc = fix.svm.get_account(&b_order).expect("buyer order account");
43594376
let b = Order::from_account(&b_acc.data).expect("decode buyer order");
4360-
assert_eq!(b.status().unwrap(), OrderStatus::Filled, "buyer filled despite stale oracle");
4377+
assert_eq!(
4378+
b.status().unwrap(),
4379+
OrderStatus::Filled,
4380+
"buyer filled despite stale oracle"
4381+
);
43614382
}
43624383

43634384
/// match_orders without oracle (back-compat): 7-account layout still works.
@@ -4383,11 +4404,18 @@ fn match_orders_without_oracle_back_compat() {
43834404
&s_order,
43844405
);
43854406
let cu = send_tx(&mut fix.svm, &[ix], &fix.payer.pubkey(), &[&fix.payer]);
4386-
assert!(cu < 30_000, "match_orders w/o oracle CU {cu} exceeded 30k limit");
4407+
assert!(
4408+
cu < 30_000,
4409+
"match_orders w/o oracle CU {cu} exceeded 30k limit"
4410+
);
43874411

43884412
let b_acc = fix.svm.get_account(&b_order).expect("buyer order account");
43894413
let b = Order::from_account(&b_acc.data).expect("decode buyer order");
4390-
assert_eq!(b.status().unwrap(), OrderStatus::Filled, "buyer filled without oracle");
4414+
assert_eq!(
4415+
b.status().unwrap(),
4416+
OrderStatus::Filled,
4417+
"buyer filled without oracle"
4418+
);
43914419
}
43924420

43934421
// ============================================================================

0 commit comments

Comments
 (0)