pushv2.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, '', 0, 200)
  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 (position <= pairsLength) {
  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 symbol0 = info['2'].replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 10)
  44. const symbol1 = info['6'].replace(/[^A-Za-z0-9 ]+/g, '').substring(0, 10)
  45. const name = `${router.slice(2, 4) + router.slice(-2)}_${symbol0}_${symbol1}`
  46. const sum2 = replaceAll(BigNumber.from(info['1']).add(BigNumber.from(info['5']))._hex, '0x0', '0x')
  47. const data = {
  48. LP: info['0'],
  49. decimals0: info['3'],
  50. decimals1: info['7'],
  51. factory: factory,
  52. feei: 30,
  53. id: position,
  54. name: name,
  55. r0: info['4'],
  56. r1: info['8'],
  57. router: router,
  58. sum2: sum2,
  59. symbol0: symbol0,
  60. symbol1: symbol1,
  61. token0: info['1'],
  62. token1: info['5']
  63. }
  64. const insertRst = await History.appendOrUpdate('0', info['0'], data)
  65. logger.debug(insertRst.msg, `, hash: ${info['0']}, ${position} / ${pairsLength}`)
  66. // 每十次更新一次position
  67. if (position % 50 == 0) {
  68. await History.appendOrUpdate('UNIV2DEX', factory, {"now": position})
  69. logger.debug(`Position updated: ${position}`)
  70. }
  71. errorCount = 0
  72. } catch (e) {
  73. logger.error(JSON.stringify(e))
  74. errorCount++
  75. }
  76. position++
  77. if (errorCount >= 5) {
  78. position = position - 5
  79. break;
  80. }
  81. }
  82. }
  83. async function main() {
  84. const fromZero: boolean = true
  85. logger.debug('Pull v2 start.')
  86. const v2_router_abi = require('../abi/UNIV2_ROUTER_ABI.json')
  87. const v2_factory_abi = require('../abi/UNIV2_FACTORY_ABI.json')
  88. const v2_tool_abi = require('../abi/410_V2_TOOLS.json')
  89. // 初始化410 v2工具箱
  90. const v2_410_tool = new web3.eth.Contract(v2_tool_abi, contracts.TOOLS_410_V2)
  91. for (let router_index = 0; router_index < v2_routers.length; router_index++) {
  92. const v2_router_address = v2_routers[router_index]
  93. try {
  94. logger.debug(`Router address: ${v2_router_address}`)
  95. // 获取工厂地址
  96. const v2_router = new web3.eth.Contract(v2_router_abi, v2_router_address)
  97. const v2_factory_address = await v2_router.methods.factory().call()
  98. // 获取工厂实例
  99. const v2_factory = new web3.eth.Contract(v2_factory_abi, v2_factory_address)
  100. // 获取当前pairsLength
  101. const pairsLength = await v2_factory.methods.allPairsLength().call()
  102. // 获取当前pull状态
  103. const position = fromZero ? 0 : await handlePosition(v2_router_address, v2_factory_address)
  104. logger.debug(`factory: ${v2_factory_address}, ${position} / ${pairsLength}.`)
  105. await handleAFactory(v2_router_address, v2_factory_address, position, pairsLength, v2_410_tool)
  106. } catch (e) {
  107. logger.error(`New contract error, router: ${v2_router_address}`)
  108. logger.error(e)
  109. }
  110. }
  111. }
  112. main().catch((error) => {
  113. console.error(error);
  114. process.exitCode = 1;
  115. })