Skip to content

Commit

Permalink
dex: emit an event when autoclosing a LP
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed Apr 29, 2024
1 parent f2aee1d commit d088689
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ impl ActionHandler for PositionClose {
// during that block's batch swap execution.
state.queue_close_position(self.position_id);

state.record_proto(event::position_close(self));
// queue position close you will...
state.record_proto(event::queue_position_close(self));

Ok(())
}
Expand Down
12 changes: 9 additions & 3 deletions crates/core/component/dex/src/component/position_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,9 @@ pub trait PositionManager: StateWrite + PositionRead {
mut new_state: Position,
context: DirectedTradingPair,
) -> Result<Position> {
let position_id = new_state.id();
let prev_state = self
.position_by_id(&new_state.id())
.position_by_id(&position_id)
.await?
.ok_or_else(|| anyhow::anyhow!("withdrew from unknown position {}", new_state.id()))?;

Expand Down Expand Up @@ -311,21 +312,26 @@ pub trait PositionManager: StateWrite + PositionRead {
prev_state.state
);

// We have already short-circuited no-op execution updates, so we can emit an execution
// event and not worry about duplicates.
self.record_proto(event::position_execution(&prev_state, &new_state, context));

// Handle "close-on-fill": automatically flip the position state to "closed" if
// either of the reserves are zero.
if new_state.close_on_fill {
if new_state.reserves.r1 == 0u64.into() || new_state.reserves.r2 == 0u64.into() {
tracing::debug!(
id = ?new_state.id(),
?position_id,
r1 = ?new_state.reserves.r1,
r2 = ?new_state.reserves.r2,
"marking position as closed due to close-on-fill"
);

new_state.state = position::State::Closed;
self.record_proto(event::position_close_by_id(position_id));
}
}

self.record_proto(event::position_execution(&prev_state, &new_state, context));
self.update_position(Some(prev_state), new_state).await
}

Expand Down
6 changes: 6 additions & 0 deletions crates/core/component/dex/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ pub fn position_open(position: &Position) -> pb::EventPositionOpen {
}
}

pub fn position_close_by_id(id: position::Id) -> pb::EventPositionClose {
pb::EventPositionClose {
position_id: Some(id.into()),
}
}

pub fn position_close(action: &PositionClose) -> pb::EventPositionClose {
pb::EventPositionClose {
position_id: Some(action.position_id.into()),
Expand Down

0 comments on commit d088689

Please sign in to comment.