@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.
@@ -1,8 +1,14 @@
1
- import { QueryClientConfig } from '@tanstack/query-core';
2
1
  /**
3
2
  * Default cache options for the QueryClient.
4
3
  * @type {QueryClientConfig}
5
4
  * @description Default cache options for the QueryClient
6
5
  * We set the default to 5s to prevent duplicate requests from being requested (e.g. query MarketObject, etc.)
7
6
  */
8
- export declare const DEFAULT_CACHE_OPTIONS: QueryClientConfig;
7
+ export declare const DEFAULT_CACHE_OPTIONS: {
8
+ defaultOptions: {
9
+ queries: {
10
+ staleTime: number;
11
+ gcTime: number;
12
+ };
13
+ };
14
+ };
@@ -26,19 +26,19 @@ export declare const queryKeys: {
26
26
  options: SuiObjectDataOptions | undefined;
27
27
  objectIds: string;
28
28
  })[];
29
- getOwnedObjects: (input: Partial<GetOwnedObjectsParams>) => (string | {
29
+ getOwnedObjects: (input?: Partial<GetOwnedObjectsParams>) => (string | {
30
30
  walletAddress: string | undefined;
31
31
  cursor: string | undefined;
32
32
  options: SuiObjectDataOptions | undefined;
33
33
  filter: string;
34
34
  limit: number | undefined;
35
35
  })[];
36
- getDynamicFields: (input: Partial<GetDynamicFieldsParams>) => (string | {
36
+ getDynamicFields: (input?: Partial<GetDynamicFieldsParams>) => (string | {
37
37
  parentId: string | undefined;
38
38
  cursor: string | undefined;
39
39
  limit: number | undefined;
40
40
  })[];
41
- getDynamicFieldObject: (input: Partial<GetDynamicFieldObjectParams>) => (string | {
41
+ getDynamicFieldObject: (input?: Partial<GetDynamicFieldObjectParams>) => (string | {
42
42
  parentId: string | undefined;
43
43
  name: {
44
44
  type: string | undefined;
package/dist/index.js CHANGED
@@ -402,27 +402,27 @@ var queryKeys = {
402
402
  "rpc",
403
403
  "getOwnedObjects",
404
404
  {
405
- walletAddress: input.owner,
406
- cursor: input.cursor ?? void 0,
407
- options: input.options ?? void 0,
408
- filter: JSON.stringify(input.filter ?? void 0),
409
- limit: input.limit ?? void 0
405
+ walletAddress: input?.owner,
406
+ cursor: input?.cursor ?? void 0,
407
+ options: input?.options ?? void 0,
408
+ filter: JSON.stringify(input?.filter ?? void 0),
409
+ limit: input?.limit ?? void 0
410
410
  }
411
411
  ],
412
412
  getDynamicFields: (input) => [
413
413
  "rpc",
414
414
  "getDynamicFields",
415
415
  {
416
- parentId: input.parentId,
417
- cursor: input.cursor ?? void 0,
418
- limit: input.limit ?? void 0
416
+ parentId: input?.parentId,
417
+ cursor: input?.cursor ?? void 0,
418
+ limit: input?.limit ?? void 0
419
419
  }
420
420
  ],
421
421
  getDynamicFieldObject: (input) => [
422
422
  "rpc",
423
423
  "getDynamicFieldObject",
424
424
  {
425
- parentId: input.parentId,
425
+ parentId: input?.parentId,
426
426
  name: {
427
427
  type: input?.name?.type,
428
428
  value: input?.name?.value
@@ -1122,7 +1122,6 @@ function withIndexerFallback(method) {
1122
1122
  }
1123
1123
 
1124
1124
  // src/models/scallopCache.ts
1125
- var DEFAULT_GC_TIME = 5 * 1e3;
1126
1125
  var ScallopCache = class {
1127
1126
  constructor(suiKit, walletAddress, cacheOptions, tokenBucket, queryClient) {
1128
1127
  this.queryClient = queryClient ?? new import_query_core.QueryClient(cacheOptions ?? DEFAULT_CACHE_OPTIONS);
@@ -1148,10 +1147,13 @@ var ScallopCache = class {
1148
1147
  * - `all`: All queries that match the refetch predicate will be refetched in the background.
1149
1148
  * - `none`: No queries will be refetched. Queries that match the refetch predicate will only be marked as invalid.
1150
1149
  */
1151
- invalidateAndRefetchAllCache(refetchType) {
1152
- return this.queryClient.invalidateQueries({
1153
- refetchType
1154
- });
1150
+ async invalidateAllCache() {
1151
+ return Object.values(queryKeys.rpc).map(
1152
+ (t) => this.queryClient.invalidateQueries({
1153
+ queryKey: t(),
1154
+ type: "all"
1155
+ })
1156
+ );
1155
1157
  }
1156
1158
  /**
1157
1159
  * @description Provides cache for inspectTxn of the SuiKit.
@@ -1173,8 +1175,7 @@ var ScallopCache = class {
1173
1175
  this.tokenBucket,
1174
1176
  () => this.suiKit.inspectTxn(txBlock)
1175
1177
  );
1176
- },
1177
- gcTime: DEFAULT_GC_TIME
1178
+ }
1178
1179
  });
1179
1180
  return query;
1180
1181
  }
@@ -1195,8 +1196,7 @@ var ScallopCache = class {
1195
1196
  options
1196
1197
  })
1197
1198
  );
1198
- },
1199
- gcTime: DEFAULT_GC_TIME
1199
+ }
1200
1200
  });
1201
1201
  }
1202
1202
  /**
@@ -1220,8 +1220,7 @@ var ScallopCache = class {
1220
1220
  this.tokenBucket,
1221
1221
  () => this.suiKit.getObjects(objectIds, options)
1222
1222
  );
1223
- },
1224
- gcTime: DEFAULT_GC_TIME
1223
+ }
1225
1224
  });
1226
1225
  }
1227
1226
  /**
@@ -1237,8 +1236,7 @@ var ScallopCache = class {
1237
1236
  this.tokenBucket,
1238
1237
  () => this.client.getOwnedObjects(input)
1239
1238
  );
1240
- },
1241
- gcTime: DEFAULT_GC_TIME
1239
+ }
1242
1240
  });
1243
1241
  }
1244
1242
  async queryGetDynamicFields(input) {
@@ -1249,8 +1247,7 @@ var ScallopCache = class {
1249
1247
  this.tokenBucket,
1250
1248
  () => this.client.getDynamicFields(input)
1251
1249
  );
1252
- },
1253
- gcTime: DEFAULT_GC_TIME
1250
+ }
1254
1251
  });
1255
1252
  }
1256
1253
  async queryGetDynamicFieldObject(input) {
@@ -1261,8 +1258,7 @@ var ScallopCache = class {
1261
1258
  this.tokenBucket,
1262
1259
  () => this.client.getDynamicFieldObject(input)
1263
1260
  );
1264
- },
1265
- gcTime: DEFAULT_GC_TIME
1261
+ }
1266
1262
  });
1267
1263
  }
1268
1264
  async queryGetAllCoinBalances(owner) {
@@ -2416,56 +2412,66 @@ var isolatedAssetZod = import_zod2.z.object({
2416
2412
  });
2417
2413
  var ISOLATED_ASSET_KEY = "0x6e641f0dca8aedab3101d047e96439178f16301bf0b57fe8745086ff1195eb3e::market_dynamic_keys::IsolatedAssetKey";
2418
2414
  var getIsolatedAssets = async (address) => {
2419
- const marketObject = address.get("core.market");
2420
- const isolatedAssets = [];
2421
- if (!marketObject)
2415
+ try {
2416
+ const marketObject = address.get("core.market");
2417
+ const isolatedAssets = [];
2418
+ if (!marketObject)
2419
+ return isolatedAssets;
2420
+ let hasNextPage = false;
2421
+ let nextCursor = null;
2422
+ const isIsolatedDynamicField = (dynamicField) => {
2423
+ return dynamicField.name.type === ISOLATED_ASSET_KEY;
2424
+ };
2425
+ do {
2426
+ const response = await address.cache.queryGetDynamicFields({
2427
+ parentId: marketObject,
2428
+ cursor: nextCursor,
2429
+ limit: 10
2430
+ });
2431
+ if (!response)
2432
+ break;
2433
+ const isolatedAssetCoinTypes = response.data.filter(isIsolatedDynamicField).map(({ name }) => `0x${name.value.type.name}`);
2434
+ isolatedAssets.push(...isolatedAssetCoinTypes);
2435
+ if (response && response.hasNextPage && response.nextCursor) {
2436
+ hasNextPage = true;
2437
+ nextCursor = response.nextCursor;
2438
+ } else {
2439
+ hasNextPage = false;
2440
+ }
2441
+ } while (hasNextPage);
2422
2442
  return isolatedAssets;
2423
- let hasNextPage = false;
2424
- let nextCursor = null;
2425
- const isIsolatedDynamicField = (dynamicField) => {
2426
- return dynamicField.name.type === ISOLATED_ASSET_KEY;
2427
- };
2428
- do {
2429
- const response = await address.cache.queryGetDynamicFields({
2430
- parentId: marketObject,
2431
- cursor: nextCursor,
2432
- limit: 10
2433
- });
2434
- if (!response)
2435
- break;
2436
- const isolatedAssetCoinTypes = response.data.filter(isIsolatedDynamicField).map(({ name }) => `0x${name.value.type.name}`);
2437
- isolatedAssets.push(...isolatedAssetCoinTypes);
2438
- if (response && response.hasNextPage && response.nextCursor) {
2439
- hasNextPage = true;
2440
- nextCursor = response.nextCursor;
2441
- } else {
2442
- hasNextPage = false;
2443
- }
2444
- } while (hasNextPage);
2445
- return isolatedAssets;
2443
+ } catch (e) {
2444
+ console.error(e);
2445
+ return [];
2446
+ }
2446
2447
  };
2447
2448
  var isIsolatedAsset = async (utils, coinName) => {
2448
- const marketObject = utils.address.get("core.market");
2449
- const cachedData = utils.address.cache.queryClient.getQueryData([
2450
- "getDynamicFields",
2451
- marketObject
2452
- ]);
2453
- if (cachedData) {
2454
- const coinType2 = utils.parseCoinType(coinName);
2455
- return cachedData.includes(coinType2);
2456
- }
2457
- const coinType = utils.parseCoinType(coinName).slice(2);
2458
- const object = await utils.cache.queryGetDynamicFieldObject({
2459
- parentId: marketObject,
2460
- name: {
2461
- type: ISOLATED_ASSET_KEY,
2462
- value: coinType
2463
- }
2464
- });
2465
- const parsedData = isolatedAssetZod.safeParse(object?.data?.content);
2466
- if (!parsedData.success)
2449
+ try {
2450
+ const marketObject = utils.address.get("core.market");
2451
+ const cachedData = utils.address.cache.queryClient.getQueryData([
2452
+ "getDynamicFields",
2453
+ marketObject
2454
+ ]);
2455
+ if (cachedData) {
2456
+ const coinType2 = utils.parseCoinType(coinName);
2457
+ return cachedData.includes(coinType2);
2458
+ }
2459
+ const coinType = utils.parseCoinType(coinName).slice(2);
2460
+ const object = await utils.cache.queryGetDynamicFieldObject({
2461
+ parentId: marketObject,
2462
+ name: {
2463
+ type: ISOLATED_ASSET_KEY,
2464
+ value: coinType
2465
+ }
2466
+ });
2467
+ const parsedData = isolatedAssetZod.safeParse(object?.data?.content);
2468
+ if (!parsedData.success)
2469
+ return false;
2470
+ return parsedData.data.fields.value;
2471
+ } catch (e) {
2472
+ console.error(e);
2467
2473
  return false;
2468
- return parsedData.data.fields.value;
2474
+ }
2469
2475
  };
2470
2476
 
2471
2477
  // src/queries/coreQuery.ts
@@ -3242,7 +3248,8 @@ var getStakeAccounts = async ({
3242
3248
  owner,
3243
3249
  filter: { StructType: stakeAccountType },
3244
3250
  options: {
3245
- showContent: true
3251
+ showContent: true,
3252
+ showType: true
3246
3253
  },
3247
3254
  cursor: nextCursor,
3248
3255
  limit: 10
@@ -3280,15 +3287,10 @@ var getStakeAccounts = async ({
3280
3287
  },
3281
3288
  {}
3282
3289
  );
3283
- const stakeObjectIds = stakeObjectsResponse.map((ref) => ref?.data?.objectId).filter((id) => id !== void 0);
3284
- const stakeObjects = await utils.cache.queryGetObjects(stakeObjectIds, {
3285
- showContent: true,
3286
- showType: true
3287
- });
3288
- for (const stakeObject of stakeObjects) {
3289
- const id = stakeObject.objectId;
3290
- const type = stakeObject.type;
3291
- if (stakeObject.content && "fields" in stakeObject.content) {
3290
+ for (const stakeObject of stakeObjectsResponse.map((ref) => ref.data)) {
3291
+ const id = stakeObject?.objectId;
3292
+ const type = stakeObject?.type;
3293
+ if (id && stakeObject?.content && "fields" in stakeObject.content) {
3292
3294
  const fields = stakeObject.content.fields;
3293
3295
  const stakePoolId = String(fields.spool_id);
3294
3296
  const stakeType = String(fields.stake_type.fields.name);
@@ -4785,6 +4787,11 @@ var ScallopUtils = class {
4785
4787
  existPricesCoinNames.push(assetCoinName);
4786
4788
  } else {
4787
4789
  lackPricesCoinNames.push(assetCoinName);
4790
+ this.cache.queryClient.invalidateQueries({
4791
+ queryKey: queryKeys.oracle.getPythLatestPriceFeed(
4792
+ this.address.get(`core.coins.${assetCoinName}.oracle.pyth.feed`)
4793
+ )
4794
+ });
4788
4795
  }
4789
4796
  });
4790
4797
  if (existPricesCoinNames.length > 0) {
@@ -4812,7 +4819,7 @@ var ScallopUtils = class {
4812
4819
  Object.entries(priceIds).map(async ([coinName, priceId]) => {
4813
4820
  const pythConnection = new import_pyth_sui_js.SuiPriceServiceConnection(endpoint);
4814
4821
  try {
4815
- const feed = await this.address.cache.queryClient.fetchQuery({
4822
+ const feed = await this.cache.queryClient.fetchQuery({
4816
4823
  queryKey: queryKeys.oracle.getPythLatestPriceFeed(priceId),
4817
4824
  queryFn: async () => {
4818
4825
  return await pythConnection.getLatestPriceFeeds([priceId]);