Skip to content

Commit

Permalink
add bids_quote_lots to ooa
Browse files Browse the repository at this point in the history
  • Loading branch information
binyebarwe committed Dec 18, 2023
1 parent 269cd09 commit 1999647
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions programs/openbook-v2/fuzz/fuzz_targets/multiple_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ fn run_fuzz(fuzz_data: FuzzData) -> Corpus {
};

assert_eq!(position.bids_base_lots, 0);
assert_eq!(position.bids_quote_lots, 0);
assert_eq!(position.asks_base_lots, 0);
assert_eq!(position.base_free_native, 0);
assert_eq!(position.quote_free_native, 0);
Expand Down
2 changes: 2 additions & 0 deletions programs/openbook-v2/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub struct OpenOrdersPositionLog {
pub market: Pubkey,
/// Base lots in open bids
pub bids_base_lots: i64,
/// Quote lots in open bids
pub bids_quote_lots: i64,
/// Base lots in open asks
pub asks_base_lots: i64,
pub base_free_native: u64,
Expand Down
32 changes: 24 additions & 8 deletions programs/openbook-v2/src/state/open_orders_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ impl OpenOrdersAccount {
market.fees_accrued += maker_fees as u128;

if fill.maker_out() {
self.remove_order(fill.maker_slot as usize, fill.quantity);
self.remove_order(fill.maker_slot as usize, fill.quantity, fill.price);
} else {
match side {
Side::Bid => pa.bids_base_lots -= fill.quantity,
Side::Bid => {
pa.bids_base_lots -= fill.quantity;
pa.bids_quote_lots -= fill.quantity * fill.price;
}
Side::Ask => pa.asks_base_lots -= fill.quantity,
};
}
Expand Down Expand Up @@ -201,6 +204,7 @@ impl OpenOrdersAccount {
open_orders_account_num: self.account_num,
market: self.market,
bids_base_lots: pa.bids_base_lots,
bids_quote_lots: pa.bids_quote_lots,
asks_base_lots: pa.asks_base_lots,
base_free_native: pa.base_free_native,
quote_free_native: pa.quote_free_native,
Expand Down Expand Up @@ -236,6 +240,7 @@ impl OpenOrdersAccount {
open_orders_account_num: self.account_num,
market: self.market,
bids_base_lots: pa.bids_base_lots,
bids_quote_lots: pa.bids_quote_lots,
asks_base_lots: pa.asks_base_lots,
base_free_native: pa.base_free_native,
quote_free_native: pa.quote_free_native,
Expand All @@ -256,7 +261,10 @@ impl OpenOrdersAccount {
) {
let position = &mut self.position;
match side {
Side::Bid => position.bids_base_lots += order.quantity,
Side::Bid => {
position.bids_base_lots += order.quantity;
position.bids_quote_lots += order.quantity * locked_price;
}
Side::Ask => position.asks_base_lots += order.quantity,
};
let slot = order.owner_slot as usize;
Expand All @@ -269,7 +277,7 @@ impl OpenOrdersAccount {
oo.locked_price = locked_price;
}

pub fn remove_order(&mut self, slot: usize, base_quantity: i64) {
pub fn remove_order(&mut self, slot: usize, base_quantity: i64, price: i64) {
let oo = self.open_order_by_raw_index(slot);
assert!(!oo.is_free());

Expand All @@ -278,7 +286,10 @@ impl OpenOrdersAccount {

// accounting
match order_side {
Side::Bid => position.bids_base_lots -= base_quantity,
Side::Bid => {
position.bids_base_lots -= base_quantity;
position.bids_quote_lots -= base_quantity * price;
}
Side::Ask => position.asks_base_lots -= base_quantity,
}

Expand All @@ -304,7 +315,7 @@ impl OpenOrdersAccount {
Side::Ask => position.base_free_native += base_quantity_native,
}

self.remove_order(slot, base_quantity);
self.remove_order(slot, base_quantity, price);
}
}

Expand All @@ -331,8 +342,11 @@ pub struct Position {
/// Cumulative taker volume in quote native units (display only)
pub taker_volume: u128,

/// Quote lots in open bids
pub bids_quote_lots: i64,

#[derivative(Debug = "ignore")]
pub reserved: [u8; 72],
pub reserved: [u8; 64],
}

const_assert_eq!(
Expand All @@ -354,7 +368,8 @@ impl Default for Position {
penalty_heap_count: 0,
maker_volume: 0,
taker_volume: 0,
reserved: [0; 72],
bids_quote_lots: 0,
reserved: [0; 64],
}
}
}
Expand All @@ -376,6 +391,7 @@ impl Position {
&& self.locked_maker_fees == 0
&& self.referrer_rebates_available == 0
&& self.penalty_heap_count == 0
&& self.bids_quote_lots == 0
}
}

Expand Down
3 changes: 3 additions & 0 deletions programs/openbook-v2/src/state/orderbook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ mod tests {
));
assert!(order_tree_contains_price(&book.bids, price_lots as u64));
assert_eq!(maker.position.bids_base_lots, bid_quantity);
assert_eq!(maker.position.bids_quote_lots, bid_quantity * price_lots);
assert_eq!(maker.position.asks_base_lots, 0);
assert_eq!(event_heap.len(), 0);

Expand Down Expand Up @@ -330,6 +331,7 @@ mod tests {
// the taker account is updated
assert!(taker.open_order_by_raw_index(1).is_free());
assert_eq!(taker.position.bids_base_lots, 0);
assert_eq!(taker.position.bids_quote_lots, 0);
assert_eq!(taker.position.asks_base_lots, 0);
// the fill gets added to the event heap
assert_eq!(event_heap.len(), 1);
Expand All @@ -350,6 +352,7 @@ mod tests {
assert_eq!(maker.position.asks_base_lots, 0);

assert_eq!(taker.position.bids_base_lots, 0);
assert_eq!(taker.position.bids_quote_lots, 0);
assert_eq!(taker.position.asks_base_lots, 0);
// Maker fee is accrued now
assert_eq!(
Expand Down

0 comments on commit 1999647

Please sign in to comment.