@lifi/perps-sdk-provider-lighter 13.0.2 → 15.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 (58) hide show
  1. package/dist/cjs/LighterProvider.d.ts.map +1 -1
  2. package/dist/cjs/LighterProvider.js +6 -3
  3. package/dist/cjs/LighterProvider.js.map +1 -1
  4. package/dist/cjs/constants.d.ts +0 -1
  5. package/dist/cjs/constants.d.ts.map +1 -1
  6. package/dist/cjs/constants.js +1 -2
  7. package/dist/cjs/constants.js.map +1 -1
  8. package/dist/cjs/index.d.ts +1 -1
  9. package/dist/cjs/index.d.ts.map +1 -1
  10. package/dist/cjs/index.js +1 -2
  11. package/dist/cjs/index.js.map +1 -1
  12. package/dist/cjs/signers/signActions.d.ts.map +1 -1
  13. package/dist/cjs/signers/signActions.js +4 -1
  14. package/dist/cjs/signers/signActions.js.map +1 -1
  15. package/dist/cjs/types/account.d.ts.map +1 -1
  16. package/dist/cjs/utils/mapPosition.d.ts.map +1 -1
  17. package/dist/cjs/utils/mapPosition.js +1 -0
  18. package/dist/cjs/utils/mapPosition.js.map +1 -1
  19. package/dist/esm/LighterProvider.d.ts.map +1 -1
  20. package/dist/esm/LighterProvider.js +11 -4
  21. package/dist/esm/LighterProvider.js.map +1 -1
  22. package/dist/esm/constants.d.ts +0 -10
  23. package/dist/esm/constants.d.ts.map +1 -1
  24. package/dist/esm/constants.js +0 -10
  25. package/dist/esm/constants.js.map +1 -1
  26. package/dist/esm/index.d.ts +1 -1
  27. package/dist/esm/index.d.ts.map +1 -1
  28. package/dist/esm/index.js +1 -1
  29. package/dist/esm/index.js.map +1 -1
  30. package/dist/esm/signers/LighterKeyStore.d.ts +1 -1
  31. package/dist/esm/signers/LighterKeyStore.d.ts.map +1 -1
  32. package/dist/esm/signers/signActions.d.ts.map +1 -1
  33. package/dist/esm/signers/signActions.js +5 -2
  34. package/dist/esm/signers/signActions.js.map +1 -1
  35. package/dist/esm/types/account.d.ts +5 -0
  36. package/dist/esm/types/account.d.ts.map +1 -1
  37. package/dist/esm/utils/mapPosition.d.ts.map +1 -1
  38. package/dist/esm/utils/mapPosition.js +1 -0
  39. package/dist/esm/utils/mapPosition.js.map +1 -1
  40. package/dist/types/LighterProvider.d.ts.map +1 -1
  41. package/dist/types/constants.d.ts +0 -10
  42. package/dist/types/constants.d.ts.map +1 -1
  43. package/dist/types/index.d.ts +1 -1
  44. package/dist/types/index.d.ts.map +1 -1
  45. package/dist/types/signers/LighterKeyStore.d.ts +1 -1
  46. package/dist/types/signers/LighterKeyStore.d.ts.map +1 -1
  47. package/dist/types/signers/signActions.d.ts.map +1 -1
  48. package/dist/types/types/account.d.ts +5 -0
  49. package/dist/types/types/account.d.ts.map +1 -1
  50. package/dist/types/utils/mapPosition.d.ts.map +1 -1
  51. package/package.json +3 -3
  52. package/src/LighterProvider.ts +12 -4
  53. package/src/constants.ts +0 -11
  54. package/src/index.ts +0 -1
  55. package/src/signers/LighterKeyStore.ts +1 -1
  56. package/src/signers/signActions.ts +8 -5
  57. package/src/types/account.ts +5 -0
  58. package/src/utils/mapPosition.ts +1 -0
@@ -60,7 +60,6 @@ import type { Address } from 'viem'
60
60
  import { projectLighterConfigSettings } from './accountConfig.js'
61
61
  import { getAccountSummary } from './accountSummary.js'
62
62
  import {
63
- DEFAULT_API_KEY_INDEX,
64
63
  DEFAULT_TRADES_LIMIT,
65
64
  LIGHTER_ALL_MARKETS_WILDCARD,
66
65
  LIGHTER_BASE_FEE_TIER,
@@ -706,6 +705,9 @@ export const createLighterProvider = (
706
705
  // Shared promise: the referral read below awaits this instance's runtime
707
706
  // metadata off the same `/providers` fetch — no second request.
708
707
  const providersPromise = getProviders(requireClient())
708
+ // Shared promise: the registered-key read names the slot off the local
709
+ // record, and the config readout reports the record — one storage read.
710
+ const localKeyPromise = keyStore.get(params.address)
709
711
  const [
710
712
  { providers },
711
713
  ,
@@ -719,7 +721,13 @@ export const createLighterProvider = (
719
721
  providersPromise,
720
722
  registry.sync(),
721
723
  assetRegistry.sync(),
722
- fetchRegisteredApiKey(client, account.index, DEFAULT_API_KEY_INDEX),
724
+ // The local record names the only slot worth reading: no record means
725
+ // nothing to compare the registered key against, so the read is skipped.
726
+ localKeyPromise.then((key) =>
727
+ key === null
728
+ ? undefined
729
+ : fetchRegisteredApiKey(client, account.index, key.apiKeyIndex)
730
+ ),
723
731
  // No token is a legitimate unauthenticated read → undefined → zero fee
724
732
  // tier. A generic fetch error is NOT: it must propagate, never be
725
733
  // coerced to a fabricated 0%/0% fee tier. One carve-out: a token the
@@ -734,7 +742,7 @@ export const createLighterProvider = (
734
742
  fetchAccountLimits(client, account.index, t)
735
743
  )
736
744
  ),
737
- keyStore.get(params.address),
745
+ localKeyPromise,
738
746
  readOnlyTokenManager.get(params.address, account.index),
739
747
  // The expected code is backend-owned runtime metadata on this
740
748
  // instance's own provider descriptor (keyed by `providerKey`, so the
@@ -842,7 +850,7 @@ export const createLighterProvider = (
842
850
  const config: LighterAccountConfig = {
843
851
  provider: providerKey,
844
852
  accountIndex: account.index,
845
- apiKeyIndex: DEFAULT_API_KEY_INDEX,
853
+ apiKeyIndex: localKey?.apiKeyIndex,
846
854
  apiKeyRegistered,
847
855
  accountType: account.account_type,
848
856
  accountTradingMode: account.account_trading_mode,
package/src/constants.ts CHANGED
@@ -208,17 +208,6 @@ export const LIGHTER_ALL_MARKETS_WILDCARD = 255
208
208
  */
209
209
  export const LIGHTER_HISTORY_PAGE_SIZE = 100
210
210
 
211
- /**
212
- * Default API key slot — reused per account. Lighter's docs reserve indexes
213
- * `{0,1,2,3}` for its own desktop/mobile interfaces; user keys start at 4
214
- * (max 254, with 255 reserved as `NilApiKeyIndex`). 42 is the LI.FI-wide
215
- * identifier so a user running both the Lighter app and the widget never
216
- * collides with the app's session key.
217
- *
218
- * @public
219
- */
220
- export const DEFAULT_API_KEY_INDEX = 42
221
-
222
211
  /** @internal */
223
212
  const LIGHTER_CODE_ACCOUNT_NOT_FOUND = 21100
224
213
 
package/src/index.ts CHANGED
@@ -20,7 +20,6 @@ export type {
20
20
  LighterDeployment,
21
21
  } from './constants.js'
22
22
  export {
23
- DEFAULT_API_KEY_INDEX,
24
23
  DEFAULT_LIGHTER_EXPLORER_TX_BASE_URL,
25
24
  DEFAULT_LIGHTER_REST_URL,
26
25
  DEFAULT_LIGHTER_WS_URL,
@@ -21,7 +21,7 @@ const STORAGE_PREFIX = 'lifi-perps-lighter-key'
21
21
  export interface LighterApiKey {
22
22
  /** Lighter account index, looked up once via accountsByL1Address. */
23
23
  accountIndex: number
24
- /** API key slot (0-255). Defaults to {@link DEFAULT_API_KEY_INDEX}. */
24
+ /** API key slot (0-255), as named by the backend registration payload. */
25
25
  apiKeyIndex: number
26
26
  /** Lighter-native private key (0x-prefixed hex). */
27
27
  apiKeyPrivateKey: string
@@ -11,10 +11,7 @@ import { ActionType, PerpsErrorCode, SigningMethod } from '@lifi/perps-types'
11
11
  import type { Address } from 'viem'
12
12
  import { parseAbi } from 'viem'
13
13
  import { waitForTransactionReceipt } from 'viem/actions'
14
- import {
15
- DEFAULT_API_KEY_INDEX,
16
- LIGHTER_MUTATION_SUCCESS_CODE,
17
- } from '../constants.js'
14
+ import { LIGHTER_MUTATION_SUCCESS_CODE } from '../constants.js'
18
15
  import type { ApiParams, LighterApiClient } from '../utils/apiClient.js'
19
16
  import {
20
17
  fetchRegisteredApiKey,
@@ -260,7 +257,13 @@ async function signRegisterApiKey(
260
257
  nonce?: number
261
258
  skip_nonce?: 0 | 1
262
259
  }
263
- const apiKeyIndex = params.api_key_index ?? DEFAULT_API_KEY_INDEX
260
+ const apiKeyIndex = params.api_key_index
261
+ if (typeof apiKeyIndex !== 'number') {
262
+ throw new PerpsError(
263
+ PerpsErrorCode.SDKError,
264
+ 'REGISTER_API_KEY wasmSignParams is missing `api_key_index`.'
265
+ )
266
+ }
264
267
  const nonce = params.nonce
265
268
  if (typeof nonce !== 'number') {
266
269
  throw new PerpsError(
@@ -26,6 +26,11 @@ export type LtAccountPosition = {
26
26
  unrealized_pnl: string
27
27
  realized_pnl: string
28
28
  liquidation_price: string
29
+ /**
30
+ * Funding accrued since the position opened, signed from the account's point
31
+ * of view despite the field name: negative is funding the account paid and
32
+ * positive is funding it received. Lighter resets it to `"0"` on close.
33
+ */
29
34
  total_funding_paid_out: string
30
35
  margin_mode: number
31
36
  allocated_margin: string
@@ -60,6 +60,7 @@ export const mapPosition = (
60
60
  : positionValue.div(size.abs()).toFixed(),
61
61
  liquidationPrice: pos.liquidation_price,
62
62
  unrealizedPnl: pos.unrealized_pnl,
63
+ accruedFunding: pos.total_funding_paid_out,
63
64
  leverage: leverageFromImf(pos.initial_margin_fraction) ?? 1,
64
65
  marginUsed,
65
66
  initialMarginRequirement: initialMarginRequirement.toFixed(),