lp-lib.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const logger = require("../../utils/logger");
  2. const BaseModel = require("../../model/base-model");
  3. const V2ToolAbi = require('../../abi/UNIV2_TOOLS_ABI.json')
  4. const V3ToolAbi = require('../../abi/UNIV3_TOOLS_ABI.json')
  5. module.exports = class LpLib {
  6. static LEVEL = {
  7. NOT: 'not',
  8. DISCARD: 'discard',
  9. INIT: 'init',
  10. NORMAL: 'normal',
  11. TOP: 'top'
  12. }
  13. constructor(web3, chain, factoryLib, tokenLib) {
  14. this.web3 = web3
  15. this.chain = chain
  16. this.factoryLib = factoryLib
  17. this.tokenLib = tokenLib
  18. this.v2LpModel = new BaseModel(this.chain.id, BaseModel.MODULES.V2_LP)
  19. this.v3LpModel = new BaseModel(this.chain.id, BaseModel.MODULES.V3_LP)
  20. // 初始化V2工具箱
  21. this.v2Tool = new this.web3.eth.Contract(V2ToolAbi, this.chain.v2ToolAddress)
  22. // 初始化V3工具箱
  23. this.v3Tool = new this.web3.eth.Contract(V3ToolAbi, this.chain.v3ToolAddress)
  24. }
  25. async saveLp(factory, lp) {
  26. let saveRst = undefined
  27. if (factory.version === 'univ2') {
  28. saveRst = await this.v2LpModel.appendOrUpdate(lp)
  29. } else if (factory.version === 'univ3') {
  30. saveRst = await this.v3LpModel.appendOrUpdate(lp)
  31. } else {
  32. throw Error(`Unknown factory version: ${factory.version}, hash is: ${factory.hash}.`)
  33. }
  34. if (!saveRst.state) throw Error(saveRst.msg)
  35. }
  36. getEffectiveLp(factory, position, lp) {
  37. // lp对象标准化
  38. lp.symbol0 = this.tokenLib.getEffectiveSymbol(lp.symbol0)
  39. lp.symbol1 = this.tokenLib.getEffectiveSymbol(lp.symbol1)
  40. lp.name = `${factory.name}_${lp.symbol0}_${lp.symbol1}`
  41. lp.router = factory.router
  42. lp.factory = factory.hash
  43. lp.fee = factory.fee
  44. lp.positionId = position
  45. lp.r0Str = String(lp.r0)
  46. lp.r1Str = String(lp.r1)
  47. lp.level = LpLib.LEVEL.INIT
  48. // lp各种地址最小化
  49. lp.hash = lp.hash.toLowerCase()
  50. lp.token0 = lp.token0.toLowerCase()
  51. lp.token1 = lp.token1.toLowerCase()
  52. // 字符化数字
  53. lp.decimals0 = parseInt(lp.decimals0)
  54. lp.decimals1 = parseInt(lp.decimals1)
  55. // 链信息
  56. lp.chainId = this.chain.id
  57. return lp
  58. }
  59. async getV2Pool(factory, position) {
  60. try {
  61. const info = await this.v2Tool.methods.getPairIdInfo(factory.hash, position).call()
  62. const lp = {
  63. hash: info['0'],
  64. decimals0: info['3'],
  65. decimals1: info['7'],
  66. r0: info['4'],
  67. r1: info['8'],
  68. symbol0: info['2'],
  69. symbol1: info['6'],
  70. token0: info['1'],
  71. token1: info['5']
  72. }
  73. return this.getEffectiveLp(factory, position, lp)
  74. } catch (e) {
  75. logger.info(e)
  76. }
  77. return undefined
  78. }
  79. async getV3Pool(factory, position) {
  80. try {
  81. const positionManager = this.factoryLib.getPositionManager(factory.positionManager)
  82. const positionInfo = await positionManager.methods.positions(position).call()
  83. const info = await this.v3Tool.methods.getMoreInfo(positionInfo.token0, positionInfo.token1, positionInfo.fee).call()
  84. const lp = {
  85. hash: info.lp,
  86. decimals0: info.decimals0,
  87. decimals1: info.decimals1,
  88. factory: factory.hash,
  89. feei: positionInfo.fee,
  90. id: position,
  91. r0: info.r0,
  92. r1: info.r1,
  93. router: factory.router,
  94. symbol0: symbol0,
  95. symbol1: symbol1,
  96. token0: positionInfo.token0,
  97. token1: positionInfo.token1
  98. }
  99. return this.getEffectiveLp(factory, position, lp)
  100. } catch (e) {}
  101. return undefined
  102. }
  103. async getLpByPosition(factory, position) {
  104. // 1. 获取lp
  105. let lp = undefined
  106. if (factory.version === 'univ2') {
  107. return await this.getV2Pool(factory, position)
  108. } else if (factory.version === 'univ3') {
  109. return await this.getV3Pool(factory, position)
  110. } else {
  111. throw Error(`Unknown factory version: ${factory.version}, hash is: ${factory.hash}.`)
  112. }
  113. }
  114. }