14.05.2026 rip
This commit is contained in:
@@ -24,6 +24,17 @@ export interface PriceEntry {
|
||||
usd: number
|
||||
}
|
||||
|
||||
export interface SendWalletPayload {
|
||||
to: string
|
||||
amount: string
|
||||
token?: string
|
||||
feeTier?: 'slow' | 'normal' | 'fast'
|
||||
}
|
||||
|
||||
export interface SendWalletResponse {
|
||||
data: { txid: string; chain: Chain }
|
||||
}
|
||||
|
||||
export const CHAINS: Chain[] = ['ETH', 'BSC', 'BTC', 'TRX', 'SOL']
|
||||
|
||||
async function walletGet<T>(path: string, allowRetry: boolean = true): Promise<T> {
|
||||
@@ -53,6 +64,36 @@ async function walletGet<T>(path: string, allowRetry: boolean = true): Promise<T
|
||||
return data as T
|
||||
}
|
||||
|
||||
async function walletPost<T>(path: string, body: unknown, allowRetry: boolean = true): Promise<T> {
|
||||
const csrf = await getCsrfToken()
|
||||
const bearer = tokenStore.get()
|
||||
|
||||
const res = await fetch(`${WALLET_API_URL}${path}`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-Token': csrf,
|
||||
...(bearer ? { Authorization: `Bearer ${bearer}` } : {}),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (res.status === 401 && allowRetry) {
|
||||
try {
|
||||
await refreshAccessToken()
|
||||
return walletPost<T>(path, body, false)
|
||||
} catch {
|
||||
tokenStore.clear()
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw data
|
||||
return data as T
|
||||
}
|
||||
|
||||
export async function getWalletBalance(chain: Chain): Promise<WalletBalanceData> {
|
||||
const res = await walletGet<{ success: boolean; data: WalletBalanceData }>(`/api/wallets/${chain}/balance`)
|
||||
return res.data
|
||||
@@ -64,3 +105,7 @@ export async function getPrices(symbols: string[]): Promise<Record<string, Price
|
||||
)
|
||||
return res.data
|
||||
}
|
||||
|
||||
export async function sendWallet(chain: Chain, payload: SendWalletPayload): Promise<SendWalletResponse> {
|
||||
return walletPost<SendWalletResponse>(`/api/wallets/${chain}/send`, payload)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export { useAllWalletBalances, usePrices } from './model/useWalletData'
|
||||
export type { Chain, FormattedAmount, WalletBalanceData, PriceEntry } from './api/walletApi'
|
||||
export { useAllWalletBalances, usePrices, useSendWallet } from './model/useWalletData'
|
||||
export type { Chain, FormattedAmount, WalletBalanceData, PriceEntry, SendWalletPayload, SendWalletResponse } from './api/walletApi'
|
||||
export { CHAINS } from './api/walletApi'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useQuery, useQueries } from '@tanstack/react-query'
|
||||
import { getWalletBalance, getPrices, CHAINS } from '../api/walletApi'
|
||||
import { useQuery, useQueries, useMutation } from '@tanstack/react-query'
|
||||
import { getWalletBalance, getPrices, sendWallet, CHAINS, type Chain, type SendWalletPayload } from '../api/walletApi'
|
||||
|
||||
export function useAllWalletBalances() {
|
||||
return useQueries({
|
||||
@@ -18,3 +18,10 @@ export function usePrices(symbols: string[]) {
|
||||
staleTime: 5 * 60 * 1000,
|
||||
})
|
||||
}
|
||||
|
||||
export function useSendWallet() {
|
||||
return useMutation({
|
||||
mutationFn: ({ chain, ...payload }: { chain: Chain } & SendWalletPayload) =>
|
||||
sendWallet(chain, payload),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user