level2Generate.ts 2.7 KB

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