Skip to content

Commit

Permalink
fix subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
none00y committed Nov 7, 2024
1 parent 6608fc8 commit bc63ebd
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/dex-invariant/src/invariant_dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use anyhow::{Context, Ok};
use async_trait::async_trait;
use invariant_types::{
math::{calculate_price_sqrt, get_max_tick, get_min_tick},
structs::{Pool, Tick, Tickmap, TickmapView, TICK_CROSSES_PER_IX, TICK_LIMIT},
structs::{
Pool, Tick, Tickmap, TickmapView, TICK_CROSSES_PER_IX, TICK_LIMIT, TICK_SEARCH_RANGE,
},
ANCHOR_DISCRIMINATOR_SIZE, TICK_SEED,
};
use router_feed_lib::router_rpc_client::{RouterRpcClient, RouterRpcClientTrait};
Expand All @@ -36,6 +38,7 @@ use crate::{
pub struct InvariantDex {
pub edges: HashMap<Pubkey, Vec<Arc<dyn DexEdgeIdentifier>>>,
}
pub const TICK_SUBSCRIPTION_RANGE: i32 = TICK_CROSSES_PER_IX as i32 * TICK_SEARCH_RANGE * 2;

#[derive(Debug)]
pub enum PriceDirection {
Expand Down Expand Up @@ -281,23 +284,29 @@ impl DexInterface for InvariantDex {
})
.into_iter()
.collect();
let tickmaps = pools.iter().map(|p| p.1.tickmap).collect();
let tickmaps = rpc.get_multiple_accounts(&tickmaps).await?;
let tickmaps = pools.iter().map(|p| p.1.tickmap).collect::<Vec<Pubkey>>();

let edges_per_pk = {
let mut map = HashMap::new();
let pools_with_edge_pairs = pools.iter().zip(tickmaps.iter()).zip(edge_pairs.iter());
for (((pool_pk, pool), (tickmap_pk, tickmap_acc)), (edge_x_to_y, edge_y_to_x)) in
pools_with_edge_pairs
for (((pool_pk, pool), tickmap_pk), (edge_x_to_y, edge_y_to_x)) in pools_with_edge_pairs
{
let entry: Vec<Arc<dyn DexEdgeIdentifier>> =
vec![edge_x_to_y.clone(), edge_y_to_x.clone()];
map.insert(*pool_pk, entry.clone());
map.insert(*tickmap_pk, entry.clone());

let tickmap_account_data = tickmap_acc.data();
let tickmap = Self::deserialize::<Tickmap>(tickmap_account_data)?;
let indexes = Self::find_all_tick_indexes(pool.tick_spacing, &tickmap)?;
let min_tick = get_min_tick(pool.tick_spacing)?;
let max_tick = get_max_tick(pool.tick_spacing)?;

let tick_range_max =
max_tick.min(pool.current_tick_index + TICK_SUBSCRIPTION_RANGE);

let tick_range_min =
min_tick.max(pool.current_tick_index - TICK_SUBSCRIPTION_RANGE);

let indexes = tick_range_min..=tick_range_max;

for tick in indexes {
map.insert(Self::tick_index_to_address(*pool_pk, tick), entry.clone());
}
Expand Down

0 comments on commit bc63ebd

Please sign in to comment.