@lifi/perps-sdk-provider-lighter 8.1.0 → 8.2.1
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/LighterProvider.d.ts.map +1 -1
- package/dist/cjs/LighterProvider.js +9 -12
- package/dist/cjs/LighterProvider.js.map +1 -1
- package/dist/cjs/constants.d.ts +7 -1
- package/dist/cjs/constants.d.ts.map +1 -1
- package/dist/cjs/constants.js +12 -2
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/signers/LighterSigner.d.ts +2 -0
- package/dist/cjs/signers/LighterSigner.d.ts.map +1 -1
- package/dist/cjs/signers/LighterSigner.js +5 -4
- package/dist/cjs/signers/LighterSigner.js.map +1 -1
- package/dist/cjs/signers/signActions.d.ts +2 -2
- package/dist/cjs/signers/signActions.d.ts.map +1 -1
- package/dist/cjs/signers/signActions.js +10 -4
- package/dist/cjs/signers/signActions.js.map +1 -1
- package/dist/cjs/types/action.d.ts.map +1 -1
- package/dist/cjs/types/action.js.map +1 -1
- package/dist/cjs/utils/lighterAsset.d.ts +2 -2
- package/dist/cjs/utils/lighterAsset.d.ts.map +1 -1
- package/dist/cjs/utils/lighterAsset.js +2 -2
- package/dist/cjs/utils/lighterAsset.js.map +1 -1
- package/dist/esm/LighterProvider.d.ts +6 -6
- package/dist/esm/LighterProvider.d.ts.map +1 -1
- package/dist/esm/LighterProvider.js +13 -15
- package/dist/esm/LighterProvider.js.map +1 -1
- package/dist/esm/constants.d.ts +32 -6
- package/dist/esm/constants.d.ts.map +1 -1
- package/dist/esm/constants.js +24 -6
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/signers/LighterSigner.d.ts +9 -2
- package/dist/esm/signers/LighterSigner.d.ts.map +1 -1
- package/dist/esm/signers/LighterSigner.js +11 -10
- package/dist/esm/signers/LighterSigner.js.map +1 -1
- package/dist/esm/signers/signActions.d.ts +7 -4
- package/dist/esm/signers/signActions.d.ts.map +1 -1
- package/dist/esm/signers/signActions.js +10 -4
- package/dist/esm/signers/signActions.js.map +1 -1
- package/dist/esm/types/action.d.ts +3 -1
- package/dist/esm/types/action.d.ts.map +1 -1
- package/dist/esm/types/action.js +3 -1
- package/dist/esm/types/action.js.map +1 -1
- package/dist/esm/utils/lighterAsset.d.ts +3 -2
- package/dist/esm/utils/lighterAsset.d.ts.map +1 -1
- package/dist/esm/utils/lighterAsset.js +3 -2
- package/dist/esm/utils/lighterAsset.js.map +1 -1
- package/dist/types/LighterProvider.d.ts +6 -6
- package/dist/types/LighterProvider.d.ts.map +1 -1
- package/dist/types/constants.d.ts +32 -6
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/signers/LighterSigner.d.ts +9 -2
- package/dist/types/signers/LighterSigner.d.ts.map +1 -1
- package/dist/types/signers/signActions.d.ts +7 -4
- package/dist/types/signers/signActions.d.ts.map +1 -1
- package/dist/types/types/action.d.ts +3 -1
- package/dist/types/types/action.d.ts.map +1 -1
- package/dist/types/utils/lighterAsset.d.ts +3 -2
- package/dist/types/utils/lighterAsset.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/LighterProvider.ts +25 -23
- package/src/constants.ts +47 -7
- package/src/index.ts +2 -0
- package/src/signers/LighterSigner.ts +19 -12
- package/src/signers/signActions.ts +32 -13
- package/src/types/action.ts +3 -1
- package/src/utils/lighterAsset.ts +8 -3
package/src/LighterProvider.ts
CHANGED
|
@@ -61,6 +61,7 @@ import {
|
|
|
61
61
|
DEFAULT_TRADES_LIMIT,
|
|
62
62
|
LIGHTER_ALL_MARKETS_WILDCARD,
|
|
63
63
|
LIGHTER_BASE_FEE_TIER,
|
|
64
|
+
LIGHTER_COLLATERAL_ASSETS,
|
|
64
65
|
LIGHTER_FEE_TICK_SCALE,
|
|
65
66
|
LIGHTER_HISTORY_PAGE_SIZE,
|
|
66
67
|
LIGHTER_PROVIDER_KEY,
|
|
@@ -187,8 +188,8 @@ const normalizeLighterPublicKey = (key: string): string =>
|
|
|
187
188
|
* - `getOrder` throws `Unauthorized`
|
|
188
189
|
* - `getAccount` returns zero fee tier rather than failing
|
|
189
190
|
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
191
|
+
* `WASM_BLOB` write actions (`signActions`) require `signer` and `keyStore` to
|
|
192
|
+
* be supplied; the `EVM_TX` arm signs with the end-user wallet alone.
|
|
192
193
|
*
|
|
193
194
|
* @public
|
|
194
195
|
*/
|
|
@@ -212,14 +213,14 @@ export interface LighterProviderOptions {
|
|
|
212
213
|
/** Pre-created Lighter read-only bearer. */
|
|
213
214
|
authToken?: string | (() => string | Promise<string>)
|
|
214
215
|
/**
|
|
215
|
-
* WASM signer instance. Required for `
|
|
216
|
-
* for on-demand auth-token creating from the user's API key. The default
|
|
216
|
+
* WASM signer instance. Required for the `WASM_BLOB` arm of `signActions`
|
|
217
|
+
* and for on-demand auth-token creating from the user's API key. The default
|
|
217
218
|
* configuration loads the WASM blob shipped with this package.
|
|
218
219
|
*/
|
|
219
220
|
signer?: LighterSigner
|
|
220
221
|
/**
|
|
221
|
-
* Store for the user's per-address Lighter API keypair. Required for
|
|
222
|
-
* `
|
|
222
|
+
* Store for the user's per-address Lighter API keypair. Required for the
|
|
223
|
+
* `WASM_BLOB` arm of `signActions` and for on-demand auth-token creating.
|
|
223
224
|
*/
|
|
224
225
|
keyStore?: LighterKeyStore
|
|
225
226
|
/**
|
|
@@ -314,6 +315,7 @@ export const lighterProvider = (
|
|
|
314
315
|
(providerKey === LIGHTER_PROVIDER_KEY
|
|
315
316
|
? DEFAULT_LIGHTER_EXPLORER_TX_BASE_URL
|
|
316
317
|
: undefined)
|
|
318
|
+
const collateral = LIGHTER_COLLATERAL_ASSETS[providerKey]
|
|
317
319
|
const authTokenSource: (() => string | Promise<string>) | undefined =
|
|
318
320
|
typeof options.authToken === 'function'
|
|
319
321
|
? options.authToken
|
|
@@ -801,20 +803,29 @@ export const lighterProvider = (
|
|
|
801
803
|
const collateralBalances: Balance[] = [
|
|
802
804
|
{
|
|
803
805
|
categoryId: perpsCategory?.id ?? providerKey,
|
|
804
|
-
asset:
|
|
806
|
+
asset:
|
|
807
|
+
perpsCategory?.quoteAsset ??
|
|
808
|
+
lighterAsset(
|
|
809
|
+
collateral.displaySymbol,
|
|
810
|
+
collateral.displaySymbol,
|
|
811
|
+
providerKey
|
|
812
|
+
),
|
|
805
813
|
units: availableMargin.toString(),
|
|
806
814
|
valueUsd: availableMargin.toString(),
|
|
807
815
|
},
|
|
808
816
|
]
|
|
809
|
-
// Spot token holdings — non-collateral.
|
|
810
|
-
// have no price source at this boundary, so
|
|
817
|
+
// Spot token holdings — non-collateral. The instance's settlement asset
|
|
818
|
+
// is valued 1:1; other tokens have no price source at this boundary, so
|
|
819
|
+
// their USD value is unknown.
|
|
811
820
|
const balances: Balance[] = account.assets.map((a) => {
|
|
812
821
|
const assetId = String(a.asset_id)
|
|
813
822
|
return {
|
|
814
823
|
categoryId: spotCategoryId,
|
|
815
|
-
asset:
|
|
824
|
+
asset:
|
|
825
|
+
assetRegistry.get(assetId) ??
|
|
826
|
+
lighterAsset(assetId, a.symbol, providerKey),
|
|
816
827
|
units: a.balance,
|
|
817
|
-
valueUsd: a.
|
|
828
|
+
valueUsd: a.asset_id === collateral.assetIndex ? a.balance : '0',
|
|
818
829
|
}
|
|
819
830
|
})
|
|
820
831
|
|
|
@@ -1373,22 +1384,13 @@ export const lighterProvider = (
|
|
|
1373
1384
|
address: Address,
|
|
1374
1385
|
ctx?: SignActionsContext
|
|
1375
1386
|
): Promise<SignedActionStep[]> {
|
|
1376
|
-
if (signer === undefined || keyStore === undefined) {
|
|
1377
|
-
throw new PerpsError(
|
|
1378
|
-
PerpsErrorCode.SDKError,
|
|
1379
|
-
'lighterProvider.signActions requires `signer` and `keyStore` to be ' +
|
|
1380
|
-
'configured at construction.'
|
|
1381
|
-
)
|
|
1382
|
-
}
|
|
1383
|
-
const signerRef = signer
|
|
1384
|
-
const keyStoreRef = keyStore
|
|
1385
1387
|
return lighterSignActions(
|
|
1386
1388
|
{
|
|
1387
|
-
signer
|
|
1388
|
-
keyStore
|
|
1389
|
+
signer,
|
|
1390
|
+
keyStore,
|
|
1389
1391
|
apiClient: apiClient(),
|
|
1390
1392
|
resolveAccountIndex: async (addr) => {
|
|
1391
|
-
const apiKey = await
|
|
1393
|
+
const apiKey = keyStore ? await keyStore.get(addr) : null
|
|
1392
1394
|
if (apiKey !== null) {
|
|
1393
1395
|
return apiKey.accountIndex
|
|
1394
1396
|
}
|
package/src/constants.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { FeeTier, LighterProviderKey } from '@lifi/perps-types'
|
|
2
|
+
import { LT_ASSET_ID_USDC } from './types/action.js'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Lighter provider key as it appears on `Provider.key` from the backend.
|
|
@@ -7,6 +8,13 @@ import type { FeeTier, LighterProviderKey } from '@lifi/perps-types'
|
|
|
7
8
|
*/
|
|
8
9
|
export const LIGHTER_PROVIDER_KEY = 'lighter'
|
|
9
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Provider key for the Lighter instance running on Robinhood chain.
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export const LIGHTER_RH_PROVIDER_KEY = 'lighter-rh'
|
|
17
|
+
|
|
10
18
|
/**
|
|
11
19
|
* Category id for Lighter spot token holdings, matching the backend's Lighter
|
|
12
20
|
* spot asset category. The perps category id is not a constant — it is read
|
|
@@ -45,6 +53,38 @@ export const DEFAULT_LIGHTER_SIGNER_CHAIN_ID = 304
|
|
|
45
53
|
export const DEFAULT_LIGHTER_EXPLORER_TX_BASE_URL =
|
|
46
54
|
'https://app.lighter.xyz/explorer/logs/'
|
|
47
55
|
|
|
56
|
+
/**
|
|
57
|
+
* The collateral asset a Lighter deployment settles in: the L2 asset index its
|
|
58
|
+
* withdrawals, transfers and route moves are signed against, plus the symbol
|
|
59
|
+
* shown for the collateral balance.
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export interface LighterCollateralAsset {
|
|
64
|
+
/** L2 asset index, as `asset_id` on the deployment's `/api/v1/assetDetails`. */
|
|
65
|
+
assetIndex: number
|
|
66
|
+
displaySymbol: string
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Collateral asset per Lighter deployment, each read from that deployment's
|
|
71
|
+
* `/api/v1/assetDetails`: mainnet settles in USDC, Robinhood chain in USDG.
|
|
72
|
+
* Both sit at asset index 3 of their own registry — the same slot holds a
|
|
73
|
+
* different token per deployment, so an index alone never identifies the asset.
|
|
74
|
+
*
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
export const LIGHTER_COLLATERAL_ASSETS: Record<
|
|
78
|
+
LighterProviderKey,
|
|
79
|
+
LighterCollateralAsset
|
|
80
|
+
> = {
|
|
81
|
+
[LIGHTER_PROVIDER_KEY]: {
|
|
82
|
+
assetIndex: LT_ASSET_ID_USDC,
|
|
83
|
+
displaySymbol: 'USDC',
|
|
84
|
+
},
|
|
85
|
+
[LIGHTER_RH_PROVIDER_KEY]: { assetIndex: 3, displaySymbol: 'USDG' },
|
|
86
|
+
}
|
|
87
|
+
|
|
48
88
|
/**
|
|
49
89
|
* The venue-specific facts that distinguish one Lighter deployment from
|
|
50
90
|
* another. One provider + WS pair is built per instance from these; a client
|
|
@@ -62,6 +102,11 @@ export interface LighterInstanceConfig {
|
|
|
62
102
|
* {@link LighterSigner} with.
|
|
63
103
|
*/
|
|
64
104
|
signerChainId: number
|
|
105
|
+
/**
|
|
106
|
+
* Collateral asset this deployment settles in — signed into its withdrawals
|
|
107
|
+
* and transfers, and reported as its collateral balance.
|
|
108
|
+
*/
|
|
109
|
+
collateral: LighterCollateralAsset
|
|
65
110
|
/**
|
|
66
111
|
* Explorer tx base URL for transfer-activity links (`${base}${txHash}`).
|
|
67
112
|
* When omitted, transfer links are not emitted for this instance.
|
|
@@ -80,16 +125,10 @@ export const LIGHTER_MAINNET_INSTANCE: LighterInstanceConfig = {
|
|
|
80
125
|
restUrl: DEFAULT_LIGHTER_REST_URL,
|
|
81
126
|
wsUrl: DEFAULT_LIGHTER_WS_URL,
|
|
82
127
|
signerChainId: DEFAULT_LIGHTER_SIGNER_CHAIN_ID,
|
|
128
|
+
collateral: LIGHTER_COLLATERAL_ASSETS[LIGHTER_PROVIDER_KEY],
|
|
83
129
|
explorerTxBaseUrl: DEFAULT_LIGHTER_EXPLORER_TX_BASE_URL,
|
|
84
130
|
}
|
|
85
131
|
|
|
86
|
-
/**
|
|
87
|
-
* Provider key for the Lighter instance running on Robinhood chain.
|
|
88
|
-
*
|
|
89
|
-
* @public
|
|
90
|
-
*/
|
|
91
|
-
export const LIGHTER_RH_PROVIDER_KEY = 'lighter-rh'
|
|
92
|
-
|
|
93
132
|
/**
|
|
94
133
|
* Default REST base URL for the Lighter Robinhood deployment.
|
|
95
134
|
*
|
|
@@ -137,6 +176,7 @@ export const lighterRhInstance = (
|
|
|
137
176
|
restUrl: LIGHTER_RH_REST_URL,
|
|
138
177
|
wsUrl: LIGHTER_RH_WS_URL,
|
|
139
178
|
signerChainId: overrides.signerChainId,
|
|
179
|
+
collateral: LIGHTER_COLLATERAL_ASSETS[LIGHTER_RH_PROVIDER_KEY],
|
|
140
180
|
explorerTxBaseUrl: overrides.explorerTxBaseUrl,
|
|
141
181
|
})
|
|
142
182
|
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
export { projectLighterConfigSettings } from './accountConfig.js'
|
|
17
17
|
export { getAccountSummary } from './accountSummary.js'
|
|
18
18
|
export type {
|
|
19
|
+
LighterCollateralAsset,
|
|
19
20
|
LighterInstanceConfig,
|
|
20
21
|
LighterRhInstanceOverrides,
|
|
21
22
|
} from './constants.js'
|
|
@@ -25,6 +26,7 @@ export {
|
|
|
25
26
|
DEFAULT_LIGHTER_REST_URL,
|
|
26
27
|
DEFAULT_LIGHTER_SIGNER_CHAIN_ID,
|
|
27
28
|
DEFAULT_LIGHTER_WS_URL,
|
|
29
|
+
LIGHTER_COLLATERAL_ASSETS,
|
|
28
30
|
LIGHTER_MAINNET_INSTANCE,
|
|
29
31
|
LIGHTER_PROVIDER_KEY,
|
|
30
32
|
LIGHTER_RH_PROVIDER_KEY,
|
|
@@ -38,8 +38,8 @@ export interface LighterSignerContext {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
|
-
* Configuration for {@link LighterSigner}
|
|
42
|
-
*
|
|
41
|
+
* Configuration for {@link LighterSigner}: optional WASM asset overrides plus
|
|
42
|
+
* the per-instance REST base URL, signing chain id and collateral asset index.
|
|
43
43
|
*
|
|
44
44
|
* @public
|
|
45
45
|
*/
|
|
@@ -48,6 +48,12 @@ export interface LighterSignerConfig extends LoadLighterWasmOptions {
|
|
|
48
48
|
apiUrl?: string
|
|
49
49
|
/** Lighter chain ID (304 = mainnet). */
|
|
50
50
|
chainId?: number
|
|
51
|
+
/**
|
|
52
|
+
* L2 asset index this instance's withdrawals and transfers are signed
|
|
53
|
+
* against — pass the instance's `collateral.assetIndex`. Defaults to
|
|
54
|
+
* mainnet's USDC slot.
|
|
55
|
+
*/
|
|
56
|
+
collateralAssetIndex?: number
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
/**
|
|
@@ -108,12 +114,11 @@ const SKIP_NONCE_DISABLED = 0
|
|
|
108
114
|
// CancelAll across every market (lighter-go `NilMarketIndex`); a real index
|
|
109
115
|
// scopes the cancel to a single market.
|
|
110
116
|
const NIL_MARKET_INDEX = 255
|
|
111
|
-
//
|
|
112
|
-
//
|
|
113
|
-
const USDC_ASSET_INDEX = 3
|
|
117
|
+
// Collateral withdrawals and transfers ride the perps route (lighter-go
|
|
118
|
+
// `AssetRouteType_Perps`). The signer rejects an asset index < 1.
|
|
114
119
|
const ASSET_ROUTE_TYPE_PERPS = 0
|
|
115
120
|
|
|
116
|
-
// SEND_ASSET is a same-account route→route
|
|
121
|
+
// SEND_ASSET is a same-account route→route collateral move (spot↔perp): no
|
|
117
122
|
// fee, and no memo. `SignTransfer` requires a 32-byte memo, so we pass 32 zero
|
|
118
123
|
// bytes in the `0x`-prefixed 64-hex form the WASM signer decodes.
|
|
119
124
|
const SEND_ASSET_NO_FEE = 0
|
|
@@ -136,6 +141,7 @@ const LIGHTER_ROUTE_BY_DEX: Record<string, number> = {
|
|
|
136
141
|
export class LighterSigner {
|
|
137
142
|
private readonly apiUrl: string
|
|
138
143
|
private readonly chainId: number
|
|
144
|
+
private readonly collateralAssetIndex: number
|
|
139
145
|
private readonly loaderOptions: LoadLighterWasmOptions
|
|
140
146
|
private wasm: LighterWasmExports | undefined
|
|
141
147
|
private readonly registeredClients = new Set<string>()
|
|
@@ -143,6 +149,7 @@ export class LighterSigner {
|
|
|
143
149
|
constructor(config: LighterSignerConfig = {}) {
|
|
144
150
|
this.apiUrl = config.apiUrl ?? DEFAULT_LIGHTER_REST_URL
|
|
145
151
|
this.chainId = config.chainId ?? DEFAULT_LIGHTER_SIGNER_CHAIN_ID
|
|
152
|
+
this.collateralAssetIndex = config.collateralAssetIndex ?? LT_ASSET_ID_USDC
|
|
146
153
|
this.loaderOptions = {
|
|
147
154
|
wasmBinaryUrl: config.wasmBinaryUrl,
|
|
148
155
|
wasmExecJsUrl: config.wasmExecJsUrl,
|
|
@@ -499,7 +506,7 @@ export class LighterSigner {
|
|
|
499
506
|
)
|
|
500
507
|
case ActionType.WITHDRAWAL:
|
|
501
508
|
return wasm.SignWithdraw(
|
|
502
|
-
|
|
509
|
+
this.collateralAssetIndex,
|
|
503
510
|
ASSET_ROUTE_TYPE_PERPS,
|
|
504
511
|
numberField(p, 'amount'),
|
|
505
512
|
SKIP_NONCE_DISABLED,
|
|
@@ -517,7 +524,7 @@ export class LighterSigner {
|
|
|
517
524
|
// long".
|
|
518
525
|
return wasm.SignTransfer(
|
|
519
526
|
numberField(p, 'to_account'),
|
|
520
|
-
|
|
527
|
+
this.collateralAssetIndex,
|
|
521
528
|
ASSET_ROUTE_TYPE_PERPS,
|
|
522
529
|
ASSET_ROUTE_TYPE_PERPS,
|
|
523
530
|
numberField(p, 'usdc_amount'),
|
|
@@ -529,9 +536,9 @@ export class LighterSigner {
|
|
|
529
536
|
ctx.accountIndex
|
|
530
537
|
)
|
|
531
538
|
case ActionType.SEND_ASSET: {
|
|
532
|
-
// Same-account
|
|
533
|
-
// `toAccountIndex` is the signer's own account; the routes come
|
|
534
|
-
// the backend-passed `sourceDex`/`destinationDex` wire strings.
|
|
539
|
+
// Same-account collateral self-transfer between the perp and spot
|
|
540
|
+
// routes. `toAccountIndex` is the signer's own account; the routes come
|
|
541
|
+
// from the backend-passed `sourceDex`/`destinationDex` wire strings.
|
|
535
542
|
const fromRouteType = routeFromDex(stringField(p, 'sourceDex'))
|
|
536
543
|
const toRouteType = routeFromDex(stringField(p, 'destinationDex'))
|
|
537
544
|
if (fromRouteType === toRouteType) {
|
|
@@ -542,7 +549,7 @@ export class LighterSigner {
|
|
|
542
549
|
}
|
|
543
550
|
return wasm.SignTransfer(
|
|
544
551
|
ctx.accountIndex,
|
|
545
|
-
|
|
552
|
+
this.collateralAssetIndex,
|
|
546
553
|
fromRouteType,
|
|
547
554
|
toRouteType,
|
|
548
555
|
numberField(p, 'amount'),
|
|
@@ -23,13 +23,16 @@ import type { LighterSigner } from './LighterSigner.js'
|
|
|
23
23
|
* Per-batch dependencies the Lighter `signActions` implementation needs:
|
|
24
24
|
* a WASM signer (`LighterSigner`), the API-key store keyed on L1 address,
|
|
25
25
|
* a REST client for the token-authenticated venue mutations that execute
|
|
26
|
-
* client-side, and
|
|
27
|
-
*
|
|
26
|
+
* client-side, and an `accountIndex` resolver for the REGISTER_API_KEY
|
|
27
|
+
* hybrid flow.
|
|
28
|
+
*
|
|
29
|
+
* `signer` and `keyStore` are optional because only the `WASM_BLOB` arm signs
|
|
30
|
+
* with them — `EVM_TX` broadcasts through the end-user wallet in `ctx`.
|
|
28
31
|
* @internal
|
|
29
32
|
*/
|
|
30
33
|
export interface LighterSignActionsDeps {
|
|
31
|
-
signer
|
|
32
|
-
keyStore
|
|
34
|
+
signer?: LighterSigner
|
|
35
|
+
keyStore?: LighterKeyStore
|
|
33
36
|
/** REST client bound to the user's Lighter base URL, used for the direct
|
|
34
37
|
* `changeAccountTier` / `referral/use` POSTs so their auth token never
|
|
35
38
|
* transits the LI.FI backend. */
|
|
@@ -43,6 +46,12 @@ export interface LighterSignActionsDeps {
|
|
|
43
46
|
resolveAccountIndex(address: Address): Promise<number>
|
|
44
47
|
}
|
|
45
48
|
|
|
49
|
+
/** {@link LighterSignActionsDeps} narrowed to the arm that signs wasm blobs. */
|
|
50
|
+
type LighterWasmSignActionsDeps = LighterSignActionsDeps & {
|
|
51
|
+
signer: LighterSigner
|
|
52
|
+
keyStore: LighterKeyStore
|
|
53
|
+
}
|
|
54
|
+
|
|
46
55
|
/**
|
|
47
56
|
* `wasmSignParams.kind` values for the Lighter mutations that authenticate with
|
|
48
57
|
* a short-lived auth token instead of a wasm-signed transaction. Both execute
|
|
@@ -77,25 +86,35 @@ export async function signWasmBlobActions(
|
|
|
77
86
|
steps: WasmBlobActionStep[],
|
|
78
87
|
ctx: SignActionsContext | undefined
|
|
79
88
|
): Promise<WasmBlobSignedActionStep[]> {
|
|
89
|
+
const { signer, keyStore } = deps
|
|
90
|
+
if (signer === undefined || keyStore === undefined) {
|
|
91
|
+
throw new PerpsError(
|
|
92
|
+
PerpsErrorCode.SDKError,
|
|
93
|
+
'lighterProvider.signActions requires `signer` and `keyStore` to be ' +
|
|
94
|
+
'configured at construction.'
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
const wasmDeps: LighterWasmSignActionsDeps = { ...deps, signer, keyStore }
|
|
98
|
+
|
|
80
99
|
const signed: WasmBlobSignedActionStep[] = []
|
|
81
100
|
for (const step of steps) {
|
|
82
101
|
if (step.action === ActionType.REGISTER_API_KEY) {
|
|
83
|
-
signed.push(await signRegisterApiKey(
|
|
102
|
+
signed.push(await signRegisterApiKey(wasmDeps, address, step, ctx))
|
|
84
103
|
} else if (step.action === ActionType.APPROVE_INTEGRATOR) {
|
|
85
|
-
signed.push(await signApproveIntegrator(
|
|
104
|
+
signed.push(await signApproveIntegrator(wasmDeps, address, step, ctx))
|
|
86
105
|
} else if (
|
|
87
106
|
TOKEN_AUTH_MUTATION_KINDS.has(step.wasmSignParams.kind as string)
|
|
88
107
|
) {
|
|
89
|
-
await executeTokenAuthMutation(
|
|
108
|
+
await executeTokenAuthMutation(wasmDeps, address, step)
|
|
90
109
|
} else {
|
|
91
|
-
signed.push(await signStandardWasmAction(
|
|
110
|
+
signed.push(await signStandardWasmAction(wasmDeps, address, step))
|
|
92
111
|
}
|
|
93
112
|
}
|
|
94
113
|
return signed
|
|
95
114
|
}
|
|
96
115
|
|
|
97
116
|
async function signStandardWasmAction(
|
|
98
|
-
deps:
|
|
117
|
+
deps: LighterWasmSignActionsDeps,
|
|
99
118
|
address: Address,
|
|
100
119
|
step: WasmBlobActionStep
|
|
101
120
|
): Promise<WasmBlobSignedActionStep> {
|
|
@@ -125,7 +144,7 @@ async function signStandardWasmAction(
|
|
|
125
144
|
* user's consent to rotate keys.
|
|
126
145
|
*/
|
|
127
146
|
async function signRegisterApiKey(
|
|
128
|
-
deps:
|
|
147
|
+
deps: LighterWasmSignActionsDeps,
|
|
129
148
|
address: Address,
|
|
130
149
|
step: WasmBlobActionStep,
|
|
131
150
|
ctx: SignActionsContext | undefined
|
|
@@ -211,7 +230,7 @@ async function signRegisterApiKey(
|
|
|
211
230
|
* different L1 address with non-zero fee caps.
|
|
212
231
|
*/
|
|
213
232
|
async function signApproveIntegrator(
|
|
214
|
-
deps:
|
|
233
|
+
deps: LighterWasmSignActionsDeps,
|
|
215
234
|
address: Address,
|
|
216
235
|
step: WasmBlobActionStep,
|
|
217
236
|
ctx: SignActionsContext | undefined
|
|
@@ -266,7 +285,7 @@ async function signApproveIntegrator(
|
|
|
266
285
|
* `code`/`message` verbatim as an {@link PerpsErrorCode.ExchangeRejected}.
|
|
267
286
|
*/
|
|
268
287
|
async function executeTokenAuthMutation(
|
|
269
|
-
deps:
|
|
288
|
+
deps: LighterWasmSignActionsDeps,
|
|
270
289
|
address: Address,
|
|
271
290
|
step: WasmBlobActionStep
|
|
272
291
|
): Promise<void> {
|
|
@@ -345,7 +364,7 @@ function buildTokenAuthMutationRequest(
|
|
|
345
364
|
}
|
|
346
365
|
|
|
347
366
|
async function requireApiKey(
|
|
348
|
-
deps:
|
|
367
|
+
deps: LighterWasmSignActionsDeps,
|
|
349
368
|
address: Address
|
|
350
369
|
): Promise<LighterApiKey> {
|
|
351
370
|
const apiKey = await deps.keyStore.get(address)
|
package/src/types/action.ts
CHANGED
|
@@ -195,7 +195,9 @@ export const LT_ROUTE_PERP = 0
|
|
|
195
195
|
export const LT_ROUTE_SPOT = 1
|
|
196
196
|
|
|
197
197
|
/**
|
|
198
|
-
*
|
|
198
|
+
* L2 asset index of USDC on Lighter mainnet — the collateral slot mainnet
|
|
199
|
+
* withdrawals, transfers and L1 bridge deposits address. Other deployments
|
|
200
|
+
* settle in their own asset; see `LIGHTER_COLLATERAL_ASSETS`.
|
|
199
201
|
* @public
|
|
200
202
|
*/
|
|
201
203
|
export const LT_ASSET_ID_USDC = 3
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Asset } from '@lifi/perps-types'
|
|
1
|
+
import type { Asset, LighterProviderKey } from '@lifi/perps-types'
|
|
2
2
|
import { LIGHTER_PROVIDER_KEY } from '../constants.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -6,10 +6,15 @@ import { LIGHTER_PROVIDER_KEY } from '../constants.js'
|
|
|
6
6
|
* symbol. Lighter assets do not provide a package-level logo URI, so this
|
|
7
7
|
* helper leaves that field empty.
|
|
8
8
|
*
|
|
9
|
+
* @param providerId - Instance the asset belongs to; defaults to mainnet.
|
|
9
10
|
* @public
|
|
10
11
|
*/
|
|
11
|
-
export const lighterAsset = (
|
|
12
|
-
|
|
12
|
+
export const lighterAsset = (
|
|
13
|
+
id: string,
|
|
14
|
+
displaySymbol: string,
|
|
15
|
+
providerId: LighterProviderKey = LIGHTER_PROVIDER_KEY
|
|
16
|
+
): Asset => ({
|
|
17
|
+
providerId,
|
|
13
18
|
id,
|
|
14
19
|
displaySymbol,
|
|
15
20
|
logoURI: '',
|