calcLevel2Path.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. await swapPath.appendOrUpdate(v2Pool.sum2, '2', checkedPath[v2Pool.sum2])
  53. }
  54. }
  55. // 处理v3的
  56. for (let v3PoolIndex = 0; v3PoolIndex < v3PoolList.length; v3PoolIndex++) {
  57. const v3Pool = v3PoolList[v3PoolIndex]
  58. v3Pool.type = 'v3'
  59. // 已经查过的sum不再查询
  60. if (checkedPath[v3Pool.sum2] != null) continue
  61. checkedPath[v3Pool.sum2] = []
  62. // 开始排查
  63. const sameSumPoolList = [v3Pool]
  64. // 排查v3的,从v3PoolIndex开始
  65. for (let findSameSumV3Index = v3PoolIndex + 1; findSameSumV3Index < v3PoolList.length; findSameSumV3Index++) {
  66. const v3PoolUnChecked = v3PoolList[findSameSumV3Index]
  67. // 同sumValue
  68. if (v3PoolUnChecked.sum2 == v3Pool.sum2) {
  69. v3PoolUnChecked.type = 'v3'
  70. sameSumPoolList.push(v3PoolUnChecked)
  71. }
  72. }
  73. if (sameSumPoolList.length > 1) {
  74. handleSamePools(sameSumPoolList, checkedPath[v3Pool.sum2])
  75. await swapPath.appendOrUpdate(v3Pool.sum2, '2', checkedPath[v3Pool.sum2])
  76. }
  77. }
  78. logger.debug('query ok.')
  79. }
  80. main().catch((error) => {
  81. console.error(error);
  82. process.exitCode = 1;
  83. })