@lifi/perps-sdk-provider-lighter 49.0.0 → 50.1.0

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.
@@ -1 +1 @@
1
- {"version":3,"file":"LighterProvider.d.ts","sourceRoot":"","sources":["../../src/LighterProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAmBL,KAAK,mBAAmB,EAwBxB,KAAK,cAAc,EAQpB,MAAM,iBAAiB,CAAA;AAkCxB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAQnC,OAAO,EAUL,KAAK,iBAAiB,EACvB,MAAM,gBAAgB,CAAA;AA+LvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,WAAW,sBAAsB;IACrC;;;;OAIG;IACH,OAAO,CAAC,EAAE,cAAc,CAAA;IACxB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,4FAA4F;IAC5F,uBAAuB,CAAC,EAAE,MAAM,CAAA;CACjC;AAcD;;;;;;;;GAQG;AACH,MAAM,WAAW,oBAAqB,SAAQ,mBAAmB;IAC/D;;;;OAIG;IACH,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAA;IAC/D,iFAAiF;IACjF,eAAe,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CACxD;AAED;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,GAChC,YAAY,iBAAiB,EAC7B,UAAS,sBAA2B,KACnC,oBA4rDF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAS,sBAA2B,KACnC,oBACyD,CAAA;AAE5D;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,iBAAiB,GAC5B,UAAS,sBAA2B,KACnC,oBAA6E,CAAA;AAEh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4BmC,CAAA"}
@@ -138,13 +138,14 @@ export declare const DEFAULT_TRADES_LIMIT = 50;
138
138
  */
139
139
  export declare const LIGHTER_FEE_TICK_SCALE = 1000000;
140
140
  /**
141
- * Decimal places a client may use for Lighter leverage. Lighter stores
142
- * leverage as an integer basis-point IMF (`round(10000 / leverage)`), and at
143
- * this precision a read-back leverage always re-saves the same IMF.
141
+ * Decimal places for leverage submission, order-compatible reads, and fill conversion.
142
+ * Native-IMF comparison does not use this limit.
144
143
  *
145
144
  * @public
146
145
  */
147
146
  export declare const LIGHTER_LEVERAGE_PRECISION = 2;
147
+ /** Decimal places for Lighter position and market-setting display leverage. */
148
+ export declare const LIGHTER_LEVERAGE_DISPLAY_PRECISION = 1;
148
149
  /**
149
150
  * Scale of the integer `initial_margin_fraction` values Lighter publishes on
150
151
  * trade rows and order-book details: the integer is a percent times 100, so
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,KAAK,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAGpE;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,YAAY,CAAA;AAE7C;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,eAAe,CAAA;AAEnD;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,wCAAwC,CAAA;AAC7E;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,6CAA6C,CAAA;AAEhF;;;;;GAKG;AACH,eAAO,MAAM,+BAA+B,MAAM,CAAA;AAElD;;;;;GAKG;AACH,eAAO,MAAM,oCAAoC,2CACP,CAAA;AAE1C;;;;;;GAMG;AACH,MAAM,WAAW,sBAAsB;IACrC,gFAAgF;IAChF,UAAU,EAAE,MAAM,CAAA;IAClB,aAAa,EAAE,MAAM,CAAA;CACtB;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,y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A;AAEvC;;;;GAIG;AACH,QAAA,MAAM,0BAA0B,QAAQ,CAAA;AAExC;;;;;;;GAOG;AACH,QAAA,MAAM,8BAA8B,QAAQ,CAAA;AAE5C;;;;;;GAMG;AACH,QAAA,MAAM,qBAAqB,aAAoB,CAAA;AAE/C;;;;;;GAMG;AACH,QAAA,MAAM,6BAA6B,MAAM,CAAA;AAEzC;;;;;;;GAOG;AACH,QAAA,MAAM,kCAAkC,QAAQ,CAAA;AAEhD,OAAO,EACL,8BAA8B,EAC9B,yBAAyB,EACzB,8BAA8B,EAC9B,6BAA6B,EAC7B,kCAAkC,EAClC,qBAAqB,EACrB,0BAA0B,GAC3B,CAAA"}
@@ -2,13 +2,13 @@ import type { PerpsMarketDisplay, Position } from '@lifi/perps-types';
2
2
  import type { LtAccountPosition } from '../types/index.js';
3
3
  /**
4
4
  * Display leverage from an IMF percent string: `100 / IMF` in exact decimal
5
- * arithmetic, rounded half-up to `LIGHTER_LEVERAGE_PRECISION`. The stored IMF
6
- * can make this differ from the requested leverage. Provider risk calculations
7
- * consume the original decimal IMF instead. `undefined` for a non-positive
5
+ * arithmetic, rounded half-up to the given precision (two decimals by default).
6
+ * The stored IMF can make this differ from the requested leverage.
7
+ * Returns `undefined` for a non-positive
8
8
  * or unparsable IMF.
9
9
  * @public
10
10
  */
11
- export declare const leverageFromImf: (imf: string) => string | undefined;
11
+ export declare const leverageFromImf: (imf: string, precision?: number) => string | undefined;
12
12
  /**
13
13
  * Display leverage from an integer IMF on `LIGHTER_IMF_PERCENT_SCALE`, the
14
14
  * unit of trade rows and order-book details: `500` is 5.00%, so 20x.
@@ -16,6 +16,10 @@ export declare const leverageFromImf: (imf: string) => string | undefined;
16
16
  * fraction a `StrictInt`, so the scale division is exact.
17
17
  */
18
18
  export declare const leverageFromScaledImf: (imf: number) => string | undefined;
19
+ /** ROE percentage from entry notional and the exact basis-point initial margin fraction. */
20
+ export declare const calculateRoe: (position: Position) => string | undefined;
21
+ /** Recalculate mark fields and ROE from the retained IMF without changing the input or snapshot margin. */
22
+ export declare const remarkPosition: (position: Position, markPrice: string) => Position;
19
23
  /**
20
24
  * Map a raw Lighter account position to the generic Position type. A row with
21
25
  * an invalid size gives `undefined`. An invalid position value or a
@@ -1 +1 @@
1
- {"version":3,"file":"mapPosition.d.ts","sourceRoot":"","sources":["../../../src/utils/mapPosition.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAOrE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAG1D;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,KAAK,MAAM,KAAG,MAAM,GAAG,SAQtD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,KAAK,MAAM,KAAG,MAAM,GAAG,SAO5D,CAAA;AAsBD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,WAAW,GACtB,KAAK,iBAAiB,EACtB,QAAQ,kBAAkB,KACzB,QAAQ,GAAG,SAoDb,CAAA"}
1
+ {"version":3,"file":"mapPosition.d.ts","sourceRoot":"","sources":["../../../src/utils/mapPosition.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAQrE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAG1D;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAC1B,KAAK,MAAM,EACX,kBAAsC,KACrC,MAAM,GAAG,SAQX,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,KAAK,MAAM,KAAG,MAAM,GAAG,SAO5D,CAAA;AAED,4FAA4F;AAC5F,eAAO,MAAM,YAAY,GAAI,UAAU,QAAQ,KAAG,MAAM,GAAG,SAyB1D,CAAA;AAED,2GAA2G;AAC3G,eAAO,MAAM,cAAc,GACzB,UAAU,QAAQ,EAClB,WAAW,MAAM,KAChB,QAUF,CAAA;AAsBD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,WAAW,GACtB,KAAK,iBAAiB,EACtB,QAAQ,kBAAkB,KACzB,QAAQ,GAAG,SAmEb,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/perps-sdk-provider-lighter",
3
- "version": "49.0.0",
3
+ "version": "50.1.0",
4
4
  "description": "Lighter provider plugin for @lifi/perps-sdk",
5
5
  "homepage": "https://github.com/lifinance/perps-sdk",
6
6
  "bugs": {
@@ -43,11 +43,11 @@
43
43
  ],
44
44
  "dependencies": {
45
45
  "big.js": "^7.0.1",
46
- "@lifi/perps-types": "^30.0.0"
46
+ "@lifi/perps-types": "^30.2.0"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "viem": ">=2.57.11 <2.57.12",
50
- "@lifi/perps-sdk": "^35.0.0"
50
+ "@lifi/perps-sdk": "^36.1.0"
51
51
  },
52
52
  "publishConfig": {
53
53
  "access": "public"
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  ACTIVE_ORDER_STATUSES,
3
+ type ActionSignerContribution,
3
4
  calculateTransferable,
4
5
  compareDecimalStrings,
5
6
  type DepositFlow,
@@ -15,6 +16,7 @@ import {
15
16
  isDecimalStringGreaterThan,
16
17
  isSpotCategory,
17
18
  localStorageAdapter,
19
+ multiplyDecimalString,
18
20
  numberToDecimalString,
19
21
  PerpsError,
20
22
  type PerpsProviderPlugin,
@@ -48,6 +50,7 @@ import {
48
50
  subtractDecimalString,
49
51
  toPerpsMarketDisplay,
50
52
  unknownToDecimalString,
53
+ validateInitialMarginFractionBps,
51
54
  type WithdrawalSourceTypes,
52
55
  } from '@lifi/perps-sdk'
53
56
  import type {
@@ -55,6 +58,7 @@ import type {
55
58
  AccountConfigSetting,
56
59
  AccountResponse,
57
60
  AccountSummary,
61
+ ActionParamsMap,
58
62
  ActionStep,
59
63
  ActivitiesResponse,
60
64
  ActivityItem,
@@ -68,6 +72,7 @@ import type {
68
72
  MarketSettings,
69
73
  Order,
70
74
  OrdersResponse,
75
+ PerpsSigner,
71
76
  PortfolioHistoryRange,
72
77
  PortfolioHistoryResponse,
73
78
  Position,
@@ -98,6 +103,8 @@ import {
98
103
  LIGHTER_CLIENT_ORDER_INDEX_ID_PREFIX,
99
104
  LIGHTER_FEE_TICK_SCALE,
100
105
  LIGHTER_HISTORY_PAGE_SIZE,
106
+ LIGHTER_IMF_PERCENT_SCALE,
107
+ LIGHTER_LEVERAGE_DISPLAY_PRECISION,
101
108
  LIGHTER_LEVERAGE_PRECISION,
102
109
  LIGHTER_MAINNET_DEPLOYMENT,
103
110
  LIGHTER_RH_DEPLOYMENT,
@@ -160,7 +167,6 @@ import {
160
167
  fetchDetailedAccount,
161
168
  LIGHTER_MARKET_SLIPPAGE_PERCENT,
162
169
  type LighterFastWithdrawal,
163
- leverageFromImf,
164
170
  lighterAsset,
165
171
  lighterFastWithdrawal,
166
172
  lighterWithdrawableBalances,
@@ -175,6 +181,7 @@ import {
175
181
  toIsoFromMs,
176
182
  toIsoFromSeconds,
177
183
  } from './utils/index.js'
184
+ import { calculateRoe, remarkPosition } from './utils/mapPosition.js'
178
185
  import {
179
186
  fetchRegisteredApiKey,
180
187
  normalizeLighterPublicKey,
@@ -200,6 +207,37 @@ const tickToFeeString = (tick: number): string =>
200
207
  const calculateLeverageImf = (leverage: string): string =>
201
208
  roundDecimalString(divideDecimalString('10000', leverage), 0, 'round')
202
209
 
210
+ /**
211
+ * The IMF that a leverage request stores on the venue: the caller's native
212
+ * source unchanged when it is present, else the half-up encoding of the
213
+ * leverage multiple.
214
+ */
215
+ const resolveRequestedImf = (
216
+ leverage: string | undefined,
217
+ initialMarginFractionBps: string | undefined
218
+ ): string => {
219
+ if (initialMarginFractionBps !== undefined) {
220
+ return validateInitialMarginFractionBps(
221
+ initialMarginFractionBps,
222
+ 'requested initialMarginFractionBps'
223
+ )
224
+ }
225
+ if (leverage === undefined || !isDecimalStringGreaterThan(leverage, '0')) {
226
+ throw new PerpsError(
227
+ PerpsErrorCode.ValidationError,
228
+ 'Leverage must be above zero'
229
+ )
230
+ }
231
+ const requestedImf = calculateLeverageImf(leverage)
232
+ if (requestedImf === '0') {
233
+ throw new PerpsError(
234
+ PerpsErrorCode.ValidationError,
235
+ `Lighter requested leverage '${leverage}' is too high to encode a positive IMF`
236
+ )
237
+ }
238
+ return requestedImf
239
+ }
240
+
203
241
  const projectFeeTier = (
204
242
  limits: LtAccountLimits
205
243
  ): { maker: string; taker: string } => ({
@@ -947,9 +985,17 @@ export const createLighterProvider = (
947
985
  `The backend market '${marketId}' carries no positive Lighter default leverage`
948
986
  )
949
987
  }
988
+ const orderLeverage = numberToDecimalString(leverage)
989
+ const initialMarginFractionBps = calculateLeverageImf(orderLeverage)
950
990
  return {
951
991
  marginMode: MarginMode.CROSS,
952
- leverage: numberToDecimalString(leverage),
992
+ leverage: orderLeverage,
993
+ displayLeverage: roundDecimalString(
994
+ divideDecimalString('10000', initialMarginFractionBps),
995
+ LIGHTER_LEVERAGE_DISPLAY_PRECISION,
996
+ 'round'
997
+ ),
998
+ initialMarginFractionBps,
953
999
  }
954
1000
  }
955
1001
 
@@ -1472,26 +1518,37 @@ export const createLighterProvider = (
1472
1518
  const row = account?.positions.find(
1473
1519
  (p) => String(p.market_id) === marketId
1474
1520
  )
1475
- // An unparsable IMF throws; a non-positive one means no stored setting.
1476
- const leverage =
1477
- row === undefined
1478
- ? undefined
1479
- : leverageFromImf(
1480
- unknownToDecimalString(
1481
- row.initial_margin_fraction,
1482
- 'initial_margin_fraction',
1483
- providerKey
1484
- )
1485
- )
1486
- if (row === undefined || leverage === undefined) {
1521
+ if (row === undefined) {
1522
+ return resolveDefaultMarketSettings(marketId)
1523
+ }
1524
+ const imf = unknownToDecimalString(
1525
+ row.initial_margin_fraction,
1526
+ 'initial_margin_fraction',
1527
+ providerKey
1528
+ )
1529
+ if (!isDecimalStringGreaterThan(imf, '0')) {
1487
1530
  return resolveDefaultMarketSettings(marketId)
1488
1531
  }
1532
+ const decodedLeverage = divideDecimalString('100', imf)
1489
1533
  return {
1490
1534
  marginMode:
1491
1535
  row.margin_mode === LT_MARGIN_MODE_ISOLATED
1492
1536
  ? MarginMode.ISOLATED
1493
1537
  : MarginMode.CROSS,
1494
- leverage,
1538
+ leverage: roundDecimalString(
1539
+ decodedLeverage,
1540
+ LIGHTER_LEVERAGE_PRECISION,
1541
+ 'round'
1542
+ ),
1543
+ displayLeverage: roundDecimalString(
1544
+ decodedLeverage,
1545
+ LIGHTER_LEVERAGE_DISPLAY_PRECISION,
1546
+ 'round'
1547
+ ),
1548
+ initialMarginFractionBps: multiplyDecimalString(
1549
+ imf,
1550
+ String(LIGHTER_IMF_PERCENT_SCALE)
1551
+ ),
1495
1552
  }
1496
1553
  },
1497
1554
 
@@ -2002,27 +2059,31 @@ export const createLighterProvider = (
2002
2059
 
2003
2060
  snapOrderSize,
2004
2061
 
2005
- isLeverageEqual(requested, reported) {
2006
- if (
2007
- compareDecimalStrings(
2008
- requested,
2009
- roundDecimalString(requested, LIGHTER_LEVERAGE_PRECISION, 'truncate')
2010
- ) !== 0
2011
- ) {
2012
- throw new PerpsError(
2013
- PerpsErrorCode.ValidationError,
2014
- `Lighter requested leverage must use at most ${LIGHTER_LEVERAGE_PRECISION} decimal places`
2015
- )
2016
- }
2017
- const requestedImf = calculateLeverageImf(requested)
2018
- const reportedImf = calculateLeverageImf(reported)
2019
- if (requestedImf === '0' || reportedImf === '0') {
2062
+ calculateRoe,
2063
+
2064
+ remarkPosition,
2065
+
2066
+ isLeverageEqual(requested, reported, requestedInitialMarginFractionBps) {
2067
+ const requestedImf = resolveRequestedImf(
2068
+ requested,
2069
+ requestedInitialMarginFractionBps
2070
+ )
2071
+ const reportedImf = reported.initialMarginFractionBps
2072
+ if (reportedImf === undefined) {
2020
2073
  throw new PerpsError(
2021
2074
  PerpsErrorCode.ValidationError,
2022
- 'Lighter leverage must produce a positive IMF'
2075
+ 'Lighter reported IMF must be a positive integer'
2023
2076
  )
2024
2077
  }
2025
- return requestedImf === reportedImf
2078
+ return (
2079
+ compareDecimalStrings(
2080
+ requestedImf,
2081
+ validateInitialMarginFractionBps(
2082
+ reportedImf,
2083
+ 'reported initialMarginFractionBps'
2084
+ )
2085
+ ) === 0
2086
+ )
2026
2087
  },
2027
2088
 
2028
2089
  // A Lighter modify keeps the order type: ModifyOrderTxReq has no Type.
@@ -2068,6 +2129,41 @@ export const createLighterProvider = (
2068
2129
  return { knownPublicKey: local.apiKeyPublicKey }
2069
2130
  },
2070
2131
 
2132
+ /**
2133
+ * Attach the integer IMF that a leverage action stores, so the backend
2134
+ * signs the caller's exact native source and never a lossy conversion of
2135
+ * the leverage multiple. A placeOrder without leverage fields gets none.
2136
+ */
2137
+ async resolveActionRequest(
2138
+ action: ActionType,
2139
+ _address: Address,
2140
+ _signer: PerpsSigner,
2141
+ params?: ActionParamsMap[ActionType]
2142
+ ): Promise<ActionSignerContribution> {
2143
+ if (
2144
+ (action !== ActionType.UPDATE_LEVERAGE &&
2145
+ action !== ActionType.PLACE_ORDER) ||
2146
+ params === undefined ||
2147
+ !('leverage' in params || 'initialMarginFractionBps' in params)
2148
+ ) {
2149
+ return {}
2150
+ }
2151
+ const { leverage, initialMarginFractionBps } = params
2152
+ if (leverage === undefined && initialMarginFractionBps === undefined) {
2153
+ return {}
2154
+ }
2155
+ return {
2156
+ params: {
2157
+ initialMarginFractionBps: resolveRequestedImf(
2158
+ leverage === undefined
2159
+ ? undefined
2160
+ : numberToDecimalString(leverage),
2161
+ initialMarginFractionBps
2162
+ ),
2163
+ },
2164
+ }
2165
+ },
2166
+
2071
2167
  /**
2072
2168
  * Lighter's WASM signer computes the L2 tx hash before submission, so an
2073
2169
  * execute result carries it and resolves against this instance's explorer.
package/src/constants.ts CHANGED
@@ -183,14 +183,16 @@ export const DEFAULT_TRADES_LIMIT = 50
183
183
  export const LIGHTER_FEE_TICK_SCALE = 1_000_000
184
184
 
185
185
  /**
186
- * Decimal places a client may use for Lighter leverage. Lighter stores
187
- * leverage as an integer basis-point IMF (`round(10000 / leverage)`), and at
188
- * this precision a read-back leverage always re-saves the same IMF.
186
+ * Decimal places for leverage submission, order-compatible reads, and fill conversion.
187
+ * Native-IMF comparison does not use this limit.
189
188
  *
190
189
  * @public
191
190
  */
192
191
  export const LIGHTER_LEVERAGE_PRECISION = 2
193
192
 
193
+ /** Decimal places for Lighter position and market-setting display leverage. */
194
+ export const LIGHTER_LEVERAGE_DISPLAY_PRECISION = 1
195
+
194
196
  /**
195
197
  * Scale of the integer `initial_margin_fraction` values Lighter publishes on
196
198
  * trade rows and order-book details: the integer is a percent times 100, so
@@ -1,11 +1,17 @@
1
1
  import {
2
2
  absDecimalString,
3
3
  buildPosition,
4
+ calculateRoe as calculateMarginRoe,
5
+ calculateNotionalValue,
6
+ compareDecimalStrings,
4
7
  createWarnOnce,
5
8
  divideDecimalString,
6
9
  isDecimalString,
10
+ isDecimalStringGreaterThan,
7
11
  isDecimalStringZero,
8
12
  multiplyDecimalString,
13
+ remarkPositionValues,
14
+ roundDecimalString,
9
15
  safeDivideDecimalString,
10
16
  safeIsDecimalStringGreaterThan,
11
17
  safeNumberToDecimalString,
@@ -16,6 +22,7 @@ import type { PerpsMarketDisplay, Position } from '@lifi/perps-types'
16
22
  import { MarginMode, PositionSide } from '@lifi/perps-types'
17
23
  import {
18
24
  LIGHTER_IMF_PERCENT_SCALE,
25
+ LIGHTER_LEVERAGE_DISPLAY_PRECISION,
19
26
  LIGHTER_LEVERAGE_PRECISION,
20
27
  LIGHTER_PROVIDER_KEY,
21
28
  } from '../constants.js'
@@ -24,20 +31,23 @@ import { LT_MARGIN_MODE_ISOLATED } from '../types/index.js'
24
31
 
25
32
  /**
26
33
  * Display leverage from an IMF percent string: `100 / IMF` in exact decimal
27
- * arithmetic, rounded half-up to `LIGHTER_LEVERAGE_PRECISION`. The stored IMF
28
- * can make this differ from the requested leverage. Provider risk calculations
29
- * consume the original decimal IMF instead. `undefined` for a non-positive
34
+ * arithmetic, rounded half-up to the given precision (two decimals by default).
35
+ * The stored IMF can make this differ from the requested leverage.
36
+ * Returns `undefined` for a non-positive
30
37
  * or unparsable IMF.
31
38
  * @public
32
39
  */
33
- export const leverageFromImf = (imf: string): string | undefined => {
40
+ export const leverageFromImf = (
41
+ imf: string,
42
+ precision = LIGHTER_LEVERAGE_PRECISION
43
+ ): string | undefined => {
34
44
  if (safeIsDecimalStringGreaterThan(imf, '0') !== true) {
35
45
  return undefined
36
46
  }
37
47
  const leverage = safeDivideDecimalString('100', imf)
38
48
  return leverage === undefined
39
49
  ? undefined
40
- : safeRoundDecimalString(leverage, LIGHTER_LEVERAGE_PRECISION, 'round')
50
+ : safeRoundDecimalString(leverage, precision, 'round')
41
51
  }
42
52
 
43
53
  /**
@@ -55,6 +65,50 @@ export const leverageFromScaledImf = (imf: number): string | undefined => {
55
65
  return percent === undefined ? undefined : leverageFromImf(percent)
56
66
  }
57
67
 
68
+ /** ROE percentage from entry notional and the exact basis-point initial margin fraction. */
69
+ export const calculateRoe = (position: Position): string | undefined => {
70
+ const { unrealizedPnl, size, entryPrice, initialMarginFractionBps } = position
71
+ if (
72
+ !isDecimalString(unrealizedPnl) ||
73
+ !isDecimalString(size) ||
74
+ !isDecimalStringGreaterThan(size, '0') ||
75
+ !isDecimalString(entryPrice) ||
76
+ !isDecimalStringGreaterThan(entryPrice, '0') ||
77
+ !isDecimalString(initialMarginFractionBps) ||
78
+ !isDecimalStringGreaterThan(initialMarginFractionBps, '0') ||
79
+ compareDecimalStrings(
80
+ initialMarginFractionBps,
81
+ roundDecimalString(initialMarginFractionBps, 0, 'truncate')
82
+ ) !== 0
83
+ ) {
84
+ return undefined
85
+ }
86
+ const margin = divideDecimalString(
87
+ multiplyDecimalString(
88
+ calculateNotionalValue(size, entryPrice),
89
+ initialMarginFractionBps
90
+ ),
91
+ '10000'
92
+ )
93
+ return calculateMarginRoe(unrealizedPnl, margin)
94
+ }
95
+
96
+ /** Recalculate mark fields and ROE from the retained IMF without changing the input or snapshot margin. */
97
+ export const remarkPosition = (
98
+ position: Position,
99
+ markPrice: string
100
+ ): Position => {
101
+ const remarked = remarkPositionValues(position, markPrice)
102
+ if (remarked === position) {
103
+ return position
104
+ }
105
+ const roe = calculateRoe(remarked)
106
+ if (roe !== undefined) {
107
+ remarked.returnOnEquity = roe
108
+ }
109
+ return remarked
110
+ }
111
+
58
112
  const skipPosition = (
59
113
  marketId: string,
60
114
  field: string,
@@ -102,7 +156,9 @@ export const mapPosition = (
102
156
  warnUnreadableField('position_value', pos.position_value)
103
157
  }
104
158
  const imf = pos.initial_margin_fraction
105
- const leverage = isDecimalString(imf) ? leverageFromImf(imf) : undefined
159
+ const leverage = isDecimalString(imf)
160
+ ? leverageFromImf(imf, LIGHTER_LEVERAGE_DISPLAY_PRECISION)
161
+ : undefined
106
162
  if (leverage === undefined) {
107
163
  warnUnreadableField('initial_margin_fraction', imf)
108
164
  }
@@ -115,7 +171,7 @@ export const mapPosition = (
115
171
  ? !isDecimalStringZero(liquidationPrice)
116
172
  : liquidationPrice !== ''
117
173
 
118
- return buildPosition({
174
+ const mapped = buildPosition({
119
175
  market,
120
176
  side: pos.sign >= 0 ? PositionSide.LONG : PositionSide.SHORT,
121
177
  size,
@@ -132,7 +188,15 @@ export const mapPosition = (
132
188
  ...(hasLiquidationPrice ? { liquidationPrice } : {}),
133
189
  unrealizedPnl: pos.unrealized_pnl,
134
190
  accruedFunding: pos.total_funding_paid_out ?? '0',
135
- ...(leverage === undefined ? {} : { leverage }),
191
+ ...(leverage === undefined
192
+ ? {}
193
+ : {
194
+ leverage,
195
+ initialMarginFractionBps: multiplyDecimalString(
196
+ imf,
197
+ String(LIGHTER_IMF_PERCENT_SCALE)
198
+ ),
199
+ }),
136
200
  marginUsed: isIsolated
137
201
  ? pos.allocated_margin
138
202
  : (initialMarginRequirement ?? '0'),
@@ -141,4 +205,9 @@ export const mapPosition = (
141
205
  : { initialMarginRequirement }),
142
206
  marginMode: isIsolated ? MarginMode.ISOLATED : MarginMode.CROSS,
143
207
  })
208
+ const roe = calculateRoe(mapped)
209
+ if (roe !== undefined) {
210
+ mapped.returnOnEquity = roe
211
+ }
212
+ return mapped
144
213
  }