@lifi/perps-sdk 6.2.0 → 6.3.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.
- package/dist/cjs/client/PerpsClient.d.ts +3 -1
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +36 -0
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/api.d.ts +4 -0
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +5 -0
- package/dist/cjs/types/provider.d.ts.map +1 -1
- package/dist/cjs/types/withdrawal.d.ts +13 -0
- package/dist/cjs/types/withdrawal.d.ts.map +1 -0
- package/dist/cjs/types/withdrawal.js +3 -0
- package/dist/cjs/types/withdrawal.js.map +1 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.d.ts.map +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/client/PerpsClient.d.ts +17 -1
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +47 -0
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/api.d.ts +9 -0
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +20 -0
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/types/withdrawal.d.ts +37 -0
- package/dist/esm/types/withdrawal.d.ts.map +1 -0
- package/dist/esm/types/withdrawal.js +2 -0
- package/dist/esm/types/withdrawal.js.map +1 -0
- package/dist/esm/version.d.ts +11 -1
- package/dist/esm/version.d.ts.map +1 -1
- package/dist/esm/version.js +11 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/types/client/PerpsClient.d.ts +17 -1
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +9 -0
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +20 -0
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/types/withdrawal.d.ts +37 -0
- package/dist/types/types/withdrawal.d.ts.map +1 -0
- package/dist/types/version.d.ts +11 -1
- package/dist/types/version.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/client/PerpsClient.ts +55 -0
- package/src/index.ts +7 -0
- package/src/types/api.ts +10 -0
- package/src/types/provider.ts +25 -0
- package/src/types/withdrawal.ts +39 -0
- package/src/version.ts +12 -1
|
@@ -20,8 +20,10 @@ import {
|
|
|
20
20
|
PerpsSigner,
|
|
21
21
|
SigningMethod,
|
|
22
22
|
} from '@lifi/perps-types'
|
|
23
|
+
import Big from 'big.js'
|
|
23
24
|
import type { Address } from 'viem'
|
|
24
25
|
import { PerpsError } from '../errors/PerpsError.js'
|
|
26
|
+
import { getAssetRegistry } from '../registry/assetRegistry.js'
|
|
25
27
|
import { createAction } from '../services/createAction.js'
|
|
26
28
|
import { executeAction } from '../services/executeAction.js'
|
|
27
29
|
import { getAccount as fetchAccount } from '../services/getAccount.js'
|
|
@@ -34,6 +36,7 @@ import type {
|
|
|
34
36
|
GetAccountResult,
|
|
35
37
|
GetDepositFlowParams,
|
|
36
38
|
GetSetupParams,
|
|
39
|
+
GetWithdrawableBalancesParams,
|
|
37
40
|
ModifyOrdersParams,
|
|
38
41
|
PerpsClientOptions,
|
|
39
42
|
PlaceOrderParams,
|
|
@@ -51,6 +54,7 @@ import type {
|
|
|
51
54
|
SignActionProgress,
|
|
52
55
|
SignActionsContext,
|
|
53
56
|
} from '../types/provider.js'
|
|
57
|
+
import type { WithdrawableBalance } from '../types/withdrawal.js'
|
|
54
58
|
import {
|
|
55
59
|
switchSigningChain,
|
|
56
60
|
userEip712TargetChainId,
|
|
@@ -493,6 +497,57 @@ export class PerpsClient {
|
|
|
493
497
|
return plugin.getDepositFlow?.({ address: params.address })
|
|
494
498
|
}
|
|
495
499
|
|
|
500
|
+
/**
|
|
501
|
+
* The `(asset, route)` selections `params.address` can actually withdraw at
|
|
502
|
+
* `params.provider`. The venue owns how its balances split across routes;
|
|
503
|
+
* this join adds the core `/assets` metadata — precision, L1 identity and
|
|
504
|
+
* the per-asset minimum — and drops every row the minimum rules out, plus
|
|
505
|
+
* any row whose asset the provider's registry does not carry, since without
|
|
506
|
+
* that metadata the amount can be neither scaled nor validated.
|
|
507
|
+
*
|
|
508
|
+
* @returns `undefined` when the registered plugin declares no withdrawable
|
|
509
|
+
* read.
|
|
510
|
+
* @throws {PerpsError} When the provider plugin is not registered, or either
|
|
511
|
+
* the plugin read or the asset sync fails.
|
|
512
|
+
* @public
|
|
513
|
+
*/
|
|
514
|
+
async getWithdrawableBalances(
|
|
515
|
+
params: GetWithdrawableBalancesParams
|
|
516
|
+
): Promise<WithdrawableBalance[] | undefined> {
|
|
517
|
+
const plugin = this.requireProvider(params.provider)
|
|
518
|
+
const rows = await plugin.getWithdrawableBalances?.({
|
|
519
|
+
address: params.address,
|
|
520
|
+
})
|
|
521
|
+
if (rows === undefined) {
|
|
522
|
+
return undefined
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const registry = getAssetRegistry(this.sdkClient, params.provider)
|
|
526
|
+
await registry.sync()
|
|
527
|
+
return rows.flatMap((row) => {
|
|
528
|
+
const asset = registry.get(row.assetId)
|
|
529
|
+
if (asset === undefined) {
|
|
530
|
+
return []
|
|
531
|
+
}
|
|
532
|
+
const minimum = asset.minWithdrawalAmount
|
|
533
|
+
if (minimum !== undefined) {
|
|
534
|
+
let floor: Big
|
|
535
|
+
try {
|
|
536
|
+
floor = new Big(minimum)
|
|
537
|
+
} catch {
|
|
538
|
+
throw new PerpsError(
|
|
539
|
+
PerpsErrorCode.SDKError,
|
|
540
|
+
`Asset '${asset.id}' field \`minWithdrawalAmount\` is not a valid decimal.`
|
|
541
|
+
)
|
|
542
|
+
}
|
|
543
|
+
if (new Big(row.available).lt(floor)) {
|
|
544
|
+
return []
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return [{ asset, route: row.route, available: row.available }]
|
|
548
|
+
})
|
|
549
|
+
}
|
|
550
|
+
|
|
496
551
|
/**
|
|
497
552
|
* Return the unsatisfied entries on `Provider.setup` for this account as a
|
|
498
553
|
* flat, self-describing list. Trading is gated on `isReady === true`.
|
package/src/index.ts
CHANGED
|
@@ -101,6 +101,7 @@ export type {
|
|
|
101
101
|
GetAccountResult,
|
|
102
102
|
GetDepositFlowParams,
|
|
103
103
|
GetSetupParams,
|
|
104
|
+
GetWithdrawableBalancesParams,
|
|
104
105
|
ModifyOrdersParams,
|
|
105
106
|
PerpsClientOptions,
|
|
106
107
|
PerpsConfig,
|
|
@@ -143,10 +144,16 @@ export type {
|
|
|
143
144
|
ProviderGetOrdersParams,
|
|
144
145
|
ProviderGetPositionsParams,
|
|
145
146
|
ProviderGetQuoteParams,
|
|
147
|
+
ProviderGetWithdrawableBalancesParams,
|
|
146
148
|
QuoteListener,
|
|
147
149
|
SignActionProgress,
|
|
148
150
|
SignActionsContext,
|
|
149
151
|
} from './types/provider.js'
|
|
152
|
+
export type {
|
|
153
|
+
ProviderWithdrawableBalance,
|
|
154
|
+
WithdrawableBalance,
|
|
155
|
+
WithdrawalRoute,
|
|
156
|
+
} from './types/withdrawal.js'
|
|
150
157
|
export {
|
|
151
158
|
type CollateralSemantics,
|
|
152
159
|
summarizeAccount,
|
package/src/types/api.ts
CHANGED
|
@@ -243,6 +243,16 @@ export interface GetDepositFlowParams {
|
|
|
243
243
|
address: Address
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Parameters for {@link PerpsClient.getWithdrawableBalances}.
|
|
248
|
+
*
|
|
249
|
+
* @public
|
|
250
|
+
*/
|
|
251
|
+
export interface GetWithdrawableBalancesParams {
|
|
252
|
+
provider: string
|
|
253
|
+
address: Address
|
|
254
|
+
}
|
|
255
|
+
|
|
246
256
|
/**
|
|
247
257
|
* Result from {@link PerpsClient.checkSetup}.
|
|
248
258
|
*
|
package/src/types/provider.ts
CHANGED
|
@@ -33,6 +33,7 @@ import type {
|
|
|
33
33
|
SDKRequestOptions,
|
|
34
34
|
} from './config.js'
|
|
35
35
|
import type { DepositFlow } from './deposit.js'
|
|
36
|
+
import type { ProviderWithdrawableBalance } from './withdrawal.js'
|
|
36
37
|
|
|
37
38
|
/**
|
|
38
39
|
* Low-level SDK client: resolved config, the optional end-user wallet, and the
|
|
@@ -176,6 +177,15 @@ export interface ProviderGetDepositFlowParams {
|
|
|
176
177
|
address: Address
|
|
177
178
|
}
|
|
178
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Read params for {@link PerpsProviderPlugin.getWithdrawableBalances}.
|
|
182
|
+
*
|
|
183
|
+
* @public
|
|
184
|
+
*/
|
|
185
|
+
export interface ProviderGetWithdrawableBalancesParams {
|
|
186
|
+
address: Address
|
|
187
|
+
}
|
|
188
|
+
|
|
179
189
|
/**
|
|
180
190
|
* Read params for {@link PerpsProvider.getPositions}.
|
|
181
191
|
*
|
|
@@ -377,6 +387,21 @@ export interface PerpsProviderPlugin {
|
|
|
377
387
|
options?: SDKRequestOptions
|
|
378
388
|
): Promise<DepositFlow>
|
|
379
389
|
|
|
390
|
+
/**
|
|
391
|
+
* The `(asset, route)` pairs `params.address` currently has something to
|
|
392
|
+
* withdraw from, keyed by provider-native asset id. Only the venue knows how
|
|
393
|
+
* its balance payload splits across routes, so the split is owned here; the
|
|
394
|
+
* per-asset venue minimum is applied by `PerpsClient.getWithdrawableBalances`,
|
|
395
|
+
* which holds the core asset registry.
|
|
396
|
+
*
|
|
397
|
+
* Optional: a provider whose withdrawals are not a per-route selection omits
|
|
398
|
+
* it, and `PerpsClient.getWithdrawableBalances` then resolves `undefined`.
|
|
399
|
+
*/
|
|
400
|
+
getWithdrawableBalances?(
|
|
401
|
+
params: ProviderGetWithdrawableBalancesParams,
|
|
402
|
+
options?: SDKRequestOptions
|
|
403
|
+
): Promise<ProviderWithdrawableBalance[]>
|
|
404
|
+
|
|
380
405
|
getPositions(
|
|
381
406
|
params: ProviderGetPositionsParams,
|
|
382
407
|
options?: SDKRequestOptions
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Asset } from '@lifi/perps-types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Which of a venue's two balance routes a withdrawal is drawn from. Lighter's
|
|
5
|
+
* own vocabulary, carried in the signed tx as `AssetRouteType` (`perps` = 0,
|
|
6
|
+
* `spot` = 1); an account's mode changes what the two balances mean, never
|
|
7
|
+
* which route the tx names.
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export type WithdrawalRoute = 'perps' | 'spot'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* One withdrawable `(asset, route)` selection as the provider reports it, keyed
|
|
15
|
+
* by provider-native asset id and awaiting the core asset-metadata join.
|
|
16
|
+
*
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export interface ProviderWithdrawableBalance {
|
|
20
|
+
/** Provider-native `Asset.id`; never a display symbol. */
|
|
21
|
+
assetId: string
|
|
22
|
+
route: WithdrawalRoute
|
|
23
|
+
/** Withdrawable amount in the asset's own units. Always greater than zero. */
|
|
24
|
+
available: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One withdrawable `(asset, route)` selection a caller can act on: the amount
|
|
29
|
+
* clears the asset's venue minimum, and `asset` carries the precision and
|
|
30
|
+
* minimum needed to scale and validate the withdrawal.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export interface WithdrawableBalance {
|
|
35
|
+
asset: Asset
|
|
36
|
+
route: WithdrawalRoute
|
|
37
|
+
/** Withdrawable amount in the asset's own units. */
|
|
38
|
+
available: string
|
|
39
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package name used in SDK telemetry and request metadata.
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
1
6
|
export const name = '@lifi/perps-sdk'
|
|
2
|
-
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Published SDK version sent in the `x-lifi-perps-sdk` request header.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export const version = '6.3.0'
|