lpMaintenance.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. tokenAssembly: 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 checkTokenByAddress(tokenAddress: any, newType: string) {
  87. const tokenDbOBj = this.tokenAssembly[tokenAddress]
  88. if (this.needUpdate(tokenDbOBj.type, tokenDbOBj.type)) {
  89. tokenDbOBj.type = newType
  90. await History.appendOrUpdate(tokenDbOBj.type, tokenDbOBj.hash, tokenDbOBj.data)
  91. logger.debug(`token:${tokenAddress},${tokenDbOBj.type}->${tokenDbOBj.type}.`)
  92. }
  93. }
  94. async handleLp(lp: any, oldType: any) {
  95. // 过滤Lp
  96. let lpType: any = await this.checkLpType(lp)
  97. let tokenType = 'token'
  98. if (lpType != 'lp') tokenType = replaceAll(lpType, 'Lp', '') + 'Token'
  99. // 一共12w个池子,非ethw的垃圾池子就不拉了
  100. if (lpType == 'lp' && !lp.isEthW) {
  101. lpType = 'ethLp'
  102. }
  103. if (lpType != oldType) {
  104. // 保存变更后的Lp
  105. await this.saveLp(lp, lpType)
  106. logger.debug(`lp:${lp.LP},${oldType}->${lpType}.`)
  107. }
  108. await this.checkTokenByAddress(lp.token0, tokenType)
  109. await this.checkTokenByAddress(lp.token1, tokenType)
  110. }
  111. showMemory (mainInfo: string) {
  112. const memoryUsage = process.memoryUsage()
  113. if (this.maxMemoryOfByte < memoryUsage.rss) {
  114. this.maxMemoryOfByte = memoryUsage.rss
  115. this.maxMemoryChanged = true
  116. }
  117. if (this.maxMemoryChanged) {
  118. logger.debug(`${mainInfo} ${this.format(memoryUsage.rss)}/${this.format(this.maxMemoryOfByte)}`)
  119. this.maxMemoryChanged = false
  120. }
  121. }
  122. format (bytes: any) {
  123. return (bytes / 1024 / 1024).toFixed(2) + ' MB';
  124. }
  125. generateAddressListByLpList (lpList: any) {
  126. const lpAddressList: any = []
  127. for (const lp of lpList) {
  128. lp.dataObj = JSON.parse(lp.data)
  129. lpAddressList.push(lp.dataObj.LP)
  130. }
  131. return lpAddressList
  132. }
  133. updateLocalLpListR0R1(lpList: any, r0s: any, r1s: any) {
  134. for (let index = 0; index < lpList.length; index++) {
  135. lpList[index].dataObj.r0 = r0s[index]
  136. lpList[index].dataObj.r1 = r1s[index]
  137. }
  138. }
  139. putAllLpGroupBySum(lpList: any) {
  140. this.allMaxValueLpGroupBySum = {}
  141. for (const lpDbObj of lpList) {
  142. const lp = lpDbObj.dataObj
  143. lp.sum2 = this.getHexSum(lp.token0, lp.token1)
  144. const notExist = !this.allMaxValueLpGroupBySum[lp.sum2]
  145. const moreValueThanNowMax = !notExist && (parseInt(lp.r0) > this.allMaxValueLpGroupBySum[lp.sum2].r0)
  146. if (notExist || moreValueThanNowMax) {
  147. const r0RealAmount = parseInt(lp.r0) / Math.pow(10, parseInt(lp.decimals0))
  148. const r1RealAmount = parseInt(lp.r1) / Math.pow(10, parseInt(lp.decimals1))
  149. // 计算0换1的价格和1换0的价格
  150. lp.in1Token0OutToken1 = r1RealAmount / r0RealAmount // 一个token0 = in1Token0OutToken1个token1
  151. lp.in1Token1OutToken0 = r0RealAmount / r1RealAmount // 一个token1 = oneForZeroPrice个token0
  152. this.allMaxValueLpGroupBySum[lp.sum2] = lp
  153. }
  154. }
  155. }
  156. needUpdate(oldType: string, newType: string) {
  157. const tokenTypeMap: any = {
  158. 'token': 1,
  159. 'normalToken': 2,
  160. 'topToken': 3
  161. }
  162. return tokenTypeMap[newType] > tokenTypeMap[oldType]
  163. }
  164. async pullAllToken(lpList: any) {
  165. for (const lpDbObj of lpList) {
  166. const lp = lpDbObj.dataObj
  167. const lpType = lpDbObj.block
  168. let tokenType = 'token'
  169. if (lpType != 'lp') tokenType = replaceAll(lpType, 'Lp', '') + 'Token'
  170. if (!this.tokenAssembly[lp.token0]) {
  171. const token0Rst = await History.findByHash(lp.token0)
  172. const token0DbObj = token0Rst.data
  173. this.tokenAssembly[lp.token0] = {
  174. type: token0DbObj.block,
  175. hash: token0DbObj.hash,
  176. data: token0DbObj.dataObj
  177. }
  178. }
  179. if (!this.tokenAssembly[lp.token1]) {
  180. const token1Rst = await History.findByHash(lp.token1)
  181. const token1DbObj = token1Rst.data
  182. this.tokenAssembly[lp.token1] = {
  183. type: token1DbObj.block,
  184. hash: token1DbObj.hash,
  185. data: token1DbObj.dataObj
  186. }
  187. }
  188. }
  189. }
  190. calcBaseTokenCovertEthValue() {
  191. for (const baseTokenAddress of baseTokenAddressList) {
  192. const isEthToken = ethTokenAddressList.indexOf(baseTokenAddress) != -1
  193. // eth兑换eth价格当然是1:1啦
  194. if (isEthToken) {
  195. this.baseTokenConvertEthValueMap[baseTokenAddress] = 1
  196. } else {
  197. // 在ETH token中查找适合兑换的
  198. for (const ethTokenAddress of ethTokenAddressList) {
  199. const maxValueLp = this.allMaxValueLpGroupBySum[this.getHexSum(baseTokenAddress, ethTokenAddress)]
  200. if (!maxValueLp) continue
  201. const token0 = maxValueLp.token0.toLowerCase()
  202. const token1 = maxValueLp.token1.toLowerCase()
  203. if (baseTokenAddress.toLowerCase() == token0) {
  204. this.baseTokenConvertEthValueMap[baseTokenAddress] = maxValueLp.in1Token0OutToken1
  205. } else if (baseTokenAddress.toLowerCase() == token1) {
  206. this.baseTokenConvertEthValueMap[baseTokenAddress] = maxValueLp.in1Token1OutToken0
  207. }
  208. }
  209. }
  210. }
  211. }
  212. getHexSum(hex0: string, hex1: string) {
  213. return replaceAll(BigNumber.from(hex0).add(BigNumber.from(hex1))._hex, '0x0', '0x')
  214. }
  215. async run() {
  216. logger.debug('LP maintenance start.')
  217. while (true) {
  218. this.showMemory('a loop...')
  219. try {
  220. const topLpPullRst = await History.findByBlock('topLp')
  221. if (!topLpPullRst.state) continue
  222. const topLpList = topLpPullRst.data
  223. const lpPullRst = await History.findByBlock('lp')
  224. if (!lpPullRst.state) continue
  225. const lpList = lpPullRst.data
  226. const normalLpPullRst = await History.findByBlock('normalLp')
  227. if (!normalLpPullRst.state) continue
  228. const normalLpList = normalLpPullRst.data
  229. // const ethLpPullRst = await History.findByBlock('ethLp')
  230. // if (!ethLpPullRst.state) continue
  231. // const ethLpList = ethLpPullRst.data
  232. const ethLpList: any = []
  233. const allTypeLpList: any = topLpList.concat(lpList).concat(normalLpList).concat(ethLpList)
  234. // const allTypeLpList: any = [].concat(normalLpList).concat(ethLpList)
  235. const lpAddressList: any = this.generateAddressListByLpList(allTypeLpList)
  236. // lp按sum分类
  237. this.putAllLpGroupBySum(allTypeLpList)
  238. // 获取所有lp的未拉取的token
  239. await this.pullAllToken(allTypeLpList)
  240. // 集中拉取r0,r1并更新本地的
  241. const size = 2000
  242. for (let from = 0; from < allTypeLpList.length; from += size) {
  243. // logger.debug(`${from}, ${allTypeLpList.length}`)
  244. const lpR0R1Info: any = await v2ToolBy410.methods.getPairSBalance(lpAddressList.slice(from, from + size)).call()
  245. const r0s = lpR0R1Info.amounts0
  246. const r1s = lpR0R1Info.amounts1
  247. this.updateLocalLpListR0R1(allTypeLpList.slice(from, from + size), r0s, r1s)
  248. }
  249. this.calcBaseTokenCovertEthValue()
  250. // 将lp类型有变动的全部更新
  251. // logger.debug(`${allTypeLpList.length}`)
  252. for (const lpDbObj of allTypeLpList) {
  253. await this.handleLp(lpDbObj.dataObj, lpDbObj.block)
  254. }
  255. } catch (e) {
  256. logger.debug(e)
  257. }
  258. }
  259. }
  260. }
  261. async function main() {
  262. await new LpMaintenance().run()
  263. }
  264. main().catch((error) => {
  265. console.error(error);
  266. process.exitCode = 1;
  267. })