@@ -376,35 +376,91 @@ function updateCombinedStats(pool1Data, pool2Data, tokenData) {
376376 }
377377}
378378
379- // Fetch token holders count from Blockscout API
379+ // Fetch token holders count from Basescan with 1-hour caching
380380async function fetchHoldersCount ( ) {
381- const FALLBACK_HOLDERS = '0 ' ;
382- const fallback = ( ) => updateElement ( 'holdersCount' , FALLBACK_HOLDERS ) ;
381+ const CACHE_KEY = 'fula_holders_cache ' ;
382+ const CACHE_DURATION = 60 * 60 * 1000 ; // 1 hour in milliseconds
383383
384+ // Check cache first
384385 try {
385- const response = await fetch ( `https://base.blockscout.com/api/v2/tokens/${ FULA_TOKEN_ADDRESS } ` ) ;
386+ const cached = localStorage . getItem ( CACHE_KEY ) ;
387+ if ( cached ) {
388+ const { holders, timestamp } = JSON . parse ( cached ) ;
389+ const age = Date . now ( ) - timestamp ;
390+ if ( age < CACHE_DURATION ) {
391+ console . log ( `Using cached holders count: ${ holders } (cached ${ Math . round ( age / 60000 ) } minutes ago)` ) ;
392+ updateElement ( 'holdersCount' , holders . toLocaleString ( 'en-US' ) ) ;
393+ return ;
394+ }
395+ console . log ( 'Cache expired, fetching fresh data...' ) ;
396+ }
397+ } catch ( e ) {
398+ console . warn ( 'Error reading holders cache:' , e ) ;
399+ }
400+
401+ // Fetch fresh data from Basescan
402+ try {
403+ const response = await fetch ( `https://basescan.org/token/${ FULA_TOKEN_ADDRESS } ` , {
404+ headers : {
405+ 'Accept' : 'text/html'
406+ }
407+ } ) ;
386408
387409 if ( ! response . ok ) {
388- console . warn ( `Holders API HTTP error: ${ response . status } ` ) ;
389- fallback ( ) ;
390- return ;
410+ throw new Error ( `HTTP error: ${ response . status } ` ) ;
411+ }
412+
413+ const html = await response . text ( ) ;
414+
415+ // Parse holdersplotData from the HTML to get the latest holder count
416+ const match = html . match ( / v a r \s + h o l d e r s p l o t D a t a \s * = \s * \[ ( [ \s \S ] * ?) \] ; / ) ;
417+ if ( match ) {
418+ // Extract all y values and get the last one (most recent holder count)
419+ const yMatches = [ ...match [ 1 ] . matchAll ( / y : \s * ( \d + ) / g) ] ;
420+ if ( yMatches . length > 0 ) {
421+ const lastHolderCount = parseInt ( yMatches [ yMatches . length - 1 ] [ 1 ] , 10 ) ;
422+
423+ if ( lastHolderCount > 0 ) {
424+ // Cache the result
425+ localStorage . setItem ( CACHE_KEY , JSON . stringify ( {
426+ holders : lastHolderCount ,
427+ timestamp : Date . now ( )
428+ } ) ) ;
429+
430+ updateElement ( 'holdersCount' , lastHolderCount . toLocaleString ( 'en-US' ) ) ;
431+ console . log ( `Holders count fetched from Basescan: ${ lastHolderCount } ` ) ;
432+ return ;
433+ }
434+ }
391435 }
392436
393- const data = await response . json ( ) ;
394- const holdersRaw = data && data . holders_count !== undefined ? data . holders_count : '0' ;
395- const holdersValue = Number ( holdersRaw ) ;
396-
397- if ( Number . isFinite ( holdersValue ) && holdersValue >= 0 ) {
398- const holdersCount = holdersValue . toLocaleString ( 'en-US' ) ;
399- updateElement ( 'holdersCount' , holdersCount ) ;
400- console . log ( `Holders count: ${ holdersCount } ` ) ;
401- } else {
402- console . warn ( 'No holders data in response, defaulting to 0' ) ;
403- fallback ( ) ;
437+ throw new Error ( 'Could not parse holders data from Basescan' ) ;
438+ } catch ( error ) {
439+ console . error ( 'Error fetching holders from Basescan:' , error ) ;
440+ // Fallback to static file
441+ await fetchHoldersFromFile ( ) ;
442+ }
443+ }
444+
445+ // Fallback: fetch holders count from static file
446+ async function fetchHoldersFromFile ( ) {
447+ try {
448+ const response = await fetch ( './token_holders.txt?' + Date . now ( ) ) ; // Cache bust
449+ if ( ! response . ok ) {
450+ throw new Error ( `HTTP error: ${ response . status } ` ) ;
451+ }
452+ const text = await response . text ( ) ;
453+ const holders = parseInt ( text . trim ( ) , 10 ) ;
454+
455+ if ( Number . isFinite ( holders ) && holders > 0 ) {
456+ updateElement ( 'holdersCount' , holders . toLocaleString ( 'en-US' ) ) ;
457+ console . log ( `Holders count from fallback file: ${ holders } ` ) ;
458+ return ;
404459 }
460+ throw new Error ( 'Invalid holders count in file' ) ;
405461 } catch ( error ) {
406- console . error ( 'Error fetching holders count :' , error ) ;
407- fallback ( ) ;
462+ console . error ( 'Error fetching holders from file :' , error ) ;
463+ updateElement ( 'holdersCount' , '-' ) ;
408464 }
409465}
410466
0 commit comments