import logger from "../../utils/logger"; import history from "../interface/history"; import SwapPath from "../interface/swapPath"; import MemoryUtils from "../../utils/memory"; class Level2Generate { lpSumObj: any = {} memoryUtils: MemoryUtils = new MemoryUtils() prevTypeLpListLength: number = 0 buildLpObj (lpList: any) { for (const lp of lpList) { lp.dataObj = JSON.parse(lp.data) } } buildSumObjGroupBySum2(allTypeLpList: any) { // 根据sum值进行池子分类 if (this.prevTypeLpListLength != allTypeLpList.length){ logger.debug(`generate sum group by sum...${allTypeLpList.length}`) this.prevTypeLpListLength = allTypeLpList.length } for (const lpDbObj of allTypeLpList) { const newLp: any = {} newLp.factory = lpDbObj.dataObj.factory newLp.LP = lpDbObj.dataObj.LP newLp.token0 = lpDbObj.dataObj.token0 newLp.token1 = lpDbObj.dataObj.token1 newLp.feei = lpDbObj.dataObj.feei if (this.lpSumObj[lpDbObj.dataObj.sum2]) { this.lpSumObj[lpDbObj.dataObj.sum2].push(newLp) } else { this.lpSumObj[lpDbObj.dataObj.sum2] = [newLp] } } } async generatePathAndUpdate() { // 组合所有路径并更新 for (const sum2 of Object.keys(this.lpSumObj)) { const lpList = this.lpSumObj[sum2] // 组成路径 const pathList: any = [] for (let i = 0; i < lpList.length; i++) { const iLp = lpList[i] for (let j = i + 1; j < lpList.length; j++) { const jLp = lpList[j] const path = [iLp, jLp] pathList.push(path) } } if (pathList.length > 0) { await SwapPath.appendOrUpdate(sum2, '2', pathList) } } } async run() { logger.debug('Level2 maintenance start.') while (true) { this.memoryUtils.logWithMemoryOnMemoryChange('Level2 a loop...') try { this.lpSumObj = {} const topLpPullRst = await history.findByBlock('topLp') if (!topLpPullRst.state) continue const topLpList = topLpPullRst.data const normalLpPullRst = await history.findByBlock('normalLp') if (!normalLpPullRst.state) continue const normalLpList = normalLpPullRst.data // const normalLpList: any = [] const allTypeLpList: any = topLpList.concat(normalLpList) // 将原始data构造成dataObj this.buildLpObj(allTypeLpList) // 根据Sum构造Lp索引 this.buildSumObjGroupBySum2(allTypeLpList) // 生成路径并更新到库 await this.generatePathAndUpdate() } catch (e) {} } } } async function main() { await new Level2Generate().run() } main().catch((error) => { console.error(error); process.exitCode = 1; })