|
| 1 | +import { isAddress, parseUnits } from 'viem' |
| 2 | +import { |
| 3 | + useQuery, |
| 4 | + type DefaultError, |
| 5 | + type QueryKey |
| 6 | +} from '@tanstack/react-query' |
| 7 | + |
| 8 | +export type HyperliquidMarginSummary = { |
| 9 | + accountValue?: string |
| 10 | + totalNtlPos?: string |
| 11 | + totalRawUsd?: string |
| 12 | + totalMarginUsed?: string |
| 13 | +} |
| 14 | + |
| 15 | +export type HyperLiquidPerpsResponse = { |
| 16 | + marginSummary?: HyperliquidMarginSummary |
| 17 | + crossMarginSummary?: HyperliquidMarginSummary |
| 18 | + crossMaintenanceMarginUsed?: string |
| 19 | + withdrawable?: string |
| 20 | + assetPositions?: any[] |
| 21 | + time?: number |
| 22 | +} |
| 23 | + |
| 24 | +export type HyperliquidSpotBalance = { |
| 25 | + coin: string |
| 26 | + token: number |
| 27 | + hold: string |
| 28 | + total: string |
| 29 | + entryNtl: string |
| 30 | +} |
| 31 | + |
| 32 | +export type HyperliquidSpotResponse = { |
| 33 | + balances: HyperliquidSpotBalance[] |
| 34 | +} |
| 35 | + |
| 36 | +type QueryType = typeof useQuery< |
| 37 | + string | undefined, |
| 38 | + DefaultError, |
| 39 | + string | undefined, |
| 40 | + QueryKey |
| 41 | +> |
| 42 | +type QueryOptions = Parameters<QueryType>['0'] |
| 43 | + |
| 44 | +// Perps USDC uses zero address |
| 45 | +const PERPS_USDC_ADDRESS = '0x00000000000000000000000000000000' |
| 46 | + |
| 47 | +// Map currency addresses to Hyperliquid spot coin symbols and decimals |
| 48 | +const SPOT_TOKEN_CONFIG: Record<string, { coin: string; decimals: number }> = { |
| 49 | + '0x2e6d84f2d7ca82e6581e03523e4389f7': { coin: 'USDe', decimals: 2 }, |
| 50 | + '0x54e00a5988577cb0b0c9ab0cb6ef7f4b': { coin: 'USDH', decimals: 2 } |
| 51 | +} |
| 52 | + |
| 53 | +export default ( |
| 54 | + address?: string, |
| 55 | + currency: string = PERPS_USDC_ADDRESS, |
| 56 | + queryOptions?: Partial<QueryOptions> |
| 57 | +) => { |
| 58 | + const isEvmAddress = isAddress(address ?? '') |
| 59 | + const isPerps = currency === PERPS_USDC_ADDRESS |
| 60 | + const spotConfig = SPOT_TOKEN_CONFIG[currency.toLowerCase()] |
| 61 | + const decimals = isPerps ? 8 : (spotConfig?.decimals ?? 2) |
| 62 | + |
| 63 | + const queryKey = ['useHyperliquidBalance', address, currency] |
| 64 | + |
| 65 | + const response = (useQuery as QueryType)({ |
| 66 | + queryKey, |
| 67 | + queryFn: async () => { |
| 68 | + if (!address || !isEvmAddress) { |
| 69 | + return undefined |
| 70 | + } |
| 71 | + |
| 72 | + if (isPerps) { |
| 73 | + // Fetch perps balance |
| 74 | + const res = await fetch('https://api.hyperliquid.xyz/info', { |
| 75 | + method: 'POST', |
| 76 | + headers: { 'Content-Type': 'application/json' }, |
| 77 | + body: JSON.stringify({ |
| 78 | + type: 'clearinghouseState', |
| 79 | + user: address |
| 80 | + }) |
| 81 | + }) |
| 82 | + const data = (await res.json()) as HyperLiquidPerpsResponse |
| 83 | + return data?.withdrawable |
| 84 | + } else if (spotConfig) { |
| 85 | + // Fetch spot balances |
| 86 | + const res = await fetch('https://api.hyperliquid.xyz/info', { |
| 87 | + method: 'POST', |
| 88 | + headers: { 'Content-Type': 'application/json' }, |
| 89 | + body: JSON.stringify({ |
| 90 | + type: 'spotClearinghouseState', |
| 91 | + user: address |
| 92 | + }) |
| 93 | + }) |
| 94 | + const data = (await res.json()) as HyperliquidSpotResponse |
| 95 | + // Find the balance matching the coin symbol |
| 96 | + const tokenBalance = data?.balances?.find( |
| 97 | + (b) => b.coin.toLowerCase() === spotConfig.coin.toLowerCase() |
| 98 | + ) |
| 99 | + return tokenBalance?.total |
| 100 | + } |
| 101 | + return undefined |
| 102 | + }, |
| 103 | + enabled: address !== undefined && isEvmAddress, |
| 104 | + ...queryOptions |
| 105 | + }) |
| 106 | + |
| 107 | + const balance = parseUnits(response.data ?? '0', decimals) |
| 108 | + |
| 109 | + return { |
| 110 | + ...response, |
| 111 | + balance, |
| 112 | + queryKey |
| 113 | + } as ReturnType<QueryType> & { |
| 114 | + balance: bigint |
| 115 | + queryKey: (string | undefined)[] |
| 116 | + } |
| 117 | +} |
0 commit comments