浏览代码

可以过滤出现频率前n的tokens

skyfffire 2 年之前
父节点
当前提交
eada1d0b02
共有 5 个文件被更改,包括 65 次插入96 次删除
  1. 5 14
      hardhat.config.ts
  2. 1 3
      package.json
  3. 59 0
      scripts/calculate_top_n_tokens.ts
  4. 0 19
      scripts/deploy.ts
  5. 0 60
      scripts/uniswapv2_swap_router_listener.ts

+ 5 - 14
hardhat.config.ts

@@ -1,24 +1,15 @@
 import { HardhatUserConfig, extendEnvironment } from "hardhat/config";
 import { HardhatUserConfig, extendEnvironment } from "hardhat/config";
+import {HardhatRuntimeEnvironment} from "hardhat/types";
 import "@nomicfoundation/hardhat-toolbox";
 import "@nomicfoundation/hardhat-toolbox";
-
 import deployer from './.secret'
 import deployer from './.secret'
-import {HardhatRuntimeEnvironment} from "hardhat/types";
-
-const BSC = 'https://bsc.meowrpc.com'
-const BSC_TESTNET = 'https://data-seed-prebsc-1-s1.binance.org:8545/'
 
 
 const config: HardhatUserConfig = {
 const config: HardhatUserConfig = {
-  defaultNetwork: 'bsc_testnet',
+  defaultNetwork: 'core',
   solidity: "0.7.6",
   solidity: "0.7.6",
   networks: {
   networks: {
-    bsc: {
-      url: BSC,
-      chainId: 56,
-      accounts: [deployer.private]
-    },
-    bsc_testnet: {
-      url: BSC_TESTNET,
-      chainId: 97,
+    core: {
+      url: 'http://127.0.0.1:8545',
+      chainId: 1116,
       accounts: [deployer.private]
       accounts: [deployer.private]
     }
     }
   },
   },

+ 1 - 3
package.json

@@ -19,8 +19,6 @@
     "ts-md5": "^1.3.1"
     "ts-md5": "^1.3.1"
   },
   },
   "scripts": {
   "scripts": {
-    "deploy-price-query-tool": "npx hardhat run scripts/deploy.ts",
-    "v2-router-listener": "npx hardhat run scripts/uniswapv2_swap_router_listener.ts --network bsc",
-    "v2-router-listener-testnet": "npx hardhat run scripts/uniswapv2_swap_router_listener.ts"
+    "calculate": "npx hardhat run scripts/calculate_top_n_tokens.ts"
   }
   }
 }
 }

+ 59 - 0
scripts/calculate_top_n_tokens.ts

@@ -0,0 +1,59 @@
+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);
+
+  console.log(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;
+})

+ 0 - 19
scripts/deploy.ts

@@ -1,19 +0,0 @@
-import { ethers } from "hardhat";
-
-async function main() {
-  const contractName = 'PriceQueryTool'
-
-  const Contract = await ethers.getContractFactory(contractName);
-  console.log('deploying...')
-  const contract = await Contract.deploy();
-  console.log(`${ contractName } deployed, confirm...`)
-  await contract.deployed();
-  console.log(`${ contractName } deployed to ${contract.address}`);
-}
-
-// We recommend this pattern to be able to use async/await everywhere
-// and properly handle errors.
-main().catch((error) => {
-  console.error(error);
-  process.exitCode = 1;
-});

+ 0 - 60
scripts/uniswapv2_swap_router_listener.ts

@@ -1,60 +0,0 @@
-import {web3} from "hardhat";
-import logger from "../utils/logger";
-
-function sleep(ms: number) {
-  return new Promise(resolve => setTimeout(resolve, ms));
-}
-
-// swapExactTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] path, address to, uint256 deadline)
-const FEATURE_STRING = "0x38ed1739"
-let lastBlockNumber = 0
-
-const v2_router_map: any = {}
-
-function txHandler(tx: any) {
-  const input = tx.input
-
-  if (input.indexOf(FEATURE_STRING) === -1) return
-
-  if (!v2_router_map[tx.to]) {
-    v2_router_map[tx.to] = true
-
-    logger.info(Object.keys(v2_router_map))
-  }
-}
-
-async function onTick() {
-  // block没变就不获取
-  const blockNumber = await web3.eth.getBlockNumber()
-  if (blockNumber === lastBlockNumber) return
-  lastBlockNumber = blockNumber
-
-  // block变了就拿block中的所有hash
-  const block = await web3.eth.getBlock(blockNumber)
-  const txHashList = block.transactions
-
-  for (const txHash of txHashList) {
-    txHandler(await web3.eth.getTransaction(txHash))
-
-    await sleep(100)
-  }
-}
-
-async function main() {
-  while (true) {
-    try {
-      await onTick()
-    } catch (error: any) {
-      if (error.toString().indexOf('Number can only safely store up to 53 bits') === -1) {
-        console.error(error)
-      }
-    }
-
-    await sleep(1000)
-  }
-}
-
-main().catch((error) => {
-  console.error(error);
-  process.exitCode = 1;
-})