lp-lib.js 3.7 KB

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