@lifi/perps-sdk 0.0.0-preview-bb1a973 → 0.0.0-preview-b49efcc

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 (48) hide show
  1. package/README.md +22 -0
  2. package/dist/cjs/client/PerpsClient.d.ts +6 -1
  3. package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
  4. package/dist/cjs/client/PerpsClient.js +20 -0
  5. package/dist/cjs/client/PerpsClient.js.map +1 -1
  6. package/dist/cjs/index.d.ts +2 -2
  7. package/dist/cjs/index.d.ts.map +1 -1
  8. package/dist/cjs/index.js +4 -3
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/registry/assetRegistry.d.ts +2 -1
  11. package/dist/cjs/registry/assetRegistry.d.ts.map +1 -1
  12. package/dist/cjs/registry/assetRegistry.js +11 -1
  13. package/dist/cjs/registry/assetRegistry.js.map +1 -1
  14. package/dist/cjs/types/provider.d.ts +6 -1
  15. package/dist/cjs/types/provider.d.ts.map +1 -1
  16. package/dist/cjs/version.d.ts +1 -1
  17. package/dist/cjs/version.js +1 -1
  18. package/dist/esm/client/PerpsClient.d.ts +20 -1
  19. package/dist/esm/client/PerpsClient.d.ts.map +1 -1
  20. package/dist/esm/client/PerpsClient.js +35 -1
  21. package/dist/esm/client/PerpsClient.js.map +1 -1
  22. package/dist/esm/index.d.ts +2 -2
  23. package/dist/esm/index.d.ts.map +1 -1
  24. package/dist/esm/index.js +1 -1
  25. package/dist/esm/index.js.map +1 -1
  26. package/dist/esm/registry/assetRegistry.d.ts +7 -1
  27. package/dist/esm/registry/assetRegistry.d.ts.map +1 -1
  28. package/dist/esm/registry/assetRegistry.js +14 -0
  29. package/dist/esm/registry/assetRegistry.js.map +1 -1
  30. package/dist/esm/types/provider.d.ts +20 -1
  31. package/dist/esm/types/provider.d.ts.map +1 -1
  32. package/dist/esm/version.d.ts +1 -1
  33. package/dist/esm/version.js +1 -1
  34. package/dist/types/client/PerpsClient.d.ts +20 -1
  35. package/dist/types/client/PerpsClient.d.ts.map +1 -1
  36. package/dist/types/index.d.ts +2 -2
  37. package/dist/types/index.d.ts.map +1 -1
  38. package/dist/types/registry/assetRegistry.d.ts +7 -1
  39. package/dist/types/registry/assetRegistry.d.ts.map +1 -1
  40. package/dist/types/types/provider.d.ts +20 -1
  41. package/dist/types/types/provider.d.ts.map +1 -1
  42. package/dist/types/version.d.ts +1 -1
  43. package/package.json +2 -2
  44. package/src/client/PerpsClient.ts +55 -1
  45. package/src/index.ts +6 -1
  46. package/src/registry/assetRegistry.ts +16 -1
  47. package/src/types/provider.ts +25 -0
  48. package/src/version.ts +1 -1
@@ -4,6 +4,7 @@ import type {
4
4
  ActionParamsMap,
5
5
  ActionResult,
6
6
  ActionStep,
7
+ AvailableToTrade,
7
8
  CreateActionResponse,
8
9
  ExecuteActionResponse,
9
10
  MarketRef,
@@ -28,7 +29,8 @@ import {
28
29
  import Big from 'big.js'
29
30
  import type { Address } from 'viem'
30
31
  import { PerpsError } from '../errors/PerpsError.js'
31
- import { getAssetRegistry } from '../registry/assetRegistry.js'
32
+ import { getAssetRegistry, toAssetDisplay } from '../registry/assetRegistry.js'
33
+ import { getMarketRegistry } from '../registry/marketRegistry.js'
32
34
  import { createAction } from '../services/createAction.js'
33
35
  import { executeAction } from '../services/executeAction.js'
34
36
  import { getAccount as fetchAccount } from '../services/getAccount.js'
@@ -498,6 +500,58 @@ export class PerpsClient {
498
500
  })
499
501
  }
500
502
 
