near / near-api-js

JavaScript library to interact with NEAR Protocol via RPC API
https://near.github.io/near-api-js
MIT License
388 stars 240 forks source link

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

Closed denbite closed 3 months ago

denbite commented 3 months ago

Pre-flight checklist

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

https://github.com/near/near-api-js/pull/1216 https://github.com/near/near-api-js/pull/760

Discussion https://github.com/near/near-api-js/issues/733

changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: 6ad04ef432571e010592ea83423b2c60df44f9c9

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