Skip to content

Commit

Permalink
enable remaining accounts in jupiter swap (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschneider authored Apr 21, 2024
1 parent 6faca8d commit 8928d4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 62 deletions.
43 changes: 12 additions & 31 deletions lib/client/src/book.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::collections::HashMap;

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,
};
use std::collections::HashSet;

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,
Expand All @@ -20,8 +20,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<I80F48>,
now_ts: u64,
Expand All @@ -31,35 +29,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<Pubkey, usize> = 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<Pubkey> = 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(
Expand Down
38 changes: 7 additions & 31 deletions lib/client/src/jup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ impl Amm for OpenBookMarket {
let related_accounts = if is_permissioned {
vec![]
} else {
let mut accs = vec![
market.bids,
market.asks,
market.event_heap,
market.market_base_vault,
market.market_quote_vault,
clock::ID,
];
let mut accs = vec![market.bids, market.asks, market.event_heap, clock::ID];

accs.extend(
[market.oracle_a, market.oracle_b]
Expand Down Expand Up @@ -156,10 +149,10 @@ impl Amm for OpenBookMarket {
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 - 1) / self.market.quote_lot_size,
),
Side::Ask => (
input_amount / self.market.base_lot_size,
input_amount + (self.market.base_lot_size - 1) / self.market.base_lot_size,
self.market.max_quote_lots(),
),
};
Expand All @@ -178,7 +171,7 @@ impl Amm for OpenBookMarket {
max_quote_lots_including_fees,
&self.market,
self.oracle_price,
self.timestamp,
0,
)?;

let (in_amount, out_amount) = match side {
Expand All @@ -204,7 +197,6 @@ impl Amm for OpenBookMarket {

fn get_swap_and_account_metas(&self, swap_params: &SwapParams) -> Result<SwapAndAccountMetas> {
let SwapParams {
in_amount,
source_mint,
user_destination_token_account,
user_source_token_account,
Expand Down Expand Up @@ -253,42 +245,26 @@ 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 {
bids: bids_ref.borrow_mut(),
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<AccountMeta> = remainigs
let remaining_accounts: Vec<AccountMeta> = remaining_accounts
.iter()
.map(|&pubkey| AccountMeta::new(pubkey, false))
.collect();
account_metas.extend(remainigs_accounts);
account_metas.extend(remaining_accounts);

Ok(SwapAndAccountMetas {
swap: Swap::Openbook { side: { jup_side } },
Expand Down

0 comments on commit 8928d4c

Please sign in to comment.