Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/only update position if order succeeded #585

Merged
merged 3 commits into from
May 3, 2023

Conversation

klochowicz
Copy link
Contributor

@klochowicz klochowicz commented May 3, 2023

Improved version of #567 . Made sure I did the least amount of changes needed, and addressed issues raised by @da-kami and @holzeis in previous review.

`taken()` gave the impression that you can only mark it as taken, but in fact
one can unmark it as well.

Whether "untaking" an order is a reasonable thing to do, I'm not so sure...
We pretended that these calls were async, but they weren't.
@klochowicz klochowicz self-assigned this May 3, 2023
@klochowicz klochowicz linked an issue May 3, 2023 that may be closed by this pull request
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that we want to check this, but for me it's unexpected that the check is happening implicitly when calling get_position_matching_order.

I think it would be clearer if we extracted this into a separate function does_order_close_position and just call it if we do have an order in the database. It's two calls rather than one, but the current behaviour is surprising to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to work on that as a follow-up.

my goal was to change as little as possible, as the previous one accidentally changed the logic during the refactor.
(including keeping the flow of error handling etc.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I guess now that you extracted this function it just feels weird that this new function can error like this, whereas originally it didn't bother me. It's a minor detail, specially since as you say it was already effectively the same before your changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @luckysori that does_order_close_position is a better name than get_position_matching_order - the function was newly introduced. I think it'd be great to rename it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@da-kami : @luckysori didn't want to rename this, we wanted to extract one more function, probably with bool return. I opted to not do this for now, out of fear of last-minute in-adverted changes to the logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's fix that up when we're removing PositionState from being stored in the db.

@klochowicz klochowicz force-pushed the fix/only-update-position-if-order-succeeded branch from abac1ea to 9529912 Compare May 3, 2023 19:22
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.
@klochowicz klochowicz force-pushed the fix/only-update-position-if-order-succeeded branch from 9529912 to 01f393c Compare May 3, 2023 19:29
@klochowicz klochowicz requested a review from luckysori May 3, 2023 19:40
mobile/native/src/trade/order/handler.rs Show resolved Hide resolved
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remembered another thing 😅

It was on purpose that we updated the position immediately after submitting the order in case we have a closing scenarios - this update is basically what sets the button state to Closing in the UI so the user does not click it again.

I think the best way would be updating the position to Closing immediately in case we are in a closing scenarios, but resetting it to open in case we fail to submit the order. I think resetting the state to open was missing, not necessarily moving the update to a later point.

Sorry, I did not think about this detail yesterday.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best would be to change the state in the UI to set it to Closing upon clock until we get an update from the backend. But that might be a bit tricky as well :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I perceive the risk from this being small: the action is swiping not just clicking and we get the success dialog in between.

(e.g. one can't fat-finger twice accidentally the same way as with buttons).

This is a shortcoming though, and I'll work to solve it!

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @luckysori that does_order_close_position is a better name than get_position_matching_order - the function was newly introduced. I think it'd be great to rename it.

@klochowicz
Copy link
Contributor Author

bors r+

@bors
Copy link
Contributor

bors bot commented May 3, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit a06b127 into main May 3, 2023
@bors bors bot deleted the fix/only-update-position-if-order-succeeded branch May 3, 2023 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix submit_order() logic
3 participants