@scallop-io/sui-scallop-sdk 1.3.1 → 1.3.2-alpha.1
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/constants/cache.d.ts +8 -2
- package/dist/constants/queryKeys.d.ts +3 -3
- package/dist/index.js +77 -66
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +77 -66
- package/dist/index.mjs.map +1 -1
- package/dist/models/scallopCache.d.ts +1 -1
- package/package.json +1 -1
- package/src/constants/cache.ts +1 -3
- package/src/constants/queryKeys.ts +12 -12
- package/src/models/scallopCache.ts +12 -14
- package/src/queries/isolatedAsset.ts +63 -53
|
@@ -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
|
-
|
|
40
|
+
invalidateAllCache(): Promise<void>[];
|
|
41
41
|
/**
|
|
42
42
|
* @description Provides cache for inspectTxn of the SuiKit.
|
|
43
43
|
* @param QueryInspectTxnParams
|
package/package.json
CHANGED
package/src/constants/cache.ts
CHANGED
|
@@ -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
|
|
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
|
|
55
|
+
getOwnedObjects: (input?: Partial<GetOwnedObjectsParams>) => [
|
|
56
56
|
'rpc',
|
|
57
57
|
'getOwnedObjects',
|
|
58
58
|
{
|
|
59
|
-
walletAddress: input
|
|
60
|
-
cursor: input
|
|
61
|
-
options: input
|
|
62
|
-
filter: JSON.stringify(input
|
|
63
|
-
limit: input
|
|
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
|
|
66
|
+
getDynamicFields: (input?: Partial<GetDynamicFieldsParams>) => [
|
|
67
67
|
'rpc',
|
|
68
68
|
'getDynamicFields',
|
|
69
69
|
{
|
|
70
|
-
parentId: input
|
|
71
|
-
cursor: input
|
|
72
|
-
limit: input
|
|
70
|
+
parentId: input?.parentId,
|
|
71
|
+
cursor: input?.cursor ?? undefined,
|
|
72
|
+
limit: input?.limit ?? undefined,
|
|
73
73
|
},
|
|
74
74
|
],
|
|
75
|
-
getDynamicFieldObject: (input
|
|
75
|
+
getDynamicFieldObject: (input?: Partial<GetDynamicFieldObjectParams>) => [
|
|
76
76
|
'rpc',
|
|
77
77
|
'getDynamicFieldObject',
|
|
78
78
|
{
|
|
79
|
-
parentId: input
|
|
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,12 @@ 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
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
100
|
+
public invalidateAllCache() {
|
|
101
|
+
return Object.values(queryKeys.rpc).map((t) =>
|
|
102
|
+
this.queryClient.invalidateQueries({
|
|
103
|
+
queryKey: t(),
|
|
104
|
+
})
|
|
105
|
+
);
|
|
108
106
|
}
|
|
109
107
|
|
|
110
108
|
/**
|
|
@@ -129,7 +127,7 @@ export class ScallopCache {
|
|
|
129
127
|
this.suiKit.inspectTxn(txBlock)
|
|
130
128
|
);
|
|
131
129
|
},
|
|
132
|
-
|
|
130
|
+
...DEFAULT_CACHE_OPTIONS.defaultOptions.queries,
|
|
133
131
|
});
|
|
134
132
|
return query;
|
|
135
133
|
}
|
|
@@ -154,7 +152,7 @@ export class ScallopCache {
|
|
|
154
152
|
})
|
|
155
153
|
);
|
|
156
154
|
},
|
|
157
|
-
|
|
155
|
+
...DEFAULT_CACHE_OPTIONS.defaultOptions.queries,
|
|
158
156
|
});
|
|
159
157
|
}
|
|
160
158
|
|
|
@@ -183,7 +181,7 @@ export class ScallopCache {
|
|
|
183
181
|
this.suiKit.getObjects(objectIds, options)
|
|
184
182
|
);
|
|
185
183
|
},
|
|
186
|
-
|
|
184
|
+
...DEFAULT_CACHE_OPTIONS.defaultOptions.queries,
|
|
187
185
|
});
|
|
188
186
|
}
|
|
189
187
|
|
|
@@ -200,7 +198,7 @@ export class ScallopCache {
|
|
|
200
198
|
this.client.getOwnedObjects(input)
|
|
201
199
|
);
|
|
202
200
|
},
|
|
203
|
-
|
|
201
|
+
...DEFAULT_CACHE_OPTIONS.defaultOptions.queries,
|
|
204
202
|
});
|
|
205
203
|
}
|
|
206
204
|
|
|
@@ -214,7 +212,7 @@ export class ScallopCache {
|
|
|
214
212
|
this.client.getDynamicFields(input)
|
|
215
213
|
);
|
|
216
214
|
},
|
|
217
|
-
|
|
215
|
+
...DEFAULT_CACHE_OPTIONS.defaultOptions.queries,
|
|
218
216
|
});
|
|
219
217
|
}
|
|
220
218
|
|
|
@@ -228,7 +226,7 @@ export class ScallopCache {
|
|
|
228
226
|
this.client.getDynamicFieldObject(input)
|
|
229
227
|
);
|
|
230
228
|
},
|
|
231
|
-
|
|
229
|
+
...DEFAULT_CACHE_OPTIONS.defaultOptions.queries,
|
|
232
230
|
});
|
|
233
231
|
}
|
|
234
232
|
|
|
@@ -29,42 +29,47 @@ const ISOLATED_ASSET_KEY =
|
|
|
29
29
|
export const getIsolatedAssets = async (
|
|
30
30
|
address: ScallopAddress
|
|
31
31
|
): Promise<string[]> => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
try {
|
|
33
|
+
const marketObject = address.get('core.market');
|
|
34
|
+
const isolatedAssets: string[] = [];
|
|
35
|
+
if (!marketObject) return isolatedAssets;
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
let hasNextPage = false;
|
|
38
|
+
let nextCursor: string | null | undefined = null;
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const isolatedAssetCoinTypes = response.data
|
|
57
|
+
.filter(isIsolatedDynamicField)
|
|
58
|
+
.map(({ name }) => `0x${name.value.type.name}`);
|
|
59
|
+
isolatedAssets.push(...isolatedAssetCoinTypes);
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
84
|
+
try {
|
|
85
|
+
const marketObject = utils.address.get('core.market');
|
|
80
86
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
92
|
-
|
|
97
|
+
// fetch dynamic field object
|
|
98
|
+
const coinType = utils.parseCoinType(coinName).slice(2);
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
};
|