@scallop-io/sui-scallop-sdk 1.3.1 → 1.3.2-alpha.2

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.
@@ -37,7 +37,7 @@ export declare class ScallopCache {
37
37
  * - `all`: All queries that match the refetch predicate will be refetched in the background.
38
38
  * - `none`: No queries will be refetched. Queries that match the refetch predicate will only be marked as invalid.
39
39
  */
40
- invalidateAndRefetchAllCache(refetchType: 'all' | 'active' | 'inactive' | 'none'): Promise<void>;
40
+ invalidateAllCache(): Promise<Promise<void>[]>;
41
41
  /**
42
42
  * @description Provides cache for inspectTxn of the SuiKit.
43
43
  * @param QueryInspectTxnParams
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scallop-io/sui-scallop-sdk",
3
- "version": "1.3.1",
3
+ "version": "1.3.2-alpha.2",
4
4
  "description": "Typescript sdk for interacting with Scallop contract on SUI",
5
5
  "keywords": [
6
6
  "sui",
@@ -1,12 +1,10 @@
1
- import { QueryClientConfig } from '@tanstack/query-core';
2
-
3
1
  /**
4
2
  * Default cache options for the QueryClient.
5
3
  * @type {QueryClientConfig}
6
4
  * @description Default cache options for the QueryClient
7
5
  * We set the default to 5s to prevent duplicate requests from being requested (e.g. query MarketObject, etc.)
8
6
  */
9
- export const DEFAULT_CACHE_OPTIONS: QueryClientConfig = {
7
+ export const DEFAULT_CACHE_OPTIONS = {
10
8
  defaultOptions: {
11
9
  queries: {
12
10
  staleTime: 5000,
@@ -52,31 +52,31 @@ export const queryKeys = {
52
52
  objectIds: JSON.stringify(objectIds ?? []),
53
53
  },
54
54
  ],
55
- getOwnedObjects: (input: Partial<GetOwnedObjectsParams>) => [
55
+ getOwnedObjects: (input?: Partial<GetOwnedObjectsParams>) => [
56
56
  'rpc',
57
57
  'getOwnedObjects',
58
58
  {
59
- walletAddress: input.owner,
60
- cursor: input.cursor ?? undefined,
61
- options: input.options ?? undefined,
62
- filter: JSON.stringify(input.filter ?? undefined),
63
- limit: input.limit ?? undefined,
59
+ walletAddress: input?.owner,
60
+ cursor: input?.cursor ?? undefined,
61
+ options: input?.options ?? undefined,
62
+ filter: JSON.stringify(input?.filter ?? undefined),
63
+ limit: input?.limit ?? undefined,
64
64
  },
65
65
  ],
66
- getDynamicFields: (input: Partial<GetDynamicFieldsParams>) => [
66
+ getDynamicFields: (input?: Partial<GetDynamicFieldsParams>) => [
67
67
  'rpc',
68
68
  'getDynamicFields',
69
69
  {
70
- parentId: input.parentId,
71
- cursor: input.cursor ?? undefined,
72
- limit: input.limit ?? undefined,
70
+ parentId: input?.parentId,
71
+ cursor: input?.cursor ?? undefined,
72
+ limit: input?.limit ?? undefined,
73
73
  },
74
74
  ],
75
- getDynamicFieldObject: (input: Partial<GetDynamicFieldObjectParams>) => [
75
+ getDynamicFieldObject: (input?: Partial<GetDynamicFieldObjectParams>) => [
76
76
  'rpc',
77
77
  'getDynamicFieldObject',
78
78
  {
79
- parentId: input.parentId,
79
+ parentId: input?.parentId,
80
80
  name: {
81
81
  type: input?.name?.type,
82
82
  value: input?.name?.value,
@@ -31,8 +31,6 @@ type QueryInspectTxnParams = {
31
31
  typeArgs?: any[];
32
32
  };
33
33
 
34
- const DEFAULT_GC_TIME = 5 * 1000; // 5 seconds
35
-
36
34
  /**
37
35
  * @description
38
36
  * It provides caching for moveCall, RPC Request, and API Request.
@@ -99,12 +97,13 @@ export class ScallopCache {
99
97
  * - `all`: All queries that match the refetch predicate will be refetched in the background.
100
98
  * - `none`: No queries will be refetched. Queries that match the refetch predicate will only be marked as invalid.
101
99
  */
102
- public invalidateAndRefetchAllCache(
103
- refetchType: 'all' | 'active' | 'inactive' | 'none'
104
- ) {
105
- return this.queryClient.invalidateQueries({
106
- refetchType,
107
- });
100
+ public async invalidateAllCache() {
101
+ return Object.values(queryKeys.rpc).map((t) =>
102
+ this.queryClient.invalidateQueries({
103
+ queryKey: t(),
104
+ type: 'all',
105
+ })
106
+ );
108
107
  }
109
108
 
110
109
  /**
@@ -129,7 +128,6 @@ export class ScallopCache {
129
128
  this.suiKit.inspectTxn(txBlock)
130
129
  );
131
130
  },
132
- gcTime: DEFAULT_GC_TIME,
133
131
  });
134
132
  return query;
135
133
  }
@@ -154,7 +152,6 @@ export class ScallopCache {
154
152
  })
155
153
  );
156
154
  },
157
- gcTime: DEFAULT_GC_TIME,
158
155
  });
159
156
  }
160
157
 
@@ -183,7 +180,6 @@ export class ScallopCache {
183
180
  this.suiKit.getObjects(objectIds, options)
184
181
  );
185
182
  },
186
- gcTime: DEFAULT_GC_TIME,
187
183
  });
188
184
  }
189
185
 
@@ -200,7 +196,6 @@ export class ScallopCache {
200
196
  this.client.getOwnedObjects(input)
201
197
  );
202
198
  },
203
- gcTime: DEFAULT_GC_TIME,
204
199
  });
205
200
  }
206
201
 
@@ -214,7 +209,6 @@ export class ScallopCache {
214
209
  this.client.getDynamicFields(input)
215
210
  );
216
211
  },
217
- gcTime: DEFAULT_GC_TIME,
218
212
  });
219
213
  }
220
214
 
@@ -228,7 +222,6 @@ export class ScallopCache {
228
222
  this.client.getDynamicFieldObject(input)
229
223
  );
230
224
  },
231
- gcTime: DEFAULT_GC_TIME,
232
225
  });
233
226
  }
234
227
 
@@ -524,6 +524,11 @@ export class ScallopUtils {
524
524
  existPricesCoinNames.push(assetCoinName);
525
525
  } else {
526
526
  lackPricesCoinNames.push(assetCoinName);
527
+ this.cache.queryClient.invalidateQueries({
528
+ queryKey: queryKeys.oracle.getPythLatestPriceFeed(
529
+ this.address.get(`core.coins.${assetCoinName}.oracle.pyth.feed`)
530
+ ),
531
+ });
527
532
  }
528
533
  });
529
534
 
@@ -558,7 +563,7 @@ export class ScallopUtils {
558
563
  Object.entries(priceIds).map(async ([coinName, priceId]) => {
559
564
  const pythConnection = new SuiPriceServiceConnection(endpoint);
560
565
  try {
561
- const feed = await this.address.cache.queryClient.fetchQuery({
566
+ const feed = await this.cache.queryClient.fetchQuery({
562
567
  queryKey: queryKeys.oracle.getPythLatestPriceFeed(priceId),
563
568
  queryFn: async () => {
564
569
  return await pythConnection.getLatestPriceFeeds([priceId]);
@@ -29,42 +29,47 @@ const ISOLATED_ASSET_KEY =
29
29
  export const getIsolatedAssets = async (
30
30
  address: ScallopAddress
31
31
  ): Promise<string[]> => {
32
- const marketObject = address.get('core.market');
33
- const isolatedAssets: string[] = [];
34
- if (!marketObject) return isolatedAssets;
32
+ try {
33
+ const marketObject = address.get('core.market');
34
+ const isolatedAssets: string[] = [];
35
+ if (!marketObject) return isolatedAssets;
35
36
 
36
- let hasNextPage = false;
37
- let nextCursor: string | null | undefined = null;
37
+ let hasNextPage = false;
38
+ let nextCursor: string | null | undefined = null;
38
39
 
39
- const isIsolatedDynamicField = (
40
- dynamicField: DynamicFieldInfo
41
- ): dynamicField is DynamicFieldInfo & {
42
- name: DynamicFieldName & { value: { type: { name: string } } };
43
- } => {
44
- return dynamicField.name.type === ISOLATED_ASSET_KEY;
45
- };
40
+ const isIsolatedDynamicField = (
41
+ dynamicField: DynamicFieldInfo
42
+ ): dynamicField is DynamicFieldInfo & {
43
+ name: DynamicFieldName & { value: { type: { name: string } } };
44
+ } => {
45
+ return dynamicField.name.type === ISOLATED_ASSET_KEY;
46
+ };
46
47
 
47
- do {
48
- const response = await address.cache.queryGetDynamicFields({
49
- parentId: marketObject,
50
- cursor: nextCursor,
51
- limit: 10,
52
- });
53
- if (!response) break;
48
+ do {
49
+ const response = await address.cache.queryGetDynamicFields({
50
+ parentId: marketObject,
51
+ cursor: nextCursor,
52
+ limit: 10,
53
+ });
54
+ if (!response) break;
54
55
 
55
- const isolatedAssetCoinTypes = response.data
56
- .filter(isIsolatedDynamicField)
57
- .map(({ name }) => `0x${name.value.type.name}`);
58
- isolatedAssets.push(...isolatedAssetCoinTypes);
56
+ const isolatedAssetCoinTypes = response.data
57
+ .filter(isIsolatedDynamicField)
58
+ .map(({ name }) => `0x${name.value.type.name}`);
59
+ isolatedAssets.push(...isolatedAssetCoinTypes);
59
60
 
60
- if (response && response.hasNextPage && response.nextCursor) {
61
- hasNextPage = true;
62
- nextCursor = response.nextCursor;
63
- } else {
64
- hasNextPage = false;
65
- }
66
- } while (hasNextPage);
67
- return isolatedAssets;
61
+ if (response && response.hasNextPage && response.nextCursor) {
62
+ hasNextPage = true;
63
+ nextCursor = response.nextCursor;
64
+ } else {
65
+ hasNextPage = false;
66
+ }
67
+ } while (hasNextPage);
68
+ return isolatedAssets;
69
+ } catch (e) {
70
+ console.error(e);
71
+ return [];
72
+ }
68
73
  };
69
74
 
70
75
  /**
@@ -76,30 +81,35 @@ export const isIsolatedAsset = async (
76
81
  utils: ScallopUtils,
77
82
  coinName: SupportPoolCoins
78
83
  ): Promise<boolean> => {
79
- const marketObject = utils.address.get('core.market');
84
+ try {
85
+ const marketObject = utils.address.get('core.market');
80
86
 
81
- // check if the coin type is in the list
82
- const cachedData = utils.address.cache.queryClient.getQueryData<string[]>([
83
- 'getDynamicFields',
84
- marketObject,
85
- ]);
86
- if (cachedData) {
87
- const coinType = utils.parseCoinType(coinName);
88
- return cachedData.includes(coinType);
89
- }
87
+ // check if the coin type is in the list
88
+ const cachedData = utils.address.cache.queryClient.getQueryData<string[]>([
89
+ 'getDynamicFields',
90
+ marketObject,
91
+ ]);
92
+ if (cachedData) {
93
+ const coinType = utils.parseCoinType(coinName);
94
+ return cachedData.includes(coinType);
95
+ }
90
96
 
91
- // fetch dynamic field object
92
- const coinType = utils.parseCoinType(coinName).slice(2);
97
+ // fetch dynamic field object
98
+ const coinType = utils.parseCoinType(coinName).slice(2);
93
99
 
94
- const object = await utils.cache.queryGetDynamicFieldObject({
95
- parentId: marketObject,
96
- name: {
97
- type: ISOLATED_ASSET_KEY,
98
- value: coinType,
99
- },
100
- });
100
+ const object = await utils.cache.queryGetDynamicFieldObject({
101
+ parentId: marketObject,
102
+ name: {
103
+ type: ISOLATED_ASSET_KEY,
104
+ value: coinType,
105
+ },
106
+ });
101
107
 
102
- const parsedData = isolatedAssetZod.safeParse(object?.data?.content);
103
- if (!parsedData.success) return false;
104
- return parsedData.data.fields.value;
108
+ const parsedData = isolatedAssetZod.safeParse(object?.data?.content);
109
+ if (!parsedData.success) return false;
110
+ return parsedData.data.fields.value;
111
+ } catch (e) {
112
+ console.error(e);
113
+ return false;
114
+ }
105
115
  };
@@ -254,6 +254,7 @@ export const getStakeAccounts = async (
254
254
  filter: { StructType: stakeAccountType },
255
255
  options: {
256
256
  showContent: true,
257
+ showType: true,
257
258
  },
258
259
  cursor: nextCursor,
259
260
  limit: 10,
@@ -304,17 +305,17 @@ export const getStakeAccounts = async (
304
305
  {} as Record<string, SupportStakeMarketCoins>
305
306
  );
306
307
 
307
- const stakeObjectIds: string[] = stakeObjectsResponse
308
- .map((ref: any) => ref?.data?.objectId)
309
- .filter((id: any) => id !== undefined);
310
- const stakeObjects = await utils.cache.queryGetObjects(stakeObjectIds, {
311
- showContent: true,
312
- showType: true,
313
- });
314
- for (const stakeObject of stakeObjects) {
315
- const id = stakeObject.objectId;
316
- const type = stakeObject.type!;
317
- if (stakeObject.content && 'fields' in stakeObject.content) {
308
+ // const stakeObjectIds: string[] = stakeObjectsResponse
309
+ // .map((ref: any) => ref?.data?.objectId)
310
+ // .filter((id: any) => id !== undefined);
311
+ // const stakeObjects = await utils.cache.queryGetObjects(stakeObjectIds, {
312
+ // showContent: true,
313
+ // showType: true,
314
+ // });
315
+ for (const stakeObject of stakeObjectsResponse.map((ref) => ref.data)) {
316
+ const id = stakeObject?.objectId;
317
+ const type = stakeObject?.type!;
318
+ if (id && stakeObject?.content && 'fields' in stakeObject.content) {
318
319
  const fields = stakeObject.content.fields as any;
319
320
  const stakePoolId = String(fields.spool_id);
320
321
  const stakeType = String(fields.stake_type.fields.name);