@lifi/perps-sdk-provider-ondo 3.0.0 → 5.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 (75) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/OndoProvider.d.ts.map +1 -1
  3. package/dist/cjs/OndoProvider.js +4 -2
  4. package/dist/cjs/OndoProvider.js.map +1 -1
  5. package/dist/cjs/auth/completeSiweLogin.d.ts +1 -5
  6. package/dist/cjs/auth/completeSiweLogin.d.ts.map +1 -1
  7. package/dist/cjs/auth/completeSiweLogin.js +1 -2
  8. package/dist/cjs/auth/completeSiweLogin.js.map +1 -1
  9. package/dist/cjs/auth/signActions.d.ts.map +1 -1
  10. package/dist/cjs/auth/signActions.js +5 -4
  11. package/dist/cjs/auth/signActions.js.map +1 -1
  12. package/dist/cjs/utils/index.d.ts +1 -0
  13. package/dist/cjs/utils/index.d.ts.map +1 -1
  14. package/dist/cjs/utils/index.js +3 -1
  15. package/dist/cjs/utils/index.js.map +1 -1
  16. package/dist/cjs/utils/mapPosition.d.ts +3 -3
  17. package/dist/cjs/utils/mapPosition.d.ts.map +1 -1
  18. package/dist/cjs/utils/mapPosition.js +1 -0
  19. package/dist/cjs/utils/mapPosition.js.map +1 -1
  20. package/dist/cjs/utils/transferMargin.d.ts +3 -0
  21. package/dist/cjs/utils/transferMargin.d.ts.map +1 -0
  22. package/dist/cjs/utils/transferMargin.js +7 -0
  23. package/dist/cjs/utils/transferMargin.js.map +1 -0
  24. package/dist/cjs/websocket/OndoWsProvider.d.ts +1 -0
  25. package/dist/cjs/websocket/OndoWsProvider.d.ts.map +1 -1
  26. package/dist/cjs/websocket/OndoWsProvider.js +13 -2
  27. package/dist/cjs/websocket/OndoWsProvider.js.map +1 -1
  28. package/dist/esm/OndoProvider.d.ts.map +1 -1
  29. package/dist/esm/OndoProvider.js +6 -4
  30. package/dist/esm/OndoProvider.js.map +1 -1
  31. package/dist/esm/auth/completeSiweLogin.d.ts +4 -9
  32. package/dist/esm/auth/completeSiweLogin.d.ts.map +1 -1
  33. package/dist/esm/auth/completeSiweLogin.js +4 -6
  34. package/dist/esm/auth/completeSiweLogin.js.map +1 -1
  35. package/dist/esm/auth/signActions.d.ts +3 -2
  36. package/dist/esm/auth/signActions.d.ts.map +1 -1
  37. package/dist/esm/auth/signActions.js +8 -6
  38. package/dist/esm/auth/signActions.js.map +1 -1
  39. package/dist/esm/utils/index.d.ts +1 -0
  40. package/dist/esm/utils/index.d.ts.map +1 -1
  41. package/dist/esm/utils/index.js +1 -0
  42. package/dist/esm/utils/index.js.map +1 -1
  43. package/dist/esm/utils/mapPosition.d.ts +3 -3
  44. package/dist/esm/utils/mapPosition.d.ts.map +1 -1
  45. package/dist/esm/utils/mapPosition.js +1 -0
  46. package/dist/esm/utils/mapPosition.js.map +1 -1
  47. package/dist/esm/utils/transferMargin.d.ts +10 -0
  48. package/dist/esm/utils/transferMargin.d.ts.map +1 -0
  49. package/dist/esm/utils/transferMargin.js +11 -0
  50. package/dist/esm/utils/transferMargin.js.map +1 -0
  51. package/dist/esm/websocket/OndoWsProvider.d.ts +1 -0
  52. package/dist/esm/websocket/OndoWsProvider.d.ts.map +1 -1
  53. package/dist/esm/websocket/OndoWsProvider.js +15 -4
  54. package/dist/esm/websocket/OndoWsProvider.js.map +1 -1
  55. package/dist/types/OndoProvider.d.ts.map +1 -1
  56. package/dist/types/auth/completeSiweLogin.d.ts +4 -9
  57. package/dist/types/auth/completeSiweLogin.d.ts.map +1 -1
  58. package/dist/types/auth/signActions.d.ts +3 -2
  59. package/dist/types/auth/signActions.d.ts.map +1 -1
  60. package/dist/types/utils/index.d.ts +1 -0
  61. package/dist/types/utils/index.d.ts.map +1 -1
  62. package/dist/types/utils/mapPosition.d.ts +3 -3
  63. package/dist/types/utils/mapPosition.d.ts.map +1 -1
  64. package/dist/types/utils/transferMargin.d.ts +10 -0
  65. package/dist/types/utils/transferMargin.d.ts.map +1 -0
  66. package/dist/types/websocket/OndoWsProvider.d.ts +1 -0
  67. package/dist/types/websocket/OndoWsProvider.d.ts.map +1 -1
  68. package/package.json +3 -3
  69. package/src/OndoProvider.ts +9 -2
  70. package/src/auth/completeSiweLogin.ts +5 -8
  71. package/src/auth/signActions.ts +8 -10
  72. package/src/utils/index.ts +1 -0
  73. package/src/utils/mapPosition.ts +4 -3
  74. package/src/utils/transferMargin.ts +12 -0
  75. package/src/websocket/OndoWsProvider.ts +21 -8
