pullv2.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, pairsLength: 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['3'],
  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: info['4'],
  52. r1: info['8'],
  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. const insertRst = await History.appendOrUpdate('0', info['0'], data)
  61. logger.debug(insertRst.msg, `, hash: ${info['0']}, ${position} / ${pairsLength}`)
  62. // 每十次更新一次position
  63. if (position % 50 == 0) {
  64. await History.appendOrUpdate('UNIV2DEX', factory, {"now": position})
  65. logger.debug(`Position updated: ${position}`)
  66. }
  67. errorCount = 0
  68. } catch (e) {
  69. logger.error(JSON.stringify(e))
  70. errorCount++
  71. }
  72. if (errorCount >= 5) {
  73. position = position - 5
  74. break;
  75. }
  76. }
  77. }
  78. async function main() {
  79. logger.debug('Pull v2 start.')
  80. const v2_router_abi = require('../abi/UNIV2_ROUTER_ABI.json')
  81. const v2_factory_abi = require('../abi/UNIV2_FACTORY_ABI.json')
  82. const v2_tool_abi = require('../abi/410_V2_TOOLS.json')
  83. // 初始化410 v2工具箱
  84. const v2_410_tool = new web3.eth.Contract(v2_tool_abi, contracts.TOOLS_410_V2)
  85. for (let router_index = 0; router_index < v2_routers.length; router_index++) {
  86. const v2_router_address = v2_routers[router_index]
  87. try {
  88. logger.debug(`Router address: ${v2_router_address}`)
  89. // 获取工厂地址
  90. const v2_router = new web3.eth.Contract(v2_router_abi, v2_router_address)
  91. const v2_factory_address = await v2_router.methods.factory().call()
  92. // 获取工厂实例
  93. const v2_factory = new web3.eth.Contract(v2_factory_abi, v2_factory_address)
  94. // 获取当前pairsLength
  95. const pairsLength = await v2_factory.methods.allPairsLength().call()
  96. // 获取当前pull状态
  97. const position = await handlePosition(v2_router_address, v2_factory_address)
  98. logger.debug(`factory: ${v2_factory_address}, ${position} / ${pairsLength}.`)
  99. await handleAFactory(v2_router_address, v2_factory_address, position, pairsLength, v2_410_tool)
  100. } catch (e) {
  101. logger.error(`New contract error, router: ${v2_router_address}`)
  102. logger.error(e)
  103. }
  104. }
  105. }
  106. main().catch((error) => {
  107. console.error(error);
  108. process.exitCode = 1;
  109. })