optimize block fetching

master
lza_menace 3 weeks ago
parent 692ac78f0a
commit a2a914a31c

@ -61,10 +61,11 @@ class Scrape extends Collection {
provider = this.getWeb3Provider(); provider = this.getWeb3Provider();
constructor (contractName) { constructor (contractName, blockNumber) {
super(contractName); super(contractName);
this.contract = new ethers.Contract(this.contractAddress, this.abi, this.provider); this.contract = new ethers.Contract(this.contractAddress, this.abi, this.provider);
this.lastFile = `./storage/lastBlock.${this.contractName}.txt`; this.lastFile = `./storage/lastBlock.${this.contractName}.txt`;
this.lastBlock = blockNumber;
createDatabaseIfNeeded(); createDatabaseIfNeeded();
} }
@ -75,7 +76,7 @@ class Scrape extends Collection {
// continuous scanning loop // continuous scanning loop
async scrape() { async scrape() {
let latestEthBlock = await this.provider.getBlockNumber(); let latestEthBlock = this.lastBlock;
let lastScrapedBlock = this.getLastBlock(); let lastScrapedBlock = this.getLastBlock();
const lastRequested = lastScrapedBlock; const lastRequested = lastScrapedBlock;
@ -105,12 +106,6 @@ class Scrape extends Collection {
if (lastScrapedBlock > latestEthBlock) lastScrapedBlock = latestEthBlock; if (lastScrapedBlock > latestEthBlock) lastScrapedBlock = latestEthBlock;
} }
while (lastScrapedBlock >= latestEthBlock) {
latestEthBlock = await this.provider.getBlockNumber();
console.log(`[ ${(new Date()).toISOString()} ][ ${this.contractName} ] [ waiting ]\n`)
return
}
} }
// query historical logs // query historical logs
@ -448,10 +443,11 @@ async function writeToDatabase(_q) {
c.getSalesEvents(process.env.TX); c.getSalesEvents(process.env.TX);
return return
} else { } else {
const latestBlock = await provider.getBlockNumber();
while(true) { while(true) {
for(const key in ALL_CONTRACTS) { for(const key in ALL_CONTRACTS) {
if (process.env.ONLY && process.env.ONLY != key) continue if (process.env.ONLY && process.env.ONLY != key) continue
const c = new Scrape(key); const c = new Scrape(key, latestBlock);
c.scrape(); c.scrape();
await sleep(1); await sleep(1);
} }

Loading…
Cancel
Save