Fix: Prevent negative plus balance from unfiltered Scrypt pending amounts#3330
Merged
TaprootFreak merged 1 commit intodevelopfrom Mar 3, 2026
Merged
Conversation
Problem: - fromScryptUnfiltered and toScryptUnfiltered could be negative - These values were added to totalPlusPending without clamping - Result: totalPlus became negative, causing negative plusBalance for assets - Specifically affected Scrypt/EUR (Asset 401) with -201,499 EUR plusBalance Root Cause: - Filtered versions (fromScrypt, toScrypt) were clamped to 0 if negative - Unfiltered versions were NOT clamped, causing negative plus balances - When useUnfilteredTx=true, negative unfiltered values corrupted totalPlus Fix: - Changed fromScryptUnfiltered and toScryptUnfiltered from const to let - Added clamping logic to set negative unfiltered values to 0 - Log errors when unfiltered values are negative for debugging - Prevents negative plus balances while maintaining error visibility Impact: - Fixes critical bug where Scrypt assets could have negative plus balances - Prevents plusBalanceChf from being stored as NULL in financial logs - Maintains consistency with existing filtered value clamping logic
TaprootFreak
added a commit
that referenced
this pull request
Mar 3, 2026
Previous Fix (PR #3330): - Clamped only fromScryptUnfiltered and toScryptUnfiltered individually - Left fromKrakenUnfiltered and toKrakenUnfiltered unclamped - Incomplete protection against negative plus balances This Fix: - Removed individual unfiltered component clamping - Added single clamp on totalPlusPending after calculation - Covers ALL negative sources: Kraken, Scrypt, and future exchanges - More robust and maintainable Rationale: - Simpler: One clamp instead of multiple - Complete: Catches negative values from any component - Better logging: Shows all component values when totalPlusPending is negative - Semantically correct: "Total pending plus balance cannot be negative" The filtered component clamps (fromKraken, toKraken, fromScrypt, toScrypt) remain in place for consistency and detailed error reporting.
TaprootFreak
added a commit
that referenced
this pull request
Mar 3, 2026
…3332) Previous Fix (PR #3330): - Clamped only fromScryptUnfiltered and toScryptUnfiltered individually - Left fromKrakenUnfiltered and toKrakenUnfiltered unclamped - Incomplete protection against negative plus balances This Fix: - Removed individual unfiltered component clamping - Added single clamp on totalPlusPending after calculation - Covers ALL negative sources: Kraken, Scrypt, and future exchanges - More robust and maintainable Rationale: - Simpler: One clamp instead of multiple - Complete: Catches negative values from any component - Better logging: Shows all component values when totalPlusPending is negative - Semantically correct: "Total pending plus balance cannot be negative" The filtered component clamps (fromKraken, toKraken, fromScrypt, toScrypt) remain in place for consistency and detailed error reporting.
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.
Problem
Critical bug causing negative plus balances in financial logs, particularly affecting Scrypt/EUR (Asset 401):
Impact
plusBalanceChfstored asNULLin logs (rejected bygetJsonValue()for negative values)plusBalanceChffield)Root Cause
Filtered versions (
fromScrypt,toScrypt) were clamped to 0 if negative (lines 775-793).Unfiltered versions were NOT clamped → negative values propagated to
totalPlus.Solution
fromScryptUnfilteredandtoScryptUnfilteredfromconsttoletTesting
totalPlusPendingfrom corrupting plus balancesVerification
Used diagnostic scripts (PR #3329) to identify the issue:
compare-balance-logs.sh: Found Scrypt asset changessum-asset-balances.sh: Showed EUR plusBalanceChf = -114k CHFinspect-asset-balance.sh: Revealed totalPlus = -201k, liquidity = 1, pending = nullRelated