Selaa lähdekoodia

准备查找uniswap_swap_router

skyfffire 2 vuotta sitten
vanhempi
commit
df30904051

+ 1 - 0
.gitignore

@@ -1,4 +1,5 @@
 .secret.ts
+.debug
 
 node_modules
 .env

+ 4 - 4
hardhat.config.ts

@@ -5,8 +5,8 @@ import deployer from './.secret'
 import {HardhatRuntimeEnvironment} from "hardhat/types";
 
 // const ETH_RPC = '/ethereum/data/geth.ipc'
-const ETH_RPC = 'http://3.227.34.41:8545'
-const ETH_W_RPC = 'https://mainnet.ethereumpow.org'
+const ETH_RPC = 'https://mainnet.infura.io/v3/'
+const ARBITRUM_GOERLI = 'https://endpoints.omniatech.io/v1/arbitrum/goerli/public'
 
 const config: HardhatUserConfig = {
   defaultNetwork: 'eth_w',
@@ -18,8 +18,8 @@ const config: HardhatUserConfig = {
       accounts: [deployer.private]
     },
     eth_w: {
-      url: ETH_W_RPC,
-      chainId: 10001,
+      url: ARBITRUM_GOERLI,
+      chainId: 421613,
       accounts: [deployer.private]
     }
   },

+ 3 - 0
package.json

@@ -17,5 +17,8 @@
     "@uniswap/v3-core": "^1.0.1",
     "@uniswap/v3-periphery": "^1.4.2",
     "ts-md5": "^1.3.1"
+  },
+  "scripts": {
+    "router_listener": "npx hardhat run scripts/uniswap_swap_router_listener.ts"
   }
 }

+ 0 - 98
scripts/calcLevel2Path.ts

@@ -1,98 +0,0 @@
-import { web3 } from "hardhat";
-import swapPath from "./interface/swapPath";
-import logger from "../utils/logger";
-
-function filterPool(pool: any) {
-  return true
-}
-
-function handleSamePools(poolList: any[], pathList: any[]) {
-  for (let poolIndexI = 0; poolIndexI < poolList.length; poolIndexI++) {
-    const x = poolList[poolIndexI]
-    if (!filterPool(x)) continue
-
-    for (let poolIndexJ = poolIndexI + 1; poolIndexJ < poolList.length; poolIndexJ++) {
-      const y = poolList[poolIndexJ]
-      if (!filterPool(y)) continue
-
-      pathList.push([x, y])
-    }
-  }
-}
-
-async function main() {
-  logger.debug('start loading...')
-  const v2PoolList = require('../config/v2PoolList.json')
-  const v3PoolList = require('../config/v3PoolList.json')
-
-  logger.debug('start filter...')
-  const checkedPath: any = {}
-  for (let v2PoolIndex = 0; v2PoolIndex < v2PoolList.length; v2PoolIndex++) {
-    const v2Pool = v2PoolList[v2PoolIndex]
-    v2Pool.type = 'v2'
-
-    // 已经查过的sum不再查询
-    if (checkedPath[v2Pool.sum2] != null) continue
-    checkedPath[v2Pool.sum2] = []
-
-    // 开始排查
-    const sameSumPoolList = [v2Pool]
-    // 排查v2的,从当前位置开始,以免重复path
-    for (let findSameSumV2Index = v2PoolIndex + 1; findSameSumV2Index < v2PoolList.length; findSameSumV2Index++) {
-      const v2PoolUnChecked = v2PoolList[findSameSumV2Index]
-      // 同sumValue
-      if (v2PoolUnChecked.sum2 == v2Pool.sum2) {
-        v2PoolUnChecked.type = 'v2'
-        sameSumPoolList.push(v2PoolUnChecked)
-      }
-    }
-    // 排查v3的,从0开始
-    for (let findSameSumV3Index = 0; findSameSumV3Index < v3PoolList.length; findSameSumV3Index++) {
-      const v3PoolUnChecked = v3PoolList[findSameSumV3Index]
-      // 同sumValue
-      if (v3PoolUnChecked.sum2 == v2Pool.sum2) {
-        v3PoolUnChecked.type = 'v3'
-        sameSumPoolList.push(v3PoolUnChecked)
-      }
-    }
-
-    if (sameSumPoolList.length > 1) {
-      handleSamePools(sameSumPoolList, checkedPath[v2Pool.sum2])
-      const rst = await swapPath.appendOrUpdate(v2Pool.sum2, '2', checkedPath[v2Pool.sum2])
-      logger.debug(`${rst.msg}, v2, ${v2PoolIndex}/${v2PoolList.length}`)
-    }
-  }
-  // 处理v3的
-  for (let v3PoolIndex = 0; v3PoolIndex < v3PoolList.length; v3PoolIndex++) {
-    const v3Pool = v3PoolList[v3PoolIndex]
-    v3Pool.type = 'v3'
-
-    // 已经查过的sum不再查询
-    if (checkedPath[v3Pool.sum2] != null) continue
-    checkedPath[v3Pool.sum2] = []
-
-    // 开始排查
-    const sameSumPoolList = [v3Pool]
-    // 排查v3的,从v3PoolIndex开始
-    for (let findSameSumV3Index = v3PoolIndex + 1; findSameSumV3Index < v3PoolList.length; findSameSumV3Index++) {
-      const v3PoolUnChecked = v3PoolList[findSameSumV3Index]
-      // 同sumValue
-      if (v3PoolUnChecked.sum2 == v3Pool.sum2) {
-        v3PoolUnChecked.type = 'v3'
-        sameSumPoolList.push(v3PoolUnChecked)
-      }
-    }
-
-    if (sameSumPoolList.length > 1) {
-      handleSamePools(sameSumPoolList, checkedPath[v3Pool.sum2])
-      const rst = await swapPath.appendOrUpdate(v3Pool.sum2, '2', checkedPath[v3Pool.sum2])
-      logger.debug(`${rst.msg}, v3, ${v3PoolIndex}/${v3PoolList.length}`)
-    }
-  }
-  logger.debug('query ok.')
-}
-
-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 = 'Flash'
-
-  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 - 20
scripts/deployV3Tool.ts

@@ -1,20 +0,0 @@
-import { ethers } from "hardhat";
-import contracts from "../config/contracts";
-
-async function main() {
-  const contractName = 'V3Tool'
-
-  const Contract = await ethers.getContractFactory(contractName);
-  console.log('deploying...')
-  const contract = await Contract.deploy(contracts.UNIV3_POSITION, contracts.UNIV3_FACTORY);
-  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 - 26
scripts/interface/history.ts

@@ -1,26 +0,0 @@
-import http from '../../utils/http'
-
-export default class History {
-  static async findByHashOrBlockOrDataVague(block: String, hashCode: String, dataVague: String,
-                                            limit1: Number, limit2: Number) {
-    let url = '/ethmev/findByHashOrBlockOrDataVague'
-    const rst = await http.post(url, {
-      block: block,
-      hash: hashCode,
-      dataVague: dataVague,
-      limit1: limit1,
-      limit2: limit2
-    })
-    return rst.data
-  }
-
-  static async appendOrUpdate(block: String, hashCode: String, data: any) {
-    let url = '/ethmev/appendOrUpdate'
-    const rst = await http.post(url, {
-      'block': block,
-      'hash': hashCode,
-      'data': JSON.stringify(data)
-    })
-    return rst.data
-  }
-}

+ 0 - 22
scripts/interface/swapPath.ts

@@ -1,22 +0,0 @@
-import http from '../../utils/http'
-
-export default class SwapPath {
-  static async findBySumValueAndLevel(sumValue: String, level: String) {
-    let url = '/swappath/findBySumValueAndLevel'
-    const rst = await http.post(url, {
-      sum_value: sumValue,
-      level: level
-    })
-    return rst.data
-  }
-
-  static async appendOrUpdate(sumValue: String, level: String, data: any) {
-    let url = '/swappath/appendOrUpdate'
-    const rst = await http.post(url, {
-      sum_value: sumValue,
-      level: level,
-      data: JSON.stringify(data)
-    })
-    return rst.data
-  }
-}

+ 0 - 136
scripts/pushv2.ts

@@ -1,136 +0,0 @@
-import { web3 } from "hardhat";
-import History from "./interface/history";
-import contracts from "../config/contracts";
-import logger from "../utils/logger";
-import v2_routers from "../config/v2_routers";
-import {BigNumber} from "ethers";
-import {replaceAll} from "hardhat/internal/util/strings";
-
-async function handlePosition(router: String, factory: String) {
-  try {
-    // 拉取当前pull状态信息
-    const pullState = await History.findByHashOrBlockOrDataVague('UNIV2DEX', factory, '', 0, 200)
-    let nowPosition = 0
-
-    // 没有状态信息的情况
-    if (pullState.data.length == 0) {
-      const appendRst = await History.appendOrUpdate('UNIV2DEX', factory, {"now": nowPosition})
-      logger.debug(appendRst)
-    } else {
-      nowPosition = pullState.data[0].dataObj.now
-    }
-
-    return nowPosition
-  } catch (e) {
-    logger.error(`Pull state error, router: ${router}, factory: ${factory}`)
-    logger.error(e)
-  }
-
-  return 0
-}
-
-async function handleAFactory(router: String, factory: String, position: number, pairsLength: number, v2_410_tool: any) {
-  let errorCount = 0
-
-  while (position <= pairsLength) {
-    try {
-      const info = await v2_410_tool.methods.getPairIdInfo(factory, position).call()
-
-      // return (
-      //  0   pairInfo.lp,
-      //  1   pairInfo.token0,
-      //  2   pairInfo.symbol0,
-      //  3   pairInfo.decimals0,
-      //  4   pairInfo.r0,
-      //  5   pairInfo.token1,
-      //  6   pairInfo.symbol1,
-      //  7   pairInfo.decimals1,
-      //  8   pairInfo.r1
-      // );
-
-      const symbol0 = info['2'].replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 10)
-      const symbol1 = info['6'].replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 10)
-      const name = `${router.slice(2, 4) + router.slice(-2)}_${symbol0}_${symbol1}`
-      const sum2 = replaceAll(BigNumber.from(info['1']).add(BigNumber.from(info['5']))._hex, '0x0', '0x')
-
-      const data = {
-        LP: info['0'],
-        decimals0: info['3'],
-        decimals1: info['7'],
-        factory: factory,
-        feei: 30,
-        id: position,
-        name: name,
-        r0: info['4'],
-        r1: info['8'],
-        router: router,
-        sum2: sum2,
-        symbol0: symbol0,
-        symbol1: symbol1,
-        token0: info['1'],
-        token1: info['5']
-      }
-      const insertRst = await History.appendOrUpdate('0', info['0'], data)
-      logger.debug(insertRst.msg, `, hash: ${info['0']}, ${position} / ${pairsLength}`)
-
-      // 每十次更新一次position
-      if (position % 50 == 0) {
-        await History.appendOrUpdate('UNIV2DEX', factory, {"now": position})
-        logger.debug(`Position updated: ${position}`)
-      }
-
-      errorCount = 0
-    } catch (e) {
-      logger.error(JSON.stringify(e))
-      errorCount++
-    }
-
-    position++
-    if (errorCount >= 5) {
-      position = position - 5
-      break;
-    }
-  }
-}
-
-async function main() {
-  const fromZero: boolean = true
-
-  logger.debug('Pull v2 start.')
-
-  const v2_router_abi = require('../abi/UNIV2_ROUTER_ABI.json')
-  const v2_factory_abi = require('../abi/UNIV2_FACTORY_ABI.json')
-  const v2_tool_abi = require('../abi/410_V2_TOOLS.json')
-
-  // 初始化410 v2工具箱
-  const v2_410_tool = new web3.eth.Contract(v2_tool_abi, contracts.TOOLS_410_V2)
-
-  for (let router_index = 0; router_index < v2_routers.length; router_index++) {
-    const v2_router_address = v2_routers[router_index]
-
-    try {
-      logger.debug(`Router address: ${v2_router_address}`)
-
-      // 获取工厂地址
-      const v2_router = new web3.eth.Contract(v2_router_abi, v2_router_address)
-      const v2_factory_address = await v2_router.methods.factory().call()
-      // 获取工厂实例
-      const v2_factory = new web3.eth.Contract(v2_factory_abi, v2_factory_address)
-      // 获取当前pairsLength
-      const pairsLength = await v2_factory.methods.allPairsLength().call()
-      // 获取当前pull状态
-      const position = fromZero ? 0 : await handlePosition(v2_router_address, v2_factory_address)
-
-      logger.debug(`factory: ${v2_factory_address}, ${position} / ${pairsLength}.`)
-      await handleAFactory(v2_router_address, v2_factory_address, position, pairsLength, v2_410_tool)
-    } catch (e) {
-      logger.error(`New contract error, router: ${v2_router_address}`)
-      logger.error(e)
-    }
-  }
-}
-
-main().catch((error) => {
-  console.error(error);
-  process.exitCode = 1;
-})

+ 0 - 124
scripts/pushv3.ts

@@ -1,124 +0,0 @@
-import { web3 } from "hardhat";
-import History from "./interface/history";
-import contracts from "../config/contracts";
-import logger from "../utils/logger";
-import v3_routers from "../config/v3_routers";
-import {BigNumber} from "ethers";
-import {replaceAll} from "hardhat/internal/util/strings";
-
-async function handlePosition(router: String, factory: String) {
-  try {
-    // 拉取当前pull状态信息
-    const pullState = await History.findByHashOrBlockOrDataVague('UNIV3DEX', factory, '', 0, 200)
-    let nowPosition = 0
-
-    // 没有状态信息的情况
-    if (pullState.data.length == 0) {
-      const appendRst = await History.appendOrUpdate('UNIV3DEX', factory, {"now": nowPosition})
-      logger.debug(appendRst)
-    } else {
-      nowPosition = pullState.data[0].dataObj.now
-    }
-
-    return nowPosition
-  } catch (e) {
-    logger.error(`Pull state error, router: ${router}, factory: ${factory}`)
-    logger.error(e)
-  }
-
-  return 0
-}
-
-async function handleAFactory(router: String, factory: String, position: number, v3_tool: any, v3PositionManager: any) {
-  let errorCount = 0
-
-  while (true) {
-    try {
-      const positionInfo = await v3PositionManager.methods.positions(position).call()
-      const info = await v3_tool.methods.getMoreInfo(positionInfo.token0, positionInfo.token1, positionInfo.fee).call()
-
-      const symbol0 = info.symbol0.replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 10)
-      const symbol1 = info.symbol1.replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 10)
-      const name = `${router.slice(2, 4) + router.slice(-2)}_${symbol0}_${symbol1}`
-      const sum2 = replaceAll(BigNumber.from(positionInfo.token0).add(BigNumber.from(positionInfo.token1))._hex, '0x0', '0x')
-
-      const data = {
-        LP: info.lp,
-        decimals0: info.decimals0,
-        decimals1: info.decimals1,
-        factory: factory,
-        feei: positionInfo.fee,
-        id: position,
-        name: name,
-        r0: info.r0,
-        r1: info.r1,
-        router: router,
-        sum2: sum2,
-        symbol0: symbol0,
-        symbol1: symbol1,
-        token0: positionInfo.token0,
-        token1: positionInfo.token1
-      }
-
-      const insertRst = await History.appendOrUpdate('0', info.lp, data)
-      logger.debug(insertRst.msg, `, hash: ${info.lp}, ${position}`)
-
-      // 每十次更新一次position
-      if (position % 50 == 0) {
-        await History.appendOrUpdate('UNIV3DEX', factory, {"now": position})
-        logger.debug(`Position updated: ${position}`)
-      }
-
-      errorCount = 0
-    } catch (e) {
-      logger.error(JSON.stringify(e))
-      errorCount++
-    }
-
-    position++
-    if (errorCount >= 5) {
-      position = position - 5
-      break;
-    }
-  }
-}
-
-async function main() {
-  const fromZero: boolean = true
-
-  logger.debug('Pull v3 start.')
-
-  const v3_router_abi = require('../abi/UNIV3_ROUTER_ABI.json')
-  const positionManagerAbi = require('../abi/UNIV3_POSITION_MANAGER_ABI.json')
-  const v3_tool_abi = require('../artifacts/contracts/V3Tool.sol/V3Tool.json').abi
-
-  // 初始化 v3工具箱
-  const v3_tool = new web3.eth.Contract(v3_tool_abi, contracts.TOOLS_V3)
-  // 初始化PositionManager
-  const v3PositionManager = new web3.eth.Contract(positionManagerAbi, contracts.UNIV3_POSITION)
-
-  for (let router_index = 0; router_index < v3_routers.length; router_index++) {
-    const v3_router_address = v3_routers[router_index]
-
-    try {
-      logger.debug(`Router address: ${v3_router_address}`)
-
-      // 获取工厂地址
-      const v3_router = new web3.eth.Contract(v3_router_abi, v3_router_address)
-      const v3_factory_address = await v3_router.methods.factory().call()
-      // 获取当前pull状态
-      const position = fromZero ? 1 : await handlePosition(v3_router_address, v3_factory_address)
-
-      logger.debug(`factory: ${v3_factory_address}, ${position}.`)
-      await handleAFactory(v3_router_address, v3_factory_address, position, v3_tool, v3PositionManager)
-    } catch (e) {
-      logger.error(`New contract error, router: ${v3_router_address}`)
-      logger.error(e)
-    }
-  }
-}
-
-main().catch((error) => {
-  console.error(error);
-  process.exitCode = 1;
-})

+ 33 - 0
scripts/uniswap_swap_router_listener.ts

@@ -0,0 +1,33 @@
+import {web3} from "hardhat";
+
+function sleep(ms: number) {
+  return new Promise(resolve => setTimeout(resolve, ms));
+}
+
+let lastBlockNumber = 0
+
+async function onTick() {
+  const nowBlockNumber = await web3.eth.getBlockNumber()
+
+  if (nowBlockNumber === lastBlockNumber) return
+  lastBlockNumber = nowBlockNumber
+
+  console.log(lastBlockNumber)
+}
+
+async function main() {
+  while (true) {
+    try {
+      await onTick()
+    } catch (error) {
+      console.error(error)
+    }
+
+    await sleep(1000)
+  }
+}
+
+main().catch((error) => {
+  console.error(error);
+  process.exitCode = 1;
+})