index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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)
  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. try {
  29. // 3.2.2.1 拉取lp
  30. const lp = await this.lpLib.getLpByPosition(factory, position)
  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. await this.lpLib.saveLp(factory, lp)
  36. // 3.3.2.4 更新或新增token
  37. await this.tokenLib.saveToken(token0)
  38. await this.tokenLib.saveToken(token1)
  39. logger.debug(`${position + 1} / ${pairsLength}, ${lp.name}-${lp.hash}-${this.chain.networkName}`)
  40. } catch (e) {
  41. logger.info(e)
  42. logger.debug(`lp get filed. ${position + 1} / ${pairsLength}, ${factory.name}-${factory.router}-${this.chain.networkName}`)
  43. }
  44. }
  45. }
  46. }
  47. async run() {
  48. logger.debug(`${this.chain.networkName} liquidity pool generate is start.`)
  49. logger.debug(`web3 is connected, block number: ${await this.web3.eth.getBlockNumber()}.`)
  50. // 3. 开始处理
  51. while (true) {
  52. try {
  53. await this.tick()
  54. } catch (e) {
  55. logger.error(e)
  56. }
  57. await Time.delay(12000)
  58. }
  59. }
  60. }
  61. async function main() {
  62. const chain = await ChainLib.getChainFromCommand()
  63. const web3 = Web3Utils.autoCreate(chain)
  64. const generator = new LpGenerate(web3, chain)
  65. await generator.run()
  66. }
  67. main().catch((error) => {
  68. console.error(error);
  69. process.exitCode = 1;
  70. })