|
@@ -1,4 +1,5 @@
|
|
|
const logger = require("../../utils/logger");
|
|
const logger = require("../../utils/logger");
|
|
|
|
|
+const debug = require('./../../utils/debug')
|
|
|
const BaseModel = require("../../model/base-model");
|
|
const BaseModel = require("../../model/base-model");
|
|
|
const V2ToolAbi = require('../../abi/UNIV2_TOOLS_ABI.json')
|
|
const V2ToolAbi = require('../../abi/UNIV2_TOOLS_ABI.json')
|
|
|
const V3ToolAbi = require('../../abi/UNIV3_TOOLS_ABI.json')
|
|
const V3ToolAbi = require('../../abi/UNIV3_TOOLS_ABI.json')
|
|
@@ -14,6 +15,11 @@ module.exports = class LpLib {
|
|
|
TOP: 'top'
|
|
TOP: 'top'
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ static VERSION = {
|
|
|
|
|
+ UNIV2: 'univ2',
|
|
|
|
|
+ UNIV3: 'univ3'
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
constructor(web3, chain) {
|
|
constructor(web3, chain) {
|
|
|
this.web3 = web3
|
|
this.web3 = web3
|
|
|
this.chain = chain
|
|
this.chain = chain
|
|
@@ -114,16 +120,85 @@ module.exports = class LpLib {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async getLpByPosition(factory, position) {
|
|
async getLpByPosition(factory, position) {
|
|
|
- if (factory.version === 'univ2') {
|
|
|
|
|
|
|
+ if (factory.version === LpLib.VERSION.UNIV2) {
|
|
|
return await this.getV2Pool(factory, position)
|
|
return await this.getV2Pool(factory, position)
|
|
|
- } else if (factory.version === 'univ3') {
|
|
|
|
|
|
|
+ } else if (factory.version === LpLib.VERSION.UNIV3) {
|
|
|
return await this.getV3Pool(factory, position)
|
|
return await this.getV3Pool(factory, position)
|
|
|
} else {
|
|
} else {
|
|
|
throw Error(`Unknown factory version: ${factory.version}, hash is: ${factory.hash}.`)
|
|
throw Error(`Unknown factory version: ${factory.version}, hash is: ${factory.hash}.`)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async getV2LpList() {
|
|
|
|
|
|
|
+ getHashListByLpList(lpList) {
|
|
|
|
|
+ const hashList = []
|
|
|
|
|
+
|
|
|
|
|
+ for (const lp of lpList) {
|
|
|
|
|
+ hashList.push(lp.hash)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return hashList
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async putR0AndR1(lpList) {
|
|
|
|
|
+ // 生成所有lp的hash
|
|
|
|
|
+ const hashList = this.getHashListByLpList(lpList)
|
|
|
|
|
+ // 以段为单位,更新r0、r1。每一段有SIZE个元素
|
|
|
|
|
+ const SIZE = 2000
|
|
|
|
|
+ for (let from = 0; from < hashList.length; from += SIZE) {
|
|
|
|
|
+ logger.debug(`${from}, ${hashList.length}`)
|
|
|
|
|
+
|
|
|
|
|
+ // 拉取该段所有r0,r1
|
|
|
|
|
+ const lpR0R1Info = await this.v2Tool.methods.getPairSBalance(hashList.slice(from, from + SIZE)).call()
|
|
|
|
|
+ const r0s = lpR0R1Info.amounts0
|
|
|
|
|
+ const r1s = lpR0R1Info.amounts1
|
|
|
|
|
+
|
|
|
|
|
+ // 更新到lp中
|
|
|
|
|
+ for (let index = from; index < from + SIZE; index++) {
|
|
|
|
|
+ lpList.r0Str = r0s[index % SIZE]
|
|
|
|
|
+ lpList.r0 = r0s[index % SIZE]
|
|
|
|
|
+
|
|
|
|
|
+ lpList.r1Str = r1s[index % SIZE]
|
|
|
|
|
+ lpList.r1 = r1s[index % SIZE]
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ calcSwapPrice() {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ getMaxValueLpMap() {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async perfectLpInfo(lpList) {
|
|
|
|
|
+ // 1. 更新所有lp的r0,r1
|
|
|
|
|
+ await this.putR0AndR1(lpList)
|
|
|
|
|
+ // 2. 获取每种交易对的最佳兑换池子(最佳:池子价值最高)
|
|
|
|
|
+ // 按相同交易对分组的对象,通过token0+token1的字符串串联方式索引
|
|
|
|
|
+ this.maxValueLpMap = this.getMaxValueLpMap()
|
|
|
|
|
+ // 3. 计算token0与token1的相对价格(需要decimals0,decimals1)
|
|
|
|
|
+ this.calcSwapPrice(lpList)
|
|
|
|
|
+ // 4. 获取池子兑基本币价值
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async getLpList(version) {
|
|
|
|
|
+ let requestRst = undefined
|
|
|
|
|
|
|
|
|
|
+ if (version === LpLib.VERSION.UNIV2) {
|
|
|
|
|
+ requestRst = await this.v2LpModel.findByPaginate(1, debug.isDev() ? 200 : 10000000)
|
|
|
|
|
+ } else if (version === LpLib.VERSION.UNIV3) {
|
|
|
|
|
+ requestRst = await this.v3LpModel.findByPaginate(1, debug.isDev() ? 200 : 10000000)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw Error(`Unknown lp version: ${version}.`)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (requestRst.state) {
|
|
|
|
|
+ return requestRst.data
|
|
|
|
|
+ } else {
|
|
|
|
|
+ logger.error(requestRst.msg)
|
|
|
|
|
+
|
|
|
|
|
+ return []
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|