Fix finance log double-counting of exchange balances#3462
Merged
TaprootFreak merged 2 commits intodevelopfrom Mar 19, 2026
Merged
Fix finance log double-counting of exchange balances#3462TaprootFreak merged 2 commits intodevelopfrom
TaprootFreak merged 2 commits intodevelopfrom
Conversation
…ithdrawals The finance log used Amount (total balance incl. locked funds) as the liquidity base and added pending exchange orders on top. For exchanges like Scrypt where Amount includes funds locked for pending withdrawals, this caused double-counting (~30k CHF overstatement). Add availableAmount to LiquidityBalance and use it in the finance log. The available balance excludes locked funds, so pending exchange orders correctly compensate for the difference.
davidleomay
approved these changes
Mar 19, 2026
TaprootFreak
added a commit
that referenced
this pull request
Mar 19, 2026
…geOrder PR #3462 switched to availableAmount globally, but this over-corrected for exchanges like XT where locked funds are trading orders not tracked as exchangeOrder. This caused ~40k CHF to silently drop from the balance. Revert to amount as liquidity base. Instead, deduct the locked portion (amount - availableAmount) from exchangeOrder to avoid the Scrypt double-counting that #3462 originally fixed.
5 tasks
TaprootFreak
added a commit
that referenced
this pull request
Mar 19, 2026
…geOrder (#3471) PR #3462 switched to availableAmount globally, but this over-corrected for exchanges like XT where locked funds are trading orders not tracked as exchangeOrder. This caused ~40k CHF to silently drop from the balance. Revert to amount as liquidity base. Instead, deduct the locked portion (amount - availableAmount) from exchangeOrder to avoid the Scrypt double-counting that #3462 originally fixed.
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.
Summary
Amount(total balance incl. locked funds) as the liquidity base and added pending exchange orders (exchangeOrder) on topAmountincludes funds locked for pending withdrawals, this caused double-counting (~30k CHF overstatement, daily swings of up to 18x between min/max)ExchangeAdapter.getForExchange()storedgetTotalBalances()(which includes locked funds) inLiquidityBalance.amount, thenlog-job.service.tsadded the same withdrawal amount again as pendingexchangeOrderFix
availableAmountfield toLiquidityBalanceentity (stores balance excluding locked funds)getAvailableBalances()toScryptService(usesAvailableAmount) andExchangeService(usesb.free)ExchangeAdapternow fetches and stores bothamount(total) andavailableAmountavailableAmountas liquidity base — pending exchange orders then correctly compensate for the locked/in-transit portionrule.verify()) continues to useamount(total) — no behavioral change for deficit/redundancy detectionTrade-off
During active trading orders (buy/sell, typically <1 min),
availableAmountis slightly lower since sell/buy aren't tracked ingetPendingTx(). This is acceptable because trading orders fill in seconds, thevalidflag threshold (5,000 CHF) catches brief outliers, and it self-corrects on the next cron run.Test plan
log-job.service.spec.tstests pass (9/9)totalBalanceChfshould stabilize (no more 3-18x daily swings)🤖 Generated with Claude Code