pullv2.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 v2_routers from "../config/v2_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('UNIV2DEX', factory, '')
  12. let nowPosition = 0
  13. // 没有状态信息的情况
  14. if (pullState.data.length == 0) {
  15. const appendRst = await History.appendOrUpdate('UNIV2DEX', 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, v2_410_tool: any) {
  28. let errorCount = 0
  29. while (true) {
  30. try {
  31. const info = await v2_410_tool.methods.getPairIdInfo(factory, position++).call()
  32. // return (
  33. // 0 pairInfo.lp,
  34. // 1 pairInfo.token0,
  35. // 2 pairInfo.symbol0,
  36. // 3 pairInfo.decimals0,
  37. // 4 pairInfo.r0,
  38. // 5 pairInfo.token1,
  39. // 6 pairInfo.symbol1,
  40. // 7 pairInfo.decimals1,
  41. // 8 pairInfo.r1
  42. // );
  43. const data = {
  44. LP: info['0'],
  45. decimals0: info['4'],
  46. decimals1: info['7'],
  47. factory: factory,
  48. feei: 30,
  49. id: position,
  50. name: `${router.slice(2, 4) + router.slice(-2)}_${info['2']}_${info['6']}`,
  51. r0: 0,
  52. r1: 0,
  53. router: router,
  54. sum2: replaceAll(BigNumber.from(info['1']).add(BigNumber.from(info['5']))._hex, '0x0', '0x'),
  55. symbol0: info['2'],
  56. symbol1: info['6'],
  57. token0: info['1'],
  58. token1: info['5']
  59. }
  60. logger.debug(await History.appendOrUpdate('0', info['0'], data), `, hash: ${info['0']}`)
  61. // 每十次更新一次position
  62. if (position % 50 == 0) {
  63. await History.appendOrUpdate('UNIV2DEX', factory, {"now": position})
  64. logger.debug(`Position updated: ${position}`)
  65. }
  66. errorCount = 0
  67. } catch (e) {
  68. logger.error(e)
  69. errorCount++
  70. }
  71. if (errorCount >= 5) {
  72. position = position - 5
  73. break;
  74. }
  75. }
  76. }
  77. async function main() {
  78. logger.debug('Pull v2 start.')
  79. const v2_router_abi = require('../abi/UNIV2_ROUTER_ABI.json')
  80. const v2_factory_abi = require('../abi/UNIV2_FACTORY_ABI.json')
  81. const v2_tool_abi = require('../abi/410_V2_TOOLS.json')
  82. // 初始化410 v2工具箱
  83. const v2_410_tool = new web3.eth.Contract(v2_tool_abi, contracts.TOOLS_410_V2)
  84. for (let router_index = 0; router_index < v2_routers.length; router_index++) {
  85. const v2_router_address = v2_routers[router_index]
  86. try {
  87. logger.debug(`Router address: ${v2_router_address}`)
  88. // 获取工厂地址
  89. const v2_router = new web3.eth.Contract(v2_router_abi, v2_router_address)
  90. const v2_factory_address = await v2_router.methods.factory().call()
  91. // 获取工厂实例
  92. const v2_factory = new web3.eth.Contract(v2_factory_abi, v2_factory_address)
  93. // 获取当前pairsLength
  94. const pairsLength = await v2_factory.methods.allPairsLength().call()
  95. // 获取当前pull状态
  96. const position = await handlePosition(v2_router_address, v2_factory_address)
  97. logger.debug(`factory: ${v2_factory}, ${position} / ${pairsLength}.`)
  98. await handleAFactory(v2_router_address, v2_factory_address, position, v2_410_tool)
  99. } catch (e) {
  100. logger.error(`New contract error, router: ${v2_router_address}`)
  101. logger.error(e)
  102. }
  103. }
  104. }
  105. main().catch((error) => {
  106. console.error(error);
  107. process.exitCode = 1;
  108. })