Skip to content

Commit

Permalink
feat: jsonrpc and websockets (#150)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
mfw78 authored Apr 3, 2024
1 parent 774ed3e commit 4f86cb4
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/services/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class ChainContext {
): Promise<ChainContext> {
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(
Expand Down Expand Up @@ -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));
}

0 comments on commit 4f86cb4

Please sign in to comment.