Skip to content

fix: remove artificial BigInt overflow guard in liquidation PnL (H7)#89

Open
0x-SquidSol wants to merge 1 commit intodcccrypto:mainfrom
0x-SquidSol:fix/h7-bigint-overflow-guard
Open

fix: remove artificial BigInt overflow guard in liquidation PnL (H7)#89
0x-SquidSol wants to merge 1 commit intodcccrypto:mainfrom
0x-SquidSol:fix/h7-bigint-overflow-guard

Conversation

@0x-SquidSol
Copy link
Copy Markdown
Contributor

@0x-SquidSol 0x-SquidSol commented Apr 9, 2026

Summary

  • The overflow check clamped PnL at Number.MAX_SAFE_INTEGER (9e15), which is meaningless for BigInt (arbitrary precision)
  • Large positions where diff * positionSize > 9e15 were clamped to a positive value
  • This could make deeply underwater accounts appear healthy, preventing valid liquidations
  • The re-verification path (line 435) never had this guard and worked correctly

Fix

Remove the MAX_SAFE_BIGINT overflow guard. BigInt arithmetic in JavaScript has arbitrary precision — no overflow is possible. The PnL calculation now matches the re-verification path.

Test plan

  • All 133 tests pass

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed large position liquidation PnL calculations by removing unnecessary overflow safeguards that were incorrectly clamping profit/loss values for substantial trading positions, ensuring accurate calculations.

…(H7)

The overflow check clamped PnL at Number.MAX_SAFE_INTEGER (9e15) which
is meaningless for BigInt (arbitrary precision, no overflow). Large
positions with diff*posSize > 9e15 were clamped to a positive value,
potentially making deeply underwater accounts appear healthy and
preventing valid liquidations.

The re-verification path (line 435) never had this guard and worked
correctly. Remove it from the scan path to match.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 9, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 512b59a9-a629-48c0-8804-a8fa024770c8

📥 Commits

Reviewing files that changed from the base of the PR and between 4fd5e86 and ddb178a.

📒 Files selected for processing (1)
  • src/services/liquidation.ts

📝 Walkthrough

Walkthrough

The liquidation service was simplified by removing BigInt overflow protection logic that compared position sizes against a MAX_SAFE_BIGINT threshold, replacing it with a straightforward PnL calculation that always computes markPnl as (diff * absPosSize) / price when prices are positive.

Changes

Cohort / File(s) Summary
PnL Calculation Simplification
src/services/liquidation.ts
Removed overflow "protection" conditional branches that clamped markPnl to ±MAX_SAFE_BIGINT. Simplified PnL computation to a single path: (diff * absPosSize) / price when entryPrice > 0n && price > 0n.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Away with overflow fears, the code now flows so clean,
One path instead of branching paths, the simplest ever seen,
BigInt calculations dance without the guards of old,
Liquidation logic lighter now—a tale efficiently told! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: removing an artificial BigInt overflow guard in liquidation PnL calculation, with reference to the issue (H7).
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant