lpMaintenance.ts 11 KB

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