pullv3.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 v3_routers from "../config/v3_routers";
  6. import {BigNumber} from "ethers";
  7. import {replaceAll} from "hardhat/internal/util/strings";
  8. async function handlePosition(router: String, factory: String) {
  9. try {
  10. // 拉取当前pull状态信息
  11. const pullState = await History.findByHashOrBlockOrDataVague('UNIV3DEX', factory, '')
  12. let nowPosition = 0
  13. // 没有状态信息的情况
  14. if (pullState.data.length == 0) {
  15. const appendRst = await History.appendOrUpdate('UNIV3DEX', factory, {"now": nowPosition})
  16. logger.debug(appendRst)
  17. } else {
  18. nowPosition = pullState.data[0].dataObj.now
  19. }
  20. return nowPosition
  21. } catch (e) {
  22. logger.error(`Pull state error, router: ${router}, factory: ${factory}`)
  23. logger.error(e)
  24. }
  25. return 0
  26. }
  27. async function handleAFactory(router: String, factory: String, position: number, v3_410_tool: any) {
  28. let errorCount = 0
  29. while (true) {
  30. try {
  31. const info = await v3_410_tool.methods.getPairIdInfo(factory, position++).call()
  32. logger.debug(info)
  33. // return (
  34. // 0 pairInfo.lp,
  35. // 1 pairInfo.token0,
  36. // 2 pairInfo.symbol0,
  37. // 3 pairInfo.decimals0,
  38. // 4 pairInfo.r0,
  39. // 5 pairInfo.token1,
  40. // 6 pairInfo.symbol1,
  41. // 7 pairInfo.decimals1,
  42. // 8 pairInfo.r1
  43. // );
  44. // const symbol0 = info['2'].replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 10)
  45. // const symbol1 = info['6'].replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 10)
  46. // const name = `${router.slice(2, 4) + router.slice(-2)}_${symbol0}_${symbol1}`
  47. // const sum2 = replaceAll(BigNumber.from(info['1']).add(BigNumber.from(info['5']))._hex, '0x0', '0x')
  48. // const data = {
  49. // LP: info['0'],
  50. // decimals0: info['3'],
  51. // decimals1: info['7'],
  52. // factory: factory,
  53. // feei: 30,
  54. // id: position,
  55. // name: name,
  56. // r0: info['4'],
  57. // r1: info['8'],
  58. // router: router,
  59. // sum2: sum2,
  60. // symbol0: symbol0,
  61. // symbol1: symbol1,
  62. // token0: info['1'],
  63. // token1: info['5']
  64. // }
  65. // const insertRst = await History.appendOrUpdate('0', info['0'], data)
  66. // logger.debug(insertRst.msg, `, hash: ${info['0']}, ${position} / ${pairsLength}`)
  67. //
  68. // // 每十次更新一次position
  69. // if (position % 50 == 0) {
  70. // await History.appendOrUpdate('UNIV3DEX', factory, {"now": position})
  71. // logger.debug(`Position updated: ${position}`)
  72. // }
  73. //
  74. // errorCount = 0
  75. } catch (e) {
  76. logger.error(JSON.stringify(e))
  77. errorCount++
  78. }
  79. if (errorCount >= 5) {
  80. position = position - 5
  81. break;
  82. }
  83. }
  84. }
  85. async function main() {
  86. const fromZero: boolean = true
  87. logger.debug('Pull v3 start.')
  88. const v3_router_abi = require('../abi/UNIV3_ROUTER_ABI.json')
  89. const v3_tool_abi = require('../abi/410_V3_TOOLS.json')
  90. // 初始化410 v3工具箱
  91. const v3_410_tool = new web3.eth.Contract(v3_tool_abi, contracts.TOOLS_410_V3)
  92. for (let router_index = 0; router_index < v3_routers.length; router_index++) {
  93. const v3_router_address = v3_routers[router_index]
  94. try {
  95. logger.debug(`Router address: ${v3_router_address}`)
  96. // 获取工厂地址
  97. const v3_router = new web3.eth.Contract(v3_router_abi, v3_router_address)
  98. const v3_factory_address = await v3_router.methods.factory().call()
  99. // 获取当前pull状态
  100. const position = fromZero ? 0 : await handlePosition(v3_router_address, v3_factory_address)
  101. logger.debug(`factory: ${v3_factory_address}, ${position}.`)
  102. await handleAFactory(v3_router_address, v3_factory_address, position, v3_410_tool)
  103. } catch (e) {
  104. logger.error(`New contract error, router: ${v3_router_address}`)
  105. logger.error(e)
  106. }
  107. }
  108. }
  109. main().catch((error) => {
  110. console.error(error);
  111. process.exitCode = 1;
  112. })