浏览代码

top token体系加入

龚成明 3 年之前
父节点
当前提交
1dce6aaf63
共有 2 个文件被更改,包括 69 次插入4 次删除
  1. 9 0
      scripts/interface/history.ts
  2. 60 4
      scripts/lp/lpMaintenance.ts

+ 9 - 0
scripts/interface/history.ts

@@ -23,6 +23,15 @@ export default class History {
     return rst.data
   }
 
+  static async findByHash(hash: string) {
+    let url = '/ethmev/findByHash'
+
+    const rst = await http.post(url, {
+      hash: hash
+    })
+    return rst.data
+  }
+
   static async appendOrUpdate(block: string, hashCode: string, data: any) {
     let url = '/ethmev/appendOrUpdate'
     const rst = await http.post(url, {

+ 60 - 4
scripts/lp/lpMaintenance.ts

@@ -17,7 +17,7 @@ const ethTokenAddressList = baseTokenAddressList.filter(baseTokenAddress => {
 })
 
 export class LpMaintenance {
-  tokenInstance: any = {}
+  tokenAssembly: any = {}
   maxMemoryOfByte: number = 0
   maxMemoryChanged: boolean = true
 
@@ -101,21 +101,34 @@ export class LpMaintenance {
     }
   }
 
+  async checkTokenByAddress(tokenAddress: any, newType: string) {
+    const tokenDbOBj = this.tokenAssembly[tokenAddress]
+
+    if (this.needUpdate(tokenDbOBj.type, tokenDbOBj.type)) {
+      tokenDbOBj.type = newType
+      await History.appendOrUpdate(tokenDbOBj.type, tokenDbOBj.hash, tokenDbOBj.data)
+      logger.debug(`token:${tokenAddress},${tokenDbOBj.type}->${tokenDbOBj.type}.`)
+    }
+  }
+
   async handleLp(lp: any, oldType: any) {
     // 过滤Lp
-    let lpType = await this.checkLpType(lp)
+    let lpType: any = await this.checkLpType(lp)
+    let tokenType = 'token'
+    if (lpType != 'lp') tokenType = replaceAll(lpType, 'Lp', '') + 'Token'
     // 一共12w个池子,非ethw的垃圾池子就不拉了
     if (lpType == 'lp' && !lp.isEthW) {
       lpType = 'ethLp'
     }
 
     if (lpType != oldType) {
-      // 保存变更之后的的Token
-      // await this.saveToken(lp, lpType)
       // 保存变更后的Lp
       await this.saveLp(lp, lpType)
       logger.debug(`lp:${lp.LP},${oldType}->${lpType}.`)
     }
+
+    await this.checkTokenByAddress(lp.token0, tokenType)
+    await this.checkTokenByAddress(lp.token1, tokenType)
   }
 
   showMemory (mainInfo: string) {
@@ -176,6 +189,47 @@ export class LpMaintenance {
     }
   }
 
+  needUpdate(oldType: string, newType: string) {
+    const tokenTypeMap: any = {
+      'token': 1,
+      'normalToken': 2,
+      'topToken': 3
+    }
+
+    return tokenTypeMap[newType] > tokenTypeMap[oldType]
+  }
+
+  async pullAllToken(lpList: any) {
+    for (const lpDbObj of lpList) {
+      const lp = lpDbObj.dataObj
+      const lpType = lpDbObj.block
+      let tokenType = 'token'
+
+      if (lpType != 'lp') tokenType = replaceAll(lpType, 'Lp', '') + 'Token'
+
+      if (!this.tokenAssembly[lp.token0]) {
+        const token0Rst = await History.findByHash(lp.token0)
+        const token0DbObj = token0Rst.data
+
+        this.tokenAssembly[lp.token0] = {
+          type: token0DbObj.block,
+          hash: token0DbObj.hash,
+          data: token0DbObj.dataObj
+        }
+      }
+      if (!this.tokenAssembly[lp.token1]) {
+        const token1Rst = await History.findByHash(lp.token1)
+        const token1DbObj = token1Rst.data
+
+        this.tokenAssembly[lp.token1] = {
+          type: token1DbObj.block,
+          hash: token1DbObj.hash,
+          data: token1DbObj.dataObj
+        }
+      }
+    }
+  }
+
   calcBaseTokenCovertEthValue() {
     for (const baseTokenAddress of baseTokenAddressList) {
       const isEthToken = ethTokenAddressList.indexOf(baseTokenAddress) != -1
@@ -230,6 +284,8 @@ export class LpMaintenance {
         const lpAddressList: any = this.generateAddressListByLpList(allTypeLpList)
         // lp按sum分类
         this.putAllLpGroupBySum(allTypeLpList)
+        // 获取所有lp的未拉取的token
+        await this.pullAllToken(allTypeLpList)
 
         // 集中拉取r0,r1并更新本地的
         const size = 2000