2022-06-08 13:45:37 +03:00
|
|
|
import fs from 'fs'
|
2022-10-30 22:44:20 +03:00
|
|
|
import zlib from 'zlib'
|
2022-06-08 13:45:37 +03:00
|
|
|
import Web3 from 'web3'
|
|
|
|
|
2023-07-04 16:57:33 +03:00
|
|
|
import networkConfig, { blockSyncInterval } from '../../networkConfig'
|
2022-06-08 13:45:37 +03:00
|
|
|
|
2023-05-13 01:19:58 +03:00
|
|
|
export function download({ name, directory }) {
|
2022-10-30 22:44:20 +03:00
|
|
|
const path = `${directory}${name}.gz`.toLowerCase()
|
2022-06-08 13:45:37 +03:00
|
|
|
|
2023-06-22 23:39:28 +03:00
|
|
|
const data = fs.readFileSync(path, { flag: 'as+' })
|
2022-10-30 22:44:20 +03:00
|
|
|
const content = zlib.inflateSync(data)
|
2022-06-08 13:45:37 +03:00
|
|
|
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2023-05-13 01:19:58 +03:00
|
|
|
export function loadCachedEvents({ name, directory, deployedBlock }) {
|
2022-06-08 13:45:37 +03:00
|
|
|
try {
|
2023-05-13 01:19:58 +03:00
|
|
|
const module = download({ contentType: 'string', directory, name })
|
2022-06-08 13:45:37 +03:00
|
|
|
|
|
|
|
if (module) {
|
|
|
|
const events = JSON.parse(module)
|
|
|
|
|
|
|
|
return {
|
|
|
|
events,
|
2023-05-13 01:19:58 +03:00
|
|
|
lastBlock: events[events.length - 1].blockNumber
|
2022-06-08 13:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(`Method loadCachedEvents has error: ${err.message}`)
|
|
|
|
return {
|
|
|
|
events: [],
|
|
|
|
lastBlock: deployedBlock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 10:13:54 +03:00
|
|
|
export async function getPastEvents({ type, fromBlock, netId, events, contractAttrs }) {
|
2022-06-08 13:45:37 +03:00
|
|
|
let downloadedEvents = events
|
|
|
|
|
2022-06-13 10:13:54 +03:00
|
|
|
let [{ url: rpcUrl }] = Object.values(networkConfig[`netId${netId}`].rpcUrls)
|
|
|
|
|
|
|
|
if (netId === '5') {
|
2022-09-08 14:14:09 +03:00
|
|
|
rpcUrl = 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'
|
2022-06-13 10:13:54 +03:00
|
|
|
}
|
|
|
|
|
2022-06-08 13:45:37 +03:00
|
|
|
const provider = new Web3.providers.HttpProvider(rpcUrl)
|
|
|
|
const web3 = new Web3(provider)
|
|
|
|
const contract = new web3.eth.Contract(...contractAttrs)
|
|
|
|
|
|
|
|
const currentBlockNumber = await web3.eth.getBlockNumber()
|
2022-09-08 14:14:09 +03:00
|
|
|
// PoS networks index blocks too fast, so a buffer is needed
|
|
|
|
const blockNumberBuffer = currentBlockNumber - 3
|
|
|
|
const blockDifference = Math.ceil(blockNumberBuffer - fromBlock)
|
2022-06-08 13:45:37 +03:00
|
|
|
|
2022-09-08 14:14:09 +03:00
|
|
|
// eth_logs and eth_filter are restricted > 10,000 block queries
|
2023-07-04 16:57:33 +03:00
|
|
|
const blockRange = blockSyncInterval ? blockSyncInterval : 10_000
|
2022-06-08 13:45:37 +03:00
|
|
|
|
|
|
|
let chunksCount = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange)
|
|
|
|
const chunkSize = Math.ceil(blockDifference / chunksCount)
|
|
|
|
|
|
|
|
let toBlock = fromBlock + chunkSize
|
|
|
|
|
|
|
|
if (fromBlock < currentBlockNumber) {
|
|
|
|
if (toBlock >= currentBlockNumber) {
|
|
|
|
toBlock = currentBlockNumber
|
|
|
|
chunksCount = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Fetching ${type}, chainId - ${netId}`, `chunksCount - ${chunksCount}`)
|
2023-05-13 01:19:58 +03:00
|
|
|
|
2022-06-08 13:45:37 +03:00
|
|
|
for (let i = 0; i < chunksCount; i++)
|
|
|
|
try {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 200))
|
|
|
|
|
|
|
|
console.log(`fromBlock - ${fromBlock}`)
|
|
|
|
console.log(`toBlock - ${toBlock}`)
|
|
|
|
|
|
|
|
const eventsChunk = await contract.getPastEvents(type, { fromBlock, toBlock })
|
|
|
|
|
|
|
|
if (eventsChunk) {
|
|
|
|
downloadedEvents = downloadedEvents.concat(eventsChunk)
|
|
|
|
console.log('downloaded events count - ', eventsChunk.length)
|
|
|
|
console.log('____________________________________________')
|
|
|
|
}
|
|
|
|
fromBlock = toBlock
|
|
|
|
toBlock += chunkSize
|
|
|
|
} catch (err) {
|
|
|
|
console.log('getPastEvents events', `chunk number - ${i}, has error: ${err.message}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return downloadedEvents
|
|
|
|
}
|