Advertisement
tenequm

Untitled

Jan 24th, 2025
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { connect } from 'puppeteer-real-browser';
  2.  
  3. interface Token {
  4.     address: string;
  5.     name: string;
  6.     symbol: string;
  7. }
  8.  
  9. interface PairData {
  10.     chainId: string;
  11.     dexId: string;
  12.     pairAddress: string;
  13.     baseToken: Token;
  14.     quoteToken: Token;
  15.     priceUsd: string;
  16.     volume: {
  17.         h24: number;
  18.     };
  19. }
  20.  
  21. interface ServerData {
  22.     route: {
  23.         data: {
  24.             dexScreenerData: {
  25.                 pairs: PairData[];
  26.             };
  27.         };
  28.     };
  29. }
  30.  
  31. async function fetchTrendingTokens(): Promise<void> {
  32.     const targetUrl = 'https://dexscreener.com/?rankBy=trendingScoreH24&order=desc&minLiq=50000';
  33.  
  34.     const { browser, page } = await connect({
  35.         headless: false,
  36.         turnstile: true,
  37.         args: [],
  38.         customConfig: {},
  39.         connectOption: {},
  40.         disableXvfb: false,
  41.         ignoreAllFlags: false
  42.     });
  43.  
  44.     try {
  45.         await page.goto(targetUrl, { waitUntil: 'networkidle0' });
  46.        
  47.         // Wait for the data to be available
  48.         await page.waitForFunction(() => {
  49.             return window.hasOwnProperty('__SERVER_DATA') &&
  50.                    (window as any).__SERVER_DATA?.route?.data?.dexScreenerData?.pairs;
  51.         }, { timeout: 30000 });
  52.  
  53.         // Extract window.__SERVER_DATA content
  54.         const serverData: ServerData = await page.evaluate(() => {
  55.             const data = (window as any).__SERVER_DATA;
  56.             // Handle any undefined values or JS objects before stringifying
  57.             const sanitizedData = JSON.parse(JSON.stringify(data, (key, value) => {
  58.                 if (value === undefined) return null;
  59.                 if (value instanceof Date) return value.toISOString();
  60.                 if (value instanceof URL) return value.toString();
  61.                 return value;
  62.             }));
  63.             return sanitizedData;
  64.         });
  65.  
  66.         const pairs = serverData.route.data.dexScreenerData.pairs;
  67.        
  68.         console.log('\nTrending Tokens:');
  69.         pairs.forEach((pair, index) => {
  70.             console.log(`\n${index + 1}. ${pair.baseToken.name} (${pair.baseToken.symbol})`);
  71.             console.log(`   Chain: ${pair.chainId}`);
  72.             console.log(`   DEX: ${pair.dexId}`);
  73.             console.log(`   Price: $${pair.priceUsd}`);
  74.             console.log(`   24h Volume: $${pair.volume.h24.toLocaleString()}`);
  75.             console.log(`   Token Address: ${pair.baseToken.address}`);
  76.         });
  77.  
  78.     } catch (error) {
  79.         console.error('Error fetching data:', error);
  80.     } finally {
  81.         await browser.close();
  82.     }
  83. }
  84.  
  85. console.log('Fetching trending tokens...');
  86. fetchTrendingTokens().catch(console.error);
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement