calculate_top_n_tokens.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import * as fs from 'fs';
  2. import * as readline from 'readline';
  3. import logger from "../utils/logger";
  4. interface TokenData {
  5. token0: string;
  6. token1: string;
  7. }
  8. interface TokenCount {
  9. token: string;
  10. count: number;
  11. }
  12. async function calculateTopNTokens(filePath: string, N: number): Promise<void> {
  13. const fileStream = fs.createReadStream(filePath);
  14. const rl = readline.createInterface({
  15. input: fileStream,
  16. crlfDelay: Infinity
  17. });
  18. const tokenCounts: { [key: string]: number } = {};
  19. for await (const line of rl) {
  20. // Assume each line is a separate JSON object
  21. const lpList: TokenData[] = JSON.parse(line);
  22. for (const lp of lpList) {
  23. tokenCounts[lp.token0] = (tokenCounts[lp.token0] || 0) + 1;
  24. tokenCounts[lp.token1] = (tokenCounts[lp.token1] || 0) + 1;
  25. }
  26. }
  27. let tokenCountArray: TokenCount[] = Object.keys(tokenCounts).map(token => {
  28. return { token: token, count: tokenCounts[token] };
  29. });
  30. // Sort the array in descending order of count
  31. tokenCountArray.sort((a, b) => b.count - a.count);
  32. // Get the top N tokens
  33. const topNTokens = tokenCountArray.slice(0, N);
  34. logger.info(topNTokens);
  35. }
  36. function sleep(ms: number) {
  37. return new Promise(resolve => setTimeout(resolve, ms));
  38. }
  39. async function main() {
  40. await calculateTopNTokens("J:\\temp\\v2-lp-list.json", 20)
  41. }
  42. main().catch((error) => {
  43. console.error(error);
  44. process.exitCode = 1;
  45. })