lpMaintenance.ts 9.1 KB

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