Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add handy utils functions #274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ impl OpenBookClient {
open_orders_account: self.open_orders_account,
signer: self.owner(),
open_orders_admin: market.open_orders_admin.into(),
user_quote_account: user_quote_account,
user_base_account: user_base_account,
user_quote_account,
user_base_account,
market: market_address,
bids: market.bids,
asks: market.asks,
Expand Down
16 changes: 16 additions & 0 deletions programs/openbook-v2/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ impl Market {
/ I80F48::from_num(self.base_lot_size)
}

/// Convert the quantity from quote lots to native quantity (e.g. 5 SOL, 3 USDC, etc)
pub fn quote_lot_to_native_quantity(&self, quote_lots: i64) -> i64 {
let quote_lot_size = self.quote_lot_size;
let quote_decimals = self.quote_decimals;
let quote_units = quote_lots * quote_lot_size;
quote_units / 10_i64.pow(quote_decimals.into())
}
wiseaidev marked this conversation as resolved.
Show resolved Hide resolved

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

pub fn native_price_to_lot(&self, price: I80F48) -> Result<i64> {
price
.checked_mul(I80F48::from_num(self.base_lot_size))
Expand Down
10 changes: 10 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,16 @@ 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()?
.node
.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
Loading