Skip to content

Commit

Permalink
feat: prevent ft tokens unnecessary requerying, closes leather-io/ext…
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Aug 21, 2024
1 parent d33adfc commit 97e13cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function useStacksFungibleTokensBalance(
client,
network: network.chain.stacks.url,
}),
select: (resp: FtAssetResponse) => {
select: (resp: FtAssetResponse | null) => {
if (!(resp && isFtAsset(resp))) return;
const { contractAssetName } = getStacksContractIdStringParts(key);
const name = resp.name || contractAssetName;
Expand Down Expand Up @@ -57,7 +57,7 @@ export function useStacksFungibleTokensMetadata(keys: string[]) {
client,
network: network.chain.stacks.url,
}),
select: (resp: FtAssetResponse) => {
select: (resp: FtAssetResponse | null) => {
if (!(resp && isFtAsset(resp))) return;
return createSip10CryptoAssetInfo(key, resp);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { QueryFunctionContext, useQuery } from '@tanstack/react-query';
import { AxiosError } from 'axios';

import { oneMonthInMs, oneWeekInMs } from '@leather.io/utils';

import { useCurrentNetworkState } from '../../../leather-query-provider';
import { StacksQueryPrefixes } from '../../../query-prefixes';
import { type StacksClient, useStacksClient } from '../../stacks-client';
import { StacksClient, useStacksClient } from '../../stacks-client';
import { FtAssetResponse } from '../token-metadata.utils';

const staleTime = 12 * 60 * 60 * 1000;

const queryOptions = {
gcTime: staleTime,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
staleTime: oneWeekInMs,
gcTime: oneMonthInMs,
retry: 0,
staleTime,
} as const;

interface CreateGetFungibleTokenMetadataQueryOptionsArgs {
address: string;
client: StacksClient;
network: string;
}

export function createGetFungibleTokenMetadataQueryOptions({
address,
client,
Expand All @@ -29,8 +31,18 @@ export function createGetFungibleTokenMetadataQueryOptions({
return {
enabled: !!address,
queryKey: [StacksQueryPrefixes.GetFtMetadata, address, network],
queryFn: ({ signal }: QueryFunctionContext) =>
client.getFtMetadata(address, signal) as unknown as FtAssetResponse,
queryFn: async ({ signal }: QueryFunctionContext) => {
try {
const res = await client.getFtMetadata(address, signal);
return res as unknown as FtAssetResponse;
} catch (error) {
const status = (error as AxiosError).request?.status;
if (status === 404 || status === 422) {
return null;
}
throw error;
}
},
...queryOptions,
} as const;
}
Expand Down

0 comments on commit 97e13cf

Please sign in to comment.