lpMaintenance.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { web3 } from "hardhat";
  2. import History from "../interface/history";
  3. import contracts from "../../config/contracts";
  4. import logger from "../../utils/logger";
  5. import {BigNumber} from "ethers";
  6. // 初始化410 v2工具箱
  7. const v2ToolBy410Abi = require('../../abi/410_V2_TOOLS.json')
  8. const v2ToolBy410 = new web3.eth.Contract(v2ToolBy410Abi, contracts.V2_TOOLS_BY_410)
  9. // basetoken
  10. const baseTokenMap = require('../../config/base-token.json')
  11. const baseTokenAddressList = Object.keys(baseTokenMap)
  12. const ethTokenAddressList = baseTokenAddressList.filter(baseTokenAddress => {
  13. return baseTokenMap[baseTokenAddress].name.indexOf('ETH') != -1
  14. })
  15. export class LpMaintenance {
  16. tokenInstance: any = {}
  17. maxMemoryOfByte: number = 0
  18. maxMemoryChanged: boolean = true
  19. // 计算价格要用
  20. allMaxValueLpGroupBySum: any = {}
  21. baseTokenConvertEthValueMap: any = {}
  22. async saveLp(lp: any, type='0') {
  23. await History.appendOrUpdate(type, lp.LP, lp)
  24. }
  25. getLpEthValue(lp: any) {
  26. const token0 = lp.token0.toLowerCase()
  27. const token1 = lp.token1.toLowerCase()
  28. // 1.两token之一能直接与ethw或wethw做直接池子的
  29. // 2.两token之一能直接与baseToken做池子的
  30. // 这两种情况直接带入baseTokenConvertEthValueMap计算
  31. for (const baseTokenAddress of baseTokenAddressList) {
  32. const token0AndBaseTokenLp = this.allMaxValueLpGroupBySum[this.getHexSum(token0, baseTokenAddress.toLowerCase())]
  33. if (token0AndBaseTokenLp) {
  34. if (ethTokenAddressList.indexOf(baseTokenAddress.toLowerCase()) == -1) {
  35. logger.debug(`${JSON.stringify(lp)}`)
  36. logger.debug(`0, ${token0AndBaseTokenLp.zeroToOnePrice}, ${this.baseTokenConvertEthValueMap[baseTokenAddress]}`)
  37. }
  38. return {
  39. '0': token0AndBaseTokenLp.zeroToOnePrice * this.baseTokenConvertEthValueMap[baseTokenAddress],
  40. '1': 1 / token0AndBaseTokenLp.zeroToOnePrice * this.baseTokenConvertEthValueMap[baseTokenAddress]
  41. }
  42. }
  43. const token1AndBaseTokenLp = this.allMaxValueLpGroupBySum[this.getHexSum(token1, baseTokenAddress.toLowerCase())]
  44. if (token1AndBaseTokenLp) {
  45. if (ethTokenAddressList.indexOf(baseTokenAddress.toLowerCase()) == -1) {
  46. logger.debug(`${JSON.stringify(lp)}`)
  47. logger.debug(`1, ${token0AndBaseTokenLp.zeroToOnePrice}, ${this.baseTokenConvertEthValueMap[baseTokenAddress]}`)
  48. }
  49. return {
  50. '0': 1 / token1AndBaseTokenLp.oneToZeroPrice * this.baseTokenConvertEthValueMap[baseTokenAddress],
  51. '1': token1AndBaseTokenLp.oneToZeroPrice * this.baseTokenConvertEthValueMap[baseTokenAddress]
  52. }
  53. }
  54. }
  55. // 3.两token都不能与baseToken做池子的
  56. // 这种情况则视为lp类型池子
  57. return {'0': 0, '1': 0}
  58. }
  59. async checkLpType(lp: any) {
  60. // lp过滤后类型表
  61. const filterTypeList = [
  62. { type: 'topLp', limit: 1 },
  63. { type: 'normalLp', limit: 0.1 },
  64. { type: 'lp', limit: 0 },
  65. ]
  66. // token折合成eth的价格
  67. let tokenConvertEthPrice: any = {
  68. '0': 1,
  69. '1': 1
  70. }
  71. // token0和token1都不是eth的,分三种情况探讨
  72. if (ethTokenAddressList.indexOf(lp.token0.toLowerCase()) == -1
  73. && ethTokenAddressList.indexOf(lp.token1.toLowerCase()) == -1) {
  74. tokenConvertEthPrice = this.getLpEthValue(lp)
  75. }
  76. // 从高到低遍历过滤条件
  77. const decimals0 = parseInt(lp.decimals0)
  78. const decimals1 = parseInt(lp.decimals0)
  79. const realAmount0 = parseInt(lp.r0) * tokenConvertEthPrice['0'] / Math.pow(10, -decimals0)
  80. const realAmount1 = parseInt(lp.r1) * tokenConvertEthPrice['1'] / Math.pow(10, -decimals1)
  81. for (const filterType of filterTypeList) {
  82. const amountLimit = filterType.limit
  83. if (realAmount0 >= amountLimit || realAmount1 >= amountLimit) {
  84. return filterType.type
  85. }
  86. }
  87. }
  88. async handleLp(lp: any, oldType: any) {
  89. // 过滤Lp
  90. let lpType = await this.checkLpType(lp)
  91. // 一共12w个池子,非ethw的垃圾池子就不拉了
  92. if (lpType == 'lp' && !lp.isEthW) {
  93. lpType = 'ethLp'
  94. }
  95. if (lpType != oldType) {
  96. // 保存变更之后的的Token
  97. // await this.saveToken(lp, lpType)
  98. // 保存变更后的Lp
  99. await this.saveLp(lp, lpType)
  100. logger.debug(`lp:${lp.LP},${oldType}->${lpType}.`)
  101. }
  102. }
  103. showMemory (mainInfo: string) {
  104. const memoryUsage = process.memoryUsage()
  105. if (this.maxMemoryOfByte < memoryUsage.rss) {
  106. this.maxMemoryOfByte = memoryUsage.rss
  107. this.maxMemoryChanged = true
  108. }
  109. if (this.maxMemoryChanged) {
  110. logger.debug(`${mainInfo} ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
  111. this.maxMemoryChanged = false
  112. }
  113. }
  114. format (bytes: any) {
  115. return (bytes / 1024 / 1024).toFixed(2) + ' MB';
  116. }
  117. generateAddressListByLpList (lpList: any) {
  118. const lpAddressList: any = []
  119. for (const lp of lpList) {
  120. lp.dataObj = JSON.parse(lp.data)
  121. lpAddressList.push(lp.dataObj.LP)
  122. }
  123. return lpAddressList
  124. }
  125. updateLocalLpListR0R1(lpList: any, r0s: any, r1s: any) {
  126. for (let index = 0; index < lpList.length; index++) {
  127. lpList[index].dataObj.r0 = r0s[index]
  128. lpList[index].dataObj.r1 = r1s[index]
  129. }
  130. }
  131. putAllLpGroupBySum(lpList: any) {
  132. this.allMaxValueLpGroupBySum = {}
  133. for (const lpDbObj of lpList) {
  134. const lp = lpDbObj.dataObj
  135. const notExist = !this.allMaxValueLpGroupBySum[lp.sum2]
  136. const moreValueThanNowMax = !notExist && (parseInt(lp.r0) > this.allMaxValueLpGroupBySum[lp.sum2].r0)
  137. if (notExist || moreValueThanNowMax) {
  138. // 计算0换1的价格和1换0的价格
  139. lp.zeroToOnePrice = parseInt(lp.r1) / parseInt(lp.r0) // 一个token0 = zeroToOnePrice个token1
  140. lp.oneToZeroPrice = parseInt(lp.r0) / parseInt(lp.r1) // 一个token1 = oneForZeroPrice个token0
  141. this.allMaxValueLpGroupBySum[lp.sum2] = lp
  142. }
  143. }
  144. }
  145. calcBaseTokenCovertEthValue() {
  146. for (const baseTokenAddress of baseTokenAddressList) {
  147. const isEthToken = ethTokenAddressList.indexOf(baseTokenAddress) != -1
  148. // eth兑换eth价格当然是1:1啦
  149. if (isEthToken) {
  150. this.baseTokenConvertEthValueMap[baseTokenAddress] = 1
  151. } else {
  152. // 在ETH token中查找适合兑换的
  153. for (const ethTokenAddress of ethTokenAddressList) {
  154. const maxValueLp = this.allMaxValueLpGroupBySum[this.getHexSum(baseTokenAddress, ethTokenAddress)]
  155. if (!maxValueLp) continue
  156. const token0 = maxValueLp.token0.toLowerCase()
  157. const token1 = maxValueLp.token1.toLowerCase()
  158. if (baseTokenAddress.toLowerCase() == token0) {
  159. this.baseTokenConvertEthValueMap[baseTokenAddress] = maxValueLp.zeroToOnePrice
  160. } else if (baseTokenAddress.toLowerCase() == token1) {
  161. this.baseTokenConvertEthValueMap[baseTokenAddress] = maxValueLp.oneToZeroPrice
  162. }
  163. }
  164. }
  165. }
  166. }
  167. getHexSum(hex0: string, hex1: string) {
  168. return BigNumber.from(hex0).add(BigNumber.from(hex1))._hex
  169. }
  170. async run() {
  171. logger.debug('LP maintenance start.')
  172. while (true) {
  173. this.showMemory('a loop...')
  174. try {
  175. const topLpPullRst = await History.findByBlock('topLp')
  176. if (!topLpPullRst.state) continue
  177. const topLpList = topLpPullRst.data
  178. const lpPullRst = await History.findByBlock('lp')
  179. if (!lpPullRst.state) continue
  180. const lpList = lpPullRst.data
  181. const normalLpPullRst = await History.findByBlock('normalLp')
  182. if (!normalLpPullRst.state) continue
  183. const normalLpList = normalLpPullRst.data
  184. const allTypeLpList: any = topLpList.concat(lpList).concat(normalLpList)
  185. const lpAddressList: any = this.generateAddressListByLpList(allTypeLpList)
  186. // lp按sum分类
  187. this.putAllLpGroupBySum(allTypeLpList)
  188. // logger.debug(`${lpAddressList.length}`)
  189. // 集中拉取r0,r1并更新本地的
  190. const size = 2000
  191. for (let from = 0; from < allTypeLpList.length; from += size) {
  192. // logger.debug(`${from}, ${allTypeLpList.length}`)
  193. const lpR0R1Info: any = await v2ToolBy410.methods.getPairSBalance(lpAddressList.slice(from, from + size)).call()
  194. const r0s = lpR0R1Info.amounts0
  195. const r1s = lpR0R1Info.amounts1
  196. this.updateLocalLpListR0R1(allTypeLpList.slice(from, from + size), r0s, r1s)
  197. }
  198. this.calcBaseTokenCovertEthValue()
  199. // 将lp类型有变动的全部更新
  200. for (const lpDbObj of allTypeLpList) {
  201. await this.handleLp(lpDbObj.dataObj, lpDbObj.block)
  202. }
  203. } catch (e) {}
  204. }
  205. }
  206. }
  207. async function main() {
  208. await new LpMaintenance().run()
  209. }
  210. main().catch((error) => {
  211. console.error(error);
  212. process.exitCode = 1;
  213. })