146 lines
4.1 KiB
TypeScript
146 lines
4.1 KiB
TypeScript
import { useQuery, useQueries, useMutation } from '@tanstack/react-query'
|
|
import { getWalletBalance, getPrices, sendWallet, getWalletAddresses, getPortfolio, getTokensList, getRelayQuote, executeRelaySwap, signRawEvmTx, signSolTx, getTrxSwapQuote, executeTrxSwap, getJumperTokens, getJumperQuote, executeBridge, createWallet, revealMnemonic, CHAINS, type Chain, type SendWalletPayload, type RelayQuotePayload, type RelaySwapStep, type TrxSwapQuotePayload, type JumperQuotePayload, type BridgeExecutePayload } from '../api/walletApi'
|
|
|
|
export function useWalletBalance(chain: Chain) {
|
|
return useQuery({
|
|
queryKey: ['wallet', 'balance', chain],
|
|
queryFn: () => getWalletBalance(chain),
|
|
staleTime: 30_000,
|
|
})
|
|
}
|
|
|
|
export function useAllWalletBalances() {
|
|
return useQueries({
|
|
queries: CHAINS.map(chain => ({
|
|
queryKey: ['wallet', 'balance', chain],
|
|
queryFn: () => getWalletBalance(chain),
|
|
staleTime: 30_000,
|
|
})),
|
|
})
|
|
}
|
|
|
|
export function usePrices(symbols: string[]) {
|
|
return useQuery({
|
|
queryKey: ['wallet', 'prices', symbols.join(',')],
|
|
queryFn: () => getPrices(symbols),
|
|
staleTime: 5 * 60 * 1000,
|
|
})
|
|
}
|
|
|
|
export function useSendWallet() {
|
|
return useMutation({
|
|
mutationFn: ({ chain, ...payload }: { chain: Chain } & SendWalletPayload) =>
|
|
sendWallet(chain, payload),
|
|
})
|
|
}
|
|
|
|
export function useWalletAddresses() {
|
|
return useQuery({
|
|
queryKey: ['wallet', 'addresses'],
|
|
queryFn: getWalletAddresses,
|
|
staleTime: 10 * 60 * 1000,
|
|
})
|
|
}
|
|
|
|
export function usePortfolio() {
|
|
return useQuery({
|
|
queryKey: ['wallet', 'portfolio'],
|
|
queryFn: getPortfolio,
|
|
staleTime: 30_000,
|
|
})
|
|
}
|
|
|
|
export function useTokensList() {
|
|
return useQuery({
|
|
queryKey: ['wallet', 'tokens'],
|
|
queryFn: getTokensList,
|
|
staleTime: 10 * 60 * 1000,
|
|
})
|
|
}
|
|
|
|
export function useJumperTokens() {
|
|
return useQuery({
|
|
queryKey: ['wallet', 'jumper', 'tokens'],
|
|
queryFn: getJumperTokens,
|
|
staleTime: 10 * 60 * 1000,
|
|
})
|
|
}
|
|
|
|
export function useJumperQuote(payload: JumperQuotePayload | null) {
|
|
return useQuery({
|
|
queryKey: ['wallet', 'jumper', 'quote',
|
|
payload?.fromChain, payload?.toChain,
|
|
payload?.fromToken, payload?.toToken,
|
|
payload?.fromAmount, payload?.fromAddress, payload?.toAddress,
|
|
],
|
|
queryFn: () => getJumperQuote(payload!),
|
|
enabled: !!payload,
|
|
staleTime: 10_000,
|
|
})
|
|
}
|
|
|
|
export function useFetchJumperQuote() {
|
|
return useMutation({ mutationFn: (payload: JumperQuotePayload) => getJumperQuote(payload) })
|
|
}
|
|
|
|
export function useExecuteBridge() {
|
|
return useMutation({ mutationFn: (payload: BridgeExecutePayload) => executeBridge(payload) })
|
|
}
|
|
|
|
export function useCreateWallet() {
|
|
return useMutation({ mutationFn: createWallet })
|
|
}
|
|
|
|
export function useRevealMnemonic() {
|
|
return useQuery({
|
|
queryKey: ['wallet', 'mnemonic'],
|
|
queryFn: revealMnemonic,
|
|
staleTime: Infinity,
|
|
retry: false,
|
|
})
|
|
}
|
|
|
|
export function useRelayQuote(payload: RelayQuotePayload | null) {
|
|
return useQuery({
|
|
queryKey: ['relay', 'quote',
|
|
payload?.originChainId, payload?.destinationChainId,
|
|
payload?.originCurrency, payload?.destinationCurrency, payload?.amount,
|
|
],
|
|
queryFn: () => getRelayQuote(payload!),
|
|
enabled: !!payload,
|
|
staleTime: 10_000,
|
|
})
|
|
}
|
|
|
|
export function useExecuteRelaySwap() {
|
|
return useMutation({
|
|
mutationFn: (payload: RelayQuotePayload) => executeRelaySwap(payload),
|
|
})
|
|
}
|
|
|
|
export function useSignSwap() {
|
|
return useMutation({
|
|
mutationFn: ({ chain, txData }: { chain: Chain; txData: unknown }) => {
|
|
if (chain === 'SOL') return signSolTx(txData)
|
|
return signRawEvmTx(chain as 'ETH' | 'BSC', txData as RelaySwapStep['items'][0]['data'])
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useTrxSwapQuote(payload: TrxSwapQuotePayload | null) {
|
|
return useQuery({
|
|
queryKey: ['trx', 'quote', payload?.from, payload?.to, payload?.amountHuman],
|
|
queryFn: () => getTrxSwapQuote(payload!),
|
|
enabled: !!payload,
|
|
staleTime: 10_000,
|
|
})
|
|
}
|
|
|
|
export function useFetchTrxQuote() {
|
|
return useMutation({ mutationFn: getTrxSwapQuote })
|
|
}
|
|
|
|
export function useExecuteTrxSwap() {
|
|
return useMutation({ mutationFn: (quoteId: string) => executeTrxSwap(quoteId) })
|
|
}
|