Skip to content

Commit

Permalink
dex: avoid recomputing position ID during execution
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalence committed Mar 27, 2024
1 parent b2251b5 commit d22279a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
let id = buy_1.id();

let position = buy_1;
state_tx.index_position_by_price(&position);
state_tx.index_position_by_price(&position, &position.id());
state_tx
.update_available_liquidity(&position, &None)
.await
Expand Down
14 changes: 7 additions & 7 deletions crates/core/component/dex/src/component/position_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,29 +366,30 @@ pub(crate) trait Inner: StateWrite {
) -> Result<()> {
tracing::debug!(?prev_state, ?new_state, "updating position state");

let id = new_state.id();

// Clear any existing indexes of the position, since changes to the
// reserves or the position state might have invalidated them.
if let Some(prev_state) = prev_state.as_ref() {
self.deindex_position_by_price(&prev_state);
self.deindex_position_by_price(&prev_state, &id);
}

// Only index the position's liquidity if it is active.
if new_state.state == position::State::Opened {
self.index_position_by_price(&new_state);
self.index_position_by_price(&new_state, &id);
}

// Update the available liquidity for this position's trading pair.
// TODO: refactor and streamline this method while implementing eviction.
self.update_available_liquidity(&new_state, &prev_state)
.await?;

let id = new_state.id();
self.put(state_key::position_by_id(&id), new_state);
Ok(())
}

fn index_position_by_price(&mut self, position: &position::Position) {
fn index_position_by_price(&mut self, position: &position::Position, id: &position::Id) {
let (pair, phi) = (position.phi.pair, &position.phi);
let id = position.id();
if position.reserves.r2 != 0u64.into() {
// Index this position for trades FROM asset 1 TO asset 2, since the position has asset 2 to give out.
let pair12 = DirectedTradingPair {
Expand Down Expand Up @@ -418,8 +419,7 @@ pub(crate) trait Inner: StateWrite {
}
}

fn deindex_position_by_price(&mut self, position: &Position) {
let id = position.id();
fn deindex_position_by_price(&mut self, position: &Position, id: &position::Id) {
tracing::debug!("deindexing position");
let pair12 = DirectedTradingPair {
start: position.phi.pair.asset_1(),
Expand Down
3 changes: 2 additions & 1 deletion crates/core/component/dex/src/component/router/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ impl<S: StateRead + 'static> Path<S> {
// Deindex the position we "consumed" in this and all descendant state forks,
// ensuring we don't double-count liquidity while traversing cycles.
use super::super::position_manager::Inner as _;
self.state.deindex_position_by_price(&best_price_position);
self.state
.deindex_position_by_price(&best_price_position, &best_price_position.id());

// Compute the effective price of a trade in the direction self.end()=>new_end
let hop_price = best_price_position
Expand Down

0 comments on commit d22279a

Please sign in to comment.