Skip to content

Commit

Permalink
feat: Add optional execution price to failed order state
Browse files Browse the repository at this point in the history
The order might be set to failed with an execution price. This information is still valueable and should be kept on the order state.
  • Loading branch information
holzeis committed Mar 22, 2024
1 parent 37c99c9 commit cb52b63
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
10 changes: 7 additions & 3 deletions mobile/native/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,10 @@ impl From<crate::trade::order::OrderState> for (OrderState, Option<f32>, Option<
crate::trade::order::OrderState::Initial => (OrderState::Initial, None, None),
crate::trade::order::OrderState::Rejected => (OrderState::Rejected, None, None),
crate::trade::order::OrderState::Open => (OrderState::Open, None, None),
crate::trade::order::OrderState::Failed { reason } => {
(OrderState::Failed, None, Some(reason.into()))
}
crate::trade::order::OrderState::Failed {
execution_price,
reason,
} => (OrderState::Failed, execution_price, Some(reason.into())),
crate::trade::order::OrderState::Filled { execution_price } => {
(OrderState::Filled, Some(execution_price), None)
}
Expand All @@ -623,9 +624,11 @@ impl TryFrom<(OrderState, Option<f32>, Option<FailureReason>)> for crate::trade:
OrderState::Open => crate::trade::order::OrderState::Open,
OrderState::Failed => match value.2 {
None => crate::trade::order::OrderState::Failed {
execution_price: value.1,
reason: crate::trade::order::FailureReason::Unknown,
},
Some(reason) => crate::trade::order::OrderState::Failed {
execution_price: value.1,
reason: reason.into(),
},
},
Expand Down Expand Up @@ -1386,6 +1389,7 @@ pub mod test {
Order::update_state(
uuid1.to_string(),
crate::trade::order::OrderState::Failed {
execution_price: None,
reason: FailureReason::FailedToSetToFilling,
}
.into(),
Expand Down
9 changes: 8 additions & 1 deletion mobile/native/src/trade/order/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub async fn submit_order(
update_order_state_in_db_and_ui(
order.id,
OrderState::Failed {
execution_price: order.execution_price(),
reason: FailureReason::OrderRejected(err.to_string()),
},
)
Expand Down Expand Up @@ -235,7 +236,13 @@ pub(crate) fn order_failed(
};

if let Some(order_id) = order_id {
update_order_state_in_db_and_ui(order_id, OrderState::Failed { reason })?;
update_order_state_in_db_and_ui(
order_id,
OrderState::Failed {
execution_price: None,
reason,
},
)?;
}

// TODO: fixme. this so ugly, even a Sphynx cat is beautiful against this.
Expand Down
14 changes: 10 additions & 4 deletions mobile/native/src/trade/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ pub enum OrderState {
/// execution, and the trade execution failed; i.e. it did not result in setting up a DLC.
/// For the MVP there won't be a retry mechanism, so this is treated as a final state.
/// This is a final state.
Failed { reason: FailureReason },
Failed {
execution_price: Option<f32>,
reason: FailureReason,
},

/// Successfully set up trade
///
Expand Down Expand Up @@ -163,9 +166,12 @@ impl Order {
/// yet.
pub fn execution_price(&self) -> Option<f32> {
match self.state {
OrderState::Filling { execution_price } | OrderState::Filled { execution_price } => {
Some(execution_price)
}
OrderState::Filling { execution_price }
| OrderState::Filled { execution_price }
| OrderState::Failed {
execution_price: Some(execution_price),
..
} => Some(execution_price),
_ => {
// TODO: The caller should decide how to handle this. Always logging an error is
// weird.
Expand Down

0 comments on commit cb52b63

Please sign in to comment.