Light wrapper around Connection
with fallback to other RPCs when something goes wrong, load-balancing strategies and custom method routing.
yarn add solana-advanced-connection
or
npm install solana-advanced-connection
Interface is identical to Connection
except constructor
import AdvancedConnectiotn from "solana-advanced-connection";
const rpc1 = "https://api.mainnet-beta.solana.com";
const rpc2 = "https://solana-api.projectserum.com";
const advCon = new AdvancedConnectiotn([rpc1, rpc2]);
advCon.getBalance(/* ... */);
Comes with 3 strategies for your choosing:
sequential
: try each RPC in sequence for each call, will always start from first, then second, etc. (default)round-robin
: round robin over all RPCs, across callsrandom
: randomly pick RPCs
import AdvancedConnection from "solana-advanced-connection";
const rpc1 = "https://api.mainnet-beta.solana.com";
const rpc2 = "https://solana-api.projectserum.com";
const advCon = new AdvancedConnection([rpc1, rpc2], {strategy: "random"});
advCon.getBalance(/* ... */);
Specify the routes
key in config to override the routing for certain functions.
The following example will route getBalance
to the Serum RPC, but allows fallback to the strategy if it fails.
Useful for when you want critical calls to always go to mainnet-beta for example.
import AdvancedConnection from "solana-advanced-connection";
const rpc1 = "https://api.mainnet-beta.solana.com";
const rpc2 = "https://solana-api.projectserum.com";
const advCon = new AdvancedConnection([rpc1, rpc2], {strategy: "random", routes: [
{ allowFallback: true, method: "getBalance", endpoint: "https://solana-api.projectserum.com" }
]});
advCon.getBalance(/* ... */);
MIT, but please consider contributing back if you make improvements ❤️