ソースを参照

计算r0,r1之间的相对价格

skyfffire 2 年 前
コミット
4356485076
2 ファイル変更32 行追加11 行削除
  1. 28 7
      scripts/lib/lp-lib.js
  2. 4 4
      scripts/maintenance/index.js

+ 28 - 7
scripts/lib/lp-lib.js

@@ -153,26 +153,47 @@ module.exports = class LpLib {
       const r1s = lpR0R1Info.amounts1
 
       // 更新到lp中
-      for (let index = from; index < from + SIZE; index++) {
-        lpList.r0Str = r0s[index % SIZE]
-        lpList.r0 = r0s[index % SIZE]
+      for (let index = from; index < from + SIZE && index < hashList.length; index++) {
+        const lp = lpList[index]
+        const relativeIndex = index % SIZE
 
-        lpList.r1Str = r1s[index % SIZE]
-        lpList.r1 = r1s[index % SIZE]
+        lp.r0Str = r0s[relativeIndex]
+        lp.r0 = parseInt(lp.r0Str)
+
+        lp.r1Str = r1s[relativeIndex]
+        lp.r1 = parseInt(lp.r1Str)
       }
     }
   }
 
   calcSwapPrice(lpList) {
+    for (const lp of lpList) {
+      const r0RealAmount = parseInt(lp.r0) / Math.pow(10, parseInt(lp.decimals0))
+      const r1RealAmount = parseInt(lp.r1) / Math.pow(10, parseInt(lp.decimals1))
 
+      // 计算0换1的价格和1换0的价格
+      lp.in1Token0OutToken1 = r1RealAmount / r0RealAmount // 一个token0 = in1Token0OutToken1个token1
+      lp.in1Token1OutToken0 = r0RealAmount / r1RealAmount // 一个token1 = in1Token1OutToken0个token0
+    }
   }
 
-  getMaxValueLpMap() {
+  calcLpValue(lpList) {
 
   }
 
-  calcLpValue(lpList) {
+  getMaxValueLpMap(lpList) {
+    const maxValueLpMap = {}
+
+    for (const lp of lpList) {
+      const key = lp.token0 + lp.token1
+      const isExists = !!maxValueLpMap[key]
+      const isMoreThanNowMax = isExists && maxValueLpMap[key].r0 < lp.r0
+
+      if (!isExists) maxValueLpMap[key] = lp
+      if (isMoreThanNowMax) maxValueLpMap[key] = lp
+    }
 
+    return maxValueLpMap
   }
 
   async getLpList(version) {

+ 4 - 4
scripts/maintenance/index.js

@@ -23,11 +23,11 @@ class LpMaintenance {
     await this.lpLib.putR0AndR1(lpList)
     // 2. 获取每种交易对的最佳兑换池子(最佳:池子价值最高)
     // 按相同交易对分组的对象,通过token0+token1的字符串串联方式索引
-    const maxValueLpMap = this.lpLib.getMaxValueLpMap()
-    // 3. 计算token0与token1的相对价格(需要decimals0,decimals1)
+    const maxValueLpMap = this.lpLib.getMaxValueLpMap(lpList)
+    // // 3. 计算token0与token1的相对价格(需要decimals0,decimals1)
     this.lpLib.calcSwapPrice(lpList)
-    // 4. 获取池子兑基本币价值
-    this.lpLib.calcLpValue(lpList)
+    // // 4. 获取池子兑基本币价值
+    // this.lpLib.calcLpValue(lpList)
   }
 
   async run() {