Skip to content

Commit

Permalink
fix(mobile): Only update position state after order submitted
Browse files Browse the repository at this point in the history
Extract the logic checking whether the order is matching the currently open position
(we fail early if it's exact opposite direction and exact quantity).

Position state is only updated if the order got submitted.
  • Loading branch information
klochowicz committed May 3, 2023
1 parent ecd1b5e commit abac1ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
4 changes: 3 additions & 1 deletion mobile/native/src/trade/order/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::trade::order::FailureReason;
use crate::trade::order::Order;
use crate::trade::order::OrderState;
use crate::trade::position;
use crate::trade::position::handler::update_position_after_order_submitted;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
Expand All @@ -17,7 +18,7 @@ pub async fn submit_order(order: Order) -> Result<()> {
let url = format!("http://{}", config::get_http_endpoint());
let orderbook_client = OrderbookClient::new(Url::parse(&url)?);

if let Err(e) = position::handler::update_position_after_order_submitted(order) {
if let Err(e) = position::handler::get_position_matching_order(&order) {
order_failed(Some(order.id), FailureReason::OrderNotAcceptable, e)?;
bail!("Could not submit order because extending/reducing the position is not part of the MVP scope");
}
Expand All @@ -35,6 +36,7 @@ pub async fn submit_order(order: Order) -> Result<()> {
}

update_order_state_in_db_and_ui(order.id, OrderState::Open)?;
update_position_after_order_submitted(&order)?;

Ok(())
}
Expand Down
32 changes: 18 additions & 14 deletions mobile/native/src/trade/position/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,28 @@ pub fn get_positions() -> Result<Vec<Position>> {
///
/// If the new order submitted is an order that closes the current position, then the position will
/// be updated to `Closing` state.
pub fn update_position_after_order_submitted(submitted_order: Order) -> Result<()> {
if let Some(position) = db::get_positions()?.first() {
// closing the position
if position.direction == submitted_order.direction.opposite()
&& position.quantity == submitted_order.quantity
{
db::update_position_state(position.contract_symbol, PositionState::Closing)?;
let mut position = position.clone();
position.position_state = PositionState::Closing;
event::publish(&EventInternal::PositionUpdateNotification(position));
} else {
bail!("Currently not possible to extend or reduce a position, you can only close the position with a counter-order");
}
pub fn update_position_after_order_submitted(submitted_order: &Order) -> Result<()> {
if let Some(position) = get_position_matching_order(submitted_order) {
db::update_position_state(position.contract_symbol, PositionState::Closing)?;
let mut position = position.clone();
position.position_state = PositionState::Closing;
event::publish(&EventInternal::PositionUpdateNotification(position));
}

Ok(())
}

/// Returns the position that would be closed by submitted, if there is any
pub fn get_position_matching_order(order: &Order) -> Result<Option<Position>> {
Ok(if let Some(position) = db::get_positions()?.first() {
// closing the position
ensure!(position.direction == order.direction.opposite()
&& position.quantity == order.quantity, "Currently not possible to extend or reduce a position, you can only close the position with a counter-order");
Some(position.clone())
} else {
None
})
}

/// Resets the position to open again
///
/// This should be called if a went in a dirty state, e.g. the position is currently in
Expand Down

0 comments on commit abac1ea

Please sign in to comment.