@sundaeswap/wallet-lite 0.0.60 → 0.0.62

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<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,aAAa,CAAC,WAC9B,MAAM,KACd,QAAQ,gBAAgB,gBAAgB,aAAa,CAAC,CAAC,CAwDzD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sundaeswap/wallet-lite",
3
- "version": "0.0.60",
3
+ "version": "0.0.62",
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,108 +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
- const state = useWalletObserver<THandleMetadata<AssetMetadata>>();
13
+ const state = useWalletObserver<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];
19
+ const queryKey = [memoizedHandleDep, state.mainAddress, state.network];
24
20
  const { data: handles, isLoading } = useQuery<
25
21
  TAssetAmountMap<THandleMetadata<AssetMetadata>> | undefined
26
22
  >({
27
23
  queryKey,
28
24
  queryFn: async () => {
29
- const currentWalletHandles: TAssetAmountMap<
30
- THandleMetadata<AssetMetadata>
31
- > = new WalletAssetMap<THandleMetadata<AssetMetadata>>([
32
- ...state.balance.getHandles(),
33
- ]);
34
-
35
- // Abort early if no handles.
36
- if (currentWalletHandles.size === 0) {
37
- return currentWalletHandles;
38
- }
39
-
40
- const cachedMetadata =
41
- queryClient.getQueryData<
42
- TAssetAmountMap<THandleMetadata<AssetMetadata>>
43
- >(queryKey);
44
-
45
- let updateMetadata = false;
46
- if (cachedMetadata) {
47
- for (const [, val] of cachedMetadata) {
48
- if (!val?.metadata?.rarity) {
49
- updateMetadata = true;
50
- break;
51
- }
52
- }
53
- }
54
-
55
- if (!updateMetadata && cachedMetadata) {
56
- return cachedMetadata;
57
- }
58
-
59
- try {
60
- const { HandleClient, HandleClientContext, KoraLabsProvider } =
61
- await getHandleLib();
62
-
63
- const context =
64
- state.network === 1
65
- ? HandleClientContext.MAINNET
66
- : HandleClientContext.PREVIEW;
67
-
68
- const sdk = new HandleClient({
69
- context,
70
- provider: new KoraLabsProvider(context),
71
- });
72
-
73
- const walletHandlesWithDataArray = [...currentWalletHandles.entries()];
74
- const walletHandleDataArray: IHandle[] = await sdk
75
- .provider()
76
- .getAllDataBatch(
77
- walletHandlesWithDataArray.map(([key]) => ({
78
- value: key.split(".")[1],
79
- })),
80
- );
81
-
82
- walletHandlesWithDataArray.forEach(([key, asset]) => {
83
- const matchingData = walletHandleDataArray.find(
84
- ({ hex }) => hex === key.split(".")[1],
85
- ) as IHandle;
86
-
87
- currentWalletHandles.set(
88
- normalizeAssetIdWithDot(key),
89
- asset
90
- .withMetadata({
91
- ...matchingData,
92
- ...asset.metadata,
93
- assetId: normalizeAssetIdWithDot(asset.metadata.assetId),
94
- decimals: 0,
95
- })
96
- .withAmount(1n),
97
- );
98
- });
99
-
100
- return currentWalletHandles;
101
- } catch (e) {
102
- console.error(e);
103
- }
104
-
105
- return currentWalletHandles;
25
+ return getHandleMetadata(state.balance.getHandles(), state.network || 0);
106
26
  },
107
27
  refetchInterval: false,
108
28
  notifyOnChangeProps: ["data", "isLoading"],
@@ -0,0 +1,71 @@
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<AssetMetadata>,
14
+ network: number,
15
+ ): Promise<TAssetAmountMap<THandleMetadata<AssetMetadata>>> => {
16
+ const currentWalletHandles = new WalletAssetMap<AssetMetadata>(balance);
17
+ const newCurrentWalletHandles = new WalletAssetMap<
18
+ THandleMetadata<AssetMetadata>
19
+ >();
20
+
21
+ // Abort early if no handles.
22
+ if (currentWalletHandles.size === 0) {
23
+ return newCurrentWalletHandles;
24
+ }
25
+
26
+ try {
27
+ const { HandleClient, HandleClientContext, KoraLabsProvider } =
28
+ await getHandleLib();
29
+
30
+ const context =
31
+ network === 1 ? HandleClientContext.MAINNET : HandleClientContext.PREVIEW;
32
+
33
+ const sdk = new HandleClient({
34
+ context,
35
+ provider: new KoraLabsProvider(context),
36
+ });
37
+
38
+ const walletHandlesWithDataArray = [...currentWalletHandles.entries()];
39
+ const walletHandleDataArray: IHandle[] = await sdk
40
+ .provider()
41
+ .getAllDataBatch(
42
+ walletHandlesWithDataArray.map(([key]) => ({
43
+ value: key.split(".")[1],
44
+ })),
45
+ );
46
+
47
+ walletHandlesWithDataArray.forEach(([key, asset]) => {
48
+ const matchingData = walletHandleDataArray.find(
49
+ ({ hex }) => hex === key.split(".")[1],
50
+ ) as IHandle;
51
+
52
+ newCurrentWalletHandles.set(
53
+ normalizeAssetIdWithDot(key),
54
+ asset
55
+ .withMetadata({
56
+ ...matchingData,
57
+ ...asset.metadata,
58
+ assetId: normalizeAssetIdWithDot(asset.metadata.assetId),
59
+ decimals: 0,
60
+ })
61
+ .withAmount(1n),
62
+ );
63
+ });
64
+
65
+ return newCurrentWalletHandles;
66
+ } catch (e) {
67
+ console.error(e);
68
+ }
69
+
70
+ return newCurrentWalletHandles;
71
+ };