@pear-protocol/exchanges-sdk 0.14.1 → 0.15.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.
@@ -3,6 +3,7 @@ import { createExchangeWs } from '../../client';
3
3
  import { fetchAccount, fetchPublicAccount, fetchOrderBookDetails, fetchPublicOrderBookDetails, fetchAccountLimits } from '../../client/lighter/rest';
4
4
  import { retrieveLighterAuthToken } from '../../helper/lighter/auth-token.helper';
5
5
  import { BaseAccountTracker } from '../base';
6
+ import { LIGHTER_QUOTE_ASSETS } from './const';
6
7
  import { buildSpotPriceIndex, mapPositions, mapRestBalance, mapMarketDefaultAssetInfo, toLighterAccountType, mapLiveBalance, toLighterAccountTier } from './mapper';
7
8
 
8
9
  var __defProp = Object.defineProperty;
@@ -32,6 +33,8 @@ class LighterAccountTracker extends BaseAccountTracker {
32
33
  __name(this, "LighterAccountTracker");
33
34
  }
34
35
  sdk;
36
+ connector;
37
+ quoteAsset;
35
38
  accountIdentifier;
36
39
  apiKeyIndex;
37
40
  apiPrivateKey;
@@ -55,9 +58,7 @@ class LighterAccountTracker extends BaseAccountTracker {
55
58
  positionsByMarketId = /* @__PURE__ */ new Map();
56
59
  marketsByMarketId = /* @__PURE__ */ new Map();
57
60
  marketsBySymbol = /* @__PURE__ */ new Map();
58
- // Seeded empty, which still prices the quote asset — spot dollars are dollars before any
59
- // market data arrives, and a balance the index cannot price would otherwise read as unknown.
60
- spotPriceBySymbol = buildSpotPriceIndex([]);
61
+ spotPriceBySymbol;
61
62
  stats = null;
62
63
  // Null until the authenticated read lands, and null for good where nothing can authenticate it.
63
64
  accountTier = null;
@@ -68,6 +69,9 @@ class LighterAccountTracker extends BaseAccountTracker {
68
69
  super(config);
69
70
  const credentials = config.credentials;
70
71
  this.sdk = config.sdk;
72
+ this.connector = config.connector;
73
+ this.quoteAsset = LIGHTER_QUOTE_ASSETS[config.connector];
74
+ this.spotPriceBySymbol = buildSpotPriceIndex([], this.quoteAsset);
71
75
  this.accountIdentifier = config.account.exchangeIdentifier;
72
76
  this.apiKeyIndex = credentials.api_key_index;
73
77
  this.apiPrivateKey = credentials.api_private_key;
@@ -119,7 +123,7 @@ class LighterAccountTracker extends BaseAccountTracker {
119
123
  };
120
124
  const { positions } = mapPositions(toPosRecord(account.positions ?? []));
121
125
  return {
122
- balance: mapRestBalance(account, toMarketContext(markets.perp, markets.spot), Date.now()),
126
+ balance: mapRestBalance(account, toMarketContext(markets.perp, markets.spot, this.quoteAsset), Date.now()),
123
127
  positions: Array.from(positions.values())
124
128
  };
125
129
  }
@@ -161,7 +165,7 @@ class LighterAccountTracker extends BaseAccountTracker {
161
165
  const lifecycleVersion = ++this.lifecycleVersion;
162
166
  const markets = await this.fetchMarkets();
163
167
  if (lifecycleVersion !== this.lifecycleVersion) return;
164
- this.spotPriceBySymbol = buildSpotPriceIndex(markets.spot);
168
+ this.spotPriceBySymbol = buildSpotPriceIndex(markets.spot, this.quoteAsset);
165
169
  const trackedAssets = /* @__PURE__ */ new Map();
166
170
  for (const m of markets.perp) {
167
171
  this.marketsByMarketId.set(m.market_id, m);
@@ -171,7 +175,7 @@ class LighterAccountTracker extends BaseAccountTracker {
171
175
  this.onStateUpdate({
172
176
  trackedAssets
173
177
  });
174
- const ws = (await createExchangeWs("lighter", {
178
+ const ws = (await createExchangeWs(this.connector, {
175
179
  demo: this.demo,
176
180
  credentials: this.credentials
177
181
  })).main;
@@ -382,20 +386,21 @@ class LighterAccountTracker extends BaseAccountTracker {
382
386
  marketContext() {
383
387
  return {
384
388
  perpByMarketId: this.marketsByMarketId,
385
- spotPriceBySymbol: this.spotPriceBySymbol
389
+ spotPriceBySymbol: this.spotPriceBySymbol,
390
+ quoteAsset: this.quoteAsset
386
391
  };
387
392
  }
388
393
  /** Reads the account with credentials when present, or explicitly uses the read-only verifier path. */
389
394
  async fetchPrimaryAccount() {
390
395
  const authToken = await this.retrieveAuthToken();
391
396
  const accountIndex = Number(this.accountIdentifier);
392
- const data = authToken ? await fetchAccount(accountIndex, authToken, this.demo) : await fetchPublicAccount(accountIndex, this.demo);
397
+ const data = authToken ? await fetchAccount(accountIndex, authToken, this.connector, this.demo) : await fetchPublicAccount(accountIndex, this.connector, this.demo);
393
398
  return data.accounts[0];
394
399
  }
395
400
  /** Reads market context under the same account identity as the snapshot that consumes it. */
396
401
  async fetchMarkets() {
397
402
  const authToken = await this.retrieveAuthToken();
398
- return authToken ? fetchOrderBookDetails(authToken, this.demo) : fetchPublicOrderBookDetails(this.demo);
403
+ return authToken ? fetchOrderBookDetails(authToken, this.connector, this.demo) : fetchPublicOrderBookDetails(this.connector, this.demo);
399
404
  }
400
405
  /** Returns this account's cached auth token, or no token for the explicit empty-key verifier. */
401
406
  async retrieveAuthToken() {
@@ -405,7 +410,7 @@ class LighterAccountTracker extends BaseAccountTracker {
405
410
  if (!this.sdk) {
406
411
  throw new Error("Lighter authenticated account reads require a Pear SDK client");
407
412
  }
408
- return retrieveLighterAuthToken(this.sdk, {
413
+ return retrieveLighterAuthToken(this.sdk, this.connector, {
409
414
  accountIndex: Number(this.accountIdentifier),
410
415
  apiKeyIndex: this.apiKeyIndex,
411
416
  apiPrivateKey: this.apiPrivateKey
@@ -499,12 +504,12 @@ class LighterAccountTracker extends BaseAccountTracker {
499
504
  try {
500
505
  const credentials = this.credentials;
501
506
  const accountIndex = Number(this.accountIdentifier);
502
- const authToken = await retrieveLighterAuthToken(this.sdk, {
507
+ const authToken = await retrieveLighterAuthToken(this.sdk, this.connector, {
503
508
  accountIndex,
504
509
  apiKeyIndex: credentials.api_key_index,
505
510
  apiPrivateKey: credentials.api_private_key
506
511
  }, this.demo);
507
- const limits = await fetchAccountLimits(accountIndex, authToken, this.demo);
512
+ const limits = await fetchAccountLimits(accountIndex, authToken, this.connector, this.demo);
508
513
  const accountTier = toLighterAccountTier(limits.user_tier);
509
514
  if (lifecycleVersion !== this.lifecycleVersion) {
510
515
  return;
@@ -565,7 +570,7 @@ class LighterAccountTracker extends BaseAccountTracker {
565
570
  this.marketsBySymbol.set(market.symbol, market);
566
571
  }
567
572
  if (markets.spot.length > 0) {
568
- this.spotPriceBySymbol = buildSpotPriceIndex(markets.spot);
573
+ this.spotPriceBySymbol = buildSpotPriceIndex(markets.spot, this.quoteAsset);
569
574
  }
570
575
  const balance = this.buildBalance();
571
576
  if (balance) this.onStateUpdate({
@@ -578,13 +583,14 @@ class LighterAccountTracker extends BaseAccountTracker {
578
583
  }
579
584
  }
580
585
  }
581
- function toMarketContext(perpMarkets, spotMarkets) {
586
+ function toMarketContext(perpMarkets, spotMarkets, quoteAsset) {
582
587
  return {
583
588
  perpByMarketId: new Map(perpMarkets.map((market) => [
584
589
  market.market_id,
585
590
  market
586
591
  ])),
587
- spotPriceBySymbol: buildSpotPriceIndex(spotMarkets)
592
+ spotPriceBySymbol: buildSpotPriceIndex(spotMarkets, quoteAsset),
593
+ quoteAsset
588
594
  };
589
595
  }
590
596
  __name(toMarketContext, "toMarketContext");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/exchanges-sdk",
3
- "version": "0.14.1",
3
+ "version": "0.15.0",
4
4
  "description": "Pear Protocol Exchanges SDK",
5
5
  "private": false,
6
6
  "type": "module",
@@ -27,9 +27,9 @@
27
27
  "test:watch": "vitest"
28
28
  },
29
29
  "dependencies": {
30
- "@pear-protocol/core-sdk": "^2.0.0",
31
- "@pear-protocol/types": "^2.0.0",
32
- "@pear-protocol/utils": "^1.0.0",
30
+ "@pear-protocol/core-sdk": "^2.2.0",
31
+ "@pear-protocol/types": "^2.2.0",
32
+ "@pear-protocol/utils": "^1.1.0",
33
33
  "bignumber.js": "^11.1.1",
34
34
  "partysocket": "^1.0.3",
35
35
  "viem": "^2.48.8",