@reyaxyz/common 0.344.0 → 0.344.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,5 +6,5 @@
6
6
 
7
7
  | Statements | Branches | Functions | Lines |
8
8
  | --------------------------- | ----------------------- | ------------------------- | ----------------- |
9
- | ![Statements](https://img.shields.io/badge/statements-28.69%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-25.14%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-17.24%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-29.88%25-red.svg?style=flat) |
9
+ | ![Statements](https://img.shields.io/badge/statements-32.57%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-30.62%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-18.37%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-33.86%25-red.svg?style=flat) |
10
10
 
@@ -99,18 +99,30 @@ export declare const calculateRequiredMargin: ({ currIMR, currentNotional, lever
99
99
  leverage: number;
100
100
  tradeNotional: number;
101
101
  }) => number;
102
- export declare const calculateMaxOrderSize: ({ currIMR, currNodeBalance, currNotional, depthFactor, extendingPosition, feeRate, isShortTrade, leverage, maxExposure, oraclePrice, poolPrice, }: {
102
+ /**
103
+ * Calculates the maximum notional trade size (unsigned, always ≥ 0).
104
+ *
105
+ * Implements the UI quant spec v2 ("Maximum trade") with two cases:
106
+ * Case 1 — Extending: trade same direction as existing position.
107
+ * Case 2 — Reverting: decompose into (a) close position exactly, then
108
+ * (b) extend from zero in the opposite direction.
109
+ *
110
+ * All quadratics use the unsigned convention (t = |tradeNotional|) so the
111
+ * positive root always corresponds to the correct direction.
112
+ */
113
+ export declare const calculateMaxOrderSize: ({ currIMR, currNodeBalance, currNotional, depthFactor, feeRate, isShortTrade, leverage, maxExposure, oraclePrice, poolPrice, currentLogF, netExposure, }: {
103
114
  currIMR: number;
104
115
  currNodeBalance: number;
105
116
  currNotional: number;
106
117
  depthFactor: number;
107
- extendingPosition: boolean;
108
118
  feeRate: number;
109
119
  isShortTrade: boolean;
110
120
  leverage: number;
111
121
  maxExposure: number;
112
122
  oraclePrice: number;
113
123
  poolPrice: number;
124
+ currentLogF: number;
125
+ netExposure: number;
114
126
  }) => number;
115
127
  export declare const calculateIMR: ({ marketLeverages, marketPrices, positions, }: {
116
128
  marketLeverages: Map<MarketId, number>;
@@ -1 +1 @@
1
- {"version":3,"file":"calculate.d.ts","sourceRoot":"/","sources":["utils/calculate.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,cAAc,CAAC;AAErC,eAAO,MAAM,QAAQ,MAAM,CAAC;AAE5B,KAAK,QAAQ,GAAG,MAAM,CAAC;AACvB,KAAK,OAAO,GAAG,MAAM,CAAC;AAEtB;;;GAGG;AACH,eAAO,MAAM,oBAAoB;IAO/B,sEAAsE;gBAC1D,MAAM;IAClB,iGAAiG;mBAClF,MAAM;IACrB;;;;OAIG;iBACU,MAAM;IACnB;;;OAGG;aACM,MAAM;IACf,uCAAuC;WAChC,MAAM;MACX,MAiBH,CAAC;AAEF,eAAO,MAAM,oBAAoB;iBAKlB,MAAM;;gBAEP,MAAM;YACiC,CAAC;AAEtD;;;GAGG;AACH,eAAO,MAAM,kBAAkB,kBACd,MAAM,mBACJ,MAAM;iBACP,MAAM;iBAAe,MAAM;CA+B5C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,cAAc;iBAOZ,MAAM;mBACJ,MAAM;qBACJ,MAAM;iBACV,MAAM;iBACN,MAAM;MACjB,MAuBH,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,gCAAgC;iBAS9B,MAAM;iBACN,MAAM;mBACJ,MAAM;qBACJ,MAAM;iBACV,MAAM;iBACN,MAAM;iBACN,MAAM;MACjB,MAoBH,CAAC;AAEF,eAAO,MAAM,iBAAiB;aAInB,MAAM;mBACA,MAAM;YACgB,CAAC;AAExC,eA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;0BACJ,MAAM;kCACE,MAAM;0BACd,MAAM;MAC1B,MAOH,CAAC;AAEF,eAAO,MAAM,iBAAiB;WAKrB,MAAM;2BACU,MAAM;0BACP,MAAM;MAC1B,MAEH,CAAC;AAEF,eAAO,MAAM,oBAAoB,gBAClB,SAAS,gBACR,SAAS,cAGxB,CAAC;AAEF,eAAO,MAAM,4BAA4B,YAC9B,SAAS,wBACI,SAAS,0BACP,SAAS,KAChC,SAEF,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,qBAAqB,eACpB,MAAM,EAAE,UACZ,MAAM,EAAE,YACN,MAAM,KACf,MAiCF,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,2BAA2B;eAO3B,MAAM;gBACL,MAAM,EAAE;YACZ,MAAM,EAAE;yBACK,MAAM;yBACN,MAAM;MACzB,MASH,CAAC;AAEF,eAAO,MAAM,uBAAuB;iCAQL,SAAS;iCACT,SAAS;kBACxB,SAAS;2BACA,SAAS;mCACD,SAAS;mCACT,SAAS;MACtC,SAqBH,CAAC"}
@@ -159,23 +159,126 @@ var calculateRequiredMargin = function (_a) {
159
159
  leverage;
160
160
  };
161
161
  exports.calculateRequiredMargin = calculateRequiredMargin;
162
+ /**
163
+ * Calculates the maximum notional trade size (unsigned, always ≥ 0).
164
+ *
165
+ * Implements the UI quant spec v2 ("Maximum trade") with two cases:
166
+ * Case 1 — Extending: trade same direction as existing position.
167
+ * Case 2 — Reverting: decompose into (a) close position exactly, then
168
+ * (b) extend from zero in the opposite direction.
169
+ *
170
+ * All quadratics use the unsigned convention (t = |tradeNotional|) so the
171
+ * positive root always corresponds to the correct direction.
172
+ */
162
173
  var calculateMaxOrderSize = function (_a) {
163
- var currIMR = _a.currIMR, currNodeBalance = _a.currNodeBalance, currNotional = _a.currNotional, depthFactor = _a.depthFactor, extendingPosition = _a.extendingPosition, feeRate = _a.feeRate, isShortTrade = _a.isShortTrade, leverage = _a.leverage, maxExposure = _a.maxExposure, oraclePrice = _a.oraclePrice, poolPrice = _a.poolPrice;
164
- var a = poolPrice / oraclePrice;
165
- var c = 1 / (depthFactor * maxExposure);
166
- var R = a - 1 - feeRate * (isShortTrade ? -1 : 1);
167
- var SN = currNotional < 0 ? -1 : 1;
168
- var Q = R - (SN / leverage) * (extendingPosition ? 1 : -1);
169
- return Math.abs((-1 * Q +
170
- Math.sqrt(Math.pow(Q, 2) -
171
- 4 *
172
- a *
173
- c *
174
- (currNodeBalance -
175
- currIMR +
176
- (extendingPosition ? 0 : ((2 * SN) / leverage) * currNotional))) *
177
- (isShortTrade ? 1 : -1)) /
178
- (2 * a * c));
174
+ var currIMR = _a.currIMR, currNodeBalance = _a.currNodeBalance, currNotional = _a.currNotional, depthFactor = _a.depthFactor, feeRate = _a.feeRate, isShortTrade = _a.isShortTrade, leverage = _a.leverage, maxExposure = _a.maxExposure, oraclePrice = _a.oraclePrice, poolPrice = _a.poolPrice, currentLogF = _a.currentLogF, netExposure = _a.netExposure;
175
+ var D = depthFactor * maxExposure;
176
+ var rho = poolPrice / oraclePrice;
177
+ var tradeSign = isShortTrade ? -1 : 1;
178
+ var s0 = currNotional > 0 ? 1 : currNotional < 0 ? -1 : tradeSign;
179
+ var absN0 = Math.abs(currNotional);
180
+ /**
181
+ * Solve the extending quadratic (spec §Case 1) for max trade size.
182
+ * a·t² + b·t + c = 0 where a = ρ·γ, b = s₀·(1/lev + fee) + (ρ−1), c = −surplus.
183
+ * Root selection: pick the root whose sign matches s₀ (dir).
184
+ * For rebalancing (a < 0), caps at |NE| (beyond which γ switches to unbalancing).
185
+ */
186
+ var solveExtending = function (dir, surplus, // aka, IMRDelta
187
+ rho_, gamma_, isRebal, absNE) {
188
+ if (surplus <= 0)
189
+ return 0;
190
+ var a = rho_ * gamma_;
191
+ var b = dir * (1 / leverage + feeRate) + (rho_ - 1);
192
+ var c = -surplus;
193
+ // Degenerate: γ = 0 → linear equation b·t + c = 0 (rebalancing with f = 0)
194
+ if (a === 0) {
195
+ if (b === 0)
196
+ return isRebal ? absNE : 0;
197
+ var t_1 = -c / b;
198
+ if (t_1 * dir <= 0)
199
+ return isRebal ? absNE : 0;
200
+ return isRebal ? Math.min(Math.abs(t_1), absNE) : Math.abs(t_1);
201
+ }
202
+ var disc = b * b - 4 * a * c;
203
+ if (isRebal) {
204
+ if (disc < 0)
205
+ return absNE;
206
+ var t_2 = (-b + dir * Math.sqrt(disc)) / (2 * a);
207
+ return t_2 * dir <= 0 ? absNE : Math.min(Math.abs(t_2), absNE);
208
+ }
209
+ if (disc < 0)
210
+ throw new Error("calculateMaxOrderSize: negative discriminant (".concat(disc, ")"));
211
+ var t = (-b + dir * Math.sqrt(disc)) / (2 * a);
212
+ if (t * dir <= 0)
213
+ throw new Error("calculateMaxOrderSize: no valid root (".concat(t, ")"));
214
+ return Math.abs(t);
215
+ };
216
+ // Determine γ for a trade of given direction against the current pool state
217
+ var getGamma = function (dir, logF, ne) {
218
+ var isRebal = ne !== 0 && dir === Math.sign(ne);
219
+ var gamma = isRebal ? -logF / ne : 1 / D;
220
+ return { gamma: gamma, isRebal: isRebal };
221
+ };
222
+ // ── Case 1: Extending ──────────────────────────────────────────────────
223
+ if (tradeSign === s0) {
224
+ var _b = getGamma(tradeSign, currentLogF, netExposure), gamma = _b.gamma, isRebal = _b.isRebal;
225
+ return solveExtending(s0, currNodeBalance - currIMR, rho, gamma, isRebal, Math.abs(netExposure));
226
+ }
227
+ // ── Case 2: Reverting — close N₀, then re-extend ──────────────────────
228
+ // Step 1: Close N₀ entirely (spec §Case 2, Step 1)
229
+ var closeTrade = -currNotional;
230
+ var closeDir = -s0;
231
+ var _c = getGamma(closeDir, currentLogF, netExposure), gammaC = _c.gamma, closeIsRebal = _c.isRebal;
232
+ var closeFlipsPool = closeIsRebal && absN0 > Math.abs(netExposure);
233
+ // Exec price ratio: ρ·exp(γ_c·closeTrade) for non-flip, dedicated formula for flip
234
+ var closeExecRatio = closeFlipsPool
235
+ ? Math.exp((closeTrade - netExposure) / D)
236
+ : rho * Math.exp(gammaC * closeTrade);
237
+ var balClose = currNodeBalance - feeRate * absN0 + closeTrade * (1 - closeExecRatio);
238
+ var imrClose = currIMR - absN0 / leverage;
239
+ var surplusClose = balClose - imrClose;
240
+ // Insolvency: can't fully close without going under margin.
241
+ // Solve partial-close quadratic (spec appendix): b has flipped feeRate sign.
242
+ // Falls back to full close (|N₀|) when no valid partial-close root exists.
243
+ if (surplusClose <= 0) {
244
+ var IMRDelta = currNodeBalance - currIMR;
245
+ var gammaP = getGamma(closeDir, currentLogF, netExposure).gamma;
246
+ var a = rho * gammaP;
247
+ var b = s0 * (1 / leverage - feeRate) + (rho - 1);
248
+ var c = -IMRDelta;
249
+ if (a === 0) {
250
+ if (b === 0)
251
+ return absN0;
252
+ var t_3 = -c / b;
253
+ if (t_3 * closeDir <= 0)
254
+ return absN0;
255
+ return Math.min(Math.abs(t_3), absN0);
256
+ }
257
+ var disc = b * b - 4 * a * c;
258
+ if (disc < 0)
259
+ return absN0;
260
+ var t = (-b + closeDir * Math.sqrt(disc)) / (2 * a);
261
+ if (t * closeDir <= 0)
262
+ return absN0;
263
+ return Math.min(Math.abs(t), absN0);
264
+ }
265
+ // Step 2: Extend from zero in -s₀ direction with post-close pool state
266
+ var neAfter = netExposure + currNotional;
267
+ var fPrime;
268
+ if (closeFlipsPool) {
269
+ fPrime = (closeTrade - netExposure) / D;
270
+ }
271
+ else if (closeIsRebal) {
272
+ fPrime = currentLogF * (1 + currNotional / netExposure);
273
+ }
274
+ else {
275
+ fPrime = currentLogF + closeTrade / D;
276
+ }
277
+ var rhoPrime = Math.exp(fPrime);
278
+ var extDir = -s0;
279
+ var _d = getGamma(extDir, fPrime, neAfter), gammaPrime = _d.gamma, isExtRebal = _d.isRebal;
280
+ return (absN0 +
281
+ solveExtending(extDir, surplusClose, rhoPrime, gammaPrime, isExtRebal, Math.abs(neAfter)));
179
282
  };
180
283
  exports.calculateMaxOrderSize = calculateMaxOrderSize;
181
284
  var calculateIMR = function (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":["import { mergeMap } from './struct';\nimport BigNumber from 'bignumber.js';\n\nexport const POOL_IMR = 9.1;\n\ntype MarketId = string;\ntype Address = string;\n\n/**\n * Calculates the maximum amount that can be withdrawn in terms of the token\n * (e.g., if rUSD then in rUSD terms, if WETH then in WETH terms)\n */\nexport const calculateMaxWithdraw = ({\n currentIMR,\n marginBalance,\n realBalance,\n haircut,\n price,\n}: {\n /** Current Initial Margin Requirement of the account in rUSD terms */\n currentIMR: number;\n /** Current margin balance of the account (expected to already include haircuts) in rUSD terms */\n marginBalance: number;\n /**\n * Current real balance of the token the user wants to withdraw in token terms.\n * For rUSD: this equals net deposits (balanceDeprecated from v2 endpoints + realized PnL).\n * For all other tokens: this equals balanceDeprecated (since realized PnL is 0 for them).\n */\n realBalance: number;\n /**\n * Haircut parameter of the token.\n * Note: haircut for rUSD is always expected to be 0.\n */\n haircut: number;\n /** Price of the token in rUSD terms */\n price: number;\n}): number => {\n if (currentIMR < 0 || price < 0 || haircut < 0) {\n throw new Error('currentIMR, price, and haircut cannot be negative');\n }\n\n if (currentIMR === 0) {\n return realBalance;\n }\n\n if (haircut === 1 || price === 0) {\n return 0;\n }\n\n const availableMargin = marginBalance - currentIMR;\n const maxFromMargin = availableMargin / (price * (1 - haircut));\n\n return Math.max(0, Math.min(realBalance, maxFromMargin));\n};\n\nexport const calculateMaxExposure = ({\n poolBalance,\n riskFactor,\n poolIMR = POOL_IMR,\n}: {\n poolBalance: number;\n poolIMR?: number;\n riskFactor: number;\n}) => poolBalance / (poolIMR * Math.sqrt(riskFactor));\n\n/**\n * Splits a trade exposure into rebalancing and unbalancing portions relative to pool net exposure.\n * Rebalancing reduces pool's absolute exposure; unbalancing increases it.\n */\nexport const splitTradeExposure = (\n tradeExposure: number,\n poolNetExposure: number,\n): { rebalancing: number; unbalancing: number } => {\n if (tradeExposure === 0) {\n return { rebalancing: 0, unbalancing: 0 };\n }\n\n // If pool has no exposure, entire trade is unbalancing\n if (poolNetExposure === 0) {\n return { rebalancing: 0, unbalancing: tradeExposure };\n }\n\n // Opposite sign means trade increases pool exposure → pure unbalancing\n const sameSign =\n (poolNetExposure > 0 && tradeExposure > 0) ||\n (poolNetExposure < 0 && tradeExposure < 0);\n if (!sameSign) {\n return { rebalancing: 0, unbalancing: tradeExposure };\n }\n\n // Same sign: trade reduces pool exposure (pool takes opposite side)\n const absPool = Math.abs(poolNetExposure);\n const absTrade = Math.abs(tradeExposure);\n\n if (absTrade <= absPool) {\n // Pure rebalancing — trade doesn't cross zero\n return { rebalancing: tradeExposure, unbalancing: 0 };\n }\n\n // Mixed: rebalance up to pool exposure, then unbalance the rest\n const rebalancing = poolNetExposure;\n const unbalancing = tradeExposure - rebalancing;\n return { rebalancing, unbalancing };\n};\n\n/**\n * Computes the new log price multiplier (logF) after a trade, handling both\n * rebalancing and unbalancing portions.\n *\n * Mirrors on-chain logic in PriceMultiplier.sol:computeNewLogPriceMultiplier.\n */\nexport const computeNewLogF = ({\n currentLogF,\n tradeExposure,\n poolNetExposure,\n depthFactor,\n maxExposure,\n}: {\n currentLogF: number;\n tradeExposure: number;\n poolNetExposure: number;\n depthFactor: number;\n maxExposure: number;\n}): number => {\n if (tradeExposure === 0) return currentLogF;\n\n const { rebalancing, unbalancing } = splitTradeExposure(\n tradeExposure,\n poolNetExposure,\n );\n\n // Case 1: Has unbalancing portion (includes mixed trades)\n if (unbalancing !== 0) {\n // If also rebalancing, it brings logF to 0 by definition (trade crosses zero)\n const startLogF = rebalancing === 0 ? currentLogF : 0;\n return startLogF + unbalancing / (depthFactor * maxExposure);\n }\n\n // Case 2: Only rebalancing\n // Both rebalancing and poolNetExposure have the same sign, so ratio is in [0, 1]\n if (rebalancing !== 0 && poolNetExposure !== 0) {\n const ratio = rebalancing / poolNetExposure;\n return currentLogF * (1 - ratio);\n }\n\n return currentLogF;\n};\n\n/**\n * Estimates the execution price for a trade using the exponential logPriceMultiplier formula.\n *\n * Mirrors on-chain logic:\n * 1. computeNewLogPriceMultiplier (PriceMultiplier.sol)\n * 2. computeAmmPrice: ammPrice = oraclePrice * exp(logF)\n * 3. computeSignedSpread: executionPrice = ammPrice * (1 + signedSpread)\n *\n * @audit spreadDiscount not applied — known limitation, to be addressed later\n * @audit priceSpacing rounding not applied\n */\nexport const calculateEstimatedExecutionPrice = ({\n oraclePrice,\n currentLogF,\n tradeExposure,\n poolNetExposure,\n depthFactor,\n maxExposure,\n priceSpread,\n}: {\n oraclePrice: number;\n currentLogF: number;\n tradeExposure: number;\n poolNetExposure: number;\n depthFactor: number;\n maxExposure: number;\n priceSpread: number;\n}): number => {\n if (tradeExposure === 0) {\n return oraclePrice * Math.exp(currentLogF);\n }\n\n // 1. Compute new logF after trade impact\n const newLogF = computeNewLogF({\n currentLogF,\n tradeExposure,\n poolNetExposure,\n depthFactor,\n maxExposure,\n });\n\n // 2. Compute AMM price: oraclePrice * exp(newLogF)\n const ammPrice = oraclePrice * Math.exp(newLogF);\n\n // 3. Apply signed spread\n const signedSpread = tradeExposure > 0 ? priceSpread : -priceSpread;\n return Math.max(0, ammPrice * (1 + signedSpread));\n};\n\nexport const calculateTradeFee = ({\n feeRate,\n tradeNotional,\n}: {\n feeRate: number;\n tradeNotional: number;\n}) => feeRate * Math.abs(tradeNotional);\n\nexport const calculateSpread = ({\n depthFactor,\n maxExposure,\n oraclePrice,\n poolPrice,\n tradeNotional,\n}: {\n depthFactor: number;\n maxExposure: number;\n oraclePrice: number;\n poolPrice: number;\n tradeNotional: number;\n}) =>\n (poolPrice / oraclePrice) *\n (1 + tradeNotional / (depthFactor * maxExposure) - 1) *\n tradeNotional;\n\nexport const calculateEstimatedLiquidationPrice = ({\n LMR,\n accountBalance,\n isShort,\n notional,\n oraclePrice,\n}: {\n LMR: number;\n accountBalance: number;\n isShort: boolean;\n notional: number;\n oraclePrice: number;\n}) =>\n Math.max(\n 0,\n oraclePrice *\n (1 - ((accountBalance - LMR) / notional) * (isShort ? -1 : 1)),\n );\n\nexport const calculateLeverage = ({\n imrMultiplier,\n riskFactor,\n}: {\n imrMultiplier: number;\n riskFactor: number;\n}) => 1 / (imrMultiplier * Math.sqrt(riskFactor));\n\nexport const calculateRequiredMargin = ({\n currIMR,\n currentNotional,\n leverage,\n tradeNotional,\n}: {\n currIMR: number;\n currentNotional: number;\n leverage: number;\n tradeNotional: number;\n}) =>\n currIMR +\n (Math.abs(currentNotional + tradeNotional) - Math.abs(currentNotional)) /\n leverage;\n\nexport const calculateMaxOrderSize = ({\n currIMR,\n currNodeBalance,\n currNotional,\n depthFactor,\n extendingPosition,\n feeRate,\n isShortTrade,\n leverage,\n maxExposure,\n oraclePrice,\n poolPrice,\n}: {\n currIMR: number;\n currNodeBalance: number;\n currNotional: number;\n depthFactor: number;\n extendingPosition: boolean;\n feeRate: number;\n isShortTrade: boolean;\n leverage: number;\n maxExposure: number;\n oraclePrice: number;\n poolPrice: number;\n}) => {\n const a = poolPrice / oraclePrice;\n const c = 1 / (depthFactor * maxExposure);\n const R = a - 1 - feeRate * (isShortTrade ? -1 : 1);\n const SN = currNotional < 0 ? -1 : 1;\n const Q = R - (SN / leverage) * (extendingPosition ? 1 : -1);\n return Math.abs(\n (-1 * Q +\n Math.sqrt(\n Math.pow(Q, 2) -\n 4 *\n a *\n c *\n (currNodeBalance -\n currIMR +\n (extendingPosition ? 0 : ((2 * SN) / leverage) * currNotional)),\n ) *\n (isShortTrade ? 1 : -1)) /\n (2 * a * c),\n );\n};\n\nexport const calculateIMR = ({\n marketLeverages,\n marketPrices,\n positions,\n}: {\n marketLeverages: Map<MarketId, number>;\n marketPrices: Map<MarketId, number>;\n positions: Map<MarketId, number>;\n}) => {\n return Array.from(positions.entries()).reduce(\n (sum, [marketId, positionSize]) => {\n const leverage = marketLeverages.get(marketId);\n return leverage\n ? sum +\n (Math.abs(positionSize) * (marketPrices.get(marketId) ?? 0)) /\n leverage\n : sum;\n },\n 0,\n );\n};\n\nexport const calculateLMR = ({\n marketPrices,\n positions,\n riskFactors,\n marketId,\n tradeSize,\n}: {\n marketId?: string;\n marketPrices: Map<MarketId, number>;\n positions: Map<MarketId, number>;\n riskFactors: Map<MarketId, number>;\n tradeSize?: number;\n}) =>\n Array.from(\n (marketId && tradeSize\n ? mergeMap(positions, [\n [marketId, (positions.get(marketId) ?? 0) + tradeSize],\n ])\n : positions\n ).entries(),\n ).reduce((sum, [marketId, positionSize]) => {\n const riskFactor = riskFactors.get(marketId);\n return riskFactor\n ? sum +\n Math.abs(positionSize) *\n (marketPrices.get(marketId) ?? 0) *\n Math.sqrt(riskFactor)\n : sum;\n }, 0);\n\n// if account balance is below LMR position is liquidable\n\nexport const calculateMarginRatio = ({\n LMR,\n totalBalance,\n tradeCost,\n}: {\n LMR: number;\n totalBalance: number;\n tradeCost: number;\n}) => {\n if (LMR === 0) {\n return 0;\n }\n if (tradeCost > totalBalance) {\n return 1;\n }\n if (totalBalance === tradeCost) {\n return Infinity;\n }\n return Math.min(1, LMR / (totalBalance - tradeCost));\n};\n\nexport const calculateTotalMargin = ({\n marketsTrackers,\n accountBalances,\n assetPrices,\n markPrices,\n marketsTokenInfo,\n positions,\n rUSDAddress,\n}: {\n accountBalances: Map<Address, number>;\n assetPrices: Map<Address, number>;\n markPrices: Map<Address, number>;\n marketsTokenInfo: Map<\n MarketId,\n {\n quoteTokenId: string;\n underlyingAssetId: string;\n }\n >;\n marketsTrackers: Map<\n MarketId,\n {\n longADLUnwindPrice: number;\n longBaseMultiplier: number;\n longFundingValue: number;\n shortADLUnwindPrice: number;\n shortBaseMultiplier: number;\n shortFundingValue: number;\n }\n >;\n positions: {\n adlUnwindPrice: number;\n base: number;\n baseMultiplier: number;\n fundingValue: number;\n lastPrice: number;\n marketId: string;\n realizedPnl: number;\n }[];\n rUSDAddress?: string;\n}) => {\n const pnl = new Map<Address, number>();\n const rUSDPrice = (rUSDAddress && assetPrices.get(rUSDAddress)) ?? 1;\n positions.forEach((position) => {\n const marketTracker = marketsTrackers?.get(position.marketId);\n const tokenInfo = marketsTokenInfo.get(position.marketId);\n if (!tokenInfo) return;\n let fundingCashFlow = 0;\n let adlCashFlow = 0;\n\n if (marketTracker) {\n // fundingCashFlow\n const marketTrackerFundingValue =\n position.base < 0\n ? marketTracker.shortFundingValue\n : marketTracker.longFundingValue;\n const trackerDelta = marketTrackerFundingValue - position.fundingValue;\n fundingCashFlow =\n (trackerDelta / position.baseMultiplier) * -1 * position.base;\n\n // adlCashFlow\n const adlUnwindPrice =\n ((position.base < 0\n ? marketTracker.shortADLUnwindPrice\n : marketTracker.longADLUnwindPrice) -\n position.adlUnwindPrice) /\n position.baseMultiplier;\n const baseMultiplier =\n (position.base < 0\n ? marketTracker.shortBaseMultiplier\n : marketTracker.longBaseMultiplier) / position.baseMultiplier;\n const lastPriceCoefficient = 1 - baseMultiplier;\n const initialPrice = lastPriceCoefficient * position.lastPrice;\n adlCashFlow = (adlUnwindPrice - initialPrice) * position.base;\n }\n\n const unrealizedPnl =\n ((markPrices.get(tokenInfo.quoteTokenId) ?? 0) - position.lastPrice) *\n position.base;\n\n pnl.set(\n tokenInfo.underlyingAssetId,\n (pnl.get(tokenInfo.underlyingAssetId) ?? 0) +\n position.realizedPnl +\n fundingCashFlow +\n adlCashFlow +\n unrealizedPnl,\n );\n });\n\n return Array.from(accountBalances.entries()).reduce(\n (sum, [address, balance]) =>\n sum +\n (balance + (pnl.get(address) ?? 0)) *\n (address === rUSDAddress\n ? 1\n : (assetPrices.get(address) ?? 0) / (rUSDPrice || 1)),\n 0,\n );\n};\n\nexport const calculateFundingPnl = ({\n marketFundingValue,\n marketBaseMultiplier,\n positionAvgEntryFundingValue,\n positionAdjustedBase,\n}: {\n marketFundingValue: number;\n marketBaseMultiplier: number;\n positionAvgEntryFundingValue: number;\n positionAdjustedBase: number;\n}): number => {\n return (\n -1 *\n (((marketFundingValue - positionAvgEntryFundingValue) *\n positionAdjustedBase) /\n marketBaseMultiplier)\n );\n};\n\nexport const calculatePricePnl = ({\n price,\n positionAvgEntryPrice,\n positionAdjustedBase,\n}: {\n price: number;\n positionAvgEntryPrice: number;\n positionAdjustedBase: number;\n}): number => {\n return (price - positionAvgEntryPrice) * positionAdjustedBase;\n};\n\nexport const calculateRealBalance = (\n netDeposits: BigNumber,\n realized_pnl: BigNumber,\n) => {\n return netDeposits.plus(realized_pnl);\n};\n\nexport const calculatePositionAdjustedQty = (\n origQty: BigNumber,\n marketBaseMultiplier: BigNumber,\n positionBaseMultiplier: BigNumber,\n): BigNumber => {\n return origQty.times(marketBaseMultiplier).div(positionBaseMultiplier);\n};\n\n/**\n * Linearly interpolate a depth ratio from a (thresholds, ratios) curve.\n * The curve implicitly starts at (exposure=0, ratio=1.0).\n * - Between thresholds: linear interpolation\n * - Above last threshold: last ratio (flat extrapolation)\n *\n * @param thresholds - Sorted ascending exposure thresholds in notional USD (must be non-empty)\n * @param ratios - Depth ratio at each threshold (same length as thresholds, must be non-empty)\n * @param exposure - Absolute exposure notional (must be >= 0)\n * @throws if thresholds or ratios are empty, or if they have different lengths\n */\nexport const interpolateDepthRatio = (\n thresholds: number[],\n ratios: number[],\n exposure: number,\n): number => {\n if (exposure < 0) {\n throw new Error('interpolateDepthRatio: exposure must be >= 0');\n }\n if (thresholds.length === 0 || ratios.length === 0) {\n throw new Error(\n 'interpolateDepthRatio: thresholds and ratios must be non-empty',\n );\n }\n if (thresholds.length !== ratios.length) {\n throw new Error(\n 'interpolateDepthRatio: thresholds and ratios must have equal length',\n );\n }\n\n const allThresholds = [0, ...thresholds];\n const allRatios = [1.0, ...ratios];\n\n if (exposure === 0) return 1.0;\n if (exposure >= allThresholds[allThresholds.length - 1]) {\n return allRatios[allRatios.length - 1];\n }\n\n for (let i = 0; i < allThresholds.length - 1; i++) {\n if (exposure <= allThresholds[i + 1]) {\n const t =\n (exposure - allThresholds[i]) /\n (allThresholds[i + 1] - allThresholds[i]);\n return allRatios[i] + t * (allRatios[i + 1] - allRatios[i]);\n }\n }\n\n throw new Error('interpolateDepthRatio: unreachable');\n};\n\n/**\n * Calculate the dynamic depth factor for a trade given pool state and config.\n *\n * The pool takes the opposite side of the trade, so:\n * poolNotionalAfterTrade = currentPoolNotional - signedTradeNotional\n *\n * The depth ratio is interpolated from the exposure curve, then multiplied\n * by baseDepth to produce the final depth factor.\n *\n * @param baseDepth - Base depth multiplier for this market\n * @param thresholds - Sorted ascending exposure thresholds (notional USD)\n * @param ratios - Depth ratio at each threshold\n * @param currentPoolNotional - Pool's current notional in this market (signed)\n * @param signedTradeNotional - Trade notional (positive=buy, negative=sell)\n */\nexport const calculateDynamicDepthFactor = ({\n baseDepth,\n thresholds,\n ratios,\n currentPoolNotional,\n signedTradeNotional,\n}: {\n baseDepth: number;\n thresholds: number[];\n ratios: number[];\n currentPoolNotional: number;\n signedTradeNotional: number;\n}): number => {\n const poolNotionalAfterTrade = currentPoolNotional - signedTradeNotional;\n const absExposureNotional = Math.abs(poolNotionalAfterTrade);\n const depthRatio = interpolateDepthRatio(\n thresholds,\n ratios,\n absExposureNotional,\n );\n return depthRatio * baseDepth;\n};\n\nexport const calculatePositionAdlPnL = ({\n marketTrackerAdlUnwindPrice,\n marketTrackerBaseMultiplier,\n positionBase,\n positionAvgEntryPrice,\n positionTrackerBaseMultiplier,\n positionTrackerAdlUnwindPrice,\n}: {\n marketTrackerAdlUnwindPrice: BigNumber;\n marketTrackerBaseMultiplier: BigNumber;\n positionBase: BigNumber;\n positionAvgEntryPrice: BigNumber;\n positionTrackerBaseMultiplier: BigNumber;\n positionTrackerAdlUnwindPrice: BigNumber;\n}): BigNumber => {\n const currentAdlUnwindPrice = marketTrackerAdlUnwindPrice\n .minus(positionTrackerAdlUnwindPrice)\n .multipliedBy(1e18)\n .div(positionTrackerBaseMultiplier);\n\n const initialPrice = positionAvgEntryPrice\n .times(\n BigNumber(1e18).minus(\n marketTrackerBaseMultiplier\n .multipliedBy(1e18)\n .div(positionTrackerBaseMultiplier),\n ),\n )\n .div(1e18);\n\n const adlPnL = positionBase\n .times(currentAdlUnwindPrice.minus(initialPrice))\n .div(1e18);\n\n return adlPnL;\n};\n"]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AAmB,CAAC;IACzE,IAAM,mBAAmB,GAAG,IAAI,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;IAC7D,IAAM,UAAU,GAAG,IAAA,6BAAqB,EACtC,UAAU,EACV,MAAM,EACN,mBAAmB,CACpB,CAAC;IACF,OAAO,UAAU,GAAG,SAAS,CAAC;AAChC,CAAC,CAAC;AArBW,QAAA,2BAA2B,+BAqBtC;AAEK,IAAM,uBAAuB,GAAG,UAAC,EAcvC;QAbC,2BAA2B,iCAAA,EAC3B,2BAA2B,iCAAA,EAC3B,YAAY,kBAAA,EACZ,qBAAqB,2BAAA,EACrB,6BAA6B,mCAAA,EAC7B,6BAA6B,mCAAA;IAS7B,IAAM,qBAAqB,GAAG,2BAA2B;SACtD,KAAK,CAAC,6BAA6B,CAAC;SACpC,YAAY,CAAC,IAAI,CAAC;SAClB,GAAG,CAAC,6BAA6B,CAAC,CAAC;IAEtC,IAAM,YAAY,GAAG,qBAAqB;SACvC,KAAK,CACJ,IAAA,sBAAS,EAAC,IAAI,CAAC,CAAC,KAAK,CACnB,2BAA2B;SACxB,YAAY,CAAC,IAAI,CAAC;SAClB,GAAG,CAAC,6BAA6B,CAAC,CACtC,CACF;SACA,GAAG,CAAC,IAAI,CAAC,CAAC;IAEb,IAAM,MAAM,GAAG,YAAY;SACxB,KAAK,CAAC,qBAAqB,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;SAChD,GAAG,CAAC,IAAI,CAAC,CAAC;IAEb,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAnCW,QAAA,uBAAuB,2BAmClC","sourcesContent":["import { mergeMap } from './struct';\nimport BigNumber from 'bignumber.js';\n\nexport const POOL_IMR = 9.1;\n\ntype MarketId = string;\ntype Address = string;\n\n/**\n * Calculates the maximum amount that can be withdrawn in terms of the token\n * (e.g., if rUSD then in rUSD terms, if WETH then in WETH terms)\n */\nexport const calculateMaxWithdraw = ({\n currentIMR,\n marginBalance,\n realBalance,\n haircut,\n price,\n}: {\n /** Current Initial Margin Requirement of the account in rUSD terms */\n currentIMR: number;\n /** Current margin balance of the account (expected to already include haircuts) in rUSD terms */\n marginBalance: number;\n /**\n * Current real balance of the token the user wants to withdraw in token terms.\n * For rUSD: this equals net deposits (balanceDeprecated from v2 endpoints + realized PnL).\n * For all other tokens: this equals balanceDeprecated (since realized PnL is 0 for them).\n */\n realBalance: number;\n /**\n * Haircut parameter of the token.\n * Note: haircut for rUSD is always expected to be 0.\n */\n haircut: number;\n /** Price of the token in rUSD terms */\n price: number;\n}): number => {\n if (currentIMR < 0 || price < 0 || haircut < 0) {\n throw new Error('currentIMR, price, and haircut cannot be negative');\n }\n\n if (currentIMR === 0) {\n return realBalance;\n }\n\n if (haircut === 1 || price === 0) {\n return 0;\n }\n\n const availableMargin = marginBalance - currentIMR;\n const maxFromMargin = availableMargin / (price * (1 - haircut));\n\n return Math.max(0, Math.min(realBalance, maxFromMargin));\n};\n\nexport const calculateMaxExposure = ({\n poolBalance,\n riskFactor,\n poolIMR = POOL_IMR,\n}: {\n poolBalance: number;\n poolIMR?: number;\n riskFactor: number;\n}) => poolBalance / (poolIMR * Math.sqrt(riskFactor));\n\n/**\n * Splits a trade exposure into rebalancing and unbalancing portions relative to pool net exposure.\n * Rebalancing reduces pool's absolute exposure; unbalancing increases it.\n */\nexport const splitTradeExposure = (\n tradeExposure: number,\n poolNetExposure: number,\n): { rebalancing: number; unbalancing: number } => {\n if (tradeExposure === 0) {\n return { rebalancing: 0, unbalancing: 0 };\n }\n\n // If pool has no exposure, entire trade is unbalancing\n if (poolNetExposure === 0) {\n return { rebalancing: 0, unbalancing: tradeExposure };\n }\n\n // Opposite sign means trade increases pool exposure → pure unbalancing\n const sameSign =\n (poolNetExposure > 0 && tradeExposure > 0) ||\n (poolNetExposure < 0 && tradeExposure < 0);\n if (!sameSign) {\n return { rebalancing: 0, unbalancing: tradeExposure };\n }\n\n // Same sign: trade reduces pool exposure (pool takes opposite side)\n const absPool = Math.abs(poolNetExposure);\n const absTrade = Math.abs(tradeExposure);\n\n if (absTrade <= absPool) {\n // Pure rebalancing — trade doesn't cross zero\n return { rebalancing: tradeExposure, unbalancing: 0 };\n }\n\n // Mixed: rebalance up to pool exposure, then unbalance the rest\n const rebalancing = poolNetExposure;\n const unbalancing = tradeExposure - rebalancing;\n return { rebalancing, unbalancing };\n};\n\n/**\n * Computes the new log price multiplier (logF) after a trade, handling both\n * rebalancing and unbalancing portions.\n *\n * Mirrors on-chain logic in PriceMultiplier.sol:computeNewLogPriceMultiplier.\n */\nexport const computeNewLogF = ({\n currentLogF,\n tradeExposure,\n poolNetExposure,\n depthFactor,\n maxExposure,\n}: {\n currentLogF: number;\n tradeExposure: number;\n poolNetExposure: number;\n depthFactor: number;\n maxExposure: number;\n}): number => {\n if (tradeExposure === 0) return currentLogF;\n\n const { rebalancing, unbalancing } = splitTradeExposure(\n tradeExposure,\n poolNetExposure,\n );\n\n // Case 1: Has unbalancing portion (includes mixed trades)\n if (unbalancing !== 0) {\n // If also rebalancing, it brings logF to 0 by definition (trade crosses zero)\n const startLogF = rebalancing === 0 ? currentLogF : 0;\n return startLogF + unbalancing / (depthFactor * maxExposure);\n }\n\n // Case 2: Only rebalancing\n // Both rebalancing and poolNetExposure have the same sign, so ratio is in [0, 1]\n if (rebalancing !== 0 && poolNetExposure !== 0) {\n const ratio = rebalancing / poolNetExposure;\n return currentLogF * (1 - ratio);\n }\n\n return currentLogF;\n};\n\n/**\n * Estimates the execution price for a trade using the exponential logPriceMultiplier formula.\n *\n * Mirrors on-chain logic:\n * 1. computeNewLogPriceMultiplier (PriceMultiplier.sol)\n * 2. computeAmmPrice: ammPrice = oraclePrice * exp(logF)\n * 3. computeSignedSpread: executionPrice = ammPrice * (1 + signedSpread)\n *\n * @audit spreadDiscount not applied — known limitation, to be addressed later\n * @audit priceSpacing rounding not applied\n */\nexport const calculateEstimatedExecutionPrice = ({\n oraclePrice,\n currentLogF,\n tradeExposure,\n poolNetExposure,\n depthFactor,\n maxExposure,\n priceSpread,\n}: {\n oraclePrice: number;\n currentLogF: number;\n tradeExposure: number;\n poolNetExposure: number;\n depthFactor: number;\n maxExposure: number;\n priceSpread: number;\n}): number => {\n if (tradeExposure === 0) {\n return oraclePrice * Math.exp(currentLogF);\n }\n\n // 1. Compute new logF after trade impact\n const newLogF = computeNewLogF({\n currentLogF,\n tradeExposure,\n poolNetExposure,\n depthFactor,\n maxExposure,\n });\n\n // 2. Compute AMM price: oraclePrice * exp(newLogF)\n const ammPrice = oraclePrice * Math.exp(newLogF);\n\n // 3. Apply signed spread\n const signedSpread = tradeExposure > 0 ? priceSpread : -priceSpread;\n return Math.max(0, ammPrice * (1 + signedSpread));\n};\n\nexport const calculateTradeFee = ({\n feeRate,\n tradeNotional,\n}: {\n feeRate: number;\n tradeNotional: number;\n}) => feeRate * Math.abs(tradeNotional);\n\nexport const calculateSpread = ({\n depthFactor,\n maxExposure,\n oraclePrice,\n poolPrice,\n tradeNotional,\n}: {\n depthFactor: number;\n maxExposure: number;\n oraclePrice: number;\n poolPrice: number;\n tradeNotional: number;\n}) =>\n (poolPrice / oraclePrice) *\n (1 + tradeNotional / (depthFactor * maxExposure) - 1) *\n tradeNotional;\n\nexport const calculateEstimatedLiquidationPrice = ({\n LMR,\n accountBalance,\n isShort,\n notional,\n oraclePrice,\n}: {\n LMR: number;\n accountBalance: number;\n isShort: boolean;\n notional: number;\n oraclePrice: number;\n}) =>\n Math.max(\n 0,\n oraclePrice *\n (1 - ((accountBalance - LMR) / notional) * (isShort ? -1 : 1)),\n );\n\nexport const calculateLeverage = ({\n imrMultiplier,\n riskFactor,\n}: {\n imrMultiplier: number;\n riskFactor: number;\n}) => 1 / (imrMultiplier * Math.sqrt(riskFactor));\n\nexport const calculateRequiredMargin = ({\n currIMR,\n currentNotional,\n leverage,\n tradeNotional,\n}: {\n currIMR: number;\n currentNotional: number;\n leverage: number;\n tradeNotional: number;\n}) =>\n currIMR +\n (Math.abs(currentNotional + tradeNotional) - Math.abs(currentNotional)) /\n leverage;\n\n/**\n * Calculates the maximum notional trade size (unsigned, always ≥ 0).\n *\n * Implements the UI quant spec v2 (\"Maximum trade\") with two cases:\n * Case 1 — Extending: trade same direction as existing position.\n * Case 2 — Reverting: decompose into (a) close position exactly, then\n * (b) extend from zero in the opposite direction.\n *\n * All quadratics use the unsigned convention (t = |tradeNotional|) so the\n * positive root always corresponds to the correct direction.\n */\nexport const calculateMaxOrderSize = ({\n currIMR,\n currNodeBalance,\n currNotional,\n depthFactor,\n feeRate,\n isShortTrade,\n leverage,\n maxExposure,\n oraclePrice,\n poolPrice,\n currentLogF,\n netExposure,\n}: {\n currIMR: number;\n currNodeBalance: number;\n currNotional: number;\n depthFactor: number;\n feeRate: number;\n isShortTrade: boolean;\n leverage: number;\n maxExposure: number;\n oraclePrice: number;\n poolPrice: number;\n currentLogF: number;\n netExposure: number;\n}): number => {\n const D = depthFactor * maxExposure;\n const rho = poolPrice / oraclePrice;\n const tradeSign = isShortTrade ? -1 : 1;\n const s0 = currNotional > 0 ? 1 : currNotional < 0 ? -1 : tradeSign;\n const absN0 = Math.abs(currNotional);\n\n /**\n * Solve the extending quadratic (spec §Case 1) for max trade size.\n * a·t² + b·t + c = 0 where a = ρ·γ, b = s₀·(1/lev + fee) + (ρ−1), c = −surplus.\n * Root selection: pick the root whose sign matches s₀ (dir).\n * For rebalancing (a < 0), caps at |NE| (beyond which γ switches to unbalancing).\n */\n const solveExtending = (\n dir: number,\n surplus: number, // aka, IMRDelta\n rho_: number,\n gamma_: number,\n isRebal: boolean,\n absNE: number,\n ): number => {\n if (surplus <= 0) return 0;\n const a = rho_ * gamma_;\n const b = dir * (1 / leverage + feeRate) + (rho_ - 1);\n const c = -surplus;\n // Degenerate: γ = 0 → linear equation b·t + c = 0 (rebalancing with f = 0)\n if (a === 0) {\n if (b === 0) return isRebal ? absNE : 0;\n const t = -c / b;\n if (t * dir <= 0) return isRebal ? absNE : 0;\n return isRebal ? Math.min(Math.abs(t), absNE) : Math.abs(t);\n }\n const disc = b * b - 4 * a * c;\n if (isRebal) {\n if (disc < 0) return absNE;\n const t = (-b + dir * Math.sqrt(disc)) / (2 * a);\n return t * dir <= 0 ? absNE : Math.min(Math.abs(t), absNE);\n }\n if (disc < 0)\n throw new Error(`calculateMaxOrderSize: negative discriminant (${disc})`);\n const t = (-b + dir * Math.sqrt(disc)) / (2 * a);\n if (t * dir <= 0)\n throw new Error(`calculateMaxOrderSize: no valid root (${t})`);\n return Math.abs(t);\n };\n\n // Determine γ for a trade of given direction against the current pool state\n const getGamma = (dir: number, logF: number, ne: number) => {\n const isRebal = ne !== 0 && dir === Math.sign(ne);\n const gamma = isRebal ? -logF / ne : 1 / D;\n return { gamma, isRebal };\n };\n\n // ── Case 1: Extending ──────────────────────────────────────────────────\n if (tradeSign === s0) {\n const { gamma, isRebal } = getGamma(tradeSign, currentLogF, netExposure);\n return solveExtending(\n s0,\n currNodeBalance - currIMR,\n rho,\n gamma,\n isRebal,\n Math.abs(netExposure),\n );\n }\n\n // ── Case 2: Reverting — close N₀, then re-extend ──────────────────────\n\n // Step 1: Close N₀ entirely (spec §Case 2, Step 1)\n const closeTrade = -currNotional;\n const closeDir = -s0;\n const { gamma: gammaC, isRebal: closeIsRebal } = getGamma(\n closeDir,\n currentLogF,\n netExposure,\n );\n const closeFlipsPool = closeIsRebal && absN0 > Math.abs(netExposure);\n\n // Exec price ratio: ρ·exp(γ_c·closeTrade) for non-flip, dedicated formula for flip\n const closeExecRatio = closeFlipsPool\n ? Math.exp((closeTrade - netExposure) / D)\n : rho * Math.exp(gammaC * closeTrade);\n\n const balClose =\n currNodeBalance - feeRate * absN0 + closeTrade * (1 - closeExecRatio);\n const imrClose = currIMR - absN0 / leverage;\n const surplusClose = balClose - imrClose;\n\n // Insolvency: can't fully close without going under margin.\n // Solve partial-close quadratic (spec appendix): b has flipped feeRate sign.\n // Falls back to full close (|N₀|) when no valid partial-close root exists.\n if (surplusClose <= 0) {\n const IMRDelta = currNodeBalance - currIMR;\n const { gamma: gammaP } = getGamma(closeDir, currentLogF, netExposure);\n const a = rho * gammaP;\n const b = s0 * (1 / leverage - feeRate) + (rho - 1);\n const c = -IMRDelta;\n\n if (a === 0) {\n if (b === 0) return absN0;\n const t = -c / b;\n if (t * closeDir <= 0) return absN0;\n return Math.min(Math.abs(t), absN0);\n }\n\n const disc = b * b - 4 * a * c;\n if (disc < 0) return absN0;\n const t = (-b + closeDir * Math.sqrt(disc)) / (2 * a);\n if (t * closeDir <= 0) return absN0;\n return Math.min(Math.abs(t), absN0);\n }\n\n // Step 2: Extend from zero in -s₀ direction with post-close pool state\n const neAfter = netExposure + currNotional;\n let fPrime: number;\n if (closeFlipsPool) {\n fPrime = (closeTrade - netExposure) / D;\n } else if (closeIsRebal) {\n fPrime = currentLogF * (1 + currNotional / netExposure);\n } else {\n fPrime = currentLogF + closeTrade / D;\n }\n const rhoPrime = Math.exp(fPrime);\n\n const extDir = -s0;\n const { gamma: gammaPrime, isRebal: isExtRebal } = getGamma(\n extDir,\n fPrime,\n neAfter,\n );\n\n return (\n absN0 +\n solveExtending(\n extDir,\n surplusClose,\n rhoPrime,\n gammaPrime,\n isExtRebal,\n Math.abs(neAfter),\n )\n );\n};\n\nexport const calculateIMR = ({\n marketLeverages,\n marketPrices,\n positions,\n}: {\n marketLeverages: Map<MarketId, number>;\n marketPrices: Map<MarketId, number>;\n positions: Map<MarketId, number>;\n}) => {\n return Array.from(positions.entries()).reduce(\n (sum, [marketId, positionSize]) => {\n const leverage = marketLeverages.get(marketId);\n return leverage\n ? sum +\n (Math.abs(positionSize) * (marketPrices.get(marketId) ?? 0)) /\n leverage\n : sum;\n },\n 0,\n );\n};\n\nexport const calculateLMR = ({\n marketPrices,\n positions,\n riskFactors,\n marketId,\n tradeSize,\n}: {\n marketId?: string;\n marketPrices: Map<MarketId, number>;\n positions: Map<MarketId, number>;\n riskFactors: Map<MarketId, number>;\n tradeSize?: number;\n}) =>\n Array.from(\n (marketId && tradeSize\n ? mergeMap(positions, [\n [marketId, (positions.get(marketId) ?? 0) + tradeSize],\n ])\n : positions\n ).entries(),\n ).reduce((sum, [marketId, positionSize]) => {\n const riskFactor = riskFactors.get(marketId);\n return riskFactor\n ? sum +\n Math.abs(positionSize) *\n (marketPrices.get(marketId) ?? 0) *\n Math.sqrt(riskFactor)\n : sum;\n }, 0);\n\n// if account balance is below LMR position is liquidable\n\nexport const calculateMarginRatio = ({\n LMR,\n totalBalance,\n tradeCost,\n}: {\n LMR: number;\n totalBalance: number;\n tradeCost: number;\n}) => {\n if (LMR === 0) {\n return 0;\n }\n if (tradeCost > totalBalance) {\n return 1;\n }\n if (totalBalance === tradeCost) {\n return Infinity;\n }\n return Math.min(1, LMR / (totalBalance - tradeCost));\n};\n\nexport const calculateTotalMargin = ({\n marketsTrackers,\n accountBalances,\n assetPrices,\n markPrices,\n marketsTokenInfo,\n positions,\n rUSDAddress,\n}: {\n accountBalances: Map<Address, number>;\n assetPrices: Map<Address, number>;\n markPrices: Map<Address, number>;\n marketsTokenInfo: Map<\n MarketId,\n {\n quoteTokenId: string;\n underlyingAssetId: string;\n }\n >;\n marketsTrackers: Map<\n MarketId,\n {\n longADLUnwindPrice: number;\n longBaseMultiplier: number;\n longFundingValue: number;\n shortADLUnwindPrice: number;\n shortBaseMultiplier: number;\n shortFundingValue: number;\n }\n >;\n positions: {\n adlUnwindPrice: number;\n base: number;\n baseMultiplier: number;\n fundingValue: number;\n lastPrice: number;\n marketId: string;\n realizedPnl: number;\n }[];\n rUSDAddress?: string;\n}) => {\n const pnl = new Map<Address, number>();\n const rUSDPrice = (rUSDAddress && assetPrices.get(rUSDAddress)) ?? 1;\n positions.forEach((position) => {\n const marketTracker = marketsTrackers?.get(position.marketId);\n const tokenInfo = marketsTokenInfo.get(position.marketId);\n if (!tokenInfo) return;\n let fundingCashFlow = 0;\n let adlCashFlow = 0;\n\n if (marketTracker) {\n // fundingCashFlow\n const marketTrackerFundingValue =\n position.base < 0\n ? marketTracker.shortFundingValue\n : marketTracker.longFundingValue;\n const trackerDelta = marketTrackerFundingValue - position.fundingValue;\n fundingCashFlow =\n (trackerDelta / position.baseMultiplier) * -1 * position.base;\n\n // adlCashFlow\n const adlUnwindPrice =\n ((position.base < 0\n ? marketTracker.shortADLUnwindPrice\n : marketTracker.longADLUnwindPrice) -\n position.adlUnwindPrice) /\n position.baseMultiplier;\n const baseMultiplier =\n (position.base < 0\n ? marketTracker.shortBaseMultiplier\n : marketTracker.longBaseMultiplier) / position.baseMultiplier;\n const lastPriceCoefficient = 1 - baseMultiplier;\n const initialPrice = lastPriceCoefficient * position.lastPrice;\n adlCashFlow = (adlUnwindPrice - initialPrice) * position.base;\n }\n\n const unrealizedPnl =\n ((markPrices.get(tokenInfo.quoteTokenId) ?? 0) - position.lastPrice) *\n position.base;\n\n pnl.set(\n tokenInfo.underlyingAssetId,\n (pnl.get(tokenInfo.underlyingAssetId) ?? 0) +\n position.realizedPnl +\n fundingCashFlow +\n adlCashFlow +\n unrealizedPnl,\n );\n });\n\n return Array.from(accountBalances.entries()).reduce(\n (sum, [address, balance]) =>\n sum +\n (balance + (pnl.get(address) ?? 0)) *\n (address === rUSDAddress\n ? 1\n : (assetPrices.get(address) ?? 0) / (rUSDPrice || 1)),\n 0,\n );\n};\n\nexport const calculateFundingPnl = ({\n marketFundingValue,\n marketBaseMultiplier,\n positionAvgEntryFundingValue,\n positionAdjustedBase,\n}: {\n marketFundingValue: number;\n marketBaseMultiplier: number;\n positionAvgEntryFundingValue: number;\n positionAdjustedBase: number;\n}): number => {\n return (\n -1 *\n (((marketFundingValue - positionAvgEntryFundingValue) *\n positionAdjustedBase) /\n marketBaseMultiplier)\n );\n};\n\nexport const calculatePricePnl = ({\n price,\n positionAvgEntryPrice,\n positionAdjustedBase,\n}: {\n price: number;\n positionAvgEntryPrice: number;\n positionAdjustedBase: number;\n}): number => {\n return (price - positionAvgEntryPrice) * positionAdjustedBase;\n};\n\nexport const calculateRealBalance = (\n netDeposits: BigNumber,\n realized_pnl: BigNumber,\n) => {\n return netDeposits.plus(realized_pnl);\n};\n\nexport const calculatePositionAdjustedQty = (\n origQty: BigNumber,\n marketBaseMultiplier: BigNumber,\n positionBaseMultiplier: BigNumber,\n): BigNumber => {\n return origQty.times(marketBaseMultiplier).div(positionBaseMultiplier);\n};\n\n/**\n * Linearly interpolate a depth ratio from a (thresholds, ratios) curve.\n * The curve implicitly starts at (exposure=0, ratio=1.0).\n * - Between thresholds: linear interpolation\n * - Above last threshold: last ratio (flat extrapolation)\n *\n * @param thresholds - Sorted ascending exposure thresholds in notional USD (must be non-empty)\n * @param ratios - Depth ratio at each threshold (same length as thresholds, must be non-empty)\n * @param exposure - Absolute exposure notional (must be >= 0)\n * @throws if thresholds or ratios are empty, or if they have different lengths\n */\nexport const interpolateDepthRatio = (\n thresholds: number[],\n ratios: number[],\n exposure: number,\n): number => {\n if (exposure < 0) {\n throw new Error('interpolateDepthRatio: exposure must be >= 0');\n }\n if (thresholds.length === 0 || ratios.length === 0) {\n throw new Error(\n 'interpolateDepthRatio: thresholds and ratios must be non-empty',\n );\n }\n if (thresholds.length !== ratios.length) {\n throw new Error(\n 'interpolateDepthRatio: thresholds and ratios must have equal length',\n );\n }\n\n const allThresholds = [0, ...thresholds];\n const allRatios = [1.0, ...ratios];\n\n if (exposure === 0) return 1.0;\n if (exposure >= allThresholds[allThresholds.length - 1]) {\n return allRatios[allRatios.length - 1];\n }\n\n for (let i = 0; i < allThresholds.length - 1; i++) {\n if (exposure <= allThresholds[i + 1]) {\n const t =\n (exposure - allThresholds[i]) /\n (allThresholds[i + 1] - allThresholds[i]);\n return allRatios[i] + t * (allRatios[i + 1] - allRatios[i]);\n }\n }\n\n throw new Error('interpolateDepthRatio: unreachable');\n};\n\n/**\n * Calculate the dynamic depth factor for a trade given pool state and config.\n *\n * The pool takes the opposite side of the trade, so:\n * poolNotionalAfterTrade = currentPoolNotional - signedTradeNotional\n *\n * The depth ratio is interpolated from the exposure curve, then multiplied\n * by baseDepth to produce the final depth factor.\n *\n * @param baseDepth - Base depth multiplier for this market\n * @param thresholds - Sorted ascending exposure thresholds (notional USD)\n * @param ratios - Depth ratio at each threshold\n * @param currentPoolNotional - Pool's current notional in this market (signed)\n * @param signedTradeNotional - Trade notional (positive=buy, negative=sell)\n */\nexport const calculateDynamicDepthFactor = ({\n baseDepth,\n thresholds,\n ratios,\n currentPoolNotional,\n signedTradeNotional,\n}: {\n baseDepth: number;\n thresholds: number[];\n ratios: number[];\n currentPoolNotional: number;\n signedTradeNotional: number;\n}): number => {\n const poolNotionalAfterTrade = currentPoolNotional - signedTradeNotional;\n const absExposureNotional = Math.abs(poolNotionalAfterTrade);\n const depthRatio = interpolateDepthRatio(\n thresholds,\n ratios,\n absExposureNotional,\n );\n return depthRatio * baseDepth;\n};\n\nexport const calculatePositionAdlPnL = ({\n marketTrackerAdlUnwindPrice,\n marketTrackerBaseMultiplier,\n positionBase,\n positionAvgEntryPrice,\n positionTrackerBaseMultiplier,\n positionTrackerAdlUnwindPrice,\n}: {\n marketTrackerAdlUnwindPrice: BigNumber;\n marketTrackerBaseMultiplier: BigNumber;\n positionBase: BigNumber;\n positionAvgEntryPrice: BigNumber;\n positionTrackerBaseMultiplier: BigNumber;\n positionTrackerAdlUnwindPrice: BigNumber;\n}): BigNumber => {\n const currentAdlUnwindPrice = marketTrackerAdlUnwindPrice\n .minus(positionTrackerAdlUnwindPrice)\n .multipliedBy(1e18)\n .div(positionTrackerBaseMultiplier);\n\n const initialPrice = positionAvgEntryPrice\n .times(\n BigNumber(1e18).minus(\n marketTrackerBaseMultiplier\n .multipliedBy(1e18)\n .div(positionTrackerBaseMultiplier),\n ),\n )\n .div(1e18);\n\n const adlPnL = positionBase\n .times(currentAdlUnwindPrice.minus(initialPrice))\n .div(1e18);\n\n return adlPnL;\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/common",
3
- "version": "0.344.0",
3
+ "version": "0.344.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
@@ -45,5 +45,5 @@
45
45
  "generate:coverage-badges": "npx istanbul-badges-readme --silent"
46
46
  },
47
47
  "packageManager": "pnpm@8.3.1",
48
- "gitHead": "0f003607f48dc02a100ec21e460d1485fa3d7e80"
48
+ "gitHead": "d053c2a017e7831f3c4dfcb8351bf1f963ea6f9d"
49
49
  }
@@ -262,49 +262,184 @@ export const calculateRequiredMargin = ({
262
262
  (Math.abs(currentNotional + tradeNotional) - Math.abs(currentNotional)) /
263
263
  leverage;
264
264
 
265
+ /**
266
+ * Calculates the maximum notional trade size (unsigned, always ≥ 0).
267
+ *
268
+ * Implements the UI quant spec v2 ("Maximum trade") with two cases:
269
+ * Case 1 — Extending: trade same direction as existing position.
270
+ * Case 2 — Reverting: decompose into (a) close position exactly, then
271
+ * (b) extend from zero in the opposite direction.
272
+ *
273
+ * All quadratics use the unsigned convention (t = |tradeNotional|) so the
274
+ * positive root always corresponds to the correct direction.
275
+ */
265
276
  export const calculateMaxOrderSize = ({
266
277
  currIMR,
267
278
  currNodeBalance,
268
279
  currNotional,
269
280
  depthFactor,
270
- extendingPosition,
271
281
  feeRate,
272
282
  isShortTrade,
273
283
  leverage,
274
284
  maxExposure,
275
285
  oraclePrice,
276
286
  poolPrice,
287
+ currentLogF,
288
+ netExposure,
277
289
  }: {
278
290
  currIMR: number;
279
291
  currNodeBalance: number;
280
292
  currNotional: number;
281
293
  depthFactor: number;
282
- extendingPosition: boolean;
283
294
  feeRate: number;
284
295
  isShortTrade: boolean;
285
296
  leverage: number;
286
297
  maxExposure: number;
287
298
  oraclePrice: number;
288
299
  poolPrice: number;
289
- }) => {
290
- const a = poolPrice / oraclePrice;
291
- const c = 1 / (depthFactor * maxExposure);
292
- const R = a - 1 - feeRate * (isShortTrade ? -1 : 1);
293
- const SN = currNotional < 0 ? -1 : 1;
294
- const Q = R - (SN / leverage) * (extendingPosition ? 1 : -1);
295
- return Math.abs(
296
- (-1 * Q +
297
- Math.sqrt(
298
- Math.pow(Q, 2) -
299
- 4 *
300
- a *
301
- c *
302
- (currNodeBalance -
303
- currIMR +
304
- (extendingPosition ? 0 : ((2 * SN) / leverage) * currNotional)),
305
- ) *
306
- (isShortTrade ? 1 : -1)) /
307
- (2 * a * c),
300
+ currentLogF: number;
301
+ netExposure: number;
302
+ }): number => {
303
+ const D = depthFactor * maxExposure;
304
+ const rho = poolPrice / oraclePrice;
305
+ const tradeSign = isShortTrade ? -1 : 1;
306
+ const s0 = currNotional > 0 ? 1 : currNotional < 0 ? -1 : tradeSign;
307
+ const absN0 = Math.abs(currNotional);
308
+
309
+ /**
310
+ * Solve the extending quadratic (spec §Case 1) for max trade size.
311
+ * a·t² + b·t + c = 0 where a = ρ·γ, b = s₀·(1/lev + fee) + (ρ−1), c = −surplus.
312
+ * Root selection: pick the root whose sign matches s₀ (dir).
313
+ * For rebalancing (a < 0), caps at |NE| (beyond which γ switches to unbalancing).
314
+ */
315
+ const solveExtending = (
316
+ dir: number,
317
+ surplus: number, // aka, IMRDelta
318
+ rho_: number,
319
+ gamma_: number,
320
+ isRebal: boolean,
321
+ absNE: number,
322
+ ): number => {
323
+ if (surplus <= 0) return 0;
324
+ const a = rho_ * gamma_;
325
+ const b = dir * (1 / leverage + feeRate) + (rho_ - 1);
326
+ const c = -surplus;
327
+ // Degenerate: γ = 0 → linear equation b·t + c = 0 (rebalancing with f = 0)
328
+ if (a === 0) {
329
+ if (b === 0) return isRebal ? absNE : 0;
330
+ const t = -c / b;
331
+ if (t * dir <= 0) return isRebal ? absNE : 0;
332
+ return isRebal ? Math.min(Math.abs(t), absNE) : Math.abs(t);
333
+ }
334
+ const disc = b * b - 4 * a * c;
335
+ if (isRebal) {
336
+ if (disc < 0) return absNE;
337
+ const t = (-b + dir * Math.sqrt(disc)) / (2 * a);
338
+ return t * dir <= 0 ? absNE : Math.min(Math.abs(t), absNE);
339
+ }
340
+ if (disc < 0)
341
+ throw new Error(`calculateMaxOrderSize: negative discriminant (${disc})`);
342
+ const t = (-b + dir * Math.sqrt(disc)) / (2 * a);
343
+ if (t * dir <= 0)
344
+ throw new Error(`calculateMaxOrderSize: no valid root (${t})`);
345
+ return Math.abs(t);
346
+ };
347
+
348
+ // Determine γ for a trade of given direction against the current pool state
349
+ const getGamma = (dir: number, logF: number, ne: number) => {
350
+ const isRebal = ne !== 0 && dir === Math.sign(ne);
351
+ const gamma = isRebal ? -logF / ne : 1 / D;
352
+ return { gamma, isRebal };
353
+ };
354
+
355
+ // ── Case 1: Extending ──────────────────────────────────────────────────
356
+ if (tradeSign === s0) {
357
+ const { gamma, isRebal } = getGamma(tradeSign, currentLogF, netExposure);
358
+ return solveExtending(
359
+ s0,
360
+ currNodeBalance - currIMR,
361
+ rho,
362
+ gamma,
363
+ isRebal,
364
+ Math.abs(netExposure),
365
+ );
366
+ }
367
+
368
+ // ── Case 2: Reverting — close N₀, then re-extend ──────────────────────
369
+
370
+ // Step 1: Close N₀ entirely (spec §Case 2, Step 1)
371
+ const closeTrade = -currNotional;
372
+ const closeDir = -s0;
373
+ const { gamma: gammaC, isRebal: closeIsRebal } = getGamma(
374
+ closeDir,
375
+ currentLogF,
376
+ netExposure,
377
+ );
378
+ const closeFlipsPool = closeIsRebal && absN0 > Math.abs(netExposure);
379
+
380
+ // Exec price ratio: ρ·exp(γ_c·closeTrade) for non-flip, dedicated formula for flip
381
+ const closeExecRatio = closeFlipsPool
382
+ ? Math.exp((closeTrade - netExposure) / D)
383
+ : rho * Math.exp(gammaC * closeTrade);
384
+
385
+ const balClose =
386
+ currNodeBalance - feeRate * absN0 + closeTrade * (1 - closeExecRatio);
387
+ const imrClose = currIMR - absN0 / leverage;
388
+ const surplusClose = balClose - imrClose;
389
+
390
+ // Insolvency: can't fully close without going under margin.
391
+ // Solve partial-close quadratic (spec appendix): b has flipped feeRate sign.
392
+ // Falls back to full close (|N₀|) when no valid partial-close root exists.
393
+ if (surplusClose <= 0) {
394
+ const IMRDelta = currNodeBalance - currIMR;
395
+ const { gamma: gammaP } = getGamma(closeDir, currentLogF, netExposure);
396
+ const a = rho * gammaP;
397
+ const b = s0 * (1 / leverage - feeRate) + (rho - 1);
398
+ const c = -IMRDelta;
399
+
400
+ if (a === 0) {
401
+ if (b === 0) return absN0;
402
+ const t = -c / b;
403
+ if (t * closeDir <= 0) return absN0;
404
+ return Math.min(Math.abs(t), absN0);
405
+ }
406
+
407
+ const disc = b * b - 4 * a * c;
408
+ if (disc < 0) return absN0;
409
+ const t = (-b + closeDir * Math.sqrt(disc)) / (2 * a);
410
+ if (t * closeDir <= 0) return absN0;
411
+ return Math.min(Math.abs(t), absN0);
412
+ }
413
+
414
+ // Step 2: Extend from zero in -s₀ direction with post-close pool state
415
+ const neAfter = netExposure + currNotional;
416
+ let fPrime: number;
417
+ if (closeFlipsPool) {
418
+ fPrime = (closeTrade - netExposure) / D;
419
+ } else if (closeIsRebal) {
420
+ fPrime = currentLogF * (1 + currNotional / netExposure);
421
+ } else {
422
+ fPrime = currentLogF + closeTrade / D;
423
+ }
424
+ const rhoPrime = Math.exp(fPrime);
425
+
426
+ const extDir = -s0;
427
+ const { gamma: gammaPrime, isRebal: isExtRebal } = getGamma(
428
+ extDir,
429
+ fPrime,
430
+ neAfter,
431
+ );
432
+
433
+ return (
434
+ absN0 +
435
+ solveExtending(
436
+ extDir,
437
+ surplusClose,
438
+ rhoPrime,
439
+ gammaPrime,
440
+ isExtRebal,
441
+ Math.abs(neAfter),
442
+ )
308
443
  );
309
444
  };
310
445