503
+ /**
504
+ * The amounts `params.address` can still buy and sell on one market, in
505
+ * that market's margin asset. The order panel reads this per-market figure;
506
+ * account displays read the account-scoped
507
+ * {@link AccountSummary.availableMargin} instead.
508
+ *
509
+ * Providers that read a per-market figure answer it directly. For every
510
+ * other provider this falls back to the account summary, so both sides
511
+ * equal `availableMargin` and the asset is the market's quote asset.
512
+ *
513
+ * @throws {PerpsError} When the provider plugin is not registered, or the
514
+ * market is unknown to the provider's market registry.
515
+ * @public
516
+ */
517
+ async getAvailableToTrade(
518
+ params: {
519
+ provider: string
520
+ address: Address
521
+ marketId: string
522
+ },
523
+ options?: SDKRequestOptions
524
+ ): Promise<AvailableToTrade> {
525
+ const plugin = this.requireProvider(params.provider)
526
+ const perMarket = await plugin.getAvailableToTrade?.(
527
+ { address: params.address, marketId: params.marketId },
528
+ options
529
+ )
530
+ if (perMarket !== undefined) {
531
+ return perMarket
532
+ }
533
+
534
+ const registry = getMarketRegistry(this.sdkClient, params.provider)
535
+ await registry.sync()
536
+ const market = registry.require(params.marketId)
537
+ const account = await fetchAccount(
538
+ this.sdkClient,
539
+ { provider: params.provider, address: params.address },
540
+ options
541
+ )
542
+ const { availableMargin } = plugin.getAccountSummary(
543
+ account,
544
+ account.positions
545
+ )
546
+ return {
547
+ providerId: market.providerId,
548
+ marketId: market.id,
549
+ asset: toAssetDisplay(market.quoteAsset),
550
+ buy: availableMargin,
551
+ sell: availableMargin,
552
+ }
553
+ }
554
+
501
555
  /**
502
556
  * Resolve the exact venue-owned margin requirements for `position`.
503
557
  * Returns `undefined` when the position has no individual margin adjustment.
package/src/index.ts CHANGED
@@ -19,7 +19,11 @@ export { PerpsClient } from './client/PerpsClient.js'
19
19
  export { PerpsErrorMessage } from './errors/constants.js'
20
20
  export { PerpsError } from './errors/PerpsError.js'
21
21
  // Registries
22
- export { AssetRegistry, getAssetRegistry } from './registry/assetRegistry.js'
22
+ export {
23
+ AssetRegistry,
24
+ getAssetRegistry,
25
+ toAssetDisplay,
26
+ } from './registry/assetRegistry.js'
23
27
  export type { DefaultMarketProvider } from './registry/marketRegistry.js'
24
28
  export {
25
29
  DEFAULT_MARKET_ID,
@@ -152,6 +156,7 @@ export type {
152
156
  ProviderAccountExistsParams,
153
157
  ProviderGetAccountParams,
154
158
  ProviderGetActivityParams,
159
+ ProviderGetAvailableToTradeParams,
155
160
  ProviderGetDepositFlowParams,
156
161
  ProviderGetFillsParams,
157
162
  ProviderGetMarketSettingsParams,
@@ -1,4 +1,4 @@
1
- import type { Asset, AssetsResponse } from '@lifi/perps-types'
1
+ import type { Asset, AssetDisplay, AssetsResponse } from '@lifi/perps-types'
2
2
  import { PerpsErrorCode } from '@lifi/perps-types'
3
3
  import { PerpsError } from '../errors/PerpsError.js'
4
4
  import { buildUrl, request } from '../transport/request.js'
@@ -59,6 +59,21 @@ export class AssetRegistry extends ReferenceDataRegistry<Asset> {
59
59
  }
60
60
  }
61
61
 
62
+ /**
63
+ * Project an {@link Asset} to the identity and labels a display needs.
64
+ *
65
+ * @public
66
+ */
67
+ export const toAssetDisplay = (asset: Asset): AssetDisplay => ({
68
+ providerId: asset.providerId,
69
+ id: asset.id,
70
+ displaySymbol: asset.displaySymbol,
71
+ logoURI: asset.logoURI,
72
+ ...(asset.displayName === undefined
73
+ ? {}
74
+ : { displayName: asset.displayName }),
75
+ })
76
+
62
77
  const registries = new WeakMap<PerpsSDKClient, Map<string, AssetRegistry>>()
63
78
 
64
79
  /**
@@ -8,6 +8,7 @@ import type {
8
8
  ActionType,
9
9
  ActivitiesResponse,
10
10
  ActivityType,
11
+ AvailableToTrade,
11
12
  FillsResponse,
12
13
  Market,
13
14
  MarketRef,
@@ -216,6 +217,17 @@ export interface ProviderGetMarketSettingsParams {
216
217
  market: MarketRef
217
218
  }
218
219
 
220
+ /**
221
+ * Read params for {@link PerpsProviderPlugin.getAvailableToTrade}.
222
+ *
223
+ * @public
224
+ */
225
+ export interface ProviderGetAvailableToTradeParams {
226
+ address: Address
227
+ /** The market's opaque `Market.id` (not a display symbol). */
228
+ marketId: string
229
+ }
230
+
219
231
  /**
220
232
  * Read params for {@link PerpsProvider.getOrders}.
221
233
  *
@@ -442,6 +454,19 @@ export interface PerpsProviderPlugin {
442
454
  options?: SDKRequestOptions
443
455
  ): Promise<MarketSettings | undefined>
444
456
 
457
+ /**
458
+ * The amounts the account can still buy and sell on one market, in that
459
+ * market's margin asset. Optional because venues expose this unevenly:
460
+ * Hyperliquid reads it directly (`activeAssetData`), other venues publish
461
+ * only an account-scoped figure. `undefined` means the venue has nothing
462
+ * to read for this market, and the caller falls back to the account
463
+ * summary.
464
+ */
465
+ getAvailableToTrade?(
466
+ params: ProviderGetAvailableToTradeParams,
467
+ options?: SDKRequestOptions
468
+ ): Promise<AvailableToTrade | undefined>
469
+
445
470
  getOrders(
446
471
  params: ProviderGetOrdersParams,
447
472
  options?: SDKRequestOptions
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 = '0.0.0-preview-bb1a973'
13
+ export const version = '0.0.0-preview-b49efcc'