index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const Web3Utils = require('../../utils/web3-utils')
  2. const logger = require("../../utils/logger")
  3. const BigNumber = require('ethers').BigNumber
  4. const ierc20abi = require('../../abi/IERC20_ABI.json')
  5. const contracts = require('../../config/contracts')
  6. const Time = require("../../utils/time")
  7. const ChainLib = require("../lib/chain-lib")
  8. const FactoryLib = require("../lib/factory-lib")
  9. const BaseModel = require("../../model/base-model")
  10. class LpGenerate {
  11. tokenInstance = {}
  12. lpInstance = {}
  13. constructor(web3, chain) {
  14. this.web3 = web3
  15. this.chain = chain
  16. // 初始化V2工具箱
  17. this.v2ToolAbi = require('../../abi/UNIV2_TOOLS_ABI.json')
  18. this.v2Tool = new this.web3.eth.Contract(this.v2ToolAbi, this.chain.v2ToolAddress)
  19. this.v2FactoryAbi = require('../../abi/UNIV2_FACTORY_ABI.json')
  20. // 初始化V3工具箱
  21. this.positionManagerAbi = require('../../abi/UNIV3_POSITION_MANAGER_ABI.json')
  22. this.v3ToolAbi = require('../../abi/UNIV3_TOOLS_ABI.json')
  23. this.v3Tool = new this.web3.eth.Contract(this.v3ToolAbi, this.chain.v3ToolAddress)
  24. // 初始化factory接口
  25. this.factoryModel = new BaseModel(this.chain.id, BaseModel.MODULES.FACTORY)
  26. }
  27. async tick() {
  28. // 3.1. 拉取指定链所有factory
  29. const factoryList = await FactoryLib.getFactoryList(this.factoryModel)
  30. logger.info(factoryList)
  31. // 3.2. 拉取指定factory的拉取情况
  32. // 3.3. 从指定位置开始拉取lp
  33. // 3.3.1 更新或新增lp
  34. // 3.3.2 更新或新增token
  35. }
  36. async run() {
  37. logger.debug(`${this.chain.networkName} liquidity pool generate is start.`)
  38. logger.debug(`web3 is connected, block number: ${await this.web3.eth.getBlockNumber()}.`)
  39. // 3. 开始处理
  40. while (true) {
  41. try {
  42. await this.tick()
  43. } catch (e) {
  44. logger.error(e)
  45. }
  46. await Time.delay(12000)
  47. }
  48. // while (true) {
  49. // for (const routerObj of routerList) {
  50. // try {
  51. // let pairsLength = 0
  52. //
  53. // const factoryAddress = routerObj.factory
  54. // // 获取当前pull状态
  55. // let position = await this.handlePosition(routerObj)
  56. // let v2Factory = undefined
  57. // // v2,v3分开处理
  58. // if (routerObj.type === 'univ2') {
  59. // // 获取工厂实例
  60. // v2Factory = this.parseFactory(routerObj, v2FactoryAbi, factoryAddress)
  61. // // 获取当前pairsLength
  62. // pairsLength = await v2Factory.methods.allPairsLength().call()
  63. // }
  64. // // 如果有未获取的池子
  65. // const haveNewLp = position < pairsLength
  66. //
  67. // // 打印信息
  68. // if (haveNewLp) {
  69. // logger.debug(`Router address: ${routerObj.router}`)
  70. // if (routerObj.type === 'univ2') {
  71. // logger.debug(`factory: ${factoryAddress}, ${position} / ${pairsLength}.`)
  72. // } else {
  73. // logger.debug(`position: ${routerObj.position}, ${position} / ${pairsLength}.`)
  74. // }
  75. // }
  76. //
  77. // // 挨个处理lp信息
  78. // for (; position < pairsLength; position++) {
  79. // await this.handleLp(position, pairsLength, routerObj, v2ToolBy410)
  80. // }
  81. //
  82. // if (haveNewLp) {
  83. // if (routerObj.type === 'univ2') {
  84. // await History.appendOrUpdate('UNIV2DEX', routerObj.factory, {"next": position})
  85. // }
  86. //
  87. // logger.debug('')
  88. // logger.debug('')
  89. // logger.debug('')
  90. // }
  91. // } catch (e) {
  92. // logger.debug(e)
  93. // }
  94. // }
  95. //
  96. // await Time.delay(12000)
  97. // }
  98. }
  99. }
  100. async function main() {
  101. const chain = await ChainLib.getChainFromCommand()
  102. const web3 = Web3Utils.autoCreate(chain)
  103. const generator = new LpGenerate(web3, chain)
  104. await generator.run()
  105. }
  106. main().catch((error) => {
  107. console.error(error);
  108. process.exitCode = 1;
  109. })