diff --git a/src/subdomains/supporting/log/log-job.service.ts b/src/subdomains/supporting/log/log-job.service.ts index 0ff2287301..ec2a043176 100644 --- a/src/subdomains/supporting/log/log-job.service.ts +++ b/src/subdomains/supporting/log/log-job.service.ts @@ -793,7 +793,7 @@ export class LogJobService { } // total pending balance - const totalPlusPending = + let totalPlusPending = cryptoInput + exchangeOrder + bridgeOrder + @@ -803,6 +803,19 @@ export class LogJobService { (useUnfilteredTx ? fromScryptUnfiltered : fromScrypt) + (useUnfilteredTx ? toScryptUnfiltered : toScrypt); + // Clamp totalPlusPending to prevent negative plus balances + // This catches any negative values from unfiltered Kraken/Scrypt or other components + if (totalPlusPending < 0) { + errors.push(`totalPlusPending < 0`); + this.logger.verbose( + `Error in financial log, totalPlusPending < 0 for asset: ${curr.id}, totalPlusPending: ${totalPlusPending}. ` + + `Components: cryptoInput=${cryptoInput}, exchangeOrder=${exchangeOrder}, bridgeOrder=${bridgeOrder}, ` + + `olky=${pendingOlkyYapealAmount}, kraken=${useUnfilteredTx ? fromKrakenUnfiltered : fromKraken}+${useUnfilteredTx ? toKrakenUnfiltered : toKraken}, ` + + `scrypt=${useUnfilteredTx ? fromScryptUnfiltered : fromScrypt}+${useUnfilteredTx ? toScryptUnfiltered : toScrypt}`, + ); + totalPlusPending = 0; + } + const totalPlus = liquidity + totalPlusPending + (totalCustomBalance ?? 0); // minus