diff --git a/src/features/wallet/api/walletApi.ts b/src/features/wallet/api/walletApi.ts index bc07744..652890c 100644 --- a/src/features/wallet/api/walletApi.ts +++ b/src/features/wallet/api/walletApi.ts @@ -41,6 +41,32 @@ export interface WalletAddress { derivationPath: string } +export interface PortfolioToken { + symbol: string + amountFormatted: string + usd: number +} + +export interface PortfolioNative { + amount: string + amountFormatted: string + usd: number +} + +export interface PortfolioChain { + address: string + stale: boolean + native: PortfolioNative + tokens: PortfolioToken[] + totalUsd: number +} + +export interface PortfolioData { + totalUsd: number + asOfMs: number + chains: Record +} + export const CHAINS: Chain[] = ['ETH', 'BSC', 'BTC', 'TRX', 'SOL'] async function walletGet(path: string, allowRetry: boolean = true): Promise { @@ -120,3 +146,8 @@ export async function getPrices(symbols: string[]): Promise { return walletPost(`/api/wallets/${chain}/send`, payload) } + +export async function getPortfolio(): Promise { + const res = await walletGet<{ success: boolean; data: PortfolioData }>('/api/wallets/portfolio') + return res.data +} diff --git a/src/features/wallet/index.ts b/src/features/wallet/index.ts index ac65156..0357ce7 100644 --- a/src/features/wallet/index.ts +++ b/src/features/wallet/index.ts @@ -1,3 +1,3 @@ -export { useAllWalletBalances, usePrices, useSendWallet, useWalletAddresses, useWalletBalance } from './model/useWalletData' -export type { Chain, FormattedAmount, WalletBalanceData, PriceEntry, SendWalletPayload, SendWalletResponse, WalletAddress } from './api/walletApi' +export { useAllWalletBalances, usePrices, useSendWallet, useWalletAddresses, useWalletBalance, usePortfolio } from './model/useWalletData' +export type { Chain, FormattedAmount, WalletBalanceData, PriceEntry, SendWalletPayload, SendWalletResponse, WalletAddress, PortfolioData, PortfolioChain, PortfolioNative, PortfolioToken } from './api/walletApi' export { CHAINS } from './api/walletApi' diff --git a/src/features/wallet/model/useWalletData.ts b/src/features/wallet/model/useWalletData.ts index 7d82db8..c887f2b 100644 --- a/src/features/wallet/model/useWalletData.ts +++ b/src/features/wallet/model/useWalletData.ts @@ -1,5 +1,5 @@ import { useQuery, useQueries, useMutation } from '@tanstack/react-query' -import { getWalletBalance, getPrices, sendWallet, getWalletAddresses, CHAINS, type Chain, type SendWalletPayload } from '../api/walletApi' +import { getWalletBalance, getPrices, sendWallet, getWalletAddresses, getPortfolio, CHAINS, type Chain, type SendWalletPayload } from '../api/walletApi' export function useWalletBalance(chain: Chain) { return useQuery({ @@ -41,3 +41,11 @@ export function useWalletAddresses() { staleTime: 10 * 60 * 1000, }) } + +export function usePortfolio() { + return useQuery({ + queryKey: ['wallet', 'portfolio'], + queryFn: getPortfolio, + staleTime: 30_000, + }) +} diff --git a/src/widgets/balance-card/ui/BalanceCard.tsx b/src/widgets/balance-card/ui/BalanceCard.tsx index 8866fe6..a9d16df 100644 --- a/src/widgets/balance-card/ui/BalanceCard.tsx +++ b/src/widgets/balance-card/ui/BalanceCard.tsx @@ -3,14 +3,20 @@ import topup from '@shared/assets/topup.svg' import swap from '@shared/assets/swap.svg' import { Link } from 'react-router-dom' import { ROUTES } from '@shared/config/routes' +import { usePortfolio } from '@features/wallet' export function BalanceCard() { + const { data, isLoading } = usePortfolio() + const display = + isLoading || !data + ? '$—' + : `$${data.totalUsd.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}` + return (
Общий баланс
-
$245.00
-
≈ 22 340,50 ₽
+
{display}