From 4f86cb4d807bf3906f6817c6f4f2243c3f1fed39 Mon Sep 17 00:00:00 2001 From: mfw78 <53399572+mfw78@users.noreply.github.com> Date: Wed, 3 Apr 2024 11:48:31 +0000 Subject: [PATCH] feat: jsonrpc and websockets (#150) # Description Adds support again for JSON-RPC now that infrastructure has been updated and no longer has whacky state inconsistencies. # Changes - [x] Automatically determine whether to use `WebSockets` or `JsonRpcProvider` ## How to test 1. Configure with JSON RPC provider, confirm starts. 2. Configure with websockets provider, confirm starts. ## Related Issues Fixes #138 --- 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)); }