From 28e5f1c3a284c3b2c6794cab171e7ca49c9c68d6 Mon Sep 17 00:00:00 2001 From: mfw78 Date: Tue, 2 Apr 2024 10:53:10 +0000 Subject: [PATCH] feat: jsonrpc and websockets --- src/services/chain.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/services/chain.ts b/src/services/chain.ts index 2784685..9543417 100644 --- a/src/services/chain.ts +++ b/src/services/chain.ts @@ -146,7 +146,7 @@ export class ChainContext { ): Promise { const { rpc, deploymentBlock } = options; - const provider = new providers.WebSocketProvider(rpc); + const provider = getProvider(rpc.toLowerCase()); const chainId = (await provider.getNetwork()).chainId; const registry = await Registry.load( @@ -528,6 +528,16 @@ function _formatResult(result: boolean) { return result ? "✅" : "❌"; } +function getProvider(rpcUrl: string): providers.Provider { + // if the rpcUrl is a websocket url, use the WebSocketProvider + if (rpcUrl.startsWith("ws")) { + return new providers.WebSocketProvider(rpcUrl); + } + + // otherwise, use the JsonRpcProvider + return new providers.JsonRpcProvider(rpcUrl); +} + async function asyncSleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); }