Fix: Stop Double-Counting Balances When Creating Orders#119
Merged
Conversation
Add from_balance_manager field to DeepPlan and update get_deep_plan to calculate it. Fix balance_manager_input_coin amount management by taking into account whether the input coin is DEEP or SUI. If it is one of them, we decrease balance_manager_input_coin by corresponding amount (which is planned to be consumed) before using it in InputCoinDepositPlan calculation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Bug: What Was Wrong?
When a user placed an order where the input coin was SUI or DEEP, our code would attempt to use the same funds for both the order amount and its associated fees. This "double-counting" meant we didn't actually have enough money to cover both, causing the transaction to fail.
A Real-World Example
Here’s a simple scenario where this bug would occur:
EBalanceManagerBalanceTooLowerror.How We Fixed It
The fix is simple: we now subtract any fees from the user's balance before we calculate the amount available for the trade. This ensures we only use funds that are actually available.
How We Know It's Fixed
We added a new test,
sell_order_from_balance_manager, that perfectly recreates this failure. The test now passes. Before this fix, running the same test on the old code would cause it to predictably fail with anEBalanceManagerBalanceTooLowerror from Deepbook.