Explorar el Código

基础逻辑写完

skyfffire hace 2 años
padre
commit
1e621c5ad6
Se han modificado 4 ficheros con 96 adiciones y 21 borrados
  1. 2 18
      hardhat.config.js
  2. 11 0
      model/base-model.js
  3. 78 3
      scripts/lib/lp-lib.js
  4. 5 0
      scripts/maintenance/index.js

+ 2 - 18
hardhat.config.js

@@ -4,10 +4,9 @@ require('@nomicfoundation/hardhat-toolbox')
 const deployer = require('./.secret-config')
 const logger = require('./utils/logger')
 const debug = require('./utils/debug')
+const web3Utils = require('./utils/web3-utils')
 
 const FTM_RPC = 'http://3.227.34.41:8545'
-const FTM_WS = 'ws://3.227.34.41:8546'
-const FTM_IPC = '/ethereum/data/opera.ipc'
 
 const config = {
   defaultNetwork: 'FTM',
@@ -30,21 +29,6 @@ const config = {
   }
 }
 
-extendEnvironment(async (hre) => {
-  const Web3 = require('web3')
-  hre.Web3 = Web3
-
-  logger.debug(`is ${debug.isDev() ? 'dev' : 'pro'} model.`)
-  if (debug.isDev()) {
-    logger.debug(`use ${FTM_WS}`)
-
-    hre.web3 = new Web3(FTM_WS)
-    logger.debug(await hre.web3.eth.getBlockNumber())
-  } else {
-    const net = require('net')
-    logger.debug(`use ${FTM_IPC}`)
-    hre.web3 = new Web3(FTM_IPC, net)
-  }
-})
+extendEnvironment(async (hre) => {})
 
 module.exports=config

+ 11 - 0
model/base-model.js

@@ -25,6 +25,17 @@ module.exports = class BaseModel {
     return rst.data
   }
 
+  async findByPaginate(pageNumber=1, pageSize=200) {
+    const url = `/${this.module}/findByChainIdAndPaginate`
+    const rst = await http.post(url, {
+      chainId: this.chainId,
+      pageNumber: pageNumber,
+      pageSize: pageSize
+    })
+
+    return rst.data
+  }
+
   async appendOrUpdate(data) {
     const url = `/${this.module}/appendOrUpdate`
     const rst = await http.post(url, data)

+ 78 - 3
scripts/lib/lp-lib.js

@@ -1,4 +1,5 @@
 const logger = require("../../utils/logger");
+const debug = require('./../../utils/debug')
 const BaseModel = require("../../model/base-model");
 const V2ToolAbi = require('../../abi/UNIV2_TOOLS_ABI.json')
 const V3ToolAbi = require('../../abi/UNIV3_TOOLS_ABI.json')
@@ -14,6 +15,11 @@ module.exports = class LpLib {
     TOP: 'top'
   }
 
+  static VERSION = {
+    UNIV2: 'univ2',
+    UNIV3: 'univ3'
+  }
+
   constructor(web3, chain) {
     this.web3 = web3
     this.chain = chain
@@ -114,16 +120,85 @@ module.exports = class LpLib {
   }
 
   async getLpByPosition(factory, position) {
-    if (factory.version === 'univ2') {
+    if (factory.version === LpLib.VERSION.UNIV2) {
       return await this.getV2Pool(factory, position)
-    } else if (factory.version === 'univ3') {
+    } else if (factory.version === LpLib.VERSION.UNIV3) {
       return await this.getV3Pool(factory, position)
     } else {
       throw Error(`Unknown factory version: ${factory.version}, hash is: ${factory.hash}.`)
     }
   }
 
-  async getV2LpList() {
+  getHashListByLpList(lpList) {
+    const hashList = []
+
+    for (const lp of lpList) {
+      hashList.push(lp.hash)
+    }
+
+    return hashList
+  }
+
+  async putR0AndR1(lpList) {
+    // 生成所有lp的hash
+    const hashList = this.getHashListByLpList(lpList)
+    // 以段为单位,更新r0、r1。每一段有SIZE个元素
+    const SIZE = 2000
+    for (let from = 0; from < hashList.length; from += SIZE) {
+      logger.debug(`${from}, ${hashList.length}`)
+
+      // 拉取该段所有r0,r1
+      const lpR0R1Info = await this.v2Tool.methods.getPairSBalance(hashList.slice(from, from + SIZE)).call()
+      const r0s = lpR0R1Info.amounts0
+      const r1s = lpR0R1Info.amounts1
+
+      // 更新到lp中
+      for (let index = from; index < from + SIZE; index++) {
+        lpList.r0Str = r0s[index % SIZE]
+        lpList.r0 = r0s[index % SIZE]
+
+        lpList.r1Str = r1s[index % SIZE]
+        lpList.r1 = r1s[index % SIZE]
+      }
+    }
+  }
+
+  calcSwapPrice() {
+
+  }
+
+  getMaxValueLpMap() {
+
+  }
+
+  async perfectLpInfo(lpList) {
+    // 1. 更新所有lp的r0,r1
+    await this.putR0AndR1(lpList)
+    // 2. 获取每种交易对的最佳兑换池子(最佳:池子价值最高)
+    // 按相同交易对分组的对象,通过token0+token1的字符串串联方式索引
+    this.maxValueLpMap = this.getMaxValueLpMap()
+    // 3. 计算token0与token1的相对价格(需要decimals0,decimals1)
+    this.calcSwapPrice(lpList)
+    // 4. 获取池子兑基本币价值
+  }
+
+  async getLpList(version) {
+    let requestRst = undefined
 
+    if (version === LpLib.VERSION.UNIV2) {
+      requestRst = await this.v2LpModel.findByPaginate(1, debug.isDev() ? 200 : 10000000)
+    } else if (version === LpLib.VERSION.UNIV3) {
+      requestRst = await this.v3LpModel.findByPaginate(1, debug.isDev() ? 200 : 10000000)
+    } else {
+      throw Error(`Unknown lp version: ${version}.`)
+    }
+
+    if (requestRst.state) {
+      return requestRst.data
+    } else {
+      logger.error(requestRst.msg)
+
+      return []
+    }
   }
 }

+ 5 - 0
scripts/maintenance/index.js

@@ -14,7 +14,12 @@ class LpMaintenance {
   }
 
   async tick() {
+    const v2LpList = await this.lpLib.getLpList(LpLib.VERSION.UNIV2)
+    const v3LpList = await this.lpLib.getLpList(LpLib.VERSION.UNIV3)
 
+    const lpList = v2LpList.concat(v3LpList)
+    // 完善lp信息
+    await this.lpLib.perfectLpInfo(lpList)
   }
 
   async run() {