| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import logger from "../../utils/logger";
- import history from "../interface/history";
- import SwapPath from "../interface/swapPath";
- class Level2Generate {
- maxMemoryOfByte: number = 0
- maxMemoryChanged: boolean = true
- lpSumObj: any = {}
- prevTypeLpListLength: number = 0
- buildLpObj (lpList: any) {
- for (const lp of lpList) {
- lp.dataObj = JSON.parse(lp.data)
- }
- }
- showMemory (mainInfo: string) {
- const memoryUsage = process.memoryUsage()
- if (this.maxMemoryOfByte < memoryUsage.rss) {
- this.maxMemoryOfByte = memoryUsage.rss
- this.maxMemoryChanged = true
- }
- if (this.maxMemoryChanged) {
- logger.debug(`${mainInfo} ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
- this.maxMemoryChanged = false
- }
- }
- format (bytes: any) {
- return (bytes / 1024 / 1024).toFixed(2) + ' MB';
- }
- 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.showMemory('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 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;
- })
|