| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- const Web3Utils = require('../../utils/web3-utils')
- const logger = require("../../utils/logger")
- const Time = require("../../utils/time")
- const ChainLib = require("../lib/chain-lib")
- const FactoryLib = require("../lib/factory-lib")
- const LpLib = require("../lib/lp-lib")
- const StateLib = require("../lib/state-lib")
- const TokenLib = require("../lib/token-lib")
- class LpGenerate {
- constructor(web3, chain) {
- this.web3 = web3
- this.chain = chain
- // 初始化依赖库
- this.factoryLib = new FactoryLib(this.web3, this.chain)
- this.tokenLib = new TokenLib(this.web3, this.chain)
- this.lpLib = new LpLib(this.web3, this.chain, this.factoryLib, this.tokenLib)
- }
- async tick() {
- // 3.1. 拉取指定链所有factory
- const factoryList = await this.factoryLib.getFactoryList()
- // 3.2 循环处理所有factory
- for (let factory of factoryList) {
- // 3.2.1 拉取指定factory的拉取情况
- let nextPosition = 0 // TODO 先都从0开始
- let pairsLength = await this.factoryLib.getPairsLength(factory)
- // 3.2.2 从指定位置开始拉取lp
- for (let position = nextPosition; position < pairsLength; position++) {
- // 3.2.2.1 拉取lp
- const lp = await this.lpLib.getLpByPosition(factory, position)
- if (lp) {
- // 3.2.2.2 构造token0, token1
- const token0 = await this.tokenLib.parseToken(lp, true)
- const token1 = await this.tokenLib.parseToken(lp, false)
- // 3.2.2.3 更新或新增lp
- logger.info(await this.lpLib.saveLp(factory, lp))
- // 3.3.2.4 更新或新增token
- logger.info(await this.tokenLib.saveToken(token0))
- logger.info(await this.tokenLib.saveToken(token1))
- logger.debug(`${position + 1} / ${pairsLength}, ${lp.name}-${lp.hash}-${this.chain.networkName}`)
- } else {
- logger.debug(`lp get filed. ${position + 1} / ${pairsLength}, ${factory.name}-${factory.router}-${this.chain.networkName}`)
- }
- }
- }
- }
- async run() {
- logger.debug(`${this.chain.networkName} liquidity pool generate is start.`)
- logger.debug(`web3 is connected, block number: ${await this.web3.eth.getBlockNumber()}.`)
- // 3. 开始处理
- while (true) {
- try {
- await this.tick()
- } catch (e) {
- logger.error(e)
- }
- await Time.delay(12000)
- }
- }
- }
- async function main() {
- const chain = await ChainLib.getChainFromCommand()
- const web3 = Web3Utils.autoCreate(chain)
- const generator = new LpGenerate(web3, chain)
- await generator.run()
- }
- main().catch((error) => {
- console.error(error);
- process.exitCode = 1;
- })
|