diff --git a/lib/client/src/book.rs b/lib/client/src/book.rs index c5cdf446b..cebf4bb63 100644 --- a/lib/client/src/book.rs +++ b/lib/client/src/book.rs @@ -1,14 +1,15 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use anchor_lang::prelude::Pubkey; use anyhow::Result; use fixed::types::I80F48; +use itertools::Itertools; use openbook_v2::state::{ Market, Orderbook, Side, DROP_EXPIRED_ORDER_LIMIT, FILL_EVENT_REMAINING_LIMIT, }; pub const MAXIMUM_TAKEN_ORDERS: u8 = 45; -const MAXIMUM_REMAINING_ACCOUNTS: usize = 0; +const MAXIMUM_REMAINING_ACCOUNTS: usize = 3; pub struct Amounts { pub total_base_taken_native: u64, @@ -20,8 +21,6 @@ pub struct Amounts { pub fn remaining_accounts_to_crank( book: Orderbook, side: Side, - max_base_lots: i64, - max_quote_lots_including_fees: i64, market: &Market, oracle_price: Option, now_ts: u64, @@ -31,35 +30,18 @@ pub fn remaining_accounts_to_crank( } else { None }; - let mut accounts = Vec::new(); - iterate_book( - book, - side, - max_base_lots, - max_quote_lots_including_fees, - market, - oracle_price_lots, - now_ts, - &mut accounts, - ); - - // Get most occurred Pubkey - let mut frequency_map: HashMap = HashMap::new(); - for &value in &accounts { - *frequency_map.entry(value).or_insert(0) += 1; - } - // Sort by occurrences in descending order - let mut sorted_pairs: Vec<(Pubkey, usize)> = frequency_map.into_iter().collect(); - sorted_pairs.sort_by(|a, b| b.1.cmp(&a.1)); + let mut remaining_accounts = HashSet::new(); + let opposing_bookside = book.bookside(side.invert_side()); + for order in opposing_bookside.iter_valid(now_ts, oracle_price_lots) { + remaining_accounts.insert(order.node.owner); - let common_accounts: Vec = sorted_pairs - .iter() - .take(MAXIMUM_REMAINING_ACCOUNTS) - .map(|(value, _)| *value) - .collect(); + if remaining_accounts.len() >= MAXIMUM_REMAINING_ACCOUNTS { + break; + } + } - Ok(common_accounts) + Ok(remaining_accounts.into_iter().collect_vec()) } pub fn amounts_from_book( diff --git a/lib/client/src/jup.rs b/lib/client/src/jup.rs index 04c3a836b..8237bed8c 100644 --- a/lib/client/src/jup.rs +++ b/lib/client/src/jup.rs @@ -253,20 +253,6 @@ impl Amm for OpenBookMarket { let mut account_metas = accounts.to_account_metas(None); - let input_amount = i64::try_from(*in_amount)?; - - let (max_base_lots, max_quote_lots_including_fees) = match side { - Side::Bid => ( - self.market.max_base_lots(), - input_amount / self.market.quote_lot_size - + input_amount % self.market.quote_lot_size, - ), - Side::Ask => ( - input_amount / self.market.base_lot_size, - self.market.max_quote_lots(), - ), - }; - let bids_ref = RefCell::new(self.bids); let asks_ref = RefCell::new(self.asks); let book = Orderbook { @@ -274,21 +260,19 @@ impl Amm for OpenBookMarket { asks: asks_ref.borrow_mut(), }; - let remainigs = remaining_accounts_to_crank( + let remaining_accounts = remaining_accounts_to_crank( book, side, - max_base_lots, - max_quote_lots_including_fees, &self.market, self.oracle_price, self.timestamp, )?; - let remainigs_accounts: Vec = remainigs + let remainig_accounts: Vec = remaining_accounts .iter() .map(|&pubkey| AccountMeta::new(pubkey, false)) .collect(); - account_metas.extend(remainigs_accounts); + account_metas.extend(remainig_accounts); Ok(SwapAndAccountMetas { swap: Swap::Openbook { side: { jup_side } },