Explorar o código

计算关键币与基本币的价格

skyfffire %!s(int64=2) %!d(string=hai) anos
pai
achega
ebc30eee97
Modificáronse 3 ficheiros con 72 adicións e 21 borrados
  1. 19 15
      config/base-token.json
  2. 47 2
      scripts/lib/lp-lib.js
  3. 6 4
      scripts/maintenance/index.js

+ 19 - 15
config/base-token.json

@@ -1,26 +1,30 @@
 {
-  "0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83": {
-    "name": "WFTM",
+  "0x7bf88d2c0e32de92cdaf2d43ccdc23e8edfd5990": {
+    "name": "WETHW",
     "decimals": 18
   },
-  "0x04068DA6C83AFCFA0e13ba15A6696662335D5B75": {
-    "name": "USDC",
-    "decimals": 6
+  "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2": {
+    "name": "WETH",
+    "decimals": 18
   },
-  "0x049d68029688eAbF473097a2fC38ef61633A3C7A": {
-    "name": "fUSDT",
+  "0x2ad7868ca212135c6119fd7ad1ce51cfc5702892": {
+    "name": "USDTW",
     "decimals": 6
   },
-  "0x8D11eC38a3EB5E956B052f67Da8Bdc9bef8Abf3E": {
-    "name": "DAI",
+  "0xc675fdbe260e1ee93106ee596b916952a9344f44": {
+    "name": "USDCW",
     "decimals": 18
   },
-  "0x658b0c7613e890EE50B8C4BC6A3f41ef411208aD": {
-    "name": "fETH",
-    "decimals": 18
+  "0x25de68ef588cb0c2c8f3537861e828ae699cd0db": {
+    "name": "USDCPOW",
+    "decimals": 6
   },
-  "0xe1146b9AC456fCbB60644c36Fd3F868A9072fc6E": {
-    "name": "fBTC",
-    "decimals": 18
+  "0x11bbb41b3e8baf7f75773db7428d5acee25fec75": {
+    "name": "USDCUNIW",
+    "decimals": 6
+  },
+  "0x8a496486f4c7cb840555bc2be327cba1447027c3": {
+    "name": "USDTUNIW",
+    "decimals": 6
   }
 }

+ 47 - 2
scripts/lib/lp-lib.js

@@ -6,6 +6,13 @@ const V3ToolAbi = require('../../abi/UNIV3_TOOLS_ABI.json')
 const FactoryLib = require("./factory-lib");
 const TokenLib = require("./token-lib");
 
+// basetoken
+const baseTokenMap = require('../../config/base-token.json')
+const baseTokenAddressList = Object.keys(baseTokenMap)
+const ethTokenAddressList = baseTokenAddressList.filter(baseTokenAddress => {
+  return baseTokenMap[baseTokenAddress].name.indexOf('ETH') !== -1
+})
+
 module.exports = class LpLib {
   static LEVEL = {
     NOT: 'not',
@@ -166,7 +173,7 @@ module.exports = class LpLib {
     }
   }
 
-  calcSwapPrice(lpList) {
+  putSwapPrice(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))
@@ -177,7 +184,45 @@ module.exports = class LpLib {
     }
   }
 
-  calcLpValue(lpList) {
+  getBaseTokenCovertEthValue(maxValueLpMap) {
+    const baseTokenConvertEthValueMap = {}
+
+    for (const baseTokenAddress of baseTokenAddressList) {
+      const isEthToken = ethTokenAddressList.indexOf(baseTokenAddress) !== -1
+      // eth兑换eth价格当然是1:1了
+      if (isEthToken) {
+        baseTokenConvertEthValueMap[baseTokenAddress] = 1
+      } else {
+        // 在ETH token中查找适合兑换的
+        for (const ethTokenAddress of ethTokenAddressList) {
+          const maxValueLp = maxValueLpMap[this.getKey(baseTokenAddress, ethTokenAddress)]
+          if (!maxValueLp) continue
+
+          const token0 = maxValueLp.token0
+          const token1 = maxValueLp.token1
+
+          if (baseTokenAddress === token0) {
+            baseTokenConvertEthValueMap[baseTokenAddress] = maxValueLp.in1Token0OutToken1
+          } else if (baseTokenAddress === token1) {
+            baseTokenConvertEthValueMap[baseTokenAddress] = maxValueLp.in1Token1OutToken0
+          }
+        }
+      }
+    }
+
+    return baseTokenConvertEthValueMap
+  }
+
+  getKey(hash0, hash1) {
+    if (hash0 < hash1) {
+      return hash0 + hash1
+    }
+    else {
+      return hash1 + hash0
+    }
+  }
+
+  putLpValue(lpList) {
 
   }
 

+ 6 - 4
scripts/maintenance/index.js

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