@@ -21,6 +21,7 @@ import {
21
21
  type SignActionsContext,
22
22
  type StorageAdapter,
23
23
  toMarketDisplay,
24
+ toPerpsMarketDisplay,
24
25
  } from '@lifi/perps-sdk'
25
26
  import type {
26
27
  AccountConfig,
@@ -36,6 +37,7 @@ import type {
36
37
  OndoAccountConfig,
37
38
  Order,
38
39
  OrdersResponse,
40
+ PerpsMarketDisplay,
39
41
  Position,
40
42
  PositionsResponse,
41
43
  ProviderAction,
@@ -92,6 +94,7 @@ import {
92
94
  mapOpenPositions,
93
95
  mapOrderDetail,
94
96
  ondoAsset,
97
+ positionMarginConstraints,
95
98
  } from './utils/index.js'
96
99
 
97
100
  /**
@@ -166,6 +169,8 @@ export const ondoProvider = (
166
169
 
167
170
  const requireMarketDisplay = (marketId: string): MarketDisplay =>
168
171
  toMarketDisplay(marketRegistry().require(marketId))
172
+ const requirePerpsMarketDisplay = (marketId: string): PerpsMarketDisplay =>
173
+ toPerpsMarketDisplay(marketRegistry().require(marketId))
169
174
 
170
175
  const emptyConfig: OndoAccountConfig = {
171
176
  provider: ONDO_PROVIDER_KEY,
@@ -257,7 +262,7 @@ export const ondoProvider = (
257
262
 
258
263
  const positions: Position[] = mapOpenPositions(
259
264
  rawPositions,
260
- requireMarketDisplay
265
+ requirePerpsMarketDisplay
261
266
  )
262
267
 
263
268
  // Single USD collateral row: `walletBalance` is gross (locked margin
@@ -363,7 +368,7 @@ export const ondoProvider = (
363
368
 
364
369
  let positions: Position[] = mapOpenPositions(
365
370
  rawPositions,
366
- requireMarketDisplay
371
+ requirePerpsMarketDisplay
367
372
  )
368
373
  if (params.marketId !== undefined) {
369
374
  positions = positions.filter((p) => p.market.id === params.marketId)
@@ -652,6 +657,8 @@ export const ondoProvider = (
652
657
 
653
658
  estimateLiquidationPrice,
654
659
 
660
+ positionMarginConstraints,
661
+
655
662
  projectConfig(
656
663
  config: AccountConfig,
657
664
  setup: ProviderAction[],
@@ -1,5 +1,4 @@
1
1
  import type { PerpsClientSigner } from '@lifi/perps-sdk'
2
- import type { Hex } from 'viem'
3
2
  import type { OndoAuthToken } from '../types/auth.js'
4
3
  import type { OndoApiClient } from '../utils/apiClient.js'
5
4
 
@@ -16,10 +15,9 @@ export interface OndoSiweChallenge {
16
15
 
17
16
  /**
18
17
  * Sign a SIWE challenge with the user's wallet and exchange it for an Ondo
19
- * session token, directly against Ondo — the returned JWT never transits the
20
- * LI.FI backend. viem's `signMessage` performs the EIP-191 `personal_sign`
21
- * encoding Ondo verifies against. The signature is returned alongside the
22
- * token so callers can hand it back as a `SiweSignedActionStep`.
18
+ * session token, directly against Ondo — neither the signature nor the
19
+ * returned JWT transits the LI.FI backend. viem's `signMessage` performs the
20
+ * EIP-191 `personal_sign` encoding Ondo verifies against.
23
21
  *
24
22
  * @public
25
23
  */
@@ -27,17 +25,16 @@ export async function completeSiweLogin(
27
25
  client: OndoApiClient,
28
26
  signer: PerpsClientSigner,
29
27
  challenge: OndoSiweChallenge
30
- ): Promise<{ token: OndoAuthToken; signature: Hex }> {
28
+ ): Promise<OndoAuthToken> {
31
29
  const signature = await signer.signMessage({
32
30
  account: signer.account,
33
31
  message: challenge.message,
34
32
  })
35
- const token = await client.post<OndoAuthToken>(
33
+ return client.post<OndoAuthToken>(
36
34
  '/v1/auth/erc-4361/login/complete_challenge',
37
35
  {
38
36
  id: challenge.id,
39
37
  signature,
40
38
  }
41
39
  )
42
- return { token, signature }
43
40
  }
@@ -128,8 +128,9 @@ async function executeSessionRequest(
128
128
  * Ondo's `signActions` arms.
129
129
  *
130
130
  * `SIWE` signs the backend-built ERC-4361 challenge with the user's wallet and
131
- * completes the login directly against Ondo — the returned session JWT is
132
- * persisted in the token store and never transits the LI.FI backend.
131
+ * completes the login directly against Ondo. The session JWT is persisted in
132
+ * the token store and no signed step is returned, so neither the JWT nor the
133
+ * wallet signature transits the LI.FI backend.
133
134
  *
134
135
  * `SESSION` executes client-only setup steps directly against the venue with
135
136
  * the stored session token. A backend-authored request-bearing step is
@@ -160,7 +161,6 @@ export async function ondoSignActions(
160
161
  'creating the perps client.'
161
162
  )
162
163
  }
163
- const signed: SignedActionStep[] = []
164
164
  for (const step of steps) {
165
165
  if (!isSiweStep(step)) {
166
166
  throw new PerpsError(
@@ -168,15 +168,13 @@ export async function ondoSignActions(
168
168
  `Ondo received a non-SIWE step ('${step.action}') under the SIWE signing method.`
169
169
  )
170
170
  }
171
- const { token, signature } = await completeSiweLogin(
172
- deps.client,
173
- userWallet,
174
- { id: step.siwe.challengeId, message: step.siwe.message }
175
- )
171
+ const token = await completeSiweLogin(deps.client, userWallet, {
172
+ id: step.siwe.challengeId,
173
+ message: step.siwe.message,
174
+ })
176
175
  await deps.tokenStore.set(address, token)
177
- signed.push({ action: step.action, siwe: step.siwe, signature })
178
176
  }
179
- return signed
177
+ return []
180
178
  }
181
179
 
182
180
  case SigningMethod.HMAC: {
@@ -41,3 +41,4 @@ export { mapOpenPositions, mapPosition } from './mapPosition.js'
41
41
  export { intervalFromBarSpan, mapInterval } from './ohlcvInterval.js'
42
42
  export { ondoAsset } from './ondoAsset.js'
43
43
  export { formatOrderPrice, formatOrderSize } from './orderFormatting.js'
44
+ export { positionMarginConstraints } from './transferMargin.js'
@@ -1,4 +1,4 @@
1
- import type { MarketDisplay, Position } from '@lifi/perps-types'
1
+ import type { PerpsMarketDisplay, Position } from '@lifi/perps-types'
2
2
  import { MarginMode, PositionSide } from '@lifi/perps-types'
3
3
  import Big from 'big.js'
4
4
  import type { OndoPosition } from '../types/wire.js'
@@ -12,7 +12,7 @@ import type { OndoPosition } from '../types/wire.js'
12
12
  */
13
13
  export const mapPosition = (
14
14
  pos: OndoPosition,
15
- market: MarketDisplay
15
+ market: PerpsMarketDisplay
16
16
  ): Position => ({
17
17
  market,
18
18
  side: pos.direction === 'short' ? PositionSide.SHORT : PositionSide.LONG,
@@ -23,6 +23,7 @@ export const mapPosition = (
23
23
  unrealizedPnl: pos.unrealizedPnl,
24
24
  leverage: Number.parseFloat(pos.leverage),
25
25
  marginUsed: pos.usedMargin,
26
+ initialMarginRequirement: pos.usedMargin,
26
27
  marginMode: MarginMode.CROSS,
27
28
  })
28
29
 
@@ -35,7 +36,7 @@ export const mapPosition = (
35
36
  */
36
37
  export const mapOpenPositions = (
37
38
  positions: OndoPosition[],
38
- resolveMarket: (market: string) => MarketDisplay
39
+ resolveMarket: (market: string) => PerpsMarketDisplay
39
40
  ): Position[] =>
40
41
  positions
41
42
  .filter(
@@ -0,0 +1,12 @@
1
+ import type { Position } from '@lifi/perps-types'
2
+
3
+ /**
4
+ * Ondo uses cross margin exclusively, so it exposes no individual position
5
+ * margin adjustment.
6
+ *
7
+ * @see https://docs.ondo.finance/ondo-global-markets/perpetuals/trading/margin
8
+ * @public
9
+ */
10
+ export function positionMarginConstraints(_position: Position): undefined {
11
+ return undefined
12
+ }
@@ -9,6 +9,7 @@ import {
9
9
  resolveSubscribeQuote,
10
10
  type StorageAdapter,
11
11
  toMarketDisplay,
12
+ toPerpsMarketDisplay,
12
13
  WsProviderBase,
13
14
  type WsProviderFactory,
14
15
  wsLog,
@@ -16,6 +17,7 @@ import {
16
17
  import type {
17
18
  MarketContext,
18
19
  MarketDisplay,
20
+ PerpsMarketDisplay,
19
21
  Subscription,
20
22
  } from '@lifi/perps-types'
21
23
  import type { Address } from 'viem'
@@ -42,7 +44,7 @@ import { OndoSessionExpiredError } from '../utils/apiClient.js'
42
44
  import {
43
45
  classifyAndMapOrders,
44
46
  mapFill,
45
- mapOpenPositions,
47
+ mapPosition,
46
48
  OndoApiClient,
47
49
  } from '../utils/index.js'
48
50
  import { intervalFromBarSpan, mapInterval } from '../utils/ohlcvInterval.js'
@@ -744,6 +746,13 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
744
746
  return known && toMarketDisplay(known)
745
747
  }
746
748
 
749
+ private resolvePerpsMarket = (
750
+ market: string
751
+ ): PerpsMarketDisplay | undefined => {
752
+ const known = this.registry?.get(market)
753
+ return known && toPerpsMarketDisplay(known)
754
+ }
755
+
747
756
  private handleOrders(orders: OndoOrder[]): void {
748
757
  const address = this.accountAddress
749
758
  if (address === undefined) {
@@ -780,15 +789,19 @@ export class OndoWsProvider extends WsProviderBase<SubState> {
780
789
  if (address === undefined) {
781
790
  return
782
791
  }
783
- const known = positions.filter(
784
- (position) => this.resolveMarket(position.market) !== undefined
785
- )
792
+ const mapped = positions.flatMap((position) => {
793
+ if (
794
+ position.direction === 'neutral' ||
795
+ Number.parseFloat(position.netQuantity) === 0
796
+ ) {
797
+ return []
798
+ }
799
+ const market = this.resolvePerpsMarket(position.market)
800
+ return market === undefined ? [] : [mapPosition(position, market)]
801
+ })
786
802
  this.emit(`positions:${address}`, {
787
803
  channel: 'positions',
788
- data: mapOpenPositions(
789
- known,
790
- (market) => this.resolveMarket(market) as MarketDisplay
791
- ),
804
+ data: mapped,
792
805
  })
793
806
  if (this.accountSummary !== undefined) {
794
807
  let marginUsed = 0