Quellcode durchsuchen

整体框架已经做完。

龚成明 vor 2 Jahren
Ursprung
Commit
44ba6a515f

+ 0 - 0
abi/410_V2_TOOLS.json → abi/UNIV2_TOOLS_ABI.json


+ 77 - 0
abi/UNIV3_TOOLS_ABI.json

@@ -0,0 +1,77 @@
+[
+  {
+    "inputs": [
+      {
+        "internalType": "address payable",
+        "name": "_positionManager",
+        "type": "address"
+      },
+      {
+        "internalType": "address payable",
+        "name": "_factory",
+        "type": "address"
+      }
+    ],
+    "stateMutability": "nonpayable",
+    "type": "constructor"
+  },
+  {
+    "inputs": [
+      {
+        "internalType": "address",
+        "name": "token0",
+        "type": "address"
+      },
+      {
+        "internalType": "address",
+        "name": "token1",
+        "type": "address"
+      },
+      {
+        "internalType": "uint24",
+        "name": "fee",
+        "type": "uint24"
+      }
+    ],
+    "name": "getMoreInfo",
+    "outputs": [
+      {
+        "internalType": "address",
+        "name": "lp",
+        "type": "address"
+      },
+      {
+        "internalType": "string",
+        "name": "symbol0",
+        "type": "string"
+      },
+      {
+        "internalType": "uint24",
+        "name": "decimals0",
+        "type": "uint24"
+      },
+      {
+        "internalType": "uint256",
+        "name": "r0",
+        "type": "uint256"
+      },
+      {
+        "internalType": "string",
+        "name": "symbol1",
+        "type": "string"
+      },
+      {
+        "internalType": "uint24",
+        "name": "decimals1",
+        "type": "uint24"
+      },
+      {
+        "internalType": "uint256",
+        "name": "r1",
+        "type": "uint256"
+      }
+    ],
+    "stateMutability": "view",
+    "type": "function"
+  }
+]

+ 1 - 2
config/contracts.json

@@ -1,4 +1,3 @@
 {
-  "ZERO": "0x0000000000000000000000000000000000000000",
-  "V2_TOOLS_BY_410": "0x22ACD9e1fE9A2507E99572d4C2e2dC64134A2B57"
+  "ZERO": "0x0000000000000000000000000000000000000000"
 }

+ 12 - 1
model/base-model.js

