|
|
@@ -32,81 +32,55 @@ async function handlePosition(router: String, factory: String) {
|
|
|
return 0
|
|
|
}
|
|
|
|
|
|
-async function handleAFactory(routerObj: any, position: number, pairsLength: number, v2_410_tool: any, lpList: any) {
|
|
|
- while (position < pairsLength) {
|
|
|
- try {
|
|
|
- const info = await v2_410_tool.methods.getPairIdInfo(routerObj.factory, position).call()
|
|
|
-
|
|
|
- const symbol0 = info['2'].replace(/[^A-Za-z0-9]+/g, '').substring(0, 10)
|
|
|
- const symbol1 = info['6'].replace(/[^A-Za-z0-9]+/g, '').substring(0, 10)
|
|
|
- const name = `${routerObj.name}_${symbol0}_${symbol1}`
|
|
|
- const sum2 = replaceAll(BigNumber.from(info['1']).add(BigNumber.from(info['5']))._hex, '0x0', '0x')
|
|
|
-
|
|
|
- const Lp = {
|
|
|
- LP: info['0'],
|
|
|
- decimals0: info['3'],
|
|
|
- decimals1: info['7'],
|
|
|
- factory: routerObj.factory,
|
|
|
- feei: routerObj.fee,
|
|
|
- id: position,
|
|
|
- name: name,
|
|
|
- r0: info['4'],
|
|
|
- r1: info['8'],
|
|
|
- router: routerObj.router,
|
|
|
- sum2: sum2,
|
|
|
- symbol0: symbol0,
|
|
|
- symbol1: symbol1,
|
|
|
- token0: info['1'],
|
|
|
- token1: info['5'],
|
|
|
- isEthW: routerObj.type == 'ETHW'
|
|
|
- }
|
|
|
- lpList.push(Lp)
|
|
|
+async function getPoolByPosition(routerObj: any, position: number, v2ToolBy410: any) {
|
|
|
+ try {
|
|
|
+ const info = await v2ToolBy410.methods.getPairIdInfo(routerObj.factory, position).call()
|
|
|
+
|
|
|
+ const symbol0 = info['2'].replace(/[^A-Za-z0-9]+/g, '').substring(0, 10)
|
|
|
+ const symbol1 = info['6'].replace(/[^A-Za-z0-9]+/g, '').substring(0, 10)
|
|
|
+ const name = `${routerObj.name}_${symbol0}_${symbol1}`
|
|
|
+ const sum2 = replaceAll(BigNumber.from(info['1']).add(BigNumber.from(info['5']))._hex, '0x0', '0x')
|
|
|
+
|
|
|
+ const lp = {
|
|
|
+ LP: info['0'],
|
|
|
+ decimals0: info['3'],
|
|
|
+ decimals1: info['7'],
|
|
|
+ factory: routerObj.factory,
|
|
|
+ feei: routerObj.fee,
|
|
|
+ id: position,
|
|
|
+ name: name,
|
|
|
+ r0: info['4'],
|
|
|
+ r1: info['8'],
|
|
|
+ router: routerObj.router,
|
|
|
+ sum2: sum2,
|
|
|
+ symbol0: symbol0,
|
|
|
+ symbol1: symbol1,
|
|
|
+ token0: info['1'],
|
|
|
+ token1: info['5'],
|
|
|
+ isEthW: routerObj.type == 'ETHW'
|
|
|
+ }
|
|
|
|
|
|
- // 更新一次position
|
|
|
- await History.appendOrUpdate('UNIV2DEX', routerObj.factory, {"now": position})
|
|
|
- } catch (e) {}
|
|
|
+ // 更新一次position
|
|
|
+ await History.appendOrUpdate('UNIV2DEX', routerObj.factory, {"now": position})
|
|
|
|
|
|
- position++
|
|
|
- }
|
|
|
-}
|
|
|
+ return lp
|
|
|
+ } catch (e) {}
|
|
|
|
|
|
-async function getAllLp(v2Router: any, fromZero: boolean, v2_410_tool: any, v2_factory_abi: any, lpList: any) {
|
|
|
- try {
|
|
|
- const v2_factory_address = v2Router.factory
|
|
|
- // 获取工厂实例
|
|
|
- const v2_factory = new web3.eth.Contract(v2_factory_abi, v2_factory_address)
|
|
|
- // 获取当前pairsLength
|
|
|
- const pairsLength = await v2_factory.methods.allPairsLength().call()
|
|
|
- // 获取当前pull状态
|
|
|
- const position = fromZero ? 0 : await handlePosition(v2Router.router, v2_factory_address)
|
|
|
-
|
|
|
- if (position + 1 < pairsLength) {
|
|
|
- logger.debug(`Router address: ${v2Router.router}`)
|
|
|
- logger.debug(`factory: ${v2_factory_address}, ${position + 1} / ${pairsLength}.`)
|
|
|
-
|
|
|
- await handleAFactory(v2Router, position, pairsLength, v2_410_tool, lpList)
|
|
|
- }
|
|
|
- } catch (e) {
|
|
|
- logger.error(`New contract error, router: ${v2Router.router}`)
|
|
|
- logger.error(e)
|
|
|
- }
|
|
|
+ return undefined
|
|
|
}
|
|
|
|
|
|
-async function saveLpToSwapPath(lpList: any) {
|
|
|
- for (let lpIndex in lpList) {
|
|
|
- const lp = lpList[lpIndex]
|
|
|
- const lpSum2Query = await swapPath.findBySumValueAndLevel(lp.sum2, '2')
|
|
|
- let lpSum2List = []
|
|
|
+async function saveLpToSwapPath(lp: any) {
|
|
|
+ const lpSum2Query = await swapPath.findBySumValueAndLevel(lp.sum2, '2')
|
|
|
+ let lpSum2List = []
|
|
|
|
|
|
- if (lpSum2Query.state) {
|
|
|
- lpSum2List = JSON.parse(lpSum2Query.data.data)
|
|
|
- }
|
|
|
+ if (lpSum2Query.state) {
|
|
|
+ lpSum2List = JSON.parse(lpSum2Query.data.data)
|
|
|
+ }
|
|
|
|
|
|
- lpSum2List.push(lp)
|
|
|
+ lpSum2List.push(lp)
|
|
|
|
|
|
- const insertRst = await swapPath.appendOrUpdate(lp.sum2, '2', lpSum2List)
|
|
|
- logger.debug(`save path: ${lp.name}: ${insertRst.msg}, ${parseInt(lpIndex) + 1}/${lpList.length}`)
|
|
|
- }
|
|
|
+ const insertRst = await swapPath.appendOrUpdate(lp.sum2, '2', lpSum2List)
|
|
|
+ logger.debug(`save path: ${lp.name}: ${insertRst.msg}`)
|
|
|
}
|
|
|
|
|
|
async function handleToken(pool: any, zero: boolean) {
|
|
|
@@ -125,91 +99,97 @@ async function handleToken(pool: any, zero: boolean) {
|
|
|
return token
|
|
|
}
|
|
|
|
|
|
-async function saveToken(lpList: any, type='token') {
|
|
|
- for (let lpIndex in lpList) {
|
|
|
- const lp = lpList[lpIndex]
|
|
|
-
|
|
|
- try {
|
|
|
- const token = await handleToken(lp, true)
|
|
|
-
|
|
|
- const saveRst = await history.appendOrUpdate(type, token.address, token)
|
|
|
- logger.debug(`${token.name} ${saveRst.msg}`)
|
|
|
- } catch (e) {}
|
|
|
+async function saveToken(lp: any, type='token') {
|
|
|
+ try {
|
|
|
+ const token = await handleToken(lp, true)
|
|
|
|
|
|
- try {
|
|
|
- const token = await handleToken(lp, false)
|
|
|
+ const saveRst = await history.appendOrUpdate(type, token.address, token)
|
|
|
+ logger.debug(`${token.name} ${saveRst.msg}`)
|
|
|
+ } catch (e) {}
|
|
|
|
|
|
- const saveRst = await history.appendOrUpdate(type, token.address, token)
|
|
|
- logger.debug(`${token.name} ${saveRst.msg}`)
|
|
|
- } catch (e) {}
|
|
|
+ try {
|
|
|
+ const token = await handleToken(lp, false)
|
|
|
|
|
|
- logger.debug(`insert token: ${parseInt(lpIndex) + 1} / ${lpList.length}`)
|
|
|
- }
|
|
|
+ const saveRst = await history.appendOrUpdate(type, token.address, token)
|
|
|
+ logger.debug(`${token.name} ${saveRst.msg}`)
|
|
|
+ } catch (e) {}
|
|
|
}
|
|
|
|
|
|
-async function saveLp(lpList: any, type='0') {
|
|
|
- for (let lpIndex in lpList) {
|
|
|
- const lp = lpList[lpIndex]
|
|
|
- const insertRst = await History.appendOrUpdate(type, lp.LP, lp)
|
|
|
- logger.debug(`insert lp:${insertRst.msg}, hash: ${lp.LP}, ${parseInt(lpIndex) + 1} / ${lpList.length}`)
|
|
|
- }
|
|
|
+async function saveLp(lp: any, type='0') {
|
|
|
+ const insertRst = await History.appendOrUpdate(type, lp.LP, lp)
|
|
|
+ logger.debug(`insert lp:${insertRst.msg}, hash: ${lp.LP}.}`)
|
|
|
}
|
|
|
|
|
|
-async function filterLp(lpList: any, filteredLpList: any) {
|
|
|
- for (let lpIndex in lpList) {
|
|
|
- const lp = lpList[lpIndex]
|
|
|
-
|
|
|
- if (lp.token0.toLowerCase() === '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2') {
|
|
|
- if (parseInt(lp.r0) > 20 * 1e18) filteredLpList.push(lp)
|
|
|
- } else if (lp.token1.toLowerCase() === '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2') {
|
|
|
- if (parseInt(lp.r1) > 20 * 1e18) filteredLpList.push(lp)
|
|
|
- } else {
|
|
|
- filteredLpList.push(lp)
|
|
|
- }
|
|
|
+async function filterLp(lp: any) {
|
|
|
+ if (lp.token0.toLowerCase() === '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2') {
|
|
|
+ return parseInt(lp.r0) > 20 * 1e18
|
|
|
+ } else if (lp.token1.toLowerCase() === '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2') {
|
|
|
+ return parseInt(lp.r1) > 20 * 1e18
|
|
|
}
|
|
|
+
|
|
|
+ return true
|
|
|
}
|
|
|
|
|
|
async function main() {
|
|
|
logger.debug('Pull v2 start.')
|
|
|
|
|
|
- const v2_factory_abi = require('../../abi/UNIV2_FACTORY_ABI.json')
|
|
|
- const v2_tool_abi = require('../../abi/410_V2_TOOLS.json')
|
|
|
-
|
|
|
// 初始化410 v2工具箱
|
|
|
- const v2_410_tool = new web3.eth.Contract(v2_tool_abi, contracts.TOOLS_410_V2)
|
|
|
- const v2RouterInfo = require('../../config/v2RouterInfo.json')
|
|
|
+ const v2ToolBy410Abi = require('../../abi/410_V2_TOOLS.json')
|
|
|
+ const v2ToolBy410 = new web3.eth.Contract(v2ToolBy410Abi, contracts.V2_TOOLS_BY_410)
|
|
|
+ const v2FactoryAbi = require('../../abi/UNIV2_FACTORY_ABI.json')
|
|
|
+
|
|
|
+
|
|
|
+ const routerList: Array<any> = require('../../config/router-list.json')
|
|
|
|
|
|
let fromZero: boolean = false
|
|
|
- const lpList: any = []
|
|
|
- const filteredLpList: any = []
|
|
|
|
|
|
while (true) {
|
|
|
- for (let router_index = 0; router_index < v2RouterInfo.length; router_index++) {
|
|
|
- lpList.length = 0
|
|
|
- filteredLpList.length = 0
|
|
|
-
|
|
|
- // 1. 获取Lp
|
|
|
- await getAllLp(v2RouterInfo[router_index], fromZero, v2_410_tool, v2_factory_abi, lpList)
|
|
|
- // 2. 保存Lp信息
|
|
|
- await saveLp(lpList, '0')
|
|
|
- // 3. 保存Lp的Token到Token表
|
|
|
- await saveToken(lpList, 'token')
|
|
|
- // 4. 过滤Lp
|
|
|
- await filterLp(lpList, filteredLpList)
|
|
|
- // 5. 将过滤后的Lp保存到二阶表
|
|
|
- await saveLpToSwapPath(filteredLpList)
|
|
|
- // 6. 保存筛选之后的的Token到TopToken表
|
|
|
- await saveToken(filteredLpList, 'topToken')
|
|
|
- // 7. 保存过滤后的Lp到TopLp
|
|
|
- await saveLp(filteredLpList, 'topLp')
|
|
|
-
|
|
|
- if (lpList.length > 0) {
|
|
|
+ for (const routerObj of routerList) {
|
|
|
+ const v2FactoryAddress = routerObj.factory
|
|
|
+ // 获取工厂实例
|
|
|
+ const v2Factory = new web3.eth.Contract(v2FactoryAbi, v2FactoryAddress)
|
|
|
+ // 获取当前pull状态
|
|
|
+ const position = fromZero ? 0 : await handlePosition(routerObj.router, v2FactoryAddress)
|
|
|
+ // 获取当前pairsLength
|
|
|
+ const pairsLength = await v2Factory.methods.allPairsLength().call()
|
|
|
+ // 如果有未获取的池子
|
|
|
+ const haveNewLp = position + 1 < pairsLength
|
|
|
+
|
|
|
+ // 打印信息
|
|
|
+ if (haveNewLp) {
|
|
|
+ logger.debug(`Router address: ${routerObj.router}`)
|
|
|
+ logger.debug(`factory: ${v2FactoryAddress}, ${position + 1} / ${pairsLength}.`)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 挨个处理lp信息
|
|
|
+ while (position < pairsLength) {
|
|
|
+ logger.debug(`${position + 1} / ${pairsLength}`)
|
|
|
+ // 1. 获取lp
|
|
|
+ const lp = await getPoolByPosition(routerObj, position, v2ToolBy410)
|
|
|
+ // 2. 保存Lp信息
|
|
|
+ await saveLp(lp, '0')
|
|
|
+ // 3. 保存Lp的Token到Token表
|
|
|
+ await saveToken(lp, 'token')
|
|
|
+ // 4. 过滤Lp
|
|
|
+ if (!(await filterLp(lp))) continue
|
|
|
+ // 5. 将过滤后的Lp保存到二阶表
|
|
|
+ await saveLpToSwapPath(lp)
|
|
|
+ // 6. 保存筛选之后的的Token到TopToken表
|
|
|
+ await saveToken(lp, 'topToken')
|
|
|
+ // 7. 保存过滤后的Lp到TopLp
|
|
|
+ await saveLp(lp, 'topLp')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (haveNewLp) {
|
|
|
logger.debug('')
|
|
|
logger.debug('')
|
|
|
logger.debug('')
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if (fromZero) fromZero = false;
|
|
|
+ }
|
|
|
+
|
|
|
// 数据流、abstruct
|
|
|
// v2RouterInfo.forEach(async (routerInfo) => {
|
|
|
// const lpList = await getAllLp(routerInfo);
|
|
|
@@ -220,9 +200,6 @@ async function main() {
|
|
|
// })
|
|
|
// })
|
|
|
//
|
|
|
-
|
|
|
- if (fromZero) fromZero = false;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
main().catch((error) => {
|