index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const ChainLib = require("../lib/chain-lib");
  2. const Web3Utils = require("../../utils/web3-utils");
  3. const logger = require("../../utils/logger");
  4. const Time = require("../../utils/time");
  5. const LpLib = require("../lib/lp-lib");
  6. class LpMaintenance {
  7. constructor(web3, chain) {
  8. this.web3 = web3
  9. this.chain = chain
  10. // 初始化依赖库
  11. this.lpLib = new LpLib(this.web3, this.chain)
  12. }
  13. async tick() {
  14. const v2LpList = await this.lpLib.getLpList(LpLib.VERSION.UNIV2)
  15. const v3LpList = await this.lpLib.getLpList(LpLib.VERSION.UNIV3)
  16. const lpList = v2LpList.concat(v3LpList)
  17. // 完善lp信息
  18. // 1. 更新所有lp的r0,r1
  19. await this.lpLib.putR0AndR1(lpList)
  20. // 2. 获取每种交易对的最佳兑换池子(最佳:池子价值最高)
  21. // 按相同交易对分组的对象,通过token0+token1的字符串串联方式索引
  22. const maxValueLpMap = this.lpLib.getMaxValueLpMap(lpList)
  23. // 3. 计算token0与token1的相对价格(需要decimals0,decimals1)
  24. this.lpLib.putSwapPrice(lpList)
  25. // 4. 获取基本币对baseToken的价值
  26. const baseTokenConvertEthValueMap = this.lpLib.getBaseTokenCovertEthValue(maxValueLpMap)
  27. // 5. 获取各池子价值
  28. this.lpLib.putLpValue(lpList)
  29. }
  30. async run() {
  31. logger.debug(`${this.chain.networkName} liquidity pool maintenance is start.`)
  32. logger.debug(`web3 is connected, block number: ${await this.web3.eth.getBlockNumber()}.`)
  33. // 3. 开始处理
  34. while (true) {
  35. try {
  36. await this.tick()
  37. } catch (e) {
  38. logger.error(e)
  39. }
  40. await Time.delay(1000)
  41. }
  42. }
  43. }
  44. async function main() {
  45. const chain = await ChainLib.getChainFromCommand()
  46. const web3 = Web3Utils.autoCreate(chain)
  47. const generator = new LpMaintenance(web3, chain)
  48. await generator.run()
  49. }
  50. main().catch((error) => {
  51. console.error(error);
  52. process.exitCode = 1;
  53. })