Skip to content

fix: respect maxPnlCap==0 as ADL-disabled, bound insurance excess (C5+C6)#83

Open
0x-SquidSol wants to merge 1 commit intodcccrypto:mainfrom
0x-SquidSol:fix/c5-c6-adl-trigger-excess
Open

fix: respect maxPnlCap==0 as ADL-disabled, bound insurance excess (C5+C6)#83
0x-SquidSol wants to merge 1 commit intodcccrypto:mainfrom
0x-SquidSol:fix/c5-c6-adl-trigger-excess

Conversation

@0x-SquidSol
Copy link
Copy Markdown
Contributor

Summary

Two interacting ADL bugs fixed:

C5 — ADL triggers on disabled markets:

  • checkAdlTrigger() allowed ADL on markets where maxPnlCap==0 (protocol semantics: "ADL disabled") when insurance utilization threshold fired
  • The deprecated isAdlNeeded() correctly returned false for maxPnlCap==0
  • Fix: gate adlNeeded with maxPnlCap > 0n, matching isAdlNeeded semantics

C6 — Over-deleveraging on insurance-triggered ADL:

  • When triggered by insurance (not cap), excess was set to the full pnlPosTot — the entire positive PnL pool
  • This caused the deleverage loop to wipe out every profitable position (up to ADL_MAX_TX_PER_SCAN)
  • Fix: use pnlPosTot / 5n (20% per scan) for a conservative ramp-down that lets multiple scans gradually reduce exposure

Combined impact: On a maxPnlCap==0 market with high insurance utilization, C5 triggered ADL and C6 then attempted to deleverage the entire positive PnL pool — maximum damage to traders.

Changes

File Change
src/services/adl.ts:209 Gate adlNeeded with maxPnlCap > 0n
src/services/adl.ts:212-215 Replace pnlPosTot with pnlPosTot / 5n in insurance-triggered excess
tests/services/adl.test.ts New test: maxPnlCap==0 with 90% insurance utilization → no ADL

Test plan

  • New test: maxPnlCap==0 with high insurance utilization returns 0 (C5)
  • All 16 ADL tests pass
  • All 134 tests pass across full suite

🤖 Generated with Claude Code

…(C5+C6)

C5: checkAdlTrigger() allowed ADL on markets where maxPnlCap==0
(meaning ADL-disabled) when insurance utilization threshold fired.
Gate adlNeeded with maxPnlCap > 0n to match isAdlNeeded semantics.

C6: When ADL was triggered by insurance (not cap exceeded), the
excess was set to the full pnlPosTot, causing massive over-deleveraging
of every profitable position. Now uses pnlPosTot/5 (20% per scan)
for a conservative ramp-down that lets multiple scans gradually
reduce exposure.

Both bugs interacted: on a maxPnlCap==0 market with high insurance
utilization, C5 triggered ADL and C6 deleveraged the entire positive
PnL pool.

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

coderabbitai Bot commented Apr 9, 2026

Warning

Rate limit exceeded

@0x-SquidSol has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 11 minutes and 50 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 11 minutes and 50 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cb718e81-9a84-40d9-8428-b80e0e589a2d

📥 Commits

Reviewing files that changed from the base of the PR and between 63ad1a0 and f8ac8eb.

📒 Files selected for processing (2)
  • src/services/adl.ts
  • tests/services/adl.test.ts
✨ 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