Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { connect } from 'puppeteer-real-browser';
- interface Token {
- address: string;
- name: string;
- symbol: string;
- }
- interface PairData {
- chainId: string;
- dexId: string;
- pairAddress: string;
- baseToken: Token;
- quoteToken: Token;
- priceUsd: string;
- volume: {
- h24: number;
- };
- }
- interface ServerData {
- route: {
- data: {
- dexScreenerData: {
- pairs: PairData[];
- };
- };
- };
- }
- async function fetchTrendingTokens(): Promise<void> {
- const targetUrl = 'https://dexscreener.com/?rankBy=trendingScoreH24&order=desc&minLiq=50000';
- const { browser, page } = await connect({
- headless: false,
- turnstile: true,
- args: [],
- customConfig: {},
- connectOption: {},
- disableXvfb: false,
- ignoreAllFlags: false
- });
- try {
- await page.goto(targetUrl, { waitUntil: 'networkidle0' });
- // Wait for the data to be available
- await page.waitForFunction(() => {
- return window.hasOwnProperty('__SERVER_DATA') &&
- (window as any).__SERVER_DATA?.route?.data?.dexScreenerData?.pairs;
- }, { timeout: 30000 });
- // Extract window.__SERVER_DATA content
- const serverData: ServerData = await page.evaluate(() => {
- const data = (window as any).__SERVER_DATA;
- // Handle any undefined values or JS objects before stringifying
- const sanitizedData = JSON.parse(JSON.stringify(data, (key, value) => {
- if (value === undefined) return null;
- if (value instanceof Date) return value.toISOString();
- if (value instanceof URL) return value.toString();
- return value;
- }));
- return sanitizedData;
- });
- const pairs = serverData.route.data.dexScreenerData.pairs;
- console.log('\nTrending Tokens:');
- pairs.forEach((pair, index) => {
- console.log(`\n${index + 1}. ${pair.baseToken.name} (${pair.baseToken.symbol})`);
- console.log(` Chain: ${pair.chainId}`);
- console.log(` DEX: ${pair.dexId}`);
- console.log(` Price: $${pair.priceUsd}`);
- console.log(` 24h Volume: $${pair.volume.h24.toLocaleString()}`);
- console.log(` Token Address: ${pair.baseToken.address}`);
- });
- } catch (error) {
- console.error('Error fetching data:', error);
- } finally {
- await browser.close();
- }
- }
- console.log('Fetching trending tokens...');
- fetchTrendingTokens().catch(console.error);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement