Skip to content

Commit

Permalink
Merge pull request #92 from openbook-dex/feature/lot_size_check
Browse files Browse the repository at this point in the history
check lots size inside market limits
  • Loading branch information
binyebarwe authored Jul 6, 2023
2 parents 79663ea + 8728582 commit bb757a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions programs/openbook-v2/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub enum OpenBookError {
InvalidInputMarketFees,
#[msg("Lots cannot be negative")]
InvalidInputLots,
#[msg("Lots size above market limits")]
InvalidInputLotsSize,
#[msg("Price lots should be greater than zero")]
InvalidInputPriceLots,
#[msg("Peg limit should be greater than zero")]
Expand Down Expand Up @@ -65,8 +67,6 @@ pub enum OpenBookError {
MarketHasExpired,
#[msg("Price lots should be greater than zero")]
InvalidPriceLots,
#[msg("Order size above market limits")]
InvalidOrderSize,
#[msg("Oracle price above market limits")]
InvalidOraclePrice,
#[msg("The Market has not expired yet.")]
Expand Down
8 changes: 8 additions & 0 deletions programs/openbook-v2/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ impl Market {
orderbook::new_node_key(side, price_data, self.seq_num)
}

pub fn max_base_lots(&self) -> i64 {
i64::MAX / self.base_lot_size
}

pub fn max_quote_lots(&self) -> i64 {
i64::MAX / self.quote_lot_size
}

/// Convert from the price stored on the book to the price used in value calculations
pub fn lot_to_native_price(&self, price: i64) -> I80F48 {
I80F48::from_num(price) * I80F48::from_num(self.quote_lot_size)
Expand Down
35 changes: 18 additions & 17 deletions programs/openbook-v2/src/state/orderbook/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,25 @@ impl<'a> Orderbook<'a> {
// Any changes to matching orders on the other side of the book are collected in
// matched_changes/matched_deletes and then applied after this loop.

let order_max_base_lots = order.max_base_lots;
let order_max_quote_lots = match side {
Side::Bid => market.subtract_taker_fees(order.max_quote_lots_including_fees),
Side::Ask => order.max_quote_lots_including_fees,
};

let mut remaining_base_lots = order.max_base_lots;
require_gte!(
market.max_base_lots(),
order_max_base_lots,
OpenBookError::InvalidInputLotsSize
);

require_gte!(
market.max_quote_lots(),
order_max_quote_lots,
OpenBookError::InvalidInputLotsSize
);

let mut remaining_base_lots = order_max_base_lots;
let mut remaining_quote_lots = order_max_quote_lots;
let mut decremented_quote_lots = 0_i64;

Expand Down Expand Up @@ -238,14 +251,8 @@ impl<'a> Orderbook<'a> {
assert!(total_quote_lots_taken >= 0);
assert!(total_base_lots_taken >= 0);

let total_base_taken_native = total_base_lots_taken
.checked_mul(market.base_lot_size)
.ok_or(OpenBookError::InvalidOrderSize)? as u64;

let mut total_quote_taken_native = total_quote_lots_taken
.checked_mul(market.quote_lot_size)
.ok_or(OpenBookError::InvalidOrderSize)?
as u64;
let total_base_taken_native = (total_base_lots_taken * market.base_lot_size) as u64;
let mut total_quote_taken_native = (total_quote_lots_taken * market.quote_lot_size) as u64;

// Record the taker trade in the account already, even though it will only be
// realized when the fill event gets executed
Expand Down Expand Up @@ -349,14 +356,8 @@ impl<'a> Orderbook<'a> {
// Open orders always exists in this case
let open_orders = open_orders_acc.as_mut().unwrap();

posted_base_native = book_base_quantity_lots
.checked_mul(market.base_lot_size)
.ok_or(OpenBookError::InvalidOrderSize)?;

posted_quote_native = book_base_quantity_lots
.checked_mul(price)
.and_then(|book_quote_lots| book_quote_lots.checked_mul(market.quote_lot_size))
.ok_or(OpenBookError::InvalidOrderSize)?;
posted_base_native = book_base_quantity_lots * market.base_lot_size;
posted_quote_native = book_base_quantity_lots * price * market.quote_lot_size;

// Subtract maker fees in bid.
if side == Side::Bid {
Expand Down

0 comments on commit bb757a3

Please sign in to comment.