calcLevel2Path.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { web3 } from "hardhat";
  2. import swapPath from "./interface/swapPath";
  3. import logger from "../utils/logger";
  4. function filterPool(pool: any) {
  5. return true
  6. }
  7. function handleSamePools(poolList: any[], pathList: any[]) {
  8. for (let poolIndexI = 0; poolIndexI < poolList.length; poolIndexI++) {
  9. const x = poolList[poolIndexI]
  10. if (!filterPool(x)) continue
  11. for (let poolIndexJ = poolIndexI + 1; poolIndexJ < poolList.length; poolIndexJ++) {
  12. const y = poolList[poolIndexJ]
  13. if (!filterPool(y)) continue
  14. pathList.push([x, y])
  15. }
  16. }
  17. }
  18. async function main() {
  19. logger.debug('start loading...')
  20. const v2PoolList = require('../config/v2PoolList.json')
  21. const v3PoolList = require('../config/v3PoolList.json')
  22. logger.debug('start filter...')
  23. const checkedPath: any = {}
  24. for (let v2PoolIndex = 0; v2PoolIndex < v2PoolList.length; v2PoolIndex++) {
  25. const v2Pool = v2PoolList[v2PoolIndex]
  26. v2Pool.type = 'v2'
  27. // 已经查过的sum不再查询
  28. if (checkedPath[v2Pool.sum2] != null) continue
  29. checkedPath[v2Pool.sum2] = []
  30. // 开始排查
  31. const sameSumPoolList = [v2Pool]
  32. // 排查v2的,从当前位置开始,以免重复path
  33. for (let findSameSumV2Index = v2PoolIndex + 1; findSameSumV2Index < v2PoolList.length; findSameSumV2Index++) {
  34. const v2PoolUnChecked = v2PoolList[findSameSumV2Index]
  35. // 同sumValue
  36. if (v2PoolUnChecked.sum2 == v2Pool.sum2) {
  37. v2PoolUnChecked.type = 'v2'
  38. sameSumPoolList.push(v2PoolUnChecked)
  39. }
  40. }
  41. // 排查v3的,从0开始
  42. for (let findSameSumV3Index = 0; findSameSumV3Index < v3PoolList.length; findSameSumV3Index++) {
  43. const v3PoolUnChecked = v3PoolList[findSameSumV3Index]
  44. // 同sumValue
  45. if (v3PoolUnChecked.sum2 == v2Pool.sum2) {
  46. v3PoolUnChecked.type = 'v3'
  47. sameSumPoolList.push(v3PoolUnChecked)
  48. }
  49. }
  50. if (sameSumPoolList.length > 1) {
  51. handleSamePools(sameSumPoolList, checkedPath[v2Pool.sum2])
  52. const rst = await swapPath.appendOrUpdate(v2Pool.sum2, '2', checkedPath[v2Pool.sum2])
  53. logger.debug(`${rst.msg}, v2, ${v2PoolIndex}/${v2PoolList.length}`)
  54. }
  55. }
  56. // 处理v3的
  57. for (let v3PoolIndex = 0; v3PoolIndex < v3PoolList.length; v3PoolIndex++) {
  58. const v3Pool = v3PoolList[v3PoolIndex]
  59. v3Pool.type = 'v3'
  60. // 已经查过的sum不再查询
  61. if (checkedPath[v3Pool.sum2] != null) continue
  62. checkedPath[v3Pool.sum2] = []
  63. // 开始排查
  64. const sameSumPoolList = [v3Pool]
  65. // 排查v3的,从v3PoolIndex开始
  66. for (let findSameSumV3Index = v3PoolIndex + 1; findSameSumV3Index < v3PoolList.length; findSameSumV3Index++) {
  67. const v3PoolUnChecked = v3PoolList[findSameSumV3Index]
  68. // 同sumValue
  69. if (v3PoolUnChecked.sum2 == v3Pool.sum2) {
  70. v3PoolUnChecked.type = 'v3'
  71. sameSumPoolList.push(v3PoolUnChecked)
  72. }
  73. }
  74. if (sameSumPoolList.length > 1) {
  75. handleSamePools(sameSumPoolList, checkedPath[v3Pool.sum2])
  76. const rst = await swapPath.appendOrUpdate(v3Pool.sum2, '2', checkedPath[v3Pool.sum2])
  77. logger.debug(`${rst.msg}, v3, ${v3PoolIndex}/${v3PoolList.length}`)
  78. }
  79. }
  80. logger.debug('query ok.')
  81. }
  82. main().catch((error) => {
  83. console.error(error);
  84. process.exitCode = 1;
  85. })