@scallop-io/sui-scallop-sdk 2.3.4 → 2.3.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scallop-io/sui-scallop-sdk",
3
- "version": "2.3.4",
3
+ "version": "2.3.6",
4
4
  "description": "Typescript sdk for interacting with Scallop contract on SUI",
5
5
  "keywords": [
6
6
  "sui",
@@ -74,7 +74,7 @@ export const queryKeys = {
74
74
  },
75
75
  },
76
76
  oracle: {
77
- getPythLatestPriceFeeds: (priceIds: string[], endpoint: string) => [
77
+ getPythLatestPriceFeeds: (endpoint?: string, priceIds?: string[]) => [
78
78
  'oracle',
79
79
  'getPythPriceIds',
80
80
  priceIds,
@@ -505,7 +505,7 @@ class ScallopUtils implements ScallopUtilsInterface {
505
505
  * Get asset coin price.
506
506
  *
507
507
  * @description
508
- * The strategy for obtaining the price is to get it through API first,
508
+ * The strategy for obtaining the price is to get it through pyth API first,
509
509
  * and then on-chain data if API cannot be retrieved.
510
510
  * Currently, we only support obtaining from pyth protocol, other
511
511
  * oracles will be supported in the future.
@@ -519,16 +519,24 @@ class ScallopUtils implements ScallopUtilsInterface {
519
519
  ...this.constants.whitelist.lending,
520
520
  ...this.constants.whitelist.collateral,
521
521
  ]),
522
- ] as string[]
522
+ ] as string[],
523
+ useOnChainObjects: boolean = false
523
524
  ) {
524
525
  const priceIdsMap = new Map(
525
- coinNames.map((coinName) => [
526
- coinName,
527
- this.address.get(`core.coins.${coinName}.oracle.pyth.feed`),
528
- ])
526
+ coinNames
527
+ .map((coinName) => {
528
+ const priceId = this.address.get(
529
+ `core.coins.${coinName}.oracle.pyth.feed`
530
+ );
531
+ return priceId
532
+ ? ([coinName, priceId] as [string, string])
533
+ : undefined;
534
+ })
535
+ .filter((entry): entry is [string, string] => !!entry)
529
536
  );
530
537
 
531
538
  const priceIds = Array.from(priceIdsMap.values());
539
+ const coinNamesMapped = Array.from(priceIdsMap.keys());
532
540
  const state = this.queryClient.getQueryState(
533
541
  queryKeys.oracle.getCoinPrices(priceIds)
534
542
  );
@@ -538,45 +546,48 @@ class ScallopUtils implements ScallopUtilsInterface {
538
546
  }
539
547
 
540
548
  let coinPrices: CoinPrices = {};
541
- for (const endpoint of this.pythEndpoints) {
542
- const pythConnection = new SuiPriceServiceConnection(endpoint, {
543
- timeout: this.timeout,
544
- httpRetries: 0,
545
- });
546
549
 
547
- try {
548
- const feeds = await this.queryClient.fetchQuery({
549
- queryKey: queryKeys.oracle.getPythLatestPriceFeeds(
550
- priceIds,
551
- endpoint
552
- ),
553
- queryFn: async () => {
554
- return await pythConnection.getLatestPriceFeeds(priceIds);
555
- },
556
- retry: false,
557
- staleTime: 30_000,
558
- gcTime: 30_000,
550
+ if (!useOnChainObjects) {
551
+ for (const endpoint of this.pythEndpoints) {
552
+ const pythConnection = new SuiPriceServiceConnection(endpoint, {
553
+ timeout: this.timeout,
554
+ httpRetries: 0,
559
555
  });
560
- if (!feeds) throw new Error('No feeds returned from pyth');
561
-
562
- if (feeds.length !== priceIds.length)
563
- throw new Error('Incomplete feeds returned from pyth');
564
556
 
565
- feeds.forEach((feed, idx) => {
566
- const coinName = coinNames[idx] as string;
567
- const data = this.parseDataFromPythPriceFeed(feed);
568
- coinPrices[coinName as string] = data.price;
569
- });
570
- this.queryClient.setQueryData(
571
- queryKeys.oracle.getCoinPrices(priceIds),
572
- coinPrices
573
- );
574
- } catch (e: any) {
575
- if ('status' in e && e.status === 403) {
576
- console.log(`trying next pyth endpoint`);
577
- continue; // try next endpoint
557
+ try {
558
+ const feeds = await this.queryClient.fetchQuery({
559
+ queryKey: queryKeys.oracle.getPythLatestPriceFeeds(
560
+ endpoint,
561
+ priceIds
562
+ ),
563
+ queryFn: async () => {
564
+ return await pythConnection.getLatestPriceFeeds(priceIds);
565
+ },
566
+ retry: false,
567
+ staleTime: 30_000,
568
+ gcTime: 30_000,
569
+ });
570
+ if (!feeds) throw new Error('No feeds returned from pyth');
571
+
572
+ if (feeds.length !== priceIds.length)
573
+ throw new Error('Incomplete feeds returned from pyth');
574
+
575
+ feeds.forEach((feed, idx) => {
576
+ const coinName = coinNamesMapped[idx] as string;
577
+ const data = this.parseDataFromPythPriceFeed(feed);
578
+ coinPrices[coinName as string] = data.price;
579
+ });
580
+ this.queryClient.setQueryData(
581
+ queryKeys.oracle.getCoinPrices(priceIds),
582
+ coinPrices
583
+ );
584
+ } catch (e: any) {
585
+ if ('status' in e && e.status === 403) {
586
+ console.log(`trying next pyth endpoint`);
587
+ continue; // try next endpoint
588
+ }
589
+ console.error(e.message);
578
590
  }
579
- console.error(e.message);
580
591
  }
581
592
  }
582
593
 
@@ -591,7 +602,13 @@ class ScallopUtils implements ScallopUtilsInterface {
591
602
  );
592
603
  }
593
604
 
594
- return coinPrices;
605
+ return {
606
+ ...coinNames.reduce((prev, coinName) => {
607
+ prev[coinName as string] = coinPrices[coinName as string] || 0;
608
+ return prev;
609
+ }, {} as CoinPrices),
610
+ ...coinPrices,
611
+ };
595
612
  }
596
613
 
597
614
  /**
@@ -42,8 +42,6 @@ const queryRequiredSpoolObjects = async (
42
42
  const objectDatas: SuiObjectData[] = [];
43
43
  const batches = partitionArray(allObjectIds, 50);
44
44
 
45
- console.log('Fetching spool objects in batches:', batches.length);
46
-
47
45
  for (const batch of batches) {
48
46
  const responses = await query.scallopSuiKit.queryGetObjects(batch);
49
47
  if (responses.length > 0) {