level2Generate.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import logger from "../../utils/logger";
  2. import history from "../interface/history";
  3. import SwapPath from "../interface/swapPath";
  4. class Level2Generate {
  5. maxMemoryOfByte: number = 0
  6. maxMemoryChanged: boolean = true
  7. lpSumObj: any = {}
  8. buildLpObj (lpList: any) {
  9. for (const lp of lpList) {
  10. lp.dataObj = JSON.parse(lp.data)
  11. }
  12. }
  13. showMemory (mainInfo: string) {
  14. const memoryUsage = process.memoryUsage()
  15. if (this.maxMemoryOfByte < memoryUsage.rss) {
  16. this.maxMemoryOfByte = memoryUsage.rss
  17. this.maxMemoryChanged = true
  18. }
  19. if (this.maxMemoryChanged) {
  20. logger.debug(`${mainInfo} ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
  21. this.maxMemoryChanged = false
  22. }
  23. }
  24. format (bytes: any) {
  25. return (bytes / 1024 / 1024).toFixed(2) + ' MB';
  26. }
  27. buildSumObjGroupBySum2(allTypeLpList: any) {
  28. // 根据sum值进行池子分类
  29. logger.debug(`generate sum group by sum...${allTypeLpList.length}`)
  30. for (const lpDbObj of allTypeLpList) {
  31. const newLp: any = {}
  32. newLp.factory = lpDbObj.dataObj.factory
  33. newLp.LP = lpDbObj.dataObj.LP
  34. newLp.token0 = lpDbObj.dataObj.token0
  35. newLp.token1 = lpDbObj.dataObj.token1
  36. newLp.feei = lpDbObj.dataObj.feei
  37. if (this.lpSumObj[lpDbObj.dataObj.sum2]) {
  38. this.lpSumObj[lpDbObj.dataObj.sum2].push(newLp)
  39. } else {
  40. this.lpSumObj[lpDbObj.dataObj.sum2] = [newLp]
  41. }
  42. }
  43. }
  44. async generatePathAndUpdate() {
  45. // 组合所有路径并更新
  46. for (const sum2 of Object.keys(this.lpSumObj)) {
  47. const lpList = this.lpSumObj[sum2]
  48. // 组成路径
  49. const pathList: any = []
  50. for (let i = 0; i < lpList.length; i++) {
  51. const iLp = lpList[i]
  52. for (let j = i + 1; j < lpList.length; j++) {
  53. const jLp = lpList[j]
  54. const path = [iLp, jLp]
  55. pathList.push(path)
  56. }
  57. }
  58. if (pathList.length > 0) {
  59. await SwapPath.appendOrUpdate(sum2, '2', pathList)
  60. }
  61. }
  62. }
  63. async run() {
  64. logger.debug('Level2 maintenance start.')
  65. while (true) {
  66. this.showMemory('Level2 a loop...')
  67. try {
  68. this.lpSumObj = {}
  69. const topLpPullRst = await history.findByBlock('topLp')
  70. if (!topLpPullRst.state) continue
  71. const topLpList = topLpPullRst.data
  72. const normalLpPullRst = await history.findByBlock('normalLp')
  73. if (!normalLpPullRst.state) continue
  74. const normalLpList = normalLpPullRst.data
  75. const allTypeLpList: any = topLpList.concat(normalLpList)
  76. // 将原始data构造成dataObj
  77. this.buildLpObj(allTypeLpList)
  78. // 根据Sum构造Lp索引
  79. this.buildSumObjGroupBySum2(allTypeLpList)
  80. // 生成路径并更新到库
  81. await this.generatePathAndUpdate()
  82. } catch (e) {}
  83. }
  84. }
  85. }
  86. async function main() {
  87. await new Level2Generate().run()
  88. }
  89. main().catch((error) => {
  90. console.error(error);
  91. process.exitCode = 1;
  92. })