14.05.2026 rip
This commit is contained in:
36
src/features/wallet/api/walletApi.ts
Normal file
36
src/features/wallet/api/walletApi.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { api } from '@shared/api/base'
|
||||
|
||||
export type Chain = 'ETH' | 'BSC' | 'BTC' | 'TRX' | 'SOL'
|
||||
|
||||
export interface FormattedAmount {
|
||||
raw: string
|
||||
formatted: string
|
||||
decimals: number
|
||||
usdPrice: number
|
||||
usdValue: number
|
||||
}
|
||||
|
||||
export interface WalletBalanceData {
|
||||
chain: Chain
|
||||
address: string
|
||||
native: FormattedAmount
|
||||
tokens: Record<string, FormattedAmount>
|
||||
}
|
||||
|
||||
export interface PriceEntry {
|
||||
usd: number
|
||||
}
|
||||
|
||||
export const CHAINS: Chain[] = ['ETH', 'BSC', 'BTC', 'TRX', 'SOL']
|
||||
|
||||
export async function getWalletBalance(chain: Chain): Promise<WalletBalanceData> {
|
||||
const res = await api.get<{ success: boolean; data: WalletBalanceData }>(`/api/wallets/${chain}/balance`)
|
||||
return res.data
|
||||
}
|
||||
|
||||
export async function getPrices(symbols: string[]): Promise<Record<string, PriceEntry>> {
|
||||
const res = await api.get<{ success: boolean; data: Record<string, PriceEntry> }>(
|
||||
`/api/prices?symbols=${symbols.join(',')}`
|
||||
)
|
||||
return res.data
|
||||
}
|
||||
3
src/features/wallet/index.ts
Normal file
3
src/features/wallet/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { useAllWalletBalances, usePrices } from './model/useWalletData'
|
||||
export type { Chain, FormattedAmount, WalletBalanceData, PriceEntry } from './api/walletApi'
|
||||
export { CHAINS } from './api/walletApi'
|
||||
20
src/features/wallet/model/useWalletData.ts
Normal file
20
src/features/wallet/model/useWalletData.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { useQuery, useQueries } from '@tanstack/react-query'
|
||||
import { getWalletBalance, getPrices, CHAINS } from '../api/walletApi'
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user