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

Introduce FailoverRpcProvider that switches between providers in case of a failure of one of them #1334

Merged
merged 9 commits into from
Apr 10, 2024

Conversation

denbite
Copy link
Contributor

@denbite denbite commented Apr 8, 2024

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • Commit messages follow the conventional commits spec
  • If this is a code change: I have written unit tests.
  • If this changes code in a published package: I have run pnpm changeset to create a changeset JSON document appropriate for this change.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

RPC providers can experience intermittent downtime, connectivity issues, or rate limits that cause client transactions to fail. This could have been prevented if near-api-js had the FailoverRpcProvider that supports multiple RPC providers.

const jsonProviders = [
  new JsonRpcProvider({
    url: 'https://rpc.mainnet.near.org',
  }),
  new JsonRpcProvider(
    {
      url: 'https://another-rpc.cloud.com',
      headers: { 'X-Api-Key': 'some string' },
    },
    { retries: 3, backoff: 2, wait: 500 }
  ),
];
const provider = new FailoverRpcProvider(jsonProviders);

await connect({
  networkId: 'mainnet',
  provider: provider,
  // this isn't used if `provider` is specified, but is still required for backward compativility
  nodeUrl: 'https://rpc.mainnet.near.org',
});

Test Plan

Related issues/PRs

#1216
#760

Discussion #733

Copy link

changeset-bot bot commented Apr 8, 2024

🦋 Changeset detected

Latest commit: 6ad04ef

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@near-js/accounts Minor
@near-js/providers Minor
@near-js/wallet-account Minor
@near-js/cookbook Patch
near-api-js Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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