level2Generate.ts 3.1 KB

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