@macalinao/grill
Version:
Modern Solana development kit for React applications with automatic account batching, caching, and transaction notifications
46 lines • 1.89 kB
JavaScript
import { fetchTokenInfo } from "@macalinao/gill-extra";
import { useQuery } from "@tanstack/react-query";
import { useMintAccount } from "../accounts/mint.js";
import { useGrillContext } from "../contexts/grill-context.js";
import { createTokenInfoQueryKey } from "../query-keys.js";
import { useTokenMetadataAccount } from "./use-token-metadata-account.js";
/**
* Hook for getting the {@link TokenInfo} for a given mint address.
* @param param0
* @returns
*/
export function useTokenInfo({ mint, }) {
const { staticTokenInfo, fetchFromCertifiedTokenList } = useGrillContext();
const { data: metadataAccount } = useTokenMetadataAccount({ mint });
const { data: mintAccount } = useMintAccount({ address: mint });
// Check for static token info
const staticInfo = mint ? staticTokenInfo.get(mint) : undefined;
return useQuery({
// eslint-disable-next-line @tanstack/query/exhaustive-deps
queryKey: createTokenInfoQueryKey(mint),
queryFn: async () => {
if (!mint) {
return null;
}
// Return static token info if available
if (staticInfo) {
return staticInfo;
}
if (!mintAccount) {
return null;
}
return fetchTokenInfo({
mint: mintAccount,
metadata: metadataAccount?.data ?? null,
fetchFromCertifiedTokenList,
});
},
enabled: !!mint &&
(staticInfo !== undefined ||
(mintAccount !== undefined && metadataAccount !== undefined)),
staleTime: staticInfo ? Number.POSITIVE_INFINITY : 5 * 60 * 1000, // Static info never goes stale
gcTime: 60 * 60 * 1000, // Keep in cache for 1 hour
placeholderData: staticInfo ?? undefined,
});
}
//# sourceMappingURL=use-token-info.js.map