Fix: balance log race condition (negative totalBalance)#3345
Merged
TaprootFreak merged 2 commits intodevelopfrom Mar 4, 2026
Merged
Fix: balance log race condition (negative totalBalance)#3345TaprootFreak merged 2 commits intodevelopfrom
TaprootFreak merged 2 commits intodevelopfrom
Conversation
Exclude BuyCrypto entries from pending obligations when their PayoutOrder is already in PAYOUT_PENDING or COMPLETE status, preventing double-counting that caused negative totalBalance.
Rename to getRecentPayoutSentCorrelationIds and add MoreThan(1h) filter. The race condition window is ~20min max, so 1h is sufficient.
davidleomay
approved these changes
Mar 4, 2026
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
Filter payout-sent BuyCrypto from pending obligations in the balance log to fix race condition causing negative
totalBalance.Problem
Separate cron jobs update wallet balance, BuyCrypto completion, and payout status at different intervals. When a payout is sent on-chain, the wallet balance drops immediately, but
BuyCrypto.isCompleteremainsfalseuntil the next completion cycle — causing double-counting.Observed today (2026-03-04):
98220for BuyCrypto115668(1.25 BTC, ~69k CHF) created at 09:44BuyCrypto.isCompletestayedfalseuntil 10:06 (~22min gap)plusChf: 742k,minusChf: 790k→ -48k CHF negativeFix
When a PayoutOrder is in
PAYOUT_PENDINGorCOMPLETEstatus, the crypto has already left the wallet. The corresponding BuyCrypto is excluded frompendingBuyCryptosince the obligation is already reflected in the reduced wallet balance.PayoutService.getRecentPayoutSentCorrelationIds()— returns correlation IDs of payout-sent orders (scoped to last hour for performance; race condition window is ~20min max)LogJobService.getAssetLog()— filterspendingBuyCryptobefore calculatingminusBalanceWith fix applied:
minusChfdrops from 790k to ~721k →totalChf: +21k CHF(positive)Edge cases
{id}-network-start-fee) don't match BuyCrypto IDs → correctly not filteredPAYOUT_DESIGNATED: tx not yet sent, wallet balance unaffected → correctly not filteredPAYOUT_UNCERTAIN: conservative to keep in pending → correctly not filteredTest plan
npm run buildpassesnpm run lintpassestotalBalanceentries after deploy