lp-lib.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if (factory.version === 'univ2') {
  27. return await this.v2LpModel.appendOrUpdate(lp)
  28. } else if (factory.version === 'univ3') {
  29. return await this.v3LpModel.appendOrUpdate(lp)
  30. } else {
  31. throw Error(`Unknown factory version: ${factory.version}, hash is: ${factory.hash}.`)
  32. }
  33. }
  34. getEffectiveLp(factory, position, lp) {
  35. // lp对象标准化
  36. lp.symbol0 = this.tokenLib.getEffectiveSymbol(lp.symbol0)
  37. lp.symbol1 = this.tokenLib.getEffectiveSymbol(lp.symbol1)
  38. lp.name = `${factory.name}_${lp.symbol0}_${lp.symbol1}`
  39. lp.router = factory.router
  40. lp.factory = factory.hash
  41. lp.fee = factory.fee
  42. lp.positionId = position
  43. lp.r0Str = String(lp.r0)
  44. lp.r1Str = String(lp.r1)
  45. lp.level = LpLib.LEVEL.INIT
  46. // lp各种地址最小化
  47. lp.hash = lp.hash.toLowerCase()
  48. lp.token0 = lp.token0.toLowerCase()
  49. lp.token1 = lp.token1.toLowerCase()
  50. // 字符化数字
  51. lp.decimals0 = parseInt(lp.decimals0)
  52. lp.decimals1 = parseInt(lp.decimals1)
  53. return lp
  54. }
  55. async getV2Pool(factory, position) {
  56. try {
  57. const info = await this.v2Tool.methods.getPairIdInfo(factory.hash, position).call()
  58. const lp = {
  59. hash: info['0'],
  60. decimals0: info['3'],
  61. decimals1: info['7'],
  62. r0: info['4'],
  63. r1: info['8'],
  64. symbol0: info['2'],
  65. symbol1: info['6'],
  66. token0: info['1'],
  67. token1: info['5']
  68. }
  69. return this.getEffectiveLp(factory, position, lp)
  70. } catch (e) {
  71. logger.info(e)
  72. }
  73. return undefined
  74. }
  75. async getV3Pool(factory, position) {
  76. try {
  77. const positionManager = this.factoryLib.getPositionManager(factory.positionManager)
  78. const positionInfo = await positionManager.methods.positions(position).call()
  79. const info = await this.v3Tool.methods.getMoreInfo(positionInfo.token0, positionInfo.token1, positionInfo.fee).call()
  80. const lp = {
  81. hash: info.lp,
  82. decimals0: info.decimals0,
  83. decimals1: info.decimals1,
  84. factory: factory.hash,
  85. feei: positionInfo.fee,
  86. id: position,
  87. r0: info.r0,
  88. r1: info.r1,
  89. router: factory.router,
  90. symbol0: symbol0,
  91. symbol1: symbol1,
  92. token0: positionInfo.token0,
  93. token1: positionInfo.token1
  94. }
  95. return this.getEffectiveLp(factory, position, lp)
  96. } catch (e) {}
  97. return undefined
  98. }
  99. async getLpByPosition(factory, position) {
  100. // 1. 获取lp
  101. let lp = undefined
  102. if (factory.version === 'univ2') {
  103. return await this.getV2Pool(factory, position)
  104. } else if (factory.version === 'univ3') {
  105. return await this.getV3Pool(factory, position)
  106. } else {
  107. throw Error(`Unknown factory version: ${factory.version}, hash is: ${factory.hash}.`)
  108. }
  109. }
  110. }