@lifi/perps-sdk-provider-lighter 28.0.0 → 29.0.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.
Files changed (33) hide show
  1. package/dist/cjs/LighterProvider.d.ts.map +1 -1
  2. package/dist/cjs/LighterProvider.js +18 -5
  3. package/dist/cjs/LighterProvider.js.map +1 -1
  4. package/dist/cjs/accountSummary.d.ts.map +1 -1
  5. package/dist/cjs/accountSummary.js +6 -3
  6. package/dist/cjs/accountSummary.js.map +1 -1
  7. package/dist/cjs/availableToTrade.d.ts +3 -0
  8. package/dist/cjs/availableToTrade.d.ts.map +1 -0
  9. package/dist/cjs/availableToTrade.js +38 -0
  10. package/dist/cjs/availableToTrade.js.map +1 -0
  11. package/dist/cjs/websocket/LighterWsProvider.js +5 -5
  12. package/dist/cjs/websocket/LighterWsProvider.js.map +1 -1
  13. package/dist/esm/LighterProvider.d.ts.map +1 -1
  14. package/dist/esm/LighterProvider.js +22 -5
  15. package/dist/esm/LighterProvider.js.map +1 -1
  16. package/dist/esm/accountSummary.d.ts.map +1 -1
  17. package/dist/esm/accountSummary.js +6 -3
  18. package/dist/esm/accountSummary.js.map +1 -1
  19. package/dist/esm/availableToTrade.d.ts +15 -0
  20. package/dist/esm/availableToTrade.d.ts.map +1 -0
  21. package/dist/esm/availableToTrade.js +45 -0
  22. package/dist/esm/availableToTrade.js.map +1 -0
  23. package/dist/esm/websocket/LighterWsProvider.js +5 -5
  24. package/dist/esm/websocket/LighterWsProvider.js.map +1 -1
  25. package/dist/types/LighterProvider.d.ts.map +1 -1
  26. package/dist/types/accountSummary.d.ts.map +1 -1
  27. package/dist/types/availableToTrade.d.ts +15 -0
  28. package/dist/types/availableToTrade.d.ts.map +1 -0
  29. package/package.json +2 -2
  30. package/src/LighterProvider.ts +30 -7
  31. package/src/accountSummary.ts +8 -3
  32. package/src/availableToTrade.ts +61 -0
  33. package/src/websocket/LighterWsProvider.ts +5 -5
@@ -0,0 +1,61 @@
1
+ import { toAssetDisplay } from '@lifi/perps-sdk'
2
+ import type { AvailableToTrade, Market, Position } from '@lifi/perps-types'
3
+ import { MarginMode, PositionSide } from '@lifi/perps-types'
4
+ import Big from 'big.js'
5
+ import { toRequiredBig } from './utils/decimal.js'
6
+
7
+ const atLeastZero = (value: Big): Big => (value.lt(0) ? new Big(0) : value)
8
+
9
+ // Lighter's `available_balance` already nets cross unrealized PnL and counts
10
+ // an isolated position's equity above its IMR, so only the rest is released.
11
+ const releasedOnClose = (position: Position): Big => {
12
+ const marginUsed = toRequiredBig(position.marginUsed, 'marginUsed')
13
+ if (position.marginMode !== MarginMode.ISOLATED) {
14
+ return marginUsed
15
+ }
16
+ const equity = marginUsed.plus(
17
+ toRequiredBig(position.unrealizedPnl, 'unrealizedPnl')
18
+ )
19
+ const imr = toRequiredBig(
20
+ position.initialMarginRequirement,
21
+ 'initialMarginRequirement'
22
+ )
23
+ return equity.lt(imr) ? equity : imr
24
+ }
25
+
26
+ /**
27
+ * The amounts the account can still buy and sell on one Lighter perps
28
+ * market. The side that adds to an open position on `market` gets
29
+ * `availableMargin`. The side that reduces or flips it gets the position's
30
+ * initial margin requirement, which the closing part of the order consumes,
31
+ * plus `availableMargin` and the margin that closing releases, floored at 0.
32
+ * A deficit therefore never cuts the closing part. Both amounts are at least 0.
33
+ *
34
+ * @param availableMargin - The account's {@link AccountSummary.availableMargin}.
35
+ * @throws {PerpsError} `SDKError` when a decimal the formula reads is malformed.
36
+ * @internal
37
+ */
38
+ export const lighterAvailableToTrade = (
39
+ market: Market,
40
+ availableMargin: string,
41
+ positions: readonly Position[]
42
+ ): AvailableToTrade => {
43
+ const available = toRequiredBig(availableMargin, 'availableMargin')
44
+ const adding = atLeastZero(available)
45
+ const position = positions.find((p) => p.market.id === market.id)
46
+ const reducing =
47
+ position === undefined
48
+ ? adding
49
+ : toRequiredBig(
50
+ position.initialMarginRequirement,
51
+ 'initialMarginRequirement'
52
+ ).plus(atLeastZero(available.plus(releasedOnClose(position))))
53
+ const isShort = position?.side === PositionSide.SHORT
54
+ return {
55
+ providerId: market.providerId,
56
+ marketId: market.id,
57
+ asset: toAssetDisplay(market.quoteAsset),
58
+ buy: (isShort ? reducing : adding).toFixed(),
59
+ sell: (isShort ? adding : reducing).toFixed(),
60
+ }
61
+ }
@@ -660,10 +660,10 @@ export class LighterWsProvider extends WsProviderBase<SubState> {
660
660
  this.emit(`accountSummary:${address}`, {
661
661
  channel: 'accountSummary',
662
662
  data: {
663
- portfolioValue: portfolio.toString(),
664
- availableMargin: available.toString(),
665
- marginUsed: marginUsed.toString(),
666
- unrealizedPnl: portfolio.minus(collateral).toString(),
663
+ portfolioValue: portfolio.toFixed(),
664
+ availableMargin: available.toFixed(),
665
+ marginUsed: marginUsed.toFixed(),
666
+ unrealizedPnl: portfolio.minus(collateral).toFixed(),
667
667
  },
668
668
  })
669
669
  }
@@ -1008,5 +1008,5 @@ export const lighterWsProvider = (
1008
1008
  Object.assign(
1009
1009
  ({ provider, wsUrl, client }: WsProviderFactoryParams) =>
1010
1010
  new LighterWsProvider(wsUrl, provider, options, client),
1011
- { streamsCandles: false }
1011
+ { streamsCandles: false, streamsAvailableToTrade: false }
1012
1012
  )