@lifi/perps-sdk 18.0.0 → 19.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 (67) hide show
  1. package/README.md +12 -0
  2. package/dist/cjs/client/PerpsClient.d.ts +15 -8
  3. package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
  4. package/dist/cjs/client/PerpsClient.js +145 -52
  5. package/dist/cjs/client/PerpsClient.js.map +1 -1
  6. package/dist/cjs/index.d.ts +4 -4
  7. package/dist/cjs/index.d.ts.map +1 -1
  8. package/dist/cjs/index.js +2 -1
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/types/api.d.ts +8 -2
  11. package/dist/cjs/types/api.d.ts.map +1 -1
  12. package/dist/cjs/types/provider.d.ts +9 -6
  13. package/dist/cjs/types/provider.d.ts.map +1 -1
  14. package/dist/cjs/types/withdrawal.d.ts +11 -1
  15. package/dist/cjs/types/withdrawal.d.ts.map +1 -1
  16. package/dist/cjs/utils/setupActions.d.ts +4 -2
  17. package/dist/cjs/utils/setupActions.d.ts.map +1 -1
  18. package/dist/cjs/utils/setupActions.js +9 -1
  19. package/dist/cjs/utils/setupActions.js.map +1 -1
  20. package/dist/cjs/utils/switchChain.js +1 -1
  21. package/dist/cjs/utils/switchChain.js.map +1 -1
  22. package/dist/cjs/version.d.ts +1 -1
  23. package/dist/cjs/version.js +1 -1
  24. package/dist/esm/client/PerpsClient.d.ts +74 -55
  25. package/dist/esm/client/PerpsClient.d.ts.map +1 -1
  26. package/dist/esm/client/PerpsClient.js +218 -100
  27. package/dist/esm/client/PerpsClient.js.map +1 -1
  28. package/dist/esm/index.d.ts +4 -4
  29. package/dist/esm/index.d.ts.map +1 -1
  30. package/dist/esm/index.js +1 -1
  31. package/dist/esm/index.js.map +1 -1
  32. package/dist/esm/types/api.d.ts +40 -14
  33. package/dist/esm/types/api.d.ts.map +1 -1
  34. package/dist/esm/types/provider.d.ts +39 -28
  35. package/dist/esm/types/provider.d.ts.map +1 -1
  36. package/dist/esm/types/withdrawal.d.ts +30 -1
  37. package/dist/esm/types/withdrawal.d.ts.map +1 -1
  38. package/dist/esm/utils/setupActions.d.ts +17 -8
  39. package/dist/esm/utils/setupActions.d.ts.map +1 -1
  40. package/dist/esm/utils/setupActions.js +20 -7
  41. package/dist/esm/utils/setupActions.js.map +1 -1
  42. package/dist/esm/utils/switchChain.js +1 -1
  43. package/dist/esm/utils/switchChain.js.map +1 -1
  44. package/dist/esm/version.d.ts +1 -1
  45. package/dist/esm/version.js +1 -1
  46. package/dist/types/client/PerpsClient.d.ts +74 -55
  47. package/dist/types/client/PerpsClient.d.ts.map +1 -1
  48. package/dist/types/index.d.ts +4 -4
  49. package/dist/types/index.d.ts.map +1 -1
  50. package/dist/types/types/api.d.ts +40 -14
  51. package/dist/types/types/api.d.ts.map +1 -1
  52. package/dist/types/types/provider.d.ts +39 -28
  53. package/dist/types/types/provider.d.ts.map +1 -1
  54. package/dist/types/types/withdrawal.d.ts +30 -1
  55. package/dist/types/types/withdrawal.d.ts.map +1 -1
  56. package/dist/types/utils/setupActions.d.ts +17 -8
  57. package/dist/types/utils/setupActions.d.ts.map +1 -1
  58. package/dist/types/version.d.ts +1 -1
  59. package/package.json +2 -2
  60. package/src/client/PerpsClient.ts +303 -130
  61. package/src/index.ts +9 -1
  62. package/src/types/api.ts +42 -14
  63. package/src/types/provider.ts +44 -30
  64. package/src/types/withdrawal.ts +33 -1
  65. package/src/utils/setupActions.ts +27 -13
  66. package/src/utils/switchChain.ts +1 -1
  67. package/src/version.ts +1 -1
package/src/index.ts CHANGED
@@ -118,6 +118,7 @@ export type {
118
118
  GetPortfolioHistoryParams,
119
119
  GetSetupParams,
120
120
  GetWithdrawableBalancesParams,
121
+ GetWithdrawFlowParams,
121
122
  ModifyOrdersParams,
122
123
  PerpsClientOptions,
123
124
  PerpsConfig,
@@ -166,6 +167,7 @@ export type {
166
167
  ProviderGetPositionsParams,
167
168
  ProviderGetQuoteParams,
168
169
  ProviderGetWithdrawableBalancesParams,
170
+ ProviderGetWithdrawFlowParams,
169
171
  QuoteListener,
170
172
  SignActionProgress,
171
173
  SignActionsContext,
@@ -174,6 +176,9 @@ export type {
174
176
  ProviderWithdrawableBalance,
175
177
  WithdrawableBalance,
176
178
  WithdrawalRoute,
179
+ WithdrawFlow,
180
+ WithdrawFlowReady,
181
+ WithdrawFlowSetupRequired,
177
182
  } from './types/withdrawal.js'
178
183
  export { paginateActivity } from './utils/activityPaging.js'
179
184
  export type { ExpectedPnl } from './utils/calculations.js'
@@ -247,7 +252,10 @@ export {
247
252
  } from './utils/positionMath.js'
248
253
  export type { ScaleToIntegerPolicy } from './utils/scaleToInteger.js'
249
254
  export { scaleToInteger } from './utils/scaleToInteger.js'
250
- export { selectUserSetupActions } from './utils/setupActions.js'
255
+ export {
256
+ isSetupOptionFor,
257
+ selectUserSetupActions,
258
+ } from './utils/setupActions.js'
251
259
  export {
252
260
  signTypedData,
253
261
  signTypedDataWithSigner,
package/src/types/api.ts CHANGED
@@ -15,7 +15,8 @@ import type {
15
15
  OrderSide,
16
16
  OrderType,
17
17
  PortfolioHistoryRange,
18
- ProviderAction,
18
+ SetupAction,
19
+ SetupOption,
19
20
  SignedActionStep,
20
21
  TimeInForce,
21
22
  TriggerOrderInput,
@@ -71,8 +72,8 @@ export interface PerpsConfig {
71
72
  */
72
73
  providers?: PerpsProviderPlugin[] | ProviderConfigs
73
74
  /**
74
- * The end-user's wallet, used whenever an action's descriptor names the user
75
- * wallet in its `signers` list. Accepts any viem-compatible WalletClient:
75
+ * The end-user's wallet, used whenever an action's descriptor names
76
+ * `PerpsSigner.USER` as its `signer`. Accepts any viem-compatible WalletClient:
76
77
  * - Browser wallet: wagmi's useWalletClient() result
77
78
  * - Private key: createWalletClient({ account: privateKeyToAccount('0x...'), transport: http() })
78
79
  * - Mnemonic: createWalletClient({ account: mnemonicToAccount('word1 ...'), transport: http() })
@@ -289,6 +290,16 @@ export interface GetDepositFlowParams {
289
290
  address: Address
290
291
  }
291
292
 
293
+ /**
294
+ * Parameters for {@link PerpsClient.getWithdrawFlow}.
295
+ *
296
+ * @public
297
+ */
298
+ export interface GetWithdrawFlowParams {
299
+ provider: string
300
+ address: Address
301
+ }
302
+
292
303
  /**
293
304
  * Parameters for {@link PerpsClient.getWithdrawableBalances}.
294
305
  *
@@ -318,9 +329,21 @@ export interface GetPortfolioHistoryParams {
318
329
  */
319
330
  export interface SetupChecklistItem {
320
331
  /** The `Provider.setup` descriptor for this step. */
321
- descriptor: ProviderAction
332
+ descriptor: SetupAction
322
333
  /** Whether the step is already satisfied for this account. */
323
334
  satisfied: boolean
335
+ /**
336
+ * For a choice step, the option whose bound params match the projected
337
+ * account value. `null` when none matches or the step is not a choice.
338
+ */
339
+ selected: SetupOption | null
340
+ /**
341
+ * For a choice step, the account's current value as the venue names it
342
+ * (the raw projected value, e.g. `standard` or `dexAbstraction`), even when
343
+ * no option binds it. `null` when the venue reports none or the value is
344
+ * unreadable, and always `null` for a non-choice step.
345
+ */
346
+ currentValue: string | null
324
347
  }
325
348
 
326
349
  /**
@@ -331,8 +354,10 @@ export interface SetupChecklistItem {
331
354
  * to the provider's `setup` descriptor, which declares the step's signer and
332
355
  * scheme), so no signer-role partition is exposed here.
333
356
  *
334
- * `Provider.options` items are NEVER included here — they don't gate trading
335
- * and are surfaced separately via `getAccount().settings`.
357
+ * A non-choice step with `signer: SDK` is NEVER included here — the SDK
358
+ * drains it itself. A choice step is listed on `checklist` but never staged or
359
+ * executed: the user picks an option through
360
+ * `PerpsClient.executeProviderOption`.
336
361
  *
337
362
  * @public
338
363
  */
@@ -346,12 +371,15 @@ export interface ProviderSetup {
346
371
  accountExists: boolean
347
372
  /** Unsatisfied setup steps, ordered by descriptor `sequence`. */
348
373
  setup: ActionStep[]
349
- /** Whether all setup items are already satisfied (ready to trade) */
374
+ /**
375
+ * `true` only when nothing is staged and every choice on `checklist` is
376
+ * satisfied: the account is ready to trade.
377
+ */
350
378
  isReady: boolean
351
379
  /**
352
- * The renderable onboarding list: every USER-signed setup descriptor with
353
- * its satisfied state, ordered by `sequence`. SDK-internal steps and
354
- * conditional steps that staged no work for this account (see
380
+ * The renderable onboarding list: every choice step and every `USER`-signed
381
+ * setup step with its satisfied state, ordered by `sequence`. SDK-drained
382
+ * steps and conditional steps that staged no work for this account (see
355
383
  * `PerpsProviderPlugin.conditionalSetupActions`) are omitted. Consumers
356
384
  * render this list directly instead of joining `Provider.setup` metadata
357
385
  * with the staged `setup` steps.
@@ -383,14 +411,14 @@ export interface ExecuteProviderSetupResult {
383
411
  * backend's `AccountResponse` with a single SDK-projected `settings` array.
384
412
  *
385
413
  * `settings` contains exactly one `AccountConfigSetting` per descriptor on
386
- * `Provider.setup` + `Provider.options` (in that order). Index the
387
- * projection by `setting.type === descriptor.type` and read
388
- * `setting.values[i].value` for each `Param` the descriptor declared.
414
+ * `Provider.setup`, in that order. Index the projection by
415
+ * `setting.type === descriptor.type` and read `setting.values[i].value` for
416
+ * each `Param` the descriptor declared.
389
417
  *
390
418
  * @public
391
419
  */
392
420
  export interface GetAccountResult extends AccountResponse {
393
- /** SDK-projected current state of every setup + options descriptor. */
421
+ /** SDK-projected current state of every `Provider.setup` descriptor. */
394
422
  settings: AccountConfigSetting[]
395
423
  }
396
424
 
@@ -22,9 +22,9 @@ import type {
22
22
  PortfolioHistoryResponse,
23
23
  Position,
24
24
  PositionsResponse,
25
- ProviderAction,
26
25
  Quote,
27
26
  QuoteSide,
27
+ SetupAction,
28
28
  SignedActionStep,
29
29
  SigningMethod,
30
30
  TradeType,
@@ -36,7 +36,7 @@ import type {
36
36
  SDKRequestOptions,
37
37
  } from './config.js'
38
38
  import type { DepositFlow } from './deposit.js'
39
- import type { ProviderWithdrawableBalance } from './withdrawal.js'
39
+ import type { ProviderWithdrawableBalance, WithdrawFlow } from './withdrawal.js'
40
40
 
41
41
  /**
42
42
  * Low-level SDK client: resolved config, the optional end-user wallet, and the
@@ -86,12 +86,12 @@ export interface SignActionsContext {
86
86
  /** End-user wallet used for USER-signed or EVM transaction legs. */
87
87
  userWallet?: PerpsClientSigner
88
88
  /**
89
- * The signing batch's declared signers, from the action's `ProviderAction`
89
+ * The signing batch's declared signer, from the action's `ProviderAction`
90
90
  * descriptor. The plugin branches on this to pick WHO signs (e.g.
91
- * Hyperliquid: `USER` → end-user wallet, `AGENT` → session keypair). Core
92
- * forwards the descriptor's `signers` as data; it does not branch on them.
91
+ * Hyperliquid: `USER` → end-user wallet, `SDK` → session keypair). Core
92
+ * forwards the descriptor's `signer` as data; it does not branch on it.
93
93
  */
94
- signers?: PerpsSigner[]
94
+ signer?: PerpsSigner
95
95
  /**
96
96
  * Switch `userWallet` to `chainId` and resolve the client to broadcast with.
97
97
  * Bound by core to the consumer's `switchChain` hook and the resolved
@@ -180,6 +180,15 @@ export interface ProviderGetDepositFlowParams {
180
180
  address: Address
181
181
  }
182
182
 
183
+ /**
184
+ * Read params for {@link PerpsProviderPlugin.getWithdrawFlow}.
185
+ *
186
+ * @public
187
+ */
188
+ export interface ProviderGetWithdrawFlowParams {
189
+ address: Address
190
+ }
191
+
183
192
  /**
184
193
  * Read params for {@link PerpsProviderPlugin.getWithdrawableBalances}.
185
194
  *
@@ -361,23 +370,12 @@ export interface PerpsProviderPlugin {
361
370
  readonly type: string
362
371
 
363
372
  /**
364
- * Setup actions the provider completes on its own, without surfacing a card
365
- * to the caller. `PerpsClient.checkSetup` drains each such pending step in
366
- * place — building, signing, and executing it with the provider's own
367
- * credentials — and omits it from the returned `setup` list. A descriptor
368
- * whose `signers` include {@link PerpsSigner.USER} is never treated as
369
- * internal, even when named here. Omit when the provider has no
370
- * self-completed setup steps.
371
- */
372
- readonly internalSetupActions?: readonly ActionType[]
373
-
374
- /**
375
- * Setup actions that stage work only when the account needs remediation
376
- * (e.g. Hyperliquid's REVOKE_AGENT frees an agent slot only when every
377
- * named slot is taken). When such a step stages no actions,
373
+ * Non-choice setup actions that stage work only when the account needs
374
+ * remediation (e.g. Hyperliquid's REVOKE_AGENT frees an agent slot only
375
+ * when every named slot is taken). When such a step stages no actions,
378
376
  * `PerpsClient.checkSetup` omits it from `ProviderSetup.checklist` instead
379
- * of rendering it as satisfied. Omit when every setup step is
380
- * unconditional.
377
+ * of rendering it as satisfied. Choices are always rendered because they
378
+ * are never staged. Omit when every setup step is unconditional.
381
379
  */
382
380
  readonly conditionalSetupActions?: readonly ActionType[]
383
381
 
@@ -421,6 +419,19 @@ export interface PerpsProviderPlugin {
421
419
  options?: SDKRequestOptions
422
420
  ): Promise<DepositFlow>
423
421
 
422
+ /**
423
+ * The withdrawal flow this venue offers `params.address`, resolved from the
424
+ * venue's own account and setup state — the destination a withdrawal goes
425
+ * to, or the venue setup that must come first.
426
+ *
427
+ * Optional: a provider without a gated withdrawal destination omits it, and
428
+ * `PerpsClient.getWithdrawFlow` then resolves `undefined`.
429
+ */
430
+ getWithdrawFlow?(
431
+ params: ProviderGetWithdrawFlowParams,
432
+ options?: SDKRequestOptions
433
+ ): Promise<WithdrawFlow>
434
+
424
435
  /**
425
436
  * The `(asset, route)` pairs `params.address` currently has something to
426
437
  * withdraw from, keyed by provider-native asset id. Only the venue knows how
@@ -576,9 +587,12 @@ export interface PerpsProviderPlugin {
576
587
 
577
588
  /**
578
589
  * Project a typed {@link AccountConfig} against the provider's `setup`
579
- * + `options` descriptors into `AccountConfigSetting[]`. Used by
590
+ * descriptors into `AccountConfigSetting[]`. Used by
580
591
  * `PerpsClient.getAccount` to attach a `settings` array to the response —
581
- * one entry per descriptor, in `setup`-then-`options` order.
592
+ * one entry per descriptor, in `setup` order. Every choice descriptor
593
+ * (`options` is not `null`) carries a boolean `satisfied`, because
594
+ * `PerpsClient.checkSetup` reads a choice's satisfied state and `selected`
595
+ * option from this projection alone.
582
596
  *
583
597
  * Implementations receive the union-typed `AccountConfig` and narrow on
584
598
  * `config.provider` themselves; the dispatcher in `PerpsClient` does not
@@ -586,8 +600,7 @@ export interface PerpsProviderPlugin {
586
600
  */
587
601
  projectConfig(
588
602
  config: AccountConfig,
589
- setup: ProviderAction[],
590
- options: ProviderAction[]
603
+ setup: SetupAction[]
591
604
  ): AccountConfigSetting[]
592
605
 
593
606
  /**
@@ -595,7 +608,8 @@ export interface PerpsProviderPlugin {
595
608
  * when staging the provider setup. Used for plugin-side state the backend
596
609
  * needs to make a correct idempotency decision (e.g. Lighter's known local
597
610
  * API public key). Returns an empty object when the plugin has no params
598
- * to contribute for the action. Optional — providers without local state
611
+ * to contribute for the action. A choice option runs with its bound params
612
+ * alone and never reads these. Optional — providers without local state
599
613
  * can omit it entirely.
600
614
  */
601
615
  resolveSetupParams?(
@@ -606,7 +620,7 @@ export interface PerpsProviderPlugin {
606
620
  /**
607
621
  * Contribute the signer-bearing wire fields for an action — the plugin owns
608
622
  * signer identity (WHO signs), so core asks the plugin rather than resolving
609
- * a `signerAddress` itself. The descriptor's `signers` are forwarded so the
623
+ * a `signerAddress` itself. The descriptor's `signer` is forwarded so the
610
624
  * plugin can branch on signer role: a provider that signs on the user's
611
625
  * behalf with a session keypair (Hyperliquid's agent) resolves — provisioning
612
626
  * one if needed — and returns the agent address as `signerAddress`; for
@@ -622,7 +636,7 @@ export interface PerpsProviderPlugin {
622
636
  resolveActionRequest?(
623
637
  action: ActionType,
624
638
  address: Address,
625
- signers: PerpsSigner[]
639
+ signer: PerpsSigner
626
640
  ): Promise<ActionSignerContribution>
627
641
 
628
642
  /**
@@ -637,7 +651,7 @@ export interface PerpsProviderPlugin {
637
651
  * provider has no `signActions`.
638
652
  *
639
653
  * `method` mirrors the descriptor's `signingMethod`. The plugin owns every
640
- * arm, branching on the descriptor's `signers` internally: the EIP712 arm
654
+ * arm, branching on the descriptor's `signer` internally: the EIP712 arm
641
655
  * signs with the user's wallet (read from `ctx.userWallet`) or the provider's
642
656
  * session keypair (Hyperliquid's agent), and `WASM_BLOB` / `EVM_TX` sign with
643
657
  * the provider's local credential (Lighter).
@@ -1,4 +1,5 @@
1
- import type { Asset } from '@lifi/perps-types'
1
+ import type { ActionType, Asset } from '@lifi/perps-types'
2
+ import type { Address } from 'viem'
2
3
 
3
4
  /**
4
5
  * Which of a venue's two balance routes a withdrawal is drawn from. Lighter's
@@ -49,3 +50,34 @@ export interface WithdrawableBalance {
49
50
  */
50
51
  withdrawalFee?: string
51
52
  }
53
+
54
+ /**
55
+ * The venue accepts a withdrawal to `destination` now.
56
+ *
57
+ * @public
58
+ */
59
+ export interface WithdrawFlowReady {
60
+ kind: 'ready'
61
+ destination: Address
62
+ }
63
+
64
+ /**
65
+ * The venue cannot accept a withdrawal until the listed setup actions are
66
+ * satisfied — Ondo sends funds only to an address in its address book, behind
67
+ * a signed-in session.
68
+ *
69
+ * @public
70
+ */
71
+ export interface WithdrawFlowSetupRequired {
72
+ kind: 'setupRequired'
73
+ /** Setup action types that must be completed, in execution order. */
74
+ setup: ActionType[]
75
+ }
76
+
77
+ /**
78
+ * The single withdrawal flow a venue offers one address, resolved from the
79
+ * venue's account and setup state.
80
+ *
81
+ * @public
82
+ */
83
+ export type WithdrawFlow = WithdrawFlowReady | WithdrawFlowSetupRequired
@@ -1,14 +1,14 @@
1
- import type { ProviderAction } from '@lifi/perps-types'
1
+ import type { ActionType, SetupAction, SetupOption } from '@lifi/perps-types'
2
2
  import { PerpsSigner } from '@lifi/perps-types'
3
3
 
4
4
  /**
5
- * Select the `Provider.setup` descriptors a user is responsible for satisfying:
6
- * those whose `signers` include {@link PerpsSigner.USER}. Descriptors the SDK
7
- * signs on its own (no `USER` signer) are held back — `checkSetup` completes
8
- * them inline, so surfacing them would render inert cards the user can't act on.
5
+ * Select the `Provider.setup` descriptors a user sees: every choice step, and
6
+ * every non-choice step signed by {@link PerpsSigner.USER}. A non-choice step
7
+ * the SDK signs on its own is held back — `checkSetup` completes it inline, so
8
+ * surfacing it would render an inert card the user can't act on.
9
9
  *
10
- * Membership is a property of the descriptor's `signers` alone, independent of
11
- * any account. When an account is available, prefer `ProviderSetup.checklist`
10
+ * Membership is a property of the descriptor alone, independent of any
11
+ * account. When an account is available, prefer `ProviderSetup.checklist`
12
12
  * from `checkSetup` for the onboarding list — it applies this filter, omits
13
13
  * not-required conditional steps, and carries each entry's satisfied state.
14
14
  * This function remains the static, account-free projection.
@@ -16,10 +16,24 @@ import { PerpsSigner } from '@lifi/perps-types'
16
16
  * @param setup The provider's `setup` descriptors, in their declared order.
17
17
  * @public
18
18
  */
19
- export function selectUserSetupActions(
20
- setup: ProviderAction[]
21
- ): ProviderAction[] {
22
- return setup.filter((descriptor) =>
23
- descriptor.signers.includes(PerpsSigner.USER)
24
- )
19
+ export function selectUserSetupActions(setup: SetupAction[]): SetupAction[] {
20
+ return setup.filter(isUserFacingSetupStep)
21
+ }
22
+
23
+ /** Whether a setup step is shown to the user rather than drained silently. */
24
+ export function isUserFacingSetupStep(descriptor: SetupAction): boolean {
25
+ return descriptor.options !== null || descriptor.signer === PerpsSigner.USER
26
+ }
27
+
28
+ /**
29
+ * Narrow a setup option to the action it executes, so its bound `params` read
30
+ * with that action's param shape.
31
+ *
32
+ * @public
33
+ */
34
+ export function isSetupOptionFor<T extends ActionType>(
35
+ option: SetupOption,
36
+ type: T
37
+ ): option is SetupOption<T> {
38
+ return option.type === type
25
39
  }
@@ -35,7 +35,7 @@ export function userEip712TargetChainId(
35
35
  ): number | undefined {
36
36
  if (
37
37
  descriptor.signingMethod !== SigningMethod.EIP712 ||
38
- !descriptor.signers.includes(PerpsSigner.USER)
38
+ descriptor.signer !== PerpsSigner.USER
39
39
  ) {
40
40
  return undefined
41
41
  }
package/src/version.ts CHANGED
@@ -10,4 +10,4 @@ export const name = '@lifi/perps-sdk'
10
10
  *
11
11
  * @public
12
12
  */
13
- export const version = '18.0.0'
13
+ export const version = '19.0.0'