fix(indexer): break-even best_trade_pnl after legacy backfill (#344) #850

Merged
PlasticDigits merged 1 commit from local1/344-best-break-even-pnl into main 2026-06-09 07:57:27 +00:00
PlasticDigits commented 2026-06-09 07:52:34 +00:00 (Migrated from gitlab.com)

Summary

  • Repair rows where 20260609120000 backfill set best_trade_pnl = worst_trade_pnl when total_realized_pnl = worst_trade_pnl for multi-trade traders (loss + break-even pattern)
  • Restore best_trade_pnl = 0 for that aggregate shape
  • Document invariant in docs/indexer-invariants.md

Context

Bugbot on !848: legacy-repair treated total = worst as a single-loss trader, overwriting a valid break-even best (0) with the worst loss.

Test plan

  • cd indexer && cargo test --lib
  • Apply migration on DB with a seeded loss+break-even row (best=-10, worst=-10, total=-10, total_trades=2) → best becomes 0
  • Trader profile / leaderboard best-trade column shows 0 (not worst loss)

Note

Low Risk
Targeted one-time DB correction plus docs/comments; no new API surface or ingestion path changes.

Overview
Fixes #344 fallout from migration 20260609120000: for multi-trade wallets where total_realized_pnl = worst_trade_pnl, the earlier backfill wrongly set best_trade_pnl = worst_trade_pnl. That aggregate shape means one losing close and the rest break-even, so best should stay 0, not the worst loss.

Adds migration 20260609140000_fix_trader_best_break_even_pnl.sql to reset best_trade_pnl = 0 on rows matching that pattern (total_trades > 1, best = worst, negative worst, total = worst). Documents the invariant in docs/indexer-invariants.md (including both migrations).

Adds comments in update_trader_pnl in positions.rs on the existing CASE branch for legacy best_trade_pnl = 0 when total = worst, clarifying that GREATEST($2, worst_trade_pnl) preserves break-even over copying worst into best—no SQL logic change beyond commentary.

Reviewed by Cursor Bugbot for commit 0860b96a3b. Bugbot is set up for automated code reviews on this repo. Configure here.

## Summary - Repair rows where `20260609120000` backfill set `best_trade_pnl = worst_trade_pnl` when `total_realized_pnl = worst_trade_pnl` for multi-trade traders (loss + break-even pattern) - Restore `best_trade_pnl = 0` for that aggregate shape - Document invariant in `docs/indexer-invariants.md` ## Context Bugbot on !848: legacy-repair treated `total = worst` as a single-loss trader, overwriting a valid break-even best (`0`) with the worst loss. ## Test plan - [ ] `cd indexer && cargo test --lib` - [ ] Apply migration on DB with a seeded loss+break-even row (`best=-10`, `worst=-10`, `total=-10`, `total_trades=2`) → `best` becomes `0` - [ ] Trader profile / leaderboard best-trade column shows `0` (not worst loss) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Targeted one-time DB correction plus docs/comments; no new API surface or ingestion path changes. > > **Overview** > Fixes **#344** fallout from migration `20260609120000`: for multi-trade wallets where **`total_realized_pnl = worst_trade_pnl`**, the earlier backfill wrongly set **`best_trade_pnl = worst_trade_pnl`**. That aggregate shape means one losing close and the rest break-even, so **best should stay `0`**, not the worst loss. > > Adds migration **`20260609140000_fix_trader_best_break_even_pnl.sql`** to reset **`best_trade_pnl = 0`** on rows matching that pattern (`total_trades > 1`, `best = worst`, negative worst, `total = worst`). Documents the invariant in **`docs/indexer-invariants.md`** (including both migrations). > > Adds comments in **`update_trader_pnl`** in **`positions.rs`** on the existing **`CASE`** branch for legacy **`best_trade_pnl = 0`** when **`total = worst`**, clarifying that **`GREATEST($2, worst_trade_pnl)`** preserves break-even over copying worst into best—no SQL logic change beyond commentary. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 0860b96a3bb3139f6afd07f8ce670e19d48cb5bc. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
ghost1 commented 2026-06-09 07:52:41 +00:00 (Migrated from gitlab.com)

changed the description

changed the description
ghost1 commented 2026-06-09 07:52:48 +00:00 (Migrated from gitlab.com)

changed the description

changed the description
PlasticDigits commented 2026-06-09 07:57:27 +00:00 (Migrated from gitlab.com)

mentioned in commit 83dc19286d

mentioned in commit 83dc19286d71d50c684d109abc333ee5dde52477
PlasticDigits (Migrated from gitlab.com) merged commit 83dc19286d into main 2026-06-09 07:57:27 +00:00
PlasticDigits commented 2026-06-09 08:34:22 +00:00 (Migrated from gitlab.com)

mentioned in issue #344

mentioned in issue #344
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
code/cl8y-dex-terraclassic!850
No description provided.