UNPKG

yamaswap-sdk

Version:
76 lines (66 loc) 2.56 kB
// // todo: 封装hooks // import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; // import { PublicKey } from '@solana/web3.js'; // import { DexClient } from '../core/client'; // import { ETFInfo } from '../utils/queries'; // // 查询键的常量 // export const ETF_QUERY_KEYS = { // all: ['etf'] as const, // info: (address: string) => [...ETF_QUERY_KEYS.all, 'info', address] as const, // balance: (address: string, owner: string) => [...ETF_QUERY_KEYS.all, 'balance', address, owner] as const, // }; // // ETF信息查询Hook // export function useETFInfo(client: DexClient, etfAddress: PublicKey) { // return useQuery({ // queryKey: ETF_QUERY_KEYS.info(etfAddress.toString()), // queryFn: () => client.queries.getETFInfo(etfAddress), // staleTime: 30000, // 30秒内认为数据是新鲜的 // gcTime: 5 * 60 * 1000, // 5分钟的缓存时间 // retry: 2, // 失败重试2次 // }); // } // // ETF余额查询Hook // export function useETFBalance( // client: DexClient, // etfAddress: PublicKey, // ownerAddress: PublicKey // ) { // return useQuery({ // queryKey: ETF_QUERY_KEYS.balance(etfAddress.toString(), ownerAddress.toString()), // queryFn: () => client.queries.getETFBalance(etfAddress, ownerAddress), // staleTime: 10000, // 10秒内认为数据是新鲜的 // gcTime: 2 * 60 * 1000, // 2分钟的缓存时间 // retry: 2, // }); // } // // ETF创建Mutation Hook // export function useCreateETF(client: DexClient) { // const queryClient = useQueryClient(); // return useMutation({ // mutationFn: client.createETF.bind(client), // onSuccess: () => { // // 创建成功后使所有ETF查询失效 // queryClient.invalidateQueries({ queryKey: ETF_QUERY_KEYS.all }); // }, // }); // } // // 自动刷新Hook // export function useETFAutoRefresh( // client: DexClient, // etfAddress: PublicKey, // ownerAddress: PublicKey, // options = { enabled: true } // ) { // const { enabled = true } = options; // // ETF信息自动刷新 // const info = useETFInfo(client, etfAddress); // // ETF余额自动刷新 // const balance = useETFBalance(client, etfAddress, ownerAddress); // return { // info, // balance, // isLoading: info.isLoading || balance.isLoading, // isError: info.isError || balance.isError, // error: info.error || balance.error, // }; // }