| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- const ChainLib = require("../lib/chain-lib");
- const Web3Utils = require("../../utils/web3-utils");
- const logger = require("../../utils/logger");
- const Time = require("../../utils/time");
- const LpLib = require("../lib/lp-lib");
- class LpMaintenance {
- constructor(web3, chain) {
- this.web3 = web3
- this.chain = chain
- // 初始化依赖库
- this.lpLib = new LpLib(this.web3, this.chain)
- }
- async tick() {
- const v2LpList = await this.lpLib.getLpList(LpLib.VERSION.UNIV2)
- const v3LpList = await this.lpLib.getLpList(LpLib.VERSION.UNIV3)
- const lpList = v2LpList.concat(v3LpList)
- // 完善lp信息
- // 1. 更新所有lp的r0,r1
- await this.lpLib.putR0AndR1(lpList)
- // 2. 获取每种交易对的最佳兑换池子(最佳:池子价值最高)
- // 按相同交易对分组的对象,通过token0+token1的字符串串联方式索引
- const maxValueLpMap = this.lpLib.getMaxValueLpMap(lpList)
- // 3. 计算token0与token1的相对价格(需要decimals0,decimals1)
- this.lpLib.putSwapPrice(lpList)
- // 4. 获取基本币对baseToken的价值
- const baseTokenConvertEthValueMap = this.lpLib.getBaseTokenCovertEthValue(maxValueLpMap)
- // 5. 获取各池子价值
- for (const lp of lpList) {
- lp.valueStr = this.lpLib.getLpEthValue(lp, baseTokenConvertEthValueMap, maxValueLpMap)
- if (lp.valueStr > 0) logger.info(`lp: ${lp.hash}, value: ${lp.valueStr}.`)
- }
- // 6. 更新池子到数据库
- for (const v2Lp of v2LpList) {
- const rst = await this.lpLib.v2LpModel.appendOrUpdate(v2Lp)
- if (!rst.state) logger.info(rst)
- }
- for (const v3Lp of v3LpList) {
- const rst = await this.lpLib.v3LpModel.appendOrUpdate(v3Lp)
- if (!rst.state) logger.info(rst)
- }
- }
- async run() {
- logger.debug(`${this.chain.networkName} liquidity pool maintenance 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(1000)
- }
- }
- }
- async function main() {
- const chain = await ChainLib.getChainFromCommand()
- const web3 = Web3Utils.autoCreate(chain)
- const generator = new LpMaintenance(web3, chain)
- await generator.run()
- }
- main().catch((error) => {
- console.error(error);
- process.exitCode = 1;
- })
|