index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const Web3Utils = require('../../utils/web3-utils')
  2. const logger = require("../../utils/logger")
  3. const Time = require("../../utils/time")
  4. const ChainLib = require("../lib/chain-lib")
  5. const FactoryLib = require("../lib/factory-lib")
  6. const LpLib = require("../lib/lp-lib")
  7. const StateLib = require("../lib/state-lib")
  8. const TokenLib = require("../lib/token-lib")
  9. class LpGenerate {
  10. constructor(web3, chain) {
  11. this.web3 = web3
  12. this.chain = chain
  13. // 初始化依赖库
  14. this.factoryLib = new FactoryLib(this.web3, this.chain)
  15. this.tokenLib = new TokenLib(this.web3, this.chain)
  16. this.lpLib = new LpLib(this.web3, this.chain, this.factoryLib, this.tokenLib)
  17. }
  18. async tick() {
  19. // 3.1. 拉取指定链所有factory
  20. const factoryList = await this.factoryLib.getFactoryList()
  21. // 3.2 循环处理所有factory
  22. for (let factory of factoryList) {
  23. // 3.2.1 拉取指定factory的拉取情况
  24. let nextPosition = 0 // TODO 先都从0开始
  25. let pairsLength = await this.factoryLib.getPairsLength(factory)
  26. // 3.2.2 从指定位置开始拉取lp
  27. for (let position = nextPosition; position < pairsLength; position++) {
  28. // 3.2.2.1 拉取lp
  29. const lp = await this.lpLib.getLpByPosition(factory, position)
  30. if (lp) {
  31. // 3.2.2.2 构造token0, token1
  32. const token0 = await this.tokenLib.parseToken(lp, true)
  33. const token1 = await this.tokenLib.parseToken(lp, false)
  34. // 3.2.2.3 更新或新增lp
  35. logger.info(await this.lpLib.saveLp(factory, lp))
  36. // 3.3.2.4 更新或新增token
  37. logger.info(await this.tokenLib.saveToken(token0))
  38. logger.info(await this.tokenLib.saveToken(token1))
  39. logger.debug(`${position + 1} / ${pairsLength}, ${lp.name}-${lp.hash}-${this.chain.networkName}`)
  40. } else {
  41. logger.debug(`lp get filed. ${position + 1} / ${pairsLength}, ${factory.name}-${factory.router}-${this.chain.networkName}`)
  42. }
  43. }
  44. }
  45. }
  46. async run() {
  47. logger.debug(`${this.chain.networkName} liquidity pool generate is start.`)
  48. logger.debug(`web3 is connected, block number: ${await this.web3.eth.getBlockNumber()}.`)
  49. // 3. 开始处理
  50. while (true) {
  51. try {
  52. await this.tick()
  53. } catch (e) {
  54. logger.error(e)
  55. }
  56. await Time.delay(12000)
  57. }
  58. }
  59. }
  60. async function main() {
  61. const chain = await ChainLib.getChainFromCommand()
  62. const web3 = Web3Utils.autoCreate(chain)
  63. const generator = new LpGenerate(web3, chain)
  64. await generator.run()
  65. }
  66. main().catch((error) => {
  67. console.error(error);
  68. process.exitCode = 1;
  69. })