@sundaeswap/wallet-lite 0.0.59 → 0.0.61

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.
@@ -0,0 +1,5 @@
1
+ import { IAssetAmountMetadata } from "@sundaeswap/asset";
2
+ import { TAssetAmountMap } from "../@types/observer.js";
3
+ import { THandleMetadata } from "../react-components/contexts/observer/types.js";
4
+ export declare const getHandleMetadata: <AssetMetadata extends IAssetAmountMetadata = IAssetAmountMetadata>(balance: TAssetAmountMap<THandleMetadata<AssetMetadata>>, network: number) => Promise<TAssetAmountMap<THandleMetadata<AssetMetadata>>>;
5
+ //# sourceMappingURL=handles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handles.d.ts","sourceRoot":"","sources":["../../../src/utils/handles.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,OAAO,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAC;AAIjF,eAAO,MAAM,iBAAiB,+EAGnB,gBAAgB,gBAAgB,aAAa,CAAC,CAAC,WAC/C,MAAM,6DAuDhB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sundaeswap/wallet-lite",
3
- "version": "0.0.59",
3
+ "version": "0.0.61",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",
package/src/index.ts CHANGED
@@ -6,3 +6,4 @@ export * from "./react-components/hooks/index.js";
6
6
  export * from "./react-components/index.js";
7
7
  export * from "./utils/assets.js";
8
8
  export * from "./utils/getLibs.js";
9
+ export * from "./utils/handles.js";
@@ -1,105 +1,28 @@
1
- import type { IHandle } from "@koralabs/adahandle-sdk";
2
1
  import { IAssetAmountMetadata } from "@sundaeswap/asset";
3
- import { useQuery, useQueryClient } from "@tanstack/react-query";
2
+ import { useQuery } from "@tanstack/react-query";
4
3
  import { useMemo } from "react";
5
4
 
6
5
  import { TAssetAmountMap } from "../../@types/observer.js";
7
- import { WalletAssetMap } from "../../classes/WalletAssetMap.class.js";
8
- import { normalizeAssetIdWithDot } from "../../utils/assets.js";
9
- import { getHandleLib } from "../../utils/getLibs.js";
6
+ import { getHandleMetadata } from "../../utils/handles.js";
10
7
  import { THandleMetadata } from "../contexts/observer/types.js";
11
8
  import { useWalletObserver } from "./useWalletObserver.js";
12
9
 
13
10
  export const useWalletHandles = <
14
11
  AssetMetadata extends IAssetAmountMetadata = IAssetAmountMetadata,
15
12
  >() => {
16
- const queryClient = useQueryClient();
17
13
  const state = useWalletObserver<THandleMetadata<AssetMetadata>>();
18
14
  const memoizedHandleDep = useMemo(
19
15
  () => [...state.balance.getHandles().keys()],
20
16
  [state.balance],
21
17
  );
22
18
 
23
- const queryKey = [memoizedHandleDep, state.mainAddress];
24
- const { data: handles, isLoading } = useQuery({
19
+ const queryKey = [memoizedHandleDep, state.mainAddress, state.network];
20
+ const { data: handles, isLoading } = useQuery<
21
+ TAssetAmountMap<THandleMetadata<AssetMetadata>> | undefined
22
+ >({
25
23
  queryKey,
26
24
  queryFn: async () => {
27
- const currentWalletHandles: TAssetAmountMap<
28
- THandleMetadata<AssetMetadata>
29
- > = new WalletAssetMap<THandleMetadata<AssetMetadata>>([
30
- ...state.balance.getHandles(),
31
- ]);
32
-
33
- if (currentWalletHandles.size === 0) {
34
- return [];
35
- }
36
-
37
- const cachedMetadata =
38
- queryClient.getQueryData<
39
- TAssetAmountMap<THandleMetadata<AssetMetadata>>
40
- >(queryKey);
41
-
42
- let updateMetadata = false;
43
- if (cachedMetadata) {
44
- for (const [, val] of cachedMetadata) {
45
- if (!val?.metadata?.rarity) {
46
- updateMetadata = true;
47
- break;
48
- }
49
- }
50
- }
51
-
52
- if (!updateMetadata && cachedMetadata) {
53
- return cachedMetadata;
54
- }
55
-
56
- try {
57
- const { HandleClient, HandleClientContext, KoraLabsProvider } =
58
- await getHandleLib();
59
-
60
- const context =
61
- state.network === 1
62
- ? HandleClientContext.MAINNET
63
- : HandleClientContext.PREVIEW;
64
-
65
- const sdk = new HandleClient({
66
- context,
67
- provider: new KoraLabsProvider(context),
68
- });
69
-
70
- const walletHandlesWithDataArray = [...currentWalletHandles.entries()];
71
- const walletHandleDataArray: IHandle[] = await sdk
72
- .provider()
73
- .getAllDataBatch(
74
- walletHandlesWithDataArray.map(([key]) => ({
75
- value: key.split(".")[1],
76
- })),
77
- );
78
-
79
- walletHandlesWithDataArray.forEach(([key, asset]) => {
80
- const matchingData = walletHandleDataArray.find(
81
- ({ hex }) => hex === key.split(".")[1],
82
- ) as IHandle;
83
-
84
- currentWalletHandles.set(
85
- normalizeAssetIdWithDot(key),
86
- asset
87
- .withMetadata({
88
- ...matchingData,
89
- ...asset.metadata,
90
- assetId: normalizeAssetIdWithDot(asset.metadata.assetId),
91
- decimals: 0,
92
- })
93
- .withAmount(1n),
94
- );
95
- });
96
-
97
- return currentWalletHandles;
98
- } catch (e) {
99
- console.error(e);
100
- }
101
-
102
- return currentWalletHandles;
25
+ return getHandleMetadata(state.balance.getHandles(), state.network || 0);
103
26
  },
104
27
  refetchInterval: false,
105
28
  notifyOnChangeProps: ["data", "isLoading"],
@@ -0,0 +1,69 @@
1
+ import { IHandle } from "@koralabs/adahandle-sdk";
2
+ import { IAssetAmountMetadata } from "@sundaeswap/asset";
3
+
4
+ import { TAssetAmountMap } from "../@types/observer.js";
5
+ import { WalletAssetMap } from "../classes/WalletAssetMap.class.js";
6
+ import { THandleMetadata } from "../react-components/contexts/observer/types.js";
7
+ import { normalizeAssetIdWithDot } from "./assets.js";
8
+ import { getHandleLib } from "./getLibs.js";
9
+
10
+ export const getHandleMetadata = async <
11
+ AssetMetadata extends IAssetAmountMetadata = IAssetAmountMetadata,
12
+ >(
13
+ balance: TAssetAmountMap<THandleMetadata<AssetMetadata>>,
14
+ network: number,
15
+ ) => {
16
+ const currentWalletHandles: TAssetAmountMap<THandleMetadata<AssetMetadata>> =
17
+ new WalletAssetMap<THandleMetadata<AssetMetadata>>(balance);
18
+
19
+ // Abort early if no handles.
20
+ if (currentWalletHandles.size === 0) {
21
+ return currentWalletHandles;
22
+ }
23
+
24
+ try {
25
+ const { HandleClient, HandleClientContext, KoraLabsProvider } =
26
+ await getHandleLib();
27
+
28
+ const context =
29
+ network === 1 ? HandleClientContext.MAINNET : HandleClientContext.PREVIEW;
30
+
31
+ const sdk = new HandleClient({
32
+ context,
33
+ provider: new KoraLabsProvider(context),
34
+ });
35
+
36
+ const walletHandlesWithDataArray = [...currentWalletHandles.entries()];
37
+ const walletHandleDataArray: IHandle[] = await sdk
38
+ .provider()
39
+ .getAllDataBatch(
40
+ walletHandlesWithDataArray.map(([key]) => ({
41
+ value: key.split(".")[1],
42
+ })),
43
+ );
44
+
45
+ walletHandlesWithDataArray.forEach(([key, asset]) => {
46
+ const matchingData = walletHandleDataArray.find(
47
+ ({ hex }) => hex === key.split(".")[1],
48
+ ) as IHandle;
49
+
50
+ currentWalletHandles.set(
51
+ normalizeAssetIdWithDot(key),
52
+ asset
53
+ .withMetadata({
54
+ ...matchingData,
55
+ ...asset.metadata,
56
+ assetId: normalizeAssetIdWithDot(asset.metadata.assetId),
57
+ decimals: 0,
58
+ })
59
+ .withAmount(1n),
60
+ );
61
+ });
62
+
63
+ return currentWalletHandles;
64
+ } catch (e) {
65
+ console.error(e);
66
+ }
67
+
68
+ return currentWalletHandles;
69
+ };