Skip to content

Commit

Permalink
dex: use ensure! instead of !matches!
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalence committed Mar 27, 2024
1 parent 4f12d01 commit b2251b5
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions crates/core/component/dex/src/component/position_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,14 @@ pub trait PositionManager: StateWrite + PositionRead {
.expect("fetching position should not fail")
.ok_or_else(|| anyhow::anyhow!("could not find position {} to close", id))?;

if !matches!(
prev_state.state,
position::State::Opened | position::State::Closed,
) {
anyhow::bail!(
"attempted to close a position with state {:?}, expected Opened or Closed",
prev_state.state
);
}
anyhow::ensure!(
matches!(
prev_state.state,
position::State::Opened | position::State::Closed,
),
"attempted to close a position with state {:?}, expected Opened or Closed",
prev_state.state
);

let new_state = {
let mut new_state = prev_state.clone();
Expand Down Expand Up @@ -232,18 +231,16 @@ pub trait PositionManager: StateWrite + PositionRead {
.await?
.ok_or_else(|| anyhow::anyhow!("withdrew from unknown position {}", new_state.id()))?;

if !matches!(&prev_state.state, position::State::Opened) {
anyhow::bail!(
"attempted to execute against a position with state {:?}, expected Opened",
prev_state.state
);
}
if !matches!(&new_state.state, position::State::Opened) {
anyhow::bail!(
"supplied post-execution state {:?}, expected Opened",
new_state.state
);
}
anyhow::ensure!(
matches!(&prev_state.state, position::State::Opened),
"attempted to execute against a position with state {:?}, expected Opened",
prev_state.state
);
anyhow::ensure!(
matches!(&new_state.state, position::State::Opened),
"supplied post-execution state {:?}, expected Opened",
prev_state.state
);

// Handle "close-on-fill": automatically flip the position state to "closed" if
// either of the reserves are zero.
Expand Down

0 comments on commit b2251b5

Please sign in to comment.