|
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import { haqqMainnet, haqqTestedge2, sepolia } from 'viem/chains'; |
| 3 | +import { haqqTestethic } from '@haqq/shell-shared'; |
| 4 | + |
| 5 | +export interface TokenBalance { |
| 6 | + symbol: string; |
| 7 | + address: string; |
| 8 | + name: string; |
| 9 | + balance: string; |
| 10 | + decimals: number; |
| 11 | + formattedBalance: number; |
| 12 | +} |
| 13 | + |
| 14 | +export interface ExplorerToken { |
| 15 | + value: string; |
| 16 | + token: { |
| 17 | + symbol: string; |
| 18 | + address_hash?: string; |
| 19 | + address?: string; |
| 20 | + name: string; |
| 21 | + decimals: string; |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +export interface ExplorerApiResponse { |
| 26 | + items: ExplorerToken[]; |
| 27 | +} |
| 28 | + |
| 29 | +export async function GET(request: NextRequest) { |
| 30 | + try { |
| 31 | + const { searchParams } = new URL(request.url); |
| 32 | + const address = searchParams.get('address'); |
| 33 | + const chainId = searchParams.get('chainId'); |
| 34 | + |
| 35 | + if (!address || !chainId) { |
| 36 | + return NextResponse.json( |
| 37 | + { error: 'Missing required parameters: address and chainId' }, |
| 38 | + { status: 400 }, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // Get chain configuration |
| 43 | + const chainConfig = getChainConfig(Number(chainId)); |
| 44 | + if (!chainConfig) { |
| 45 | + return NextResponse.json( |
| 46 | + { error: `Unsupported chain ID: ${chainId}` }, |
| 47 | + { status: 400 }, |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + // Make the API request to the explorer |
| 52 | + const url = `${chainConfig.apiUrl}/v2/addresses/${address}/tokens?type=ERC-20`; |
| 53 | + |
| 54 | + console.log('Explorer API URL:', url); |
| 55 | + |
| 56 | + const response = await fetch(url, { |
| 57 | + headers: { |
| 58 | + Accept: 'application/json', |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + console.log('Explorer API response status:', response.status); |
| 63 | + |
| 64 | + if (!response.ok) { |
| 65 | + console.log('Explorer API error:', response.status, response.statusText); |
| 66 | + |
| 67 | + return NextResponse.json( |
| 68 | + { |
| 69 | + error: `Explorer API error: ${response.status} ${response.statusText}`, |
| 70 | + }, |
| 71 | + { status: response.status }, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + const data = (await response.json()) as ExplorerApiResponse; |
| 76 | + |
| 77 | + console.log('Explorer API data items count:', data.items?.length || 0); |
| 78 | + |
| 79 | + // Filter out tokens with zero balance |
| 80 | + const filteredTokens: ExplorerToken[] = data.items.filter((item) => { |
| 81 | + return item.value !== '0' && item.value !== '0x0'; |
| 82 | + }); |
| 83 | + |
| 84 | + console.log( |
| 85 | + `Found ${filteredTokens.length} tokens with non-zero balance out of ${data.items.length} total tokens`, |
| 86 | + ); |
| 87 | + |
| 88 | + // Process token balances |
| 89 | + const tokenBalances: TokenBalance[] = filteredTokens.map((item) => { |
| 90 | + const decimals = Number(item.token.decimals); |
| 91 | + const balanceWei = BigInt(item.value); |
| 92 | + const formattedBalance = Number(balanceWei) / Math.pow(10, decimals); |
| 93 | + |
| 94 | + return { |
| 95 | + symbol: item.token.symbol, |
| 96 | + address: item.token.address_hash || item.token.address || '', |
| 97 | + name: item.token.name, |
| 98 | + balance: item.value, |
| 99 | + decimals, |
| 100 | + formattedBalance, |
| 101 | + }; |
| 102 | + }); |
| 103 | + |
| 104 | + console.log( |
| 105 | + 'Processed token balances:', |
| 106 | + tokenBalances.map((t) => { |
| 107 | + return `${t.symbol}: ${t.formattedBalance}`; |
| 108 | + }), |
| 109 | + ); |
| 110 | + |
| 111 | + return NextResponse.json({ |
| 112 | + tokens: tokenBalances, |
| 113 | + total: filteredTokens.length, |
| 114 | + }); |
| 115 | + } catch (error) { |
| 116 | + console.error('Error in token balances API:', error); |
| 117 | + return NextResponse.json( |
| 118 | + { error: 'Internal server error' }, |
| 119 | + { status: 500 }, |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +interface ChainConfig { |
| 125 | + apiUrl: string; |
| 126 | + nativeSymbol: string; |
| 127 | + nativeName: string; |
| 128 | +} |
| 129 | + |
| 130 | +function getChainConfig(chainId: number): ChainConfig | null { |
| 131 | + const configs: Record<number, ChainConfig> = { |
| 132 | + [haqqTestethic.id]: { |
| 133 | + // HAQQ Devnet1 |
| 134 | + apiUrl: haqqTestethic.blockExplorers.default.apiUrl, |
| 135 | + nativeSymbol: 'ISLM', |
| 136 | + nativeName: 'Islamic Coin', |
| 137 | + }, |
| 138 | + [sepolia.id]: { |
| 139 | + // Sepolia |
| 140 | + apiUrl: 'https://eth-sepolia.blockscout.com/api', |
| 141 | + nativeSymbol: 'ETH', |
| 142 | + nativeName: 'Ethereum', |
| 143 | + }, |
| 144 | + [haqqMainnet.id]: { |
| 145 | + // HAQQ Mainnet |
| 146 | + apiUrl: haqqMainnet.blockExplorers.default.apiUrl, |
| 147 | + nativeSymbol: 'ISLM', |
| 148 | + nativeName: 'Islamic Coin', |
| 149 | + }, |
| 150 | + [haqqTestedge2.id]: { |
| 151 | + // HAQQ Testedge2 |
| 152 | + apiUrl: haqqTestedge2.blockExplorers.default.apiUrl, |
| 153 | + nativeSymbol: 'ISLM', |
| 154 | + nativeName: 'Islamic Coin', |
| 155 | + }, |
| 156 | + }; |
| 157 | + |
| 158 | + return configs[chainId] || null; |
| 159 | +} |
0 commit comments