Skip to content

Commit

Permalink
Merge pull request #1334 from denbite/rpc_failover
Browse files Browse the repository at this point in the history
Introduce `FailoverRpcProvider` that switches between providers in case of a failure of one of them
  • Loading branch information
vikinatora committed Apr 10, 2024
2 parents 57f7d0d + 6ad04ef commit 47e17f4
Show file tree
Hide file tree
Showing 12 changed files with 737 additions and 155 deletions.
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

0 comments on commit 47e17f4

Please sign in to comment.