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
7 changes: 7 additions & 0 deletions .changeset/shiny-coats-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@near-js/accounts": minor
"@near-js/providers": minor
"@near-js/wallet-account": minor
---

Introduce FailoverRpcProvider that switches between providers in case of a failure of one of them
6 changes: 5 additions & 1 deletion packages/accounts/src/connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Signer, InMemorySigner } from '@near-js/signers';
import { Provider, JsonRpcProvider } from '@near-js/providers';
import { Provider, JsonRpcProvider, FailoverRpcProvider } from '@near-js/providers';
import { IntoConnection } from './interface';

/**
Expand All @@ -11,6 +11,10 @@ function getProvider(config: any): Provider {
case undefined:
return config;
case 'JsonRpcProvider': return new JsonRpcProvider({ ...config.args });
case 'FailoverRpcProvider': {
const providers = (config?.args || []).map((arg) => new JsonRpcProvider(arg));
return new FailoverRpcProvider(providers);
}
default: throw new Error(`Unknown provider type ${config.type}`);
}
}
Expand Down
27 changes: 27 additions & 0 deletions packages/cookbook/api-keys/backup-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// demonstrates how to use multiple providers with different API-KEYs
const { providers } = require('near-api-js');

const RPC_API_ENDPOINT_1 = '<Replace this string with your RPC server URL>';
const API_KEY_1 = '<Replace this string with your API KEY>';

const RPC_API_ENDPOINT_2 = '<Replace this string with another RPC server URL>';
const API_KEY_2 = '<Replace this string with another API KEY>';

const jsonProviders = [
new providers.JsonRpcProvider({
url: RPC_API_ENDPOINT_1,
headers: { 'x-api-key': API_KEY_1 },
}),
new providers.JsonRpcProvider({
url: RPC_API_ENDPOINT_2,
headers: { 'x-api-key': API_KEY_2 },
}),
];
const provider = new providers.FailoverRpcProvider(jsonProviders);

getNetworkStatus();

async function getNetworkStatus() {
const result = await provider.status();
console.log(result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { FailoverRpcProvider } from '@near-js/providers';
2 changes: 2 additions & 0 deletions packages/near-api-js/src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import { Provider, FinalExecutionOutcome, ExecutionOutcomeWithId, getTransactionLastResult, FinalExecutionStatus, FinalExecutionStatusBasic } from './provider';
import { JsonRpcProvider, TypedError, ErrorContext } from './json-rpc-provider';
import { FailoverRpcProvider } from './failover-rpc-provider';

export {
Provider,
FinalExecutionOutcome,
JsonRpcProvider,
FailoverRpcProvider,
ExecutionOutcomeWithId,
FinalExecutionStatus,
FinalExecutionStatusBasic,
Expand Down
Loading
Loading