From 5bc3313019dc555cbfeacdb44b9b0018a7ace721 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sun, 12 Feb 2023 22:04:20 -0500 Subject: [PATCH] More robust support on networks which throw when filters are not supported (#3767). --- src.ts/providers/subscriber-filterid.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src.ts/providers/subscriber-filterid.ts b/src.ts/providers/subscriber-filterid.ts index f440e78d4c..b6fb21ac20 100644 --- a/src.ts/providers/subscriber-filterid.ts +++ b/src.ts/providers/subscriber-filterid.ts @@ -1,3 +1,5 @@ +import { isError } from "../utils/index.js"; + import { PollingEventSubscriber } from "./subscriber-polling.js"; import type { AbstractProvider, Subscriber } from "./abstract-provider.js"; @@ -5,7 +7,6 @@ import type { Network } from "./network.js"; import type { EventFilter } from "./provider.js"; import type { JsonRpcApiProvider } from "./provider-jsonrpc.js"; - function copy(obj: any): any { return JSON.parse(JSON.stringify(obj)); } @@ -59,12 +60,25 @@ export class FilterIdSubscriber implements Subscriber { async #poll(blockNumber: number): Promise { try { + // Subscribe if necessary if (this.#filterIdPromise == null) { this.#filterIdPromise = this._subscribe(this.#provider); } - const filterId = await this.#filterIdPromise; + // Get the Filter ID + let filterId: null | string = null; + try { + filterId = await this.#filterIdPromise; + } catch (error) { + if (!isError(error, "UNSUPPORTED_OPERATION") || error.operation !== "eth_newFilter") { + throw error; + } + } + + // The backend does not support Filter ID; downgrade to + // polling if (filterId == null) { + this.#filterIdPromise = null; this.#provider._recoverSubscriber(this, this._recover(this.#provider)); return; }