@chainstream-io/sdk 0.1.21 → 0.1.23

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/index.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- export { q as DexAggregatorOptions, r as DexClient, p as DexRequestContext, L as LIB_VERSION, P as PostOptions, o as TokenProvider } from './index-D3RVUCiG.cjs';
2
- import './WatchlistApi-18jD3YH5.cjs';
1
+ export { q as DexAggregatorOptions, r as DexClient, p as DexRequestContext, L as LIB_VERSION, P as PostOptions, o as TokenProvider } from './index-CjzuX57e.cjs';
2
+ import './WatchlistApi-VmKx6Q1Q.cjs';
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { q as DexAggregatorOptions, r as DexClient, p as DexRequestContext, L as LIB_VERSION, P as PostOptions, o as TokenProvider } from './index-BhyMM-wS.js';
2
- import './WatchlistApi-18jD3YH5.js';
1
+ export { q as DexAggregatorOptions, r as DexClient, p as DexRequestContext, L as LIB_VERSION, P as PostOptions, o as TokenProvider } from './index-omaVsSSm.js';
2
+ import './WatchlistApi-VmKx6Q1Q.js';
package/dist/index.mjs CHANGED
@@ -264,10 +264,24 @@ var StreamApi = class {
264
264
  constructor(context) {
265
265
  __publicField(this, "realtimeClient");
266
266
  __publicField(this, "listenersMap");
267
- const realtimeEndpoint = this.buildWsUrl(context.streamUrl, context.accessToken);
267
+ __publicField(this, "context");
268
+ __publicField(this, "initPromise", null);
269
+ this.context = context;
270
+ this.listenersMap = /* @__PURE__ */ new Map();
271
+ }
272
+ /**
273
+ * Initialize Centrifuge client with current token
274
+ * This is called lazily when connect() is called
275
+ */
276
+ async initClient() {
277
+ if (this.realtimeClient) {
278
+ return;
279
+ }
280
+ const token = await this.getTokenValue();
281
+ const realtimeEndpoint = this.buildWsUrl(this.context.streamUrl, token);
268
282
  this.realtimeClient = new Centrifuge(realtimeEndpoint, {
269
283
  getToken: async (_ctx) => {
270
- return typeof context.accessToken === "string" ? context.accessToken : await context.accessToken.getToken();
284
+ return this.getTokenValue();
271
285
  }
272
286
  });
273
287
  this.realtimeClient.on("connected", () => {
@@ -277,34 +291,73 @@ var StreamApi = class {
277
291
  }).on("error", (err) => {
278
292
  console.error("[streaming] error: ", err);
279
293
  });
280
- this.listenersMap = /* @__PURE__ */ new Map();
294
+ }
295
+ /**
296
+ * Wait for initialization to complete
297
+ * If not initialized, auto-connect first
298
+ */
299
+ async ensureInitialized() {
300
+ if (!this.initPromise) {
301
+ this.connect();
302
+ }
303
+ await this.initPromise;
304
+ }
305
+ /**
306
+ * Get token value from string or TokenProvider
307
+ */
308
+ async getTokenValue() {
309
+ return typeof this.context.accessToken === "string" ? this.context.accessToken : await this.context.accessToken.getToken();
281
310
  }
282
311
  /**
283
312
  * Build WebSocket URL with token as query parameter
284
313
  */
285
- buildWsUrl(endpoint, accessToken) {
314
+ buildWsUrl(endpoint, token) {
286
315
  const url = new URL(endpoint);
287
- if (typeof accessToken === "string") {
288
- url.searchParams.set("token", accessToken);
289
- }
316
+ url.searchParams.set("token", token);
290
317
  return url.toString();
291
318
  }
319
+ /**
320
+ * Connect to the WebSocket server.
321
+ * This is automatically called when you use subscribe methods if not already connected.
322
+ * You can also call this method manually for explicit control.
323
+ */
292
324
  connect() {
293
- this.realtimeClient.connect();
325
+ this.initPromise = this.initClient().then(() => {
326
+ this.realtimeClient.connect();
327
+ });
328
+ }
329
+ /**
330
+ * Disconnect from the WebSocket server.
331
+ * All subscriptions will be automatically removed.
332
+ */
333
+ disconnect() {
334
+ if (this.realtimeClient) {
335
+ this.realtimeClient.disconnect();
336
+ }
337
+ }
338
+ /**
339
+ * Check if the WebSocket is currently connected.
340
+ */
341
+ isConnected() {
342
+ return this.realtimeClient?.state === "connected";
294
343
  }
295
344
  /**
296
345
  * Start batching commands for efficient bulk operations
297
346
  * All subscription commands after this call will be batched until stopBatching is called
298
347
  */
299
348
  startBatching() {
300
- this.realtimeClient.startBatching();
349
+ if (this.realtimeClient) {
350
+ this.realtimeClient.startBatching();
351
+ }
301
352
  }
302
353
  /**
303
354
  * Stop batching and flush all collected commands to the server
304
355
  * This will send all batched subscription commands in a single network request
305
356
  */
306
357
  stopBatching() {
307
- this.realtimeClient.stopBatching();
358
+ if (this.realtimeClient) {
359
+ this.realtimeClient.stopBatching();
360
+ }
308
361
  }
309
362
  /**
310
363
  * Batch subscribe method that accepts a function containing subscription calls
@@ -334,26 +387,31 @@ var StreamApi = class {
334
387
  });
335
388
  }
336
389
  subscribe(channel, fn, filter, methodName) {
337
- let sub = this.realtimeClient.getSubscription(channel);
338
390
  let listeners = this.listenersMap.get(channel);
339
- if (!sub) {
391
+ if (!listeners) {
340
392
  listeners = /* @__PURE__ */ new Set();
341
393
  this.listenersMap.set(channel, listeners);
342
- console.log("[xrealtime] create new sub: ", channel);
343
- const processedFilter = filter && methodName ? replaceFilterFields(filter, methodName) : filter;
344
- sub = this.realtimeClient.newSubscription(channel, {
345
- delta: "fossil",
346
- ...processedFilter && { filter: processedFilter }
347
- });
348
- sub.on("subscribed", () => {
349
- console.log("[xrealtime] subscribed", channel);
350
- }).on("unsubscribed", () => {
351
- console.log("[xrealtime] unsubscribed", channel);
352
- }).on("publication", (ctx) => {
353
- listeners?.forEach((it) => it(ctx.data));
354
- }).subscribe();
355
- }
356
- listeners?.add(fn);
394
+ }
395
+ listeners.add(fn);
396
+ this.ensureInitialized().then(() => {
397
+ let sub = this.realtimeClient.getSubscription(channel);
398
+ if (!sub) {
399
+ console.log("[xrealtime] create new sub: ", channel);
400
+ const processedFilter = filter && methodName ? replaceFilterFields(filter, methodName) : filter;
401
+ sub = this.realtimeClient.newSubscription(channel, {
402
+ delta: "fossil",
403
+ ...processedFilter && { filter: processedFilter }
404
+ });
405
+ sub.on("subscribed", () => {
406
+ console.log("[xrealtime] subscribed", channel);
407
+ }).on("unsubscribed", () => {
408
+ console.log("[xrealtime] unsubscribed", channel);
409
+ }).on("publication", (ctx) => {
410
+ const currentListeners = this.listenersMap.get(channel);
411
+ currentListeners?.forEach((it) => it(ctx.data));
412
+ }).subscribe();
413
+ }
414
+ });
357
415
  return new StreamUnsubscrible(this, channel, fn);
358
416
  }
359
417
  unsubscribe(channel, fn) {
@@ -365,12 +423,14 @@ var StreamApi = class {
365
423
  console.log("unsubscribe, remain listeners: ", listeners.size);
366
424
  if (listeners.size === 0) {
367
425
  console.log("unsubscribe channel: ", channel);
368
- const sub = this.realtimeClient.getSubscription(channel);
369
- if (sub) {
370
- sub.unsubscribe();
371
- this.realtimeClient.removeSubscription(sub);
372
- }
373
426
  this.listenersMap.delete(channel);
427
+ this.ensureInitialized().then(() => {
428
+ const sub = this.realtimeClient.getSubscription(channel);
429
+ if (sub) {
430
+ sub.unsubscribe();
431
+ this.realtimeClient.removeSubscription(sub);
432
+ }
433
+ });
374
434
  }
375
435
  }
376
436
  formatScientificNotation(value) {
@@ -1839,7 +1899,9 @@ function AddressExposureFromJSONTyped(json, ignoreDiscriminator) {
1839
1899
  }
1840
1900
  return {
1841
1901
  "category": json["category"],
1842
- "value": json["value"]
1902
+ "value": json["value"],
1903
+ "exposureType": json["exposureType"],
1904
+ "direction": json["direction"]
1843
1905
  };
1844
1906
  }
1845
1907
 
@@ -2715,110 +2777,6 @@ function KYTRegisterWithdrawalRequestToJSONTyped(value, ignoreDiscriminator = fa
2715
2777
  };
2716
2778
  }
2717
2779
 
2718
- // src/openapi/models/KytAddressDTO.ts
2719
- function KytAddressDTOFromJSON(json) {
2720
- return KytAddressDTOFromJSONTyped(json, false);
2721
- }
2722
- function KytAddressDTOFromJSONTyped(json, ignoreDiscriminator) {
2723
- if (json == null) {
2724
- return json;
2725
- }
2726
- return {
2727
- "id": json["id"],
2728
- "orgId": json["orgId"],
2729
- "address": json["address"],
2730
- "createdAt": new Date(json["createdAt"]),
2731
- "updatedAt": new Date(json["updatedAt"])
2732
- };
2733
- }
2734
-
2735
- // src/openapi/models/KytAddressPage.ts
2736
- function KytAddressPageFromJSON(json) {
2737
- return KytAddressPageFromJSONTyped(json, false);
2738
- }
2739
- function KytAddressPageFromJSONTyped(json, ignoreDiscriminator) {
2740
- if (json == null) {
2741
- return json;
2742
- }
2743
- return {
2744
- "total": json["total"],
2745
- "page": json["page"],
2746
- "pageSize": json["pageSize"],
2747
- "totalPages": json["totalPages"],
2748
- "data": json["data"].map(KytAddressDTOFromJSON)
2749
- };
2750
- }
2751
-
2752
- // src/openapi/models/KytTransferDTO.ts
2753
- function KytTransferDTOFromJSON(json) {
2754
- return KytTransferDTOFromJSONTyped(json, false);
2755
- }
2756
- function KytTransferDTOFromJSONTyped(json, ignoreDiscriminator) {
2757
- if (json == null) {
2758
- return json;
2759
- }
2760
- return {
2761
- "id": json["id"],
2762
- "orgId": json["orgId"],
2763
- "txHash": json["txHash"],
2764
- "externalId": json["externalId"],
2765
- "createdAt": new Date(json["createdAt"]),
2766
- "updatedAt": new Date(json["updatedAt"])
2767
- };
2768
- }
2769
-
2770
- // src/openapi/models/KytTransferPage.ts
2771
- function KytTransferPageFromJSON(json) {
2772
- return KytTransferPageFromJSONTyped(json, false);
2773
- }
2774
- function KytTransferPageFromJSONTyped(json, ignoreDiscriminator) {
2775
- if (json == null) {
2776
- return json;
2777
- }
2778
- return {
2779
- "total": json["total"],
2780
- "page": json["page"],
2781
- "pageSize": json["pageSize"],
2782
- "totalPages": json["totalPages"],
2783
- "data": json["data"].map(KytTransferDTOFromJSON)
2784
- };
2785
- }
2786
-
2787
- // src/openapi/models/KytWithdrawalDTO.ts
2788
- function KytWithdrawalDTOFromJSON(json) {
2789
- return KytWithdrawalDTOFromJSONTyped(json, false);
2790
- }
2791
- function KytWithdrawalDTOFromJSONTyped(json, ignoreDiscriminator) {
2792
- if (json == null) {
2793
- return json;
2794
- }
2795
- return {
2796
- "id": json["id"],
2797
- "orgId": json["orgId"],
2798
- "address": json["address"],
2799
- "externalId": json["externalId"],
2800
- "createdAt": new Date(json["createdAt"]),
2801
- "updatedAt": new Date(json["updatedAt"])
2802
- };
2803
- }
2804
-
2805
- // src/openapi/models/KytWithdrawalPage.ts
2806
- function KytWithdrawalPageFromJSON(json) {
2807
- return KytWithdrawalPageFromJSONTyped(json, false);
2808
- }
2809
- function KytWithdrawalPageFromJSONTyped(json, ignoreDiscriminator) {
2810
- if (json == null) {
2811
- return json;
2812
- }
2813
- return {
2814
- "total": json["total"],
2815
- "page": json["page"],
2816
- "pageSize": json["pageSize"],
2817
- "totalPages": json["totalPages"],
2818
- "data": json["data"].map(KytWithdrawalDTOFromJSON)
2819
- };
2820
- }
2821
-
2822
2780
  // src/openapi/models/Link.ts
2823
2781
  function LinkToJSON(json) {
2824
2782
  return LinkToJSONTyped(json, false);
@@ -4537,7 +4495,7 @@ var EndpointApi = class extends BaseAPI {
4537
4495
  headerParameters["Authorization"] = `Bearer ${tokenString}`;
4538
4496
  }
4539
4497
  }
4540
- let urlPath = `/v1/webhook/endpoint/{id}`;
4498
+ let urlPath = `/v1/webhook/endpoint`;
4541
4499
  const response = await this.request({
4542
4500
  path: urlPath,
4543
4501
  method: "PATCH",
@@ -4716,117 +4674,6 @@ var KYTApi = class extends BaseAPI {
4716
4674
  const response = await this.getAddressRiskRaw(requestParameters, initOverrides);
4717
4675
  return await response.value();
4718
4676
  }
4719
- /**
4720
- * CONTROLLER.KYT.GET_KYT_ADDRESSES.DESCRIPTION
4721
- * CONTROLLER.KYT.GET_KYT_ADDRESSES.SUMMARY
4722
- */
4723
- async getKytAddressesRaw(requestParameters, initOverrides) {
4724
- const queryParameters = {};
4725
- if (requestParameters["page"] != null) {
4726
- queryParameters["page"] = requestParameters["page"];
4727
- }
4728
- if (requestParameters["pageSize"] != null) {
4729
- queryParameters["pageSize"] = requestParameters["pageSize"];
4730
- }
4731
- const headerParameters = {};
4732
- if (this.configuration && this.configuration.accessToken) {
4733
- const token = this.configuration.accessToken;
4734
- const tokenString = await token("bearer", []);
4735
- if (tokenString) {
4736
- headerParameters["Authorization"] = `Bearer ${tokenString}`;
4737
- }
4738
- }
4739
- let urlPath = `/v1/kyt/addresses`;
4740
- const response = await this.request({
4741
- path: urlPath,
4742
- method: "GET",
4743
- headers: headerParameters,
4744
- query: queryParameters
4745
- }, initOverrides);
4746
- return new JSONApiResponse(response, (jsonValue) => KytAddressPageFromJSON(jsonValue));
4747
- }
4748
- /**
4749
- * CONTROLLER.KYT.GET_KYT_ADDRESSES.DESCRIPTION
4750
- * CONTROLLER.KYT.GET_KYT_ADDRESSES.SUMMARY
4751
- */
4752
- async getKytAddresses(requestParameters = {}, initOverrides) {
4753
- const response = await this.getKytAddressesRaw(requestParameters, initOverrides);
4754
- return await response.value();
4755
- }
4756
- /**
4757
- * CONTROLLER.KYT.GET_KYT_TRANSFERS.DESCRIPTION
4758
- * CONTROLLER.KYT.GET_KYT_TRANSFERS.SUMMARY
4759
- */
4760
- async getKytTransfersRaw(requestParameters, initOverrides) {
4761
- const queryParameters = {};
4762
- if (requestParameters["page"] != null) {
4763
- queryParameters["page"] = requestParameters["page"];
4764
- }
4765
- if (requestParameters["pageSize"] != null) {
4766
- queryParameters["pageSize"] = requestParameters["pageSize"];
4767
- }
4768
- const headerParameters = {};
4769
- if (this.configuration && this.configuration.accessToken) {
4770
- const token = this.configuration.accessToken;
4771
- const tokenString = await token("bearer", []);
4772
- if (tokenString) {
4773
- headerParameters["Authorization"] = `Bearer ${tokenString}`;
4774
- }
4775
- }
4776
- let urlPath = `/v1/kyt/transfers`;
4777
- const response = await this.request({
4778
- path: urlPath,
4779
- method: "GET",
4780
- headers: headerParameters,
4781
- query: queryParameters
4782
- }, initOverrides);
4783
- return new JSONApiResponse(response, (jsonValue) => KytTransferPageFromJSON(jsonValue));
4784
- }
4785
- /**
4786
- * CONTROLLER.KYT.GET_KYT_TRANSFERS.DESCRIPTION
4787
- * CONTROLLER.KYT.GET_KYT_TRANSFERS.SUMMARY
4788
- */
4789
- async getKytTransfers(requestParameters = {}, initOverrides) {
4790
- const response = await this.getKytTransfersRaw(requestParameters, initOverrides);
4791
- return await response.value();
4792
- }
4793
- /**
4794
- * CONTROLLER.KYT.GET_KYT_WITHDRAWALS.DESCRIPTION
4795
- * CONTROLLER.KYT.GET_KYT_WITHDRAWALS.SUMMARY
4796
- */
4797
- async getKytWithdrawalsRaw(requestParameters, initOverrides) {
4798
- const queryParameters = {};
4799
- if (requestParameters["page"] != null) {
4800
- queryParameters["page"] = requestParameters["page"];
4801
- }
4802
- if (requestParameters["pageSize"] != null) {
4803
- queryParameters["pageSize"] = requestParameters["pageSize"];
4804
- }
4805
- const headerParameters = {};
4806
- if (this.configuration && this.configuration.accessToken) {
4807
- const token = this.configuration.accessToken;
4808
- const tokenString = await token("bearer", []);
4809
- if (tokenString) {
4810
- headerParameters["Authorization"] = `Bearer ${tokenString}`;
4811
- }
4812
- }
4813
- let urlPath = `/v1/kyt/withdrawals`;
4814
- const response = await this.request({
4815
- path: urlPath,
4816
- method: "GET",
4817
- headers: headerParameters,
4818
- query: queryParameters
4819
- }, initOverrides);
4820
- return new JSONApiResponse(response, (jsonValue) => KytWithdrawalPageFromJSON(jsonValue));
4821
- }
4822
- /**
4823
- * CONTROLLER.KYT.GET_KYT_WITHDRAWALS.DESCRIPTION
4824
- * CONTROLLER.KYT.GET_KYT_WITHDRAWALS.SUMMARY
4825
- */
4826
- async getKytWithdrawals(requestParameters = {}, initOverrides) {
4827
- const response = await this.getKytWithdrawalsRaw(requestParameters, initOverrides);
4828
- return await response.value();
4829
- }
4830
4677
  /**
4831
4678
  * CONTROLLER.KYT.GET_TRANSFER_ALERTS.DESCRIPTION
4832
4679
  * CONTROLLER.KYT.GET_TRANSFER_ALERTS.SUMMARY
@@ -8147,7 +7994,7 @@ var WatchlistApi = class extends BaseAPI {
8147
7994
 
8148
7995
  // src/index.ts
8149
7996
  import { EventSourcePolyfill } from "event-source-polyfill";
8150
- var LIB_VERSION = "0.1.20";
7997
+ var LIB_VERSION = "0.1.23";
8151
7998
  var UserAgentMiddleware = class {
8152
7999
  async pre(context) {
8153
8000
  if (!context.init.headers) {
@@ -8185,9 +8032,6 @@ var DexClient = class {
8185
8032
  __publicField(this, "endpoint");
8186
8033
  const baseUrl = options.serverUrl ?? "https://api-dex.chainstream.io";
8187
8034
  const streamUrl = options.streamUrl ?? "wss://realtime-dex.chainstream.io/connection/websocket";
8188
- const tokenProvider = typeof accessToken === "string" ? {
8189
- getToken: () => accessToken
8190
- } : accessToken;
8191
8035
  this.requestCtx = { baseUrl, streamUrl, accessToken };
8192
8036
  const config = new Configuration({
8193
8037
  basePath: baseUrl,
@@ -8217,7 +8061,9 @@ var DexClient = class {
8217
8061
  this.jobs = new JobsApi(config);
8218
8062
  this.kyt = new KYTApi(config);
8219
8063
  this.endpoint = new EndpointApi(config);
8220
- this.stream.connect();
8064
+ if (options.autoConnectWebSocket === true) {
8065
+ this.stream.connect();
8066
+ }
8221
8067
  }
8222
8068
  async waitForJob(jobId, timeout = 6e4) {
8223
8069
  const accessToken = typeof this.requestCtx.accessToken === "string" ? this.requestCtx.accessToken : await this.requestCtx.accessToken.getToken();