From 0bd602411d6c5fc629424c40410e1db550e273fe Mon Sep 17 00:00:00 2001 From: Jordan Sheinfeld Date: Tue, 17 Dec 2024 11:36:17 +0200 Subject: [PATCH] add fallback for web3 json-rpc... --- src/ethereum/ethereum-reader.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ethereum/ethereum-reader.ts b/src/ethereum/ethereum-reader.ts index 523abd1..2cc9183 100644 --- a/src/ethereum/ethereum-reader.ts +++ b/src/ethereum/ethereum-reader.ts @@ -18,6 +18,7 @@ export type EthereumConfiguration = { }; export class EthereumReader { + private lastSwitchTime = 0; private currentWeb3Index = 0; private web3s: Web3[]; private throttled?: pThrottle.ThrottledFunction<[], void>; @@ -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; @@ -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 }); - } + }); } }