Skip to content

Commit

Permalink
Filter out fixed candidates from the recently accessed asset list
Browse files Browse the repository at this point in the history
  • Loading branch information
zbuc committed May 3, 2024
1 parent d66ea67 commit c6449fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
10 changes: 8 additions & 2 deletions crates/core/component/dex/src/component/action_handler/swap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use anyhow::{ensure, Result};
use async_trait::async_trait;
use cnidarium::StateWrite;
Expand Down Expand Up @@ -68,8 +70,12 @@ impl ActionHandler for Swap {
.await;

// Mark the assets for the swap's trading pair as accessed during this block.
state.add_recently_accessed_asset(swap.body.trading_pair.asset_1());
state.add_recently_accessed_asset(swap.body.trading_pair.asset_2());
let fixed_candidates = Arc::new(dex_params.fixed_candidates.clone());
state.add_recently_accessed_asset(
swap.body.trading_pair.asset_1(),
fixed_candidates.clone(),
);
state.add_recently_accessed_asset(swap.body.trading_pair.asset_2(), fixed_candidates);

metrics::histogram!(crate::component::metrics::DEX_SWAP_DURATION)
.record(swap_start.elapsed());
Expand Down
31 changes: 25 additions & 6 deletions crates/core/component/dex/src/component/position_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ use penumbra_asset::{asset, Balance};
use penumbra_proto::DomainType;
use penumbra_proto::{StateReadProto, StateWriteProto};

use crate::component::position_manager::{
base_liquidity_index::AssetByLiquidityIndex, inventory_index::PositionByInventoryIndex,
price_index::PositionByPriceIndex,
use crate::component::{
dex::StateReadExt as _,
position_manager::{
base_liquidity_index::AssetByLiquidityIndex, inventory_index::PositionByInventoryIndex,
price_index::PositionByPriceIndex,
},
};
use crate::lp::Reserves;
use crate::{
Expand Down Expand Up @@ -253,8 +256,15 @@ pub trait PositionManager: StateWrite + PositionRead {

// Add the asset IDs from the new position's trading pair
// to the candidate set for this block.
self.add_recently_accessed_asset(position.phi.pair.asset_1());
self.add_recently_accessed_asset(position.phi.pair.asset_2());
let routing_params = self.routing_params().await?;
self.add_recently_accessed_asset(
position.phi.pair.asset_1(),
routing_params.fixed_candidates.clone(),
);
self.add_recently_accessed_asset(
position.phi.pair.asset_2(),
routing_params.fixed_candidates,
);

// Finally, record the new position state.
self.record_proto(event::position_open(&position));
Expand All @@ -269,7 +279,11 @@ pub trait PositionManager: StateWrite + PositionRead {
/// This ensures that assets associated with recently active positions
/// will be eligible for arbitrage if mispriced positions are opened.
#[tracing::instrument(level = "debug", skip_all)]
fn add_recently_accessed_asset(&mut self, asset_id: asset::Id) {
fn add_recently_accessed_asset(
&mut self,
asset_id: asset::Id,
fixed_candidates: Arc<Vec<asset::Id>>,
) {
let mut assets = self.recently_accessed_assets();

// Limit the number of recently accessed assets to prevent blowing
Expand All @@ -278,6 +292,11 @@ pub trait PositionManager: StateWrite + PositionRead {
return;
}

// If the asset is already in the fixed candidate list, don't insert it.
if fixed_candidates.contains(&asset_id) {
return;
}

assets.insert(asset_id);
self.object_put(state_key::recently_accessed_assets(), assets);
}
Expand Down

0 comments on commit c6449fa

Please sign in to comment.