Skip to content

fix(trading): compute pre-trade liq fee on notional value, not raw position size#179

Open
6figpsolseeker wants to merge 1 commit intodcccrypto:mainfrom
6figpsolseeker:fix/pretrade-liq-fee-notional-base
Open

fix(trading): compute pre-trade liq fee on notional value, not raw position size#179
6figpsolseeker wants to merge 1 commit intodcccrypto:mainfrom
6figpsolseeker:fix/pretrade-liq-fee-notional-base

Conversation

@6figpsolseeker
Copy link
Copy Markdown

Summary

computePreTradeLiqPrice applied the trading fee (feeBps) to the raw position size instead of the notional value (position × oracle price). Trading fees on perp DEXes are assessed on notional, not lots.

Impact example — 100M-unit position at $100 with 30 bps fee:

  • Old (wrong): fee = 100_000_000 * 30 / 10000 = 300,000
  • New (correct): fee = (100_000_000 * 100_000_000 / 1e6) * 30 / 10000 = 30,000,000

The old formula underestimated fees by a factor proportional to the oracle price (in e6 scale / 10,000). This caused liquidation price estimates to be overly optimistic — users would be liquidated sooner than the SDK predicted because the fee deduction from effective capital was too small.

Changes

  • src/math/trading.ts:111 — compute notional = (absPos * oracleE6) / 1_000_000n first, then apply fee = (notional * feeBps) / 10000n. This is consistent with the existing computeTradingFee function which also expects notional as its base.

Test plan

  • npm run lint (tsc --noEmit) clean
  • npx tsx test/trading.test.ts — all 50 tests pass. Existing assertions check directional relationships (withFee > noFee for longs, withFee < noFee for shorts) rather than exact values, so they pass with the corrected fee magnitude.
  • Full vitest run: same 14 pre-existing failures as main, zero new failures
  • Recommended follow-up: add test cases with exact expected liq prices to prevent regression on the fee base

…sition size

computePreTradeLiqPrice applied the trading fee (feeBps) to the raw
position size instead of the notional value (position × oracle price).
Trading fees on perp DEXes are assessed on notional, not lots.

For a 100M-unit position at $100 with 30 bps fee:
  - Old (wrong): fee = 100_000_000 * 30 / 10000 = 300_000
  - New (correct): fee = (100_000_000 * 100_000_000 / 1e6) * 30 / 10000
                       = 30_000_000

The old formula underestimated fees by a factor proportional to the
oracle price (in e6 scale / 10000). This caused liquidation price
estimates to be overly optimistic — users would be liquidated sooner
than the SDK predicted because the fee deduction from effective capital
was too small.

The fix computes notional = absPos * oracleE6 / 1_000_000 first, then
applies feeBps / 10000 to the notional. This is consistent with the
existing computeTradingFee function which also expects notional as its
base.

All 50 existing trading math tests continue to pass because the test
assertions check directional relationships (withFee > noFee for longs,
withFee < noFee for shorts) rather than exact values.

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

@6figpsolseeker has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 1 minutes and 3 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 1 minutes and 3 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: f2f50d71-0acb-452c-8875-fdc72fcdc770

📥 Commits

Reviewing files that changed from the base of the PR and between 2f80d79 and c01be8d.

📒 Files selected for processing (1)
  • src/math/trading.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