@@ -1,7 +1,9 @@
+const http = require("../utils/axios");
 module.exports = class BaseModel {
   static MODULES = {
     HISTORY: 'history',
-    PENDING: 'pending'
+    PENDING: 'pending',
+    FACTORY: 'factory'
   }
 
   constructor(chainId, module) {
@@ -10,4 +12,13 @@ module.exports = class BaseModel {
     this.chainId = chainId
     this.module = module
   }
+
+  async find() {
+    const url = `/${this.module}/findByChainId`
+    const rst = await http.post(url, {
+      chainId: this.chainId
+    })
+
+    return rst.data
+  }
 }

+ 3 - 0
package.json

@@ -1,5 +1,8 @@
 {
   "name": "hardhat-project",
+  "scripts": {
+    "lp-generate": "node /scripts/generate/index.js"
+  },
   "devDependencies": {
     "@nomicfoundation/hardhat-toolbox": "^2.0.0",
     "@nomiclabs/hardhat-web3": "^2.0.0",

+ 41 - 43
scripts/generate/index.js

@@ -1,64 +1,58 @@
-const web3 = require('hardhat').web3
+const Web3Utils = require('../../utils/web3-utils')
 const logger = require("../../utils/logger")
 const BigNumber = require('ethers').BigNumber
 const ierc20abi = require('../../abi/IERC20_ABI.json')
 const contracts = require('../../config/contracts')
 const Time = require("../../utils/time")
-const ChainLib = require("../lib/chain")
+const ChainLib = require("../lib/chain-lib")
+const FactoryLib = require("../lib/factory-lib")
+const BaseModel = require("../../model/base-model")
 
 class LpGenerate {
   tokenInstance = {}
   lpInstance = {}
 
-  async handlePosition(routerObj) {
-    const block = routerObj.type === 'univ2' ? 'UNIV2DEX' : 'UNIV3DEX'
-    const hash = routerObj.type === 'univ2' ? routerObj.factory : routerObj.router + '_router'
+  constructor(web3, chain) {
+    this.web3 = web3
+    this.chain = chain
 
-    try {
-      // 拉取当前pull状态信息
-      const pullState = await History.findByHashOrBlockOrDataVague(block, hash, '', 0, 200)
-      let nowPosition = 0
-
-      // 没有状态信息的情况
-      if (pullState.data.length === 0) {
-        await History.appendOrUpdate(block, hash, {"next": 0})
-      } else {
-        nowPosition = pullState.data[0].dataObj.next
-      }
-
-      return nowPosition
-    } catch (e) {
-      logger.error(`Pull state error, router: ${routerObj.router}, factory: ${routerObj.factory}`)
-      logger.error(e)
-    }
-
-    return 0
+    // 初始化V2工具箱
+    this.v2ToolAbi = require('../../abi/UNIV2_TOOLS_ABI.json')
+    this.v2Tool = new this.web3.eth.Contract(this.v2ToolAbi, this.chain.v2ToolAddress)
+    this.v2FactoryAbi = require('../../abi/UNIV2_FACTORY_ABI.json')
+    // 初始化V3工具箱
+    this.positionManagerAbi = require('../../abi/UNIV3_POSITION_MANAGER_ABI.json')
+    this.v3ToolAbi = require('../../abi/UNIV3_TOOLS_ABI.json')
+    this.v3Tool = new this.web3.eth.Contract(this.v3ToolAbi, this.chain.v3ToolAddress)
+    // 初始化factory接口
+    this.factoryModel = new BaseModel(this.chain.id, BaseModel.MODULES.FACTORY)
   }
 
-  parseFactory(router, factoryAbi, factoryAddress) {
-    if (router.factoryObj === null) {
-      router.factoryObj = new web3.eth.Contract(factoryAbi, factoryAddress)
-    }
-
-    return router.factoryObj
+  async tick() {
+    // 3.1. 拉取指定链所有factory
+    const factoryList = await FactoryLib.getFactoryList(this.factoryModel)
+    logger.info(factoryList)
+    // 3.2. 拉取指定factory的拉取情况
+    // 3.3. 从指定位置开始拉取lp
+    // 3.3.1 更新或新增lp
+    // 3.3.2 更新或新增token
   }
 
   async run() {
-    logger.debug('Pull lp start.')
+    logger.debug(`${this.chain.networkName} liquidity pool generate is start.`)
+    logger.debug(`web3 is connected, block number: ${await this.web3.eth.getBlockNumber()}.`)
 
-    // 1. 构造web3对象
-    // 2. abi加载
     // 3. 开始处理
-    // 3.1. 拉取指定链所有factory(记得新增链工具合约配置信息)
-    // 3.2. 拉取指定链所有factory的拉取情况
-    // 3.3. 从指定位置开始拉取lp
-    // 3.3.1 更新或新增lp
-    // 3.3.2 更新或新增token
+    while (true) {
+      try {
+        await this.tick()
+      } catch (e) {
+        logger.error(e)
+      }
+
+      await Time.delay(12000)
+    }
 
-    // 初始化410 v2工具箱
-    const v2ToolBy410Abi = require('../../abi/410_V2_TOOLS.json')
-    const v2ToolBy410 = new web3.eth.Contract(v2ToolBy410Abi, contracts.V2_TOOLS_BY_410)
-    const v2FactoryAbi = require('../../abi/UNIV2_FACTORY_ABI.json')
 
     // while (true) {
     //   for (const routerObj of routerList) {
@@ -114,7 +108,11 @@ class LpGenerate {
 }
 
 async function main() {
-  logger.info(await ChainLib.getChainFromCommand())
+  const chain = await ChainLib.getChainFromCommand()
+  const web3 = Web3Utils.autoCreate(chain)
+  const generator = new LpGenerate(web3, chain)
+
+  await generator.run()
 }
 
 main().catch((error) => {

+ 1 - 0
scripts/lib/chain.js → scripts/lib/chain-lib.js

@@ -1,4 +1,5 @@
 const Chain = require("../../model/chain");
+
 module.exports = class ChainLib {
   static getCommandChainId() {
     const input = process.argv[process.argv.length - 1]

+ 23 - 0
scripts/lib/factory-lib.js

@@ -0,0 +1,23 @@
+const logger = require("../../utils/logger")
+
+module.exports = class FactoryLib {
+  static async getFactoryList(baseModel) {
+    const requestRst = await baseModel.find()
+
+    if (requestRst.state) {
+      return requestRst.data
+    } else {
+      logger.error(requestRst.msg)
+
+      return []
+    }
+  }
+
+  static parseFactory(router, factoryAbi, factoryAddress) {
+    if (router.factoryObj === null) {
+      router.factoryObj = new web3.eth.Contract(factoryAbi, factoryAddress)
+    }
+
+    return router.factoryObj
+  }
+}

+ 0 - 0
scripts/lib/lp.js → scripts/lib/lp-lib.js


+ 28 - 0
scripts/lib/state-lib.js

@@ -0,0 +1,28 @@
+const logger = require("../../utils/logger");
+
+module.exports = class StateLib {
+  async handlePosition(routerObj) {
+    const block = routerObj.type === 'univ2' ? 'UNIV2DEX' : 'UNIV3DEX'
+    const hash = routerObj.type === 'univ2' ? routerObj.factory : routerObj.router + '_router'
+
+    try {
+      // 拉取当前pull状态信息
+      const pullState = await History.findByHashOrBlockOrDataVague(block, hash, '', 0, 200)
+      let nowPosition = 0
+
+      // 没有状态信息的情况
+      if (pullState.data.length === 0) {
+        await History.appendOrUpdate(block, hash, {"next": 0})
+      } else {
+        nowPosition = pullState.data[0].dataObj.next
+      }
+
+      return nowPosition
+    } catch (e) {
+      logger.error(`Pull state error, router: ${routerObj.router}, factory: ${routerObj.factory}`)
+      logger.error(e)
+    }
+
+    return 0
+  }
+}

+ 0 - 0
scripts/lib/state.js


+ 0 - 0
scripts/lib/token.js → scripts/lib/token-lib.js


+ 27 - 16
utils/web3-utils.js

@@ -1,27 +1,38 @@
-const Web3 = require('web3');
+const Web3 = require('web3')
+const debug = require('./debug')
+const logger = require("./logger")
 
 module.exports = class Web3Utils {
-    static buildByWs(ws) {
-        if (!Web3Utils.wsWeb3) {
-            Web3Utils.wsWeb3 = new Web3(ws)
-        }
+  static buildByWs(ws) {
+    if (!Web3Utils.wsWeb3) {
+      Web3Utils.wsWeb3 = new Web3(ws)
+    }
+
+    return Web3Utils.wsWeb3
+  }
 
-        return Web3Utils.wsWeb3
+  static buildByHttp(http) {
+    if (!Web3Utils.httpWeb3) {
+      Web3Utils.httpWeb3 = new Web3(http)
     }
 
-    static buildByHttp(http) {
-        if (!Web3Utils.httpWeb3) {
-            Web3Utils.httpWeb3 = new Web3(http)
-        }
+    return Web3Utils.httpWeb3
+  }
 
-        return Web3Utils.httpWeb3
+  static buildByIpc(ipc) {
+    if (!Web3Utils.ipcWeb3) {
+      const net = require('net')
+      Web3Utils.ipcWeb3 = new Web3(ipc, net)
     }
 
-    static buildByIpc(ipc) {
-        if (!Web3Utils.ipcWeb3) {
-            Web3Utils.ipcWeb3 = new Web3(ipc)
-        }
+    return Web3Utils.ipcWeb3
+  }
 
-        return Web3Utils.ipcWeb3
+  static autoCreate(chain) {
+    if (debug.isDev()) {
+      return Web3Utils.buildByHttp(chain.http)
+    } else {
+      return Web3Utils.buildByIpc(chain.ipc)
     }
+  }
 }