Skip to content

Commit

Permalink
feat: add handy utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseaidev committed Jun 23, 2024
1 parent cfbf82d commit e225f86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions programs/openbook-v2/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ impl Market {
pub fn max_quote_lots(&self) -> i64 {
i64::MAX / self.quote_lot_size
}
a

pub fn max_base_lots_from_lamports(&self, lamports: u64) -> i64 {
let base_lots = lamports / self.base_lot_size as u64;
Expand All @@ -218,6 +219,24 @@ impl Market {
/ I80F48::from_num(self.base_lot_size)
}

/// Convert the quantity from quote lots to native quantity (e.g. 5 SOL, 0.3 USDC, etc)
pub fn quote_lot_to_native_quantity(&self, quote_lots: i64) -> f64 {
let quote_lot_size = self.quote_lot_size as f64;
let quote_decimals = self.quote_decimals as u32;
let quote_units = quote_lots as f64 * quote_lot_size;
let quantity = quote_units / 10_i64.pow(quote_decimals) as f64;
quantity
}

/// Convert the quantity from base lots to native quantity (e.g. 5 SOL, 0.3 USDC, etc)
pub fn base_lot_to_native_quantity(&self, base_lots: i64) -> f64 {
let base_lot_size = self.base_lot_size as f64;
let base_decimals = self.base_decimals as u32;
let base_units = base_lots as f64 * base_lot_size;
let quantity = base_units / 10_i64.pow(base_decimals) as f64;
quantity
}

pub fn native_price_to_lot(&self, price: I80F48) -> Result<i64> {
price
.checked_mul(I80F48::from_num(self.base_lot_size))
Expand Down
9 changes: 9 additions & 0 deletions programs/openbook-v2/src/state/orderbook/bookside.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ impl BookSide {
)
}

/// Return the quantity of the order closest to the spread
pub fn best_quantity(&self, now_ts: u64, oracle_price_lots: Option<i64>) -> Option<i64> {
Some(
self.iter_valid(now_ts, oracle_price_lots)
.next()?
.quantity,
)
}

/// Walk up the book `quantity` units and return the price at that level. If `quantity` units
/// not on book, return None
pub fn impact_price(&self, quantity: i64, now_ts: u64, oracle_price_lots: i64) -> Option<i64> {
Expand Down

0 comments on commit e225f86

Please sign in to comment.