11import { NextRequest , NextResponse } from 'next/server' ;
2+ import { chains } from '@/config/networks' ;
23
34const ZAPPER_API_KEY = process . env . ZAPPER_API_KEY ;
45
6+ // Helper function to get readable network name
7+ const getReadableNetworkName = ( networkName : string ) : string => {
8+ const networkMap : Record < string , string > = {
9+ 'ETHEREUM_MAINNET' : 'Ethereum' ,
10+ 'BASE_MAINNET' : 'Base' ,
11+ 'OPTIMISM_MAINNET' : 'Optimism' ,
12+ 'ARBITRUM_MAINNET' : 'Arbitrum' ,
13+ 'Ethereum' : 'Ethereum' ,
14+ 'Base' : 'Base' ,
15+ 'OP Mainnet' : 'Optimism' ,
16+ 'Arbitrum One' : 'Arbitrum' ,
17+ // Additional possible formats from Zapper
18+ 'ethereum' : 'Ethereum' ,
19+ 'base' : 'Base' ,
20+ 'optimism' : 'Optimism' ,
21+ 'arbitrum' : 'Arbitrum' ,
22+ 'Ethereum Mainnet' : 'Ethereum' ,
23+ 'Base Mainnet' : 'Base' ,
24+ 'Optimism Mainnet' : 'Optimism' ,
25+ 'Arbitrum Mainnet' : 'Arbitrum' ,
26+ } ;
27+
28+ // Check exact match first
29+ if ( networkMap [ networkName ] ) {
30+ return networkMap [ networkName ] ;
31+ }
32+
33+ // Check if network name contains keywords (case insensitive)
34+ const lowerName = networkName . toLowerCase ( ) ;
35+ if ( lowerName . includes ( 'ethereum' ) || lowerName . includes ( 'eth' ) ) {
36+ return 'Ethereum' ;
37+ }
38+ if ( lowerName . includes ( 'base' ) ) {
39+ return 'Base' ;
40+ }
41+ if ( lowerName . includes ( 'optimism' ) || lowerName . includes ( 'op' ) ) {
42+ return 'Optimism' ;
43+ }
44+ if ( lowerName . includes ( 'arbitrum' ) || lowerName . includes ( 'arb' ) ) {
45+ return 'Arbitrum' ;
46+ }
47+
48+ return networkName ;
49+ } ;
50+
551export async function POST ( request : NextRequest ) {
652 try {
7- const { address, network = 'base' } = await request . json ( ) ;
53+ const { address } = await request . json ( ) ;
854
9- console . log ( 'Portfolio API called for address:' , address , 'network:' , network ) ;
55+ console . log ( 'Portfolio API called for address:' , address ) ;
1056
1157 if ( ! address ) {
1258 return NextResponse . json (
@@ -22,15 +68,8 @@ export async function POST(request: NextRequest) {
2268 ) ;
2369 }
2470
25- // Network configuration
26- const networkConfig = {
27- mainnet : { chainId : 1 , network : 'ETHEREUM_MAINNET' } ,
28- 'op-mainnet' : { chainId : 10 , network : 'OPTIMISM_MAINNET' } ,
29- base : { chainId : 8453 , network : 'BASE_MAINNET' } ,
30- arbitrum : { chainId : 42161 , network : 'ARBITRUM_MAINNET' }
31- } ;
32-
33- const selectedNetwork = networkConfig [ network as keyof typeof networkConfig ] || networkConfig . base ;
71+ // Get all chain IDs from the chains configuration
72+ const chainIds = chains . map ( chain => chain . id ) ;
3473
3574 const headers = {
3675 'Content-Type' : 'application/json' ,
@@ -75,7 +114,7 @@ export async function POST(request: NextRequest) {
75114 query : portfolioQuery ,
76115 variables : {
77116 addresses : [ address ] ,
78- chainIds : [ selectedNetwork . chainId ] ,
117+ chainIds : chainIds ,
79118 } ,
80119 } ) ,
81120 } ) ;
@@ -92,7 +131,7 @@ export async function POST(request: NextRequest) {
92131
93132 console . log ( 'Portfolio data fetched successfully' ) ;
94133
95- // Fetch activity data for Base chain only
134+ // Fetch activity data for all chains
96135 const activityQuery = `
97136 query TransactionDescriptionExample($subjects: [Address!]!, $perspective: TransactionHistoryV2Perspective, $first: Int, $filters: TransactionHistoryV2FiltersArgs) {
98137 transactionHistoryV2(subjects: $subjects, perspective: $perspective, first: $first, filters: $filters) {
@@ -151,7 +190,7 @@ export async function POST(request: NextRequest) {
151190 perspective : 'All' ,
152191 first : 20 ,
153192 filters : {
154- chainIds : [ selectedNetwork . chainId ]
193+ chainIds : chainIds
155194 }
156195 } ,
157196 } ) ,
@@ -191,10 +230,13 @@ export async function POST(request: NextRequest) {
191230 }
192231
193232 // Add transaction data in the format expected by the frontend
233+ // Find the chain name for this network and make it readable
234+ const chain = chains . find ( c => c . id === node . network ) ;
235+ const networkName = chain ?. name || `Chain ${ node . network } ` ;
194236 node . transaction = {
195237 hash : node . transactionHash ,
196238 timestamp : node . transactionBlockTimestamp ,
197- network : node . network
239+ network : getReadableNetworkName ( networkName )
198240 } ;
199241 }
200242 } ) ;
@@ -213,7 +255,14 @@ export async function POST(request: NextRequest) {
213255 const portfolio = portfolioResult . data . portfolioV2 ;
214256
215257 // Filter tokens with value >= $0.01 and calculate total from tokens only
216- const allTokenBalances = portfolio . tokenBalances ?. byToken ?. edges ?. map ( ( edge : any ) => edge . node ) || [ ] ;
258+ const allTokenBalances = portfolio . tokenBalances ?. byToken ?. edges ?. map ( ( edge : any ) => {
259+ const token = edge . node ;
260+ // Update network name to be readable
261+ if ( token . network ?. name ) {
262+ token . network . name = getReadableNetworkName ( token . network . name ) ;
263+ }
264+ return token ;
265+ } ) || [ ] ;
217266 const filteredTokenBalances = allTokenBalances . filter ( ( token : any ) => token . balanceUSD >= 0.01 ) ;
218267
219268 const totalValue = filteredTokenBalances . reduce ( ( sum : number , token : any ) => sum + token . balanceUSD , 0 ) ;
0 commit comments