-
Notifications
You must be signed in to change notification settings - Fork 5
Fix usage of WebSocketProvider and absence of pollingInterval #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3160b9a
6cbd8bf
d668994
540ba16
4b9542a
f278c5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { FallbackProvider, JsonRpcProvider, WebSocketProvider } from 'ethers'; | ||
| import { FallbackProviderConfig } from 'ethers/lib.commonjs/providers/provider-fallback'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've did not notice we were importing this type from the internal structure of ethers!, given that any update on this might be a breaking change for us, is there any better way we can handle this type ? Mirroring this type with an interface of ours probably ?? or inferring the type.. idk 🤣 |
||
| import { isDefined } from './util'; | ||
|
|
||
| export type FallbackProviderJsonConfig = { | ||
| chainId: number; | ||
| providers: ProviderJson[]; | ||
| }; | ||
|
|
||
| export type ProviderJson = { | ||
| priority: number; | ||
| weight: number; | ||
| provider: string; | ||
| chainId?: number; | ||
| stallTimeout?: number; | ||
| maxLogsPerBatch?: number; | ||
| }; | ||
|
|
||
| export const createProviderFromJsonConfig = ( | ||
| config: FallbackProviderJsonConfig | ProviderJson, | ||
| pollingInterval?: number, | ||
| ): FallbackProvider | WebSocketProvider | JsonRpcProvider => { | ||
| try { | ||
| // Handle single provider case | ||
| if (!('providers' in config)) { | ||
| // Get RPC URL | ||
| const providerURL = config.provider; | ||
| // Ensure providerURL exists and is a string | ||
| if (!isDefined(providerURL)) { | ||
| throw new Error('provider is required for single provider configuration'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just as a reminder, lets try to make this errors look similar to #38
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you provide an example? Not clear what change is being asked for |
||
| } else if (typeof providerURL !== 'string') { | ||
| throw new Error('provider must be a string'); | ||
| } | ||
|
|
||
| // Ensure chainId is present | ||
| if (!isDefined(config.chainId)) { | ||
| throw new Error('chainId is required for single provider configuration'); | ||
| } | ||
|
|
||
| // Create singular provider depending on the URL | ||
| let provider: JsonRpcProvider | WebSocketProvider; | ||
| if (providerURL.startsWith('wss')) { | ||
| provider = new WebSocketProvider(providerURL, config.chainId, { | ||
| staticNetwork: true, | ||
| }); | ||
| } else { | ||
| provider = new JsonRpcProvider(providerURL, config.chainId, { | ||
| staticNetwork: true, | ||
| pollingInterval, | ||
| }); | ||
| } | ||
|
|
||
| return provider; | ||
| } | ||
|
|
||
| const totalWeight = config.providers.reduce( | ||
| (acc, { weight }) => acc + weight, | ||
| 0, | ||
| ); | ||
| if (totalWeight < 2) { | ||
| throw new Error( | ||
| 'Total weight across providers must be >= 2 for fallback quorum.', | ||
| ); | ||
| } | ||
|
|
||
| const providers: FallbackProviderConfig[] = config.providers.map( | ||
| ({ | ||
| provider: providerURL, | ||
| priority, | ||
| weight, | ||
| stallTimeout, | ||
| maxLogsPerBatch, | ||
| }) => { | ||
| const isWebsocket = providerURL.startsWith('wss'); | ||
| if (isWebsocket) { | ||
| throw new Error( | ||
| 'WebSocketProvider not supported in FallbackProvider as it will use polling instead of eth_subscribe', | ||
| ); | ||
| } | ||
|
|
||
| // Create JsonRpcProvider | ||
| const provider = new JsonRpcProvider( | ||
| providerURL, | ||
| config.chainId, | ||
| { | ||
| staticNetwork: true, | ||
| pollingInterval, | ||
| batchMaxCount: maxLogsPerBatch, | ||
| }, | ||
| ); | ||
|
|
||
| // Add JsonRpcProvider to FallbackProviderConfig | ||
| const fallbackProviderConfig: FallbackProviderConfig = { | ||
| provider, | ||
| priority, | ||
| weight, | ||
| stallTimeout, | ||
| }; | ||
| return fallbackProviderConfig; | ||
| }, | ||
| ); | ||
|
|
||
| // Return FallbackProvider containing array of JsonRpcProviders | ||
| return new FallbackProvider(providers, config.chainId, { | ||
| pollingInterval | ||
| }); | ||
| } catch (cause) { | ||
| if (!(cause instanceof Error)) { | ||
| throw new Error( | ||
| 'Non-error thrown from createFallbackProviderFromJsonConfig', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might this be a typo!? this error message is |
||
| { cause }, | ||
| ); | ||
| } | ||
| // Preserve the original error message | ||
| throw cause; | ||
| } | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.