Skip to content

Commit

Permalink
chore: abstract handle fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
cjkoepke committed Sep 18, 2024
1 parent bdeec41 commit 4b65c40
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 84 deletions.
1 change: 1 addition & 0 deletions lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./react-components/hooks/index.js";
export * from "./react-components/index.js";
export * from "./utils/assets.js";
export * from "./utils/getLibs.js";
export * from "./utils/handles.js";
88 changes: 4 additions & 84 deletions lib/src/react-components/hooks/useWalletHandles.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,28 @@
import type { IHandle } from "@koralabs/adahandle-sdk";
import { IAssetAmountMetadata } from "@sundaeswap/asset";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";

import { TAssetAmountMap } from "../../@types/observer.js";
import { WalletAssetMap } from "../../classes/WalletAssetMap.class.js";
import { normalizeAssetIdWithDot } from "../../utils/assets.js";
import { getHandleLib } from "../../utils/getLibs.js";
import { getHandleMetadata } from "../../utils/handles.js";
import { THandleMetadata } from "../contexts/observer/types.js";
import { useWalletObserver } from "./useWalletObserver.js";

export const useWalletHandles = <
AssetMetadata extends IAssetAmountMetadata = IAssetAmountMetadata,
>() => {
const queryClient = useQueryClient();
const state = useWalletObserver<THandleMetadata<AssetMetadata>>();
const memoizedHandleDep = useMemo(
() => [...state.balance.getHandles().keys()],
[state.balance],
);

const queryKey = [memoizedHandleDep, state.mainAddress];
const queryKey = [memoizedHandleDep, state.mainAddress, state.network];
const { data: handles, isLoading } = useQuery<
TAssetAmountMap<THandleMetadata<AssetMetadata>> | undefined
>({
queryKey,
queryFn: async () => {
const currentWalletHandles: TAssetAmountMap<
THandleMetadata<AssetMetadata>
> = new WalletAssetMap<THandleMetadata<AssetMetadata>>([
...state.balance.getHandles(),
]);

// Abort early if no handles.
if (currentWalletHandles.size === 0) {
return currentWalletHandles;
}

const cachedMetadata =
queryClient.getQueryData<
TAssetAmountMap<THandleMetadata<AssetMetadata>>
>(queryKey);

let updateMetadata = false;
if (cachedMetadata) {
for (const [, val] of cachedMetadata) {
if (!val?.metadata?.rarity) {
updateMetadata = true;
break;
}
}
}

if (!updateMetadata && cachedMetadata) {
return cachedMetadata;
}

try {
const { HandleClient, HandleClientContext, KoraLabsProvider } =
await getHandleLib();

const context =
state.network === 1
? HandleClientContext.MAINNET
: HandleClientContext.PREVIEW;

const sdk = new HandleClient({
context,
provider: new KoraLabsProvider(context),
});

const walletHandlesWithDataArray = [...currentWalletHandles.entries()];
const walletHandleDataArray: IHandle[] = await sdk
.provider()
.getAllDataBatch(
walletHandlesWithDataArray.map(([key]) => ({
value: key.split(".")[1],
})),
);

walletHandlesWithDataArray.forEach(([key, asset]) => {
const matchingData = walletHandleDataArray.find(
({ hex }) => hex === key.split(".")[1],
) as IHandle;

currentWalletHandles.set(
normalizeAssetIdWithDot(key),
asset
.withMetadata({
...matchingData,
...asset.metadata,
assetId: normalizeAssetIdWithDot(asset.metadata.assetId),
decimals: 0,
})
.withAmount(1n),
);
});

return currentWalletHandles;
} catch (e) {
console.error(e);
}

return currentWalletHandles;
return getHandleMetadata(state.balance.getHandles(), state.network || 0);
},
refetchInterval: false,
notifyOnChangeProps: ["data", "isLoading"],
Expand Down
69 changes: 69 additions & 0 deletions lib/src/utils/handles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { IHandle } from "@koralabs/adahandle-sdk";
import { IAssetAmountMetadata } from "@sundaeswap/asset";

import { TAssetAmountMap } from "../@types/observer.js";
import { WalletAssetMap } from "../classes/WalletAssetMap.class.js";
import { THandleMetadata } from "../react-components/contexts/observer/types.js";
import { normalizeAssetIdWithDot } from "./assets.js";
import { getHandleLib } from "./getLibs.js";

export const getHandleMetadata = async <
AssetMetadata extends IAssetAmountMetadata = IAssetAmountMetadata,
>(
balance: TAssetAmountMap<THandleMetadata<AssetMetadata>>,
network: number,
) => {
const currentWalletHandles: TAssetAmountMap<THandleMetadata<AssetMetadata>> =
new WalletAssetMap<THandleMetadata<AssetMetadata>>(balance);

// Abort early if no handles.
if (currentWalletHandles.size === 0) {
return currentWalletHandles;
}

try {
const { HandleClient, HandleClientContext, KoraLabsProvider } =
await getHandleLib();

const context =
network === 1 ? HandleClientContext.MAINNET : HandleClientContext.PREVIEW;

const sdk = new HandleClient({
context,
provider: new KoraLabsProvider(context),
});

const walletHandlesWithDataArray = [...currentWalletHandles.entries()];
const walletHandleDataArray: IHandle[] = await sdk
.provider()
.getAllDataBatch(
walletHandlesWithDataArray.map(([key]) => ({
value: key.split(".")[1],
})),
);

walletHandlesWithDataArray.forEach(([key, asset]) => {
const matchingData = walletHandleDataArray.find(
({ hex }) => hex === key.split(".")[1],
) as IHandle;

currentWalletHandles.set(
normalizeAssetIdWithDot(key),
asset
.withMetadata({
...matchingData,
...asset.metadata,
assetId: normalizeAssetIdWithDot(asset.metadata.assetId),
decimals: 0,
})
.withAmount(1n),
);
});

return currentWalletHandles;
} catch (e) {
console.error(e);
}

return currentWalletHandles;
};

0 comments on commit 4b65c40

Please sign in to comment.