Skip to content

Commit

Permalink
add fallback for web3 json-rpc...
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagden committed Dec 17, 2024
1 parent 61d7b15 commit 0bd6024
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/ethereum/ethereum-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type EthereumConfiguration = {
};

export class EthereumReader {
private lastSwitchTime = 0;
private currentWeb3Index = 0;
private web3s: Web3[];
private throttled?: pThrottle.ThrottledFunction<[], void>;
Expand Down Expand Up @@ -52,6 +53,12 @@ export class EthereumReader {
}

switchWeb3() {
if (this.lastSwitchTime > Date.now() - 10000) {
console.log('switchWeb3: ignoring switch request, too soon.');
return;
}

this.lastSwitchTime = Date.now();
this.currentWeb3Index = (this.currentWeb3Index + 1) % this.web3s.length;

const currentProvider = this.getWeb3().eth.currentProvider;
Expand Down Expand Up @@ -175,18 +182,16 @@ export class EthereumReader {
if (this.throttled) await this.throttled();
this.requestStats.add(1);

try {
return contract.getPastEvents(eventName, {
fromBlock,
toBlock
});
} catch (e) {
return contract.getPastEvents(eventName, {
fromBlock,
toBlock
}).catch((e) => {
console.error("Error fetching past events:", e);
this.switchWeb3();
return contract.getPastEvents(eventName, {
fromBlock,
toBlock
});
}
});
}
}

0 comments on commit 0bd6024

Please sign in to comment.