Skip to content
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

Potential Fallback Provider (Solves #22) #39

Closed
wants to merge 1 commit into from
Closed

Potential Fallback Provider (Solves #22) #39

wants to merge 1 commit into from

Conversation

arimgibson
Copy link
Contributor

Inelegant but functional way to add a fallback provider in case the first JSONRpc fails. Currently only supports one other RPC, i.e. one preferred RPC and one fallback.

Not ready to be merged into main but can be forked and used in situations where this functionality is crucial.

@arimgibson
Copy link
Contributor Author

Tagging #22 to add this to the issue history... didn't realize that adding it to the title wouldn't reference it

@@ -189,6 +189,15 @@ const essentialEth = new JsonRpcProvider(
const essentialEth = new JsonRpcProvider();
```

If you want to use a fallback provider in case your main provider fails:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 Awesome documentation!

@@ -19,6 +20,7 @@ export {
isAddress,
jsonRpcProvider,
JsonRpcProvider,
FallbackProvider,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect exporting ✅

Copy link
Owner

@dawsbot dawsbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great first work!

/**
* Helper function to avoid "new"
*/
export function jsonRpcProvider(rpcUrl?: string) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This export seems to be left-over from copying this file in. We won't need this here, let's just export one class

returnTransactionObjects = false,
): Promise<Block> {
const firstCall = new JsonRpcProvider(this._rpcUrls[0]);
return firstCall.getBlock('latest').catch((e) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good first-solution, let's make this dynamic for any quantity of RPC url's before merging

_rpcUrls: Array<string>;
constructor(rpcUrls?: Array<string>) {
this._rpcUrls = rpcUrls || [
'https://free-eth-junk.com/api/eth',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall we did this for initial testing, but I think before going live with this we'll probably want this to be just the one valid RPC endpoint

import { Network } from '../types/Network.types';
import { JsonRpcProvider } from './JsonRpcProvider';
import chainsInfo from './utils/chains-info';
export class FallbackProvider {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extend from a shared class here before merging this.

Here's an example of how Ethers.js does this: https://github.com/ethers-io/ethers.js/blob/73a46efea32c3f9a4833ed77896a216e3d3752a0/packages/providers/src.ts/fallback-provider.ts#L403

And they extend this BaseProvider

/**
* The URL to your Eth node. Consider POKT or Infura
*/
_rpcUrls: Array<string>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure this is private and readonly which we'll do soon to the JsonRpcProvider here: #42

@arimgibson
Copy link
Contributor Author

Closing this PR in favor of #44 actually, sorry for not getting to your comments!

@arimgibson arimgibson closed this Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants