[object Object]

← back to Ken

Ken: ROOT-CAUSE the paper over-credit — node-postgres returns bigint cost_cents as a STRING, so 'returned = cost_cents + pnl' CONCATENATED ('71'+21='7121') instead of adding, over-crediting every WIN ~100-1000x (this, not double-counting, was the balloon; survived the idempotency fix). Coerce cost to Number. Removed temp debug probe.

eb2e485931cd84b65088f36ee2a11e0ec6ac64cb · 2026-08-03 09:57:19 -0700 · steve@designerwallcoverings.com

Files touched

Diff

commit eb2e485931cd84b65088f36ee2a11e0ec6ac64cb
Author: steve@designerwallcoverings.com <steve@designerwallcoverings.com>
Date:   Mon Aug 3 09:57:19 2026 -0700

    Ken: ROOT-CAUSE the paper over-credit — node-postgres returns bigint cost_cents as a STRING, so 'returned = cost_cents + pnl' CONCATENATED ('71'+21='7121') instead of adding, over-crediting every WIN ~100-1000x (this, not double-counting, was the balloon; survived the idempotency fix). Coerce cost to Number. Removed temp debug probe.
---
 kalshi-dash/server.js | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/kalshi-dash/server.js b/kalshi-dash/server.js
index bb142ca..5a416d0 100644
--- a/kalshi-dash/server.js
+++ b/kalshi-dash/server.js
@@ -9015,14 +9015,16 @@ async function resolvePortfolioTrades() {
 
       // Update portfolio running totals
       const streakVal = status === 'won' ? 1 : status === 'lost' ? -1 : 0;
-      // total_returned = cost_cents + pnl for winning trades (you get your money back + profit)
-      const returned = pnl > 0 ? trade.cost_cents + pnl : (pnl === 0 ? trade.cost_cents : 0);
+      // total_returned = cost_cents + pnl for winning trades (you get your money back + profit).
+      // BUG FIX 2026-08-03: node-postgres returns bigint columns as STRINGS, so trade.cost_cents
+      // was "71" (string). `"71" + pnl` CONCATENATED ("71"+21 = "7121") instead of adding — a
+      // ~100-1000x over-credit on every WIN (pnl>0 is the only branch using `+`; `-`/`*` coerce
+      // to number so pnl itself was fine). Coerce cost to Number so `+` does arithmetic.
+      const costCents = Number(trade.cost_cents);
+      const returned = pnl > 0 ? costCents + pnl : (pnl === 0 ? costCents : 0);
       const wonInc = status === 'won' ? 1 : 0;
       const lostInc = status === 'lost' ? 1 : 0;
       const expInc = status === 'expired' ? 1 : 0;
-      // TEMP DEBUG 2026-08-03: hunt the residual ~100x over-credit. A single settle should
-      // return <= cost+pnl (<= ~$26). Anything bigger means returned/cost/pnl are wrong at runtime.
-      if (returned > 5000) console.error(`[Ken/DEBUG] BIG-CREDIT trade=${trade.id} returned=${returned} cost=${trade.cost_cents} pnl=${pnl} price=${currentPrice} qty=${trade.contracts} entry=${trade.entry_price_cents} status=${status}`);
       await kenQ(`
         UPDATE ken_portfolios SET
           balance_cents = balance_cents + $1,

← 2e71021 Ken: TEMP debug probe — log any settle crediting >$50 (retur  ·  back to Ken  ·  Ken (Cody gate b): independence gate on winner-consensus — e 2f7617c →