@scallop-io/sui-scallop-sdk 0.44.27 → 0.45.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.
Files changed (39) hide show
  1. package/dist/builders/borrowIncentiveBuilder.d.ts +0 -7
  2. package/dist/constants/cache.d.ts +8 -0
  3. package/dist/index.js +538 -268
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +522 -253
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/models/index.d.ts +1 -0
  8. package/dist/models/scallop.d.ts +7 -1
  9. package/dist/models/scallopAddress.d.ts +4 -2
  10. package/dist/models/scallopBuilder.d.ts +2 -0
  11. package/dist/models/scallopCache.d.ts +71 -0
  12. package/dist/models/scallopClient.d.ts +2 -0
  13. package/dist/models/scallopIndexer.d.ts +5 -3
  14. package/dist/models/scallopQuery.d.ts +14 -0
  15. package/dist/models/scallopUtils.d.ts +1 -0
  16. package/dist/queries/borrowIncentiveQuery.d.ts +8 -0
  17. package/dist/types/model.d.ts +2 -0
  18. package/dist/types/query/vesca.d.ts +2 -1
  19. package/package.json +2 -1
  20. package/src/builders/borrowIncentiveBuilder.ts +8 -57
  21. package/src/builders/vescaBuilder.ts +7 -3
  22. package/src/constants/cache.ts +15 -0
  23. package/src/models/index.ts +1 -0
  24. package/src/models/scallop.ts +26 -7
  25. package/src/models/scallopAddress.ts +24 -19
  26. package/src/models/scallopBuilder.ts +14 -4
  27. package/src/models/scallopCache.ts +245 -0
  28. package/src/models/scallopClient.ts +15 -4
  29. package/src/models/scallopIndexer.ts +58 -84
  30. package/src/models/scallopQuery.ts +34 -5
  31. package/src/models/scallopUtils.ts +53 -24
  32. package/src/queries/borrowIncentiveQuery.ts +99 -7
  33. package/src/queries/coreQuery.ts +62 -75
  34. package/src/queries/priceQuery.ts +4 -6
  35. package/src/queries/spoolQuery.ts +12 -15
  36. package/src/queries/vescaQuery.ts +20 -8
  37. package/src/types/model.ts +2 -0
  38. package/src/types/query/borrowIncentive.ts +0 -107
  39. package/src/types/query/vesca.ts +2 -1
@@ -5,3 +5,4 @@ export * from './scallopBuilder';
5
5
  export * from './scallopQuery';
6
6
  export * from './scallopUtils';
7
7
  export * from './scallopIndexer';
8
+ export * from './scallopCache';
@@ -6,7 +6,12 @@ import { ScallopQuery } from './scallopQuery';
6
6
  import { ScallopUtils } from './scallopUtils';
7
7
  import type { ScallopParams } from '../types/';
8
8
  import { ScallopIndexer } from './scallopIndexer';
9
+ import { ScallopCache } from './scallopCache';
10
+ import { QueryClientConfig } from '@tanstack/query-core';
9
11
  /**
12
+ * @argument params - The parameters for the Scallop instance.
13
+ * @argument cacheOptions - The cache options for the QueryClient.
14
+ *
10
15
  * @description
11
16
  * The main instance that controls interaction with the Scallop contract.
12
17
  *
@@ -23,8 +28,9 @@ import { ScallopIndexer } from './scallopIndexer';
23
28
  export declare class Scallop {
24
29
  params: ScallopParams;
25
30
  suiKit: SuiKit;
31
+ cache: ScallopCache;
26
32
  private _address;
27
- constructor(params: ScallopParams);
33
+ constructor(params: ScallopParams, cacheOptions?: QueryClientConfig);
28
34
  /**
29
35
  * Get a scallop address instance that already has read addresses.
30
36
  *
@@ -1,5 +1,6 @@
1
1
  import type { NetworkType } from '@scallop-io/sui-kit';
2
2
  import type { ScallopAddressParams, AddressesInterface, AddressStringPath } from '../types';
3
+ import { ScallopCache } from './scallopCache';
3
4
  /**
4
5
  * @description
5
6
  * It provides methods for managing addresses.
@@ -13,12 +14,13 @@ import type { ScallopAddressParams, AddressesInterface, AddressStringPath } from
13
14
  */
14
15
  export declare class ScallopAddress {
15
16
  private readonly _auth?;
16
- private _requestClient;
17
+ private readonly _requestClient;
17
18
  private _id?;
18
19
  private _network;
19
20
  private _currentAddresses?;
20
21
  private _addressesMap;
21
- constructor(params: ScallopAddressParams);
22
+ private _cache;
23
+ constructor(params: ScallopAddressParams, cache?: ScallopCache);
22
24
  /**
23
25
  * Get addresses API id.
24
26
  *
@@ -6,6 +6,7 @@ import type { SuiTransactionBlockResponse } from '@mysten/sui.js/client';
6
6
  import type { TransactionBlock } from '@mysten/sui.js/transactions';
7
7
  import type { SuiTxBlock as SuiKitTxBlock } from '@scallop-io/sui-kit';
8
8
  import type { ScallopInstanceParams, ScallopBuilderParams, ScallopTxBlock, SupportMarketCoins, SupportAssetCoins } from '../types';
9
+ import { ScallopCache } from './scallopCache';
9
10
  /**
10
11
  * @description
11
12
  * It provides methods for operating the transaction block, making it more convenient to organize transaction combinations.
@@ -25,6 +26,7 @@ export declare class ScallopBuilder {
25
26
  query: ScallopQuery;
26
27
  utils: ScallopUtils;
27
28
  walletAddress: string;
29
+ cache: ScallopCache;
28
30
  constructor(params: ScallopBuilderParams, instance?: ScallopInstanceParams);
29
31
  /**
30
32
  * Request the scallop API to initialize data.
@@ -0,0 +1,71 @@
1
+ import { QueryClient, QueryClientConfig } from '@tanstack/query-core';
2
+ import { SuiTxArg } from '@scallop-io/sui-kit';
3
+ import { SuiKit } from '@scallop-io/sui-kit';
4
+ import type { SuiObjectResponse, SuiObjectDataOptions, SuiObjectData, PaginatedObjectsResponse, GetOwnedObjectsParams, DevInspectResults, GetDynamicFieldsParams, DynamicFieldPage, GetDynamicFieldObjectParams } from '@mysten/sui.js/client';
5
+ type QueryInspectTxnParams = {
6
+ queryTarget: string;
7
+ args: SuiTxArg[];
8
+ typeArgs?: any[];
9
+ };
10
+ /**
11
+ * @description
12
+ * It provides caching for moveCall, RPC Request, and API Request.
13
+ *
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const scallopCache = new scallopCache(<parameters>);
18
+ * scallopCache.<indexer functions>();
19
+ * await scallopCache.<indexer async functions>();
20
+ * ```
21
+ */
22
+ export declare class ScallopCache {
23
+ readonly queryClient: QueryClient;
24
+ readonly _suiKit?: SuiKit;
25
+ constructor(cacheOptions?: QueryClientConfig, suiKit?: SuiKit);
26
+ private get suiKit();
27
+ /**
28
+ * @description Invalidate cache based on the refetchType parameter
29
+ * @param refetchType Determines the type of queries to be refetched. Defaults to `active`.
30
+ *
31
+ * - `active`: Only queries that match the refetch predicate and are actively being rendered via useQuery and related functions will be refetched in the background.
32
+ * - `inactive`: Only queries that match the refetch predicate and are NOT actively being rendered via useQuery and related functions will be refetched in the background.
33
+ * - `all`: All queries that match the refetch predicate will be refetched in the background.
34
+ * - `none`: No queries will be refetched. Queries that match the refetch predicate will only be marked as invalid.
35
+ */
36
+ invalidateAndRefetchAllCache(refetchType: 'all' | 'active' | 'inactive' | 'none'): Promise<void>;
37
+ /**
38
+ * @description Cache protocol config call for 60 seconds.
39
+ * @returns Promise<ProtocolConfig>
40
+ */
41
+ private getProtocolConfig;
42
+ /**
43
+ * @description Provides cache for inspectTxn of the SuiKit.
44
+ * @param QueryInspectTxnParams
45
+ * @param txBlock
46
+ * @returns Promise<DevInspectResults>
47
+ */
48
+ queryInspectTxn({ queryTarget, args, typeArgs, }: QueryInspectTxnParams): Promise<DevInspectResults>;
49
+ /**
50
+ * @description Provides cache for getObject of the SuiKit.
51
+ * @param objectId
52
+ * @param QueryObjectParams
53
+ * @returns Promise<SuiObjectResponse>
54
+ */
55
+ queryGetObject(objectId: string, options?: SuiObjectDataOptions): Promise<SuiObjectResponse>;
56
+ /**
57
+ * @description Provides cache for getObjects of the SuiKit.
58
+ * @param objectIds
59
+ * @returns Promise<SuiObjectData[]>
60
+ */
61
+ queryGetObjects(objectIds: string[], options?: SuiObjectDataOptions): Promise<SuiObjectData[]>;
62
+ /**
63
+ * @description Provides cache for getOwnedObjects of the SuiKit.
64
+ * @param input
65
+ * @returns Promise<PaginatedObjectsResponse>
66
+ */
67
+ queryGetOwnedObjects(input: GetOwnedObjectsParams): Promise<PaginatedObjectsResponse>;
68
+ queryGetDynamicFields(input: GetDynamicFieldsParams): Promise<DynamicFieldPage>;
69
+ queryGetDynamicFieldObject(input: GetDynamicFieldObjectParams): Promise<SuiObjectResponse>;
70
+ }
71
+ export {};
@@ -7,6 +7,7 @@ import type { SuiTransactionBlockResponse } from '@mysten/sui.js/client';
7
7
  import type { TransactionObjectArgument } from '@mysten/sui.js/transactions';
8
8
  import type { SuiObjectArg } from '@scallop-io/sui-kit';
9
9
  import type { ScallopClientFnReturnType, ScallopInstanceParams, ScallopClientParams, SupportPoolCoins, SupportCollateralCoins, SupportAssetCoins, SupportStakeCoins, SupportStakeMarketCoins, SupportBorrowIncentiveCoins, ScallopTxBlock } from '../types';
10
+ import { ScallopCache } from './scallopCache';
10
11
  /**
11
12
  * @description
12
13
  * It provides contract interaction operations for general users.
@@ -26,6 +27,7 @@ export declare class ScallopClient {
26
27
  builder: ScallopBuilder;
27
28
  query: ScallopQuery;
28
29
  utils: ScallopUtils;
30
+ cache: ScallopCache;
29
31
  walletAddress: string;
30
32
  constructor(params: ScallopClientParams, instance?: ScallopInstanceParams);
31
33
  /**
@@ -1,4 +1,4 @@
1
- import type { Market, MarketPools, MarketPool, MarketCollaterals, MarketCollateral, Spools, Spool, BorrowIncentivePools, BorrowIncentivePool, SupportPoolCoins, SupportCollateralCoins, SupportStakeMarketCoins, SupportBorrowIncentiveCoins, TotalValueLocked } from '../types';
1
+ import type { Market, MarketPools, MarketPool, MarketCollaterals, MarketCollateral, Spools, Spool, BorrowIncentivePools, BorrowIncentivePool, SupportPoolCoins, SupportCollateralCoins, SupportStakeMarketCoins, SupportBorrowIncentiveCoins, TotalValueLocked, ScallopQueryParams, ScallopParams, ScallopInstanceParams } from '../types';
2
2
  /**
3
3
  * @description
4
4
  * It provides methods to obtain sdk index data from mainnet.
@@ -12,8 +12,10 @@ import type { Market, MarketPools, MarketPool, MarketCollaterals, MarketCollater
12
12
  * ```
13
13
  */
14
14
  export declare class ScallopIndexer {
15
- private _requestClient;
16
- constructor();
15
+ private readonly _cache;
16
+ readonly params: ScallopQueryParams;
17
+ private readonly _requestClient;
18
+ constructor(params: ScallopParams, instance?: ScallopInstanceParams);
17
19
  /**
18
20
  * Get market index data.
19
21
  *
@@ -3,6 +3,7 @@ import { ScallopQueryParams, ScallopInstanceParams, SupportStakeMarketCoins, Sup
3
3
  import { ScallopAddress } from './scallopAddress';
4
4
  import { ScallopUtils } from './scallopUtils';
5
5
  import { ScallopIndexer } from './scallopIndexer';
6
+ import { ScallopCache } from './scallopCache';
6
7
  /**
7
8
  * @description
8
9
  * It provides methods for getting on-chain data from the Scallop contract.
@@ -21,6 +22,7 @@ export declare class ScallopQuery {
21
22
  address: ScallopAddress;
22
23
  utils: ScallopUtils;
23
24
  indexer: ScallopIndexer;
25
+ cache: ScallopCache;
24
26
  constructor(params: ScallopQueryParams, instance?: ScallopInstanceParams);
25
27
  /**
26
28
  * Request the scallop API to initialize data.
@@ -382,4 +384,16 @@ export declare class ScallopQuery {
382
384
  * @return Total value locked.
383
385
  */
384
386
  getTvl(indexer?: boolean): Promise<import("../types").TotalValueLocked>;
387
+ /**
388
+ * Get binded obligationId from a veScaKey if it exists.
389
+ * @param veScaKey
390
+ * @returns obligationId
391
+ */
392
+ getBindedObligationId(veScaKey: string): Promise<string | null>;
393
+ /**
394
+ * Get binded veSCA key from a obligationId if it exists.
395
+ * @param obligationId
396
+ * @returns veScaKey
397
+ */
398
+ getBindedVeScaKey(obligationId: string): Promise<string | null>;
385
399
  }
@@ -20,6 +20,7 @@ export declare class ScallopUtils {
20
20
  private _address;
21
21
  private _query;
22
22
  private _priceMap;
23
+ private _cache;
23
24
  constructor(params: ScallopUtilsParams, instance?: ScallopInstanceParams);
24
25
  /**
25
26
  * Request the scallop API to initialize data.
@@ -35,3 +35,11 @@ export declare const queryBorrowIncentiveAccounts: (query: ScallopQuery, obligat
35
35
  vsui?: import("../types").ParsedBorrowIncentiveAccountData | undefined;
36
36
  sca?: import("../types").ParsedBorrowIncentiveAccountData | undefined;
37
37
  }>;
38
+ /**
39
+ * Check veSca bind status
40
+ * @param query
41
+ * @param veScaKey
42
+ * @returns
43
+ */
44
+ export declare const getBindedObligationId: (query: ScallopQuery, veScaKeyId: string) => Promise<string | null>;
45
+ export declare const getBindedVeScaKey: (query: ScallopQuery, obliationId: string) => Promise<string | null>;
@@ -2,6 +2,7 @@ import type { SuiTransactionBlockResponse } from '@mysten/sui.js/client';
2
2
  import type { TransactionBlock } from '@mysten/sui.js/transactions';
3
3
  import type { SuiKit, SuiKitParams, NetworkType } from '@scallop-io/sui-kit';
4
4
  import type { ScallopAddress, ScallopQuery, ScallopUtils, ScallopBuilder } from '../models';
5
+ import { ScallopCache } from 'src/models/scallopCache';
5
6
  export type ScallopClientFnReturnType<T extends boolean> = T extends true ? SuiTransactionBlockResponse : TransactionBlock;
6
7
  export type ScallopInstanceParams = {
7
8
  suiKit?: SuiKit;
@@ -9,6 +10,7 @@ export type ScallopInstanceParams = {
9
10
  query?: ScallopQuery;
10
11
  utils?: ScallopUtils;
11
12
  builder?: ScallopBuilder;
13
+ cache: ScallopCache;
12
14
  };
13
15
  export type ScallopAddressParams = {
14
16
  id: string;
@@ -1,7 +1,8 @@
1
1
  export type Vesca = {
2
2
  id: string;
3
3
  keyId: string;
4
- lockedScaAmount: number;
4
+ lockedScaAmount: string;
5
5
  lockedScaCoin: number;
6
+ currentVeScaBalance: number;
6
7
  unlockAt: number;
7
8
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scallop-io/sui-scallop-sdk",
3
- "version": "0.44.27",
3
+ "version": "0.45.0",
4
4
  "description": "Typescript sdk for interacting with Scallop contract on SUI",
5
5
  "keywords": [
6
6
  "sui",
@@ -46,6 +46,7 @@
46
46
  "@pythnetwork/pyth-sui-js": "^1.2.4",
47
47
  "@scallop-io/sui-kit": "^0.44.2",
48
48
  "@scure/bip39": "^1.2.1",
49
+ "@tanstack/query-core": "^5.28.0",
49
50
  "axios": "^1.6.0",
50
51
  "bignumber.js": "^9.1.2",
51
52
  "superstruct": "^1.0.3",
@@ -1,8 +1,11 @@
1
1
  import { TransactionBlock } from '@mysten/sui.js/transactions';
2
2
  import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui.js/utils';
3
3
  import { SuiTxBlock as SuiKitTxBlock } from '@scallop-io/sui-kit';
4
- import { borrowIncentiveRewardCoins } from '../constants/enum';
5
- import { getObligations, getObligationLocked } from '../queries';
4
+ import {
5
+ getObligations,
6
+ getObligationLocked,
7
+ getBindedObligationId,
8
+ } from '../queries';
6
9
  import { requireSender } from '../utils';
7
10
  import type { SuiObjectArg } from '@scallop-io/sui-kit';
8
11
  import type { ScallopBuilder } from '../models';
@@ -75,59 +78,6 @@ const requireObligationInfo = async (
75
78
  };
76
79
  };
77
80
 
78
- /**
79
- * Check veSca bind status
80
- * @param query
81
- * @param veScaKey
82
- * @returns
83
- */
84
- export const getBindedObligationId = async (
85
- builder: ScallopBuilder,
86
- veScaKey: string
87
- ) => {
88
- const borrowIncentiveObjectId = builder.address.get('borrowIncentive.object');
89
- const incentivePoolsId = builder.address.get(
90
- 'borrowIncentive.incentivePools'
91
- );
92
- const veScaPkgId = IS_VE_SCA_TEST
93
- ? '0xb220d034bdf335d77ae5bfbf6daf059c2cc7a1f719b12bfed75d1736fac038c8'
94
- : builder.address.get('vesca.id');
95
-
96
- const client = builder.suiKit.client();
97
-
98
- // get incentive pools
99
- const incentivePoolsResponse = await client.getObject({
100
- id: incentivePoolsId,
101
- options: {
102
- showContent: true,
103
- },
104
- });
105
-
106
- if (incentivePoolsResponse.data?.content?.dataType !== 'moveObject')
107
- return false;
108
- const incentivePoolFields = incentivePoolsResponse.data.content.fields as any;
109
- const veScaBindTableId = incentivePoolFields.ve_sca_bind.fields.id
110
- .id as string;
111
-
112
- // check if veSca is inside the bind table
113
- const keyType = `${borrowIncentiveObjectId}::typed_id::TypedID<${veScaPkgId}::ve_sca::VeScaKey>`;
114
- const veScaBindTableResponse = await client.getDynamicFieldObject({
115
- parentId: veScaBindTableId,
116
- name: {
117
- type: keyType,
118
- value: veScaKey,
119
- },
120
- });
121
-
122
- if (veScaBindTableResponse.data?.content?.dataType !== 'moveObject')
123
- return false;
124
- const veScaBindTableFields = veScaBindTableResponse.data.content
125
- .fields as any;
126
- // get obligationId pair
127
- const obligationId = veScaBindTableFields.value.fields.id as string;
128
-
129
- return obligationId;
130
- };
131
81
  /**
132
82
  * Generate borrow incentive normal methods.
133
83
  *
@@ -209,7 +159,8 @@ const generateBorrowIncentiveNormalMethod: GenerateBorrowIncentiveNormalMethod =
209
159
  coinName,
210
160
  rewardCoinName
211
161
  ) => {
212
- const rewardCoinNames = borrowIncentiveRewardCoins[coinName];
162
+ const rewardCoinNames =
163
+ builder.utils.getBorrowIncentiveRewardCoinName(coinName);
213
164
  if (rewardCoinNames.includes(rewardCoinName) === false) {
214
165
  throw new Error(`Invalid reward coin name ${rewardCoinName}`);
215
166
  }
@@ -322,7 +273,7 @@ const generateBorrowIncentiveQuickMethod: GenerateBorrowIncentiveQuickMethod =
322
273
  const veSca = await requireVeSca(builder, txBlock, veScaKey);
323
274
  if (veSca) {
324
275
  const bindedObligationId = await getBindedObligationId(
325
- builder,
276
+ builder.query,
326
277
  veSca.keyId
327
278
  );
328
279
  // if bindedObligationId is equal to obligationId, then use it again
@@ -65,7 +65,11 @@ export const requireVeSca = async (
65
65
  return undefined;
66
66
  }
67
67
 
68
- return veScas[0];
68
+ return veScas.reduce(
69
+ (prev, acc) =>
70
+ acc.currentVeScaBalance > prev.currentVeScaBalance ? acc : prev,
71
+ veScas[0]
72
+ ); // return veSCA with highest veSCA balance
69
73
  };
70
74
 
71
75
  /**
@@ -228,7 +232,7 @@ const generateQuickVeScaMethod: GenerateVeScaQuickMethod = ({
228
232
  transferObjects.push(veScaKey);
229
233
  } else {
230
234
  // user must withdraw current unlocked SCA first if any
231
- if (veSca.lockedScaAmount !== 0) {
235
+ if (veSca.lockedScaCoin !== 0) {
232
236
  const unlockedSca = txBlock.redeemSca(veSca.keyId);
233
237
  transferObjects.push(unlockedSca);
234
238
  }
@@ -309,7 +313,7 @@ const generateQuickVeScaMethod: GenerateVeScaQuickMethod = ({
309
313
 
310
314
  if (veSca) {
311
315
  const transferObjects = [];
312
- if (veSca.lockedScaAmount !== 0) {
316
+ if (veSca.lockedScaCoin !== 0) {
313
317
  const unlockedSca = txBlock.redeemSca(veSca.keyId);
314
318
  transferObjects.push(unlockedSca);
315
319
  }
@@ -0,0 +1,15 @@
1
+ import { QueryClientConfig } from '@tanstack/query-core';
2
+
3
+ /**
4
+ * Default cache options for the QueryClient.
5
+ * @type {QueryClientConfig}
6
+ * @description Default cache options for the QueryClient
7
+ * We set the default to 5s to prevent duplicate requests from being requested (e.g. query MarketObject, etc.)
8
+ */
9
+ export const DEFAULT_CACHE_OPTIONS: QueryClientConfig = {
10
+ defaultOptions: {
11
+ queries: {
12
+ staleTime: 3000,
13
+ },
14
+ },
15
+ };
@@ -5,3 +5,4 @@ export * from './scallopBuilder';
5
5
  export * from './scallopQuery';
6
6
  export * from './scallopUtils';
7
7
  export * from './scallopIndexer';
8
+ export * from './scallopCache';
@@ -7,8 +7,14 @@ import { ScallopUtils } from './scallopUtils';
7
7
  import { ADDRESSES_ID } from '../constants';
8
8
  import type { ScallopParams } from '../types/';
9
9
  import { ScallopIndexer } from './scallopIndexer';
10
+ import { ScallopCache } from './scallopCache';
11
+ import { QueryClientConfig } from '@tanstack/query-core';
12
+ import { DEFAULT_CACHE_OPTIONS } from 'src/constants/cache';
10
13
 
11
14
  /**
15
+ * @argument params - The parameters for the Scallop instance.
16
+ * @argument cacheOptions - The cache options for the QueryClient.
17
+ *
12
18
  * @description
13
19
  * The main instance that controls interaction with the Scallop contract.
14
20
  *
@@ -25,16 +31,24 @@ import { ScallopIndexer } from './scallopIndexer';
25
31
  export class Scallop {
26
32
  public params: ScallopParams;
27
33
  public suiKit: SuiKit;
34
+ public cache: ScallopCache;
28
35
 
29
36
  private _address: ScallopAddress;
30
37
 
31
- public constructor(params: ScallopParams) {
38
+ public constructor(params: ScallopParams, cacheOptions?: QueryClientConfig) {
32
39
  this.params = params;
33
40
  this.suiKit = new SuiKit(params);
34
- this._address = new ScallopAddress({
35
- id: params?.addressesId || ADDRESSES_ID,
36
- network: params?.networkType,
37
- });
41
+ this.cache = new ScallopCache(
42
+ cacheOptions ?? DEFAULT_CACHE_OPTIONS,
43
+ this.suiKit
44
+ );
45
+ this._address = new ScallopAddress(
46
+ {
47
+ id: params?.addressesId || ADDRESSES_ID,
48
+ network: params?.networkType,
49
+ },
50
+ this.cache
51
+ );
38
52
  }
39
53
 
40
54
  /**
@@ -59,6 +73,7 @@ export class Scallop {
59
73
  const scallopBuilder = new ScallopBuilder(this.params, {
60
74
  suiKit: this.suiKit,
61
75
  address: this._address,
76
+ cache: this.cache,
62
77
  });
63
78
 
64
79
  return scallopBuilder;
@@ -74,7 +89,7 @@ export class Scallop {
74
89
  if (!this._address.getAddresses()) await this._address.read();
75
90
  const scallopClient = new ScallopClient(
76
91
  { ...this.params, walletAddress },
77
- { suiKit: this.suiKit, address: this._address }
92
+ { suiKit: this.suiKit, address: this._address, cache: this.cache }
78
93
  );
79
94
 
80
95
  return scallopClient;
@@ -90,6 +105,7 @@ export class Scallop {
90
105
  const scallopQuery = new ScallopQuery(this.params, {
91
106
  suiKit: this.suiKit,
92
107
  address: this._address,
108
+ cache: this.cache,
93
109
  });
94
110
 
95
111
  return scallopQuery;
@@ -101,7 +117,9 @@ export class Scallop {
101
117
  * @return Scallop Indexer.
102
118
  */
103
119
  public async createScallopIndexer() {
104
- const scallopIndexer = new ScallopIndexer();
120
+ const scallopIndexer = new ScallopIndexer(this.params, {
121
+ cache: this.cache,
122
+ });
105
123
 
106
124
  return scallopIndexer;
107
125
  }
@@ -116,6 +134,7 @@ export class Scallop {
116
134
  const scallopUtils = new ScallopUtils(this.params, {
117
135
  suiKit: this.suiKit,
118
136
  address: this._address,
137
+ cache: this.cache,
119
138
  });
120
139
 
121
140
  return scallopUtils;
@@ -1,4 +1,3 @@
1
- import axios, { AxiosInstance } from 'axios';
2
1
  import { API_BASE_URL } from '../constants';
3
2
  import type { NetworkType } from '@scallop-io/sui-kit';
4
3
  import type {
@@ -6,6 +5,9 @@ import type {
6
5
  AddressesInterface,
7
6
  AddressStringPath,
8
7
  } from '../types';
8
+ import { ScallopCache } from './scallopCache';
9
+ import { DEFAULT_CACHE_OPTIONS } from 'src/constants/cache';
10
+ import axios, { AxiosInstance } from 'axios';
9
11
 
10
12
  const EMPTY_ADDRESSES: AddressesInterface = {
11
13
  core: {
@@ -270,20 +272,17 @@ const EMPTY_ADDRESSES: AddressesInterface = {
270
272
  */
271
273
  export class ScallopAddress {
272
274
  private readonly _auth?: string;
275
+ private readonly _requestClient: AxiosInstance;
273
276
 
274
- private _requestClient: AxiosInstance;
275
277
  private _id?: string;
276
278
  private _network: NetworkType;
277
279
  private _currentAddresses?: AddressesInterface;
278
280
  private _addressesMap: Map<NetworkType, AddressesInterface>;
281
+ private _cache: ScallopCache;
279
282
 
280
- public constructor(params: ScallopAddressParams) {
283
+ public constructor(params: ScallopAddressParams, cache?: ScallopCache) {
281
284
  const { id, auth, network } = params;
282
-
283
- if (auth) this._auth = auth;
284
- this._id = id;
285
- this._network = network || 'mainnet';
286
- this._addressesMap = new Map();
285
+ this._cache = cache ?? new ScallopCache(DEFAULT_CACHE_OPTIONS);
287
286
  this._requestClient = axios.create({
288
287
  baseURL: API_BASE_URL,
289
288
  headers: {
@@ -292,6 +291,10 @@ export class ScallopAddress {
292
291
  },
293
292
  timeout: 30000,
294
293
  });
294
+ if (auth) this._auth = auth;
295
+ this._id = id;
296
+ this._network = network || 'mainnet';
297
+ this._addressesMap = new Map();
295
298
  }
296
299
 
297
300
  /**
@@ -437,7 +440,7 @@ export class ScallopAddress {
437
440
  this._addressesMap.clear();
438
441
  this.setAddresses(targetAddresses, targetNetwork);
439
442
  const response = await this._requestClient.post(
440
- `${API_BASE_URL}/addresses`,
443
+ `/addresses`,
441
444
  JSON.stringify({ ...Object.fromEntries(this._addressesMap), memo }),
442
445
  {
443
446
  headers: {
@@ -475,14 +478,16 @@ export class ScallopAddress {
475
478
  public async read(id?: string) {
476
479
  const addressesId = id || this._id || undefined;
477
480
  if (addressesId !== undefined) {
478
- const response = await this._requestClient.get(
479
- `${API_BASE_URL}/addresses/${addressesId}`,
480
- {
481
- headers: {
482
- 'Content-Type': 'application/json',
483
- },
484
- }
485
- );
481
+ const response = await this._cache.queryClient.fetchQuery({
482
+ queryKey: ['api-getAddresses', addressesId],
483
+ queryFn: async () => {
484
+ return await this._requestClient.get(`/addresses/${addressesId}`, {
485
+ headers: {
486
+ 'Content-Type': 'application/json',
487
+ },
488
+ });
489
+ },
490
+ });
486
491
 
487
492
  if (response.status === 200) {
488
493
  for (const [network, addresses] of Object.entries<AddressesInterface>(
@@ -548,7 +553,7 @@ export class ScallopAddress {
548
553
  }
549
554
  this.setAddresses(targetAddresses, targetNetwork);
550
555
  const response = await this._requestClient.put(
551
- `${API_BASE_URL}/addresses/${targetId}`,
556
+ `/addresses/${targetId}`,
552
557
  JSON.stringify({ ...Object.fromEntries(this._addressesMap), memo }),
553
558
  {
554
559
  headers: {
@@ -592,7 +597,7 @@ export class ScallopAddress {
592
597
  throw Error('Require specific addresses id to be deleted.');
593
598
  if (apiKey !== undefined) {
594
599
  const response = await this._requestClient.delete(
595
- `${API_BASE_URL}/addresses/${targetId}`,
600
+ `/addresses/${targetId}`,
596
601
  {
597
602
  headers: {
598
603
  'Content-Type': 'application/json',
@@ -15,6 +15,8 @@ import type {
15
15
  SupportMarketCoins,
16
16
  SupportAssetCoins,
17
17
  } from '../types';
18
+ import { ScallopCache } from './scallopCache';
19
+ import { DEFAULT_CACHE_OPTIONS } from 'src/constants/cache';
18
20
 
19
21
  /**
20
22
  * @description
@@ -36,6 +38,7 @@ export class ScallopBuilder {
36
38
  public query: ScallopQuery;
37
39
  public utils: ScallopUtils;
38
40
  public walletAddress: string;
41
+ public cache: ScallopCache;
39
42
 
40
43
  public constructor(
41
44
  params: ScallopBuilderParams,
@@ -43,17 +46,23 @@ export class ScallopBuilder {
43
46
  ) {
44
47
  this.params = params;
45
48
  this.suiKit = instance?.suiKit ?? new SuiKit(params);
49
+ this.cache =
50
+ instance?.cache ?? new ScallopCache(DEFAULT_CACHE_OPTIONS, this.suiKit);
46
51
  this.address =
47
52
  instance?.address ??
48
- new ScallopAddress({
49
- id: params?.addressesId || ADDRESSES_ID,
50
- network: params?.networkType,
51
- });
53
+ new ScallopAddress(
54
+ {
55
+ id: params?.addressesId || ADDRESSES_ID,
56
+ network: params?.networkType,
57
+ },
58
+ this.cache
59
+ );
52
60
  this.query =
53
61
  instance?.query ??
54
62
  new ScallopQuery(params, {
55
63
  suiKit: this.suiKit,
56
64
  address: this.address,
65
+ cache: this.cache,
57
66
  });
58
67
  this.utils =
59
68
  instance?.utils ??
@@ -61,6 +70,7 @@ export class ScallopBuilder {
61
70
  suiKit: this.suiKit,
62
71
  address: this.address,
63
72
  query: this.query,
73
+ cache: this.cache,
64
74
  });
65
75
  this.walletAddress = normalizeSuiAddress(
66
76
  params?.walletAddress || this.suiKit.currentAddress()