| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import * as fs from 'fs';
- import * as readline from 'readline';
- import logger from "../utils/logger";
- interface TokenData {
- token0: string;
- token1: string;
- }
- interface TokenCount {
- token: string;
- count: number;
- }
- async function calculateTopNTokens(filePath: string, N: number): Promise<void> {
- const fileStream = fs.createReadStream(filePath);
- const rl = readline.createInterface({
- input: fileStream,
- crlfDelay: Infinity
- });
- const tokenCounts: { [key: string]: number } = {};
- for await (const line of rl) {
- // Assume each line is a separate JSON object
- const lpList: TokenData[] = JSON.parse(line);
- for (const lp of lpList) {
- tokenCounts[lp.token0] = (tokenCounts[lp.token0] || 0) + 1;
- tokenCounts[lp.token1] = (tokenCounts[lp.token1] || 0) + 1;
- }
- }
- let tokenCountArray: TokenCount[] = Object.keys(tokenCounts).map(token => {
- return { token: token, count: tokenCounts[token] };
- });
- // Sort the array in descending order of count
- tokenCountArray.sort((a, b) => b.count - a.count);
- // Get the top N tokens
- const topNTokens = tokenCountArray.slice(0, N);
- logger.info(topNTokens);
- }
- function sleep(ms: number) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- async function main() {
- await calculateTopNTokens("J:\\temp\\v2-lp-list.json", 20)
- }
- main().catch((error) => {
- console.error(error);
- process.exitCode = 1;
- })
|