
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
ethers-dynamic-provider
Advanced tools
ethers.js provider that supports multiple RPC endpoints with various routing strategies
Drop-in replacement for ethers.js JsonRpcProvider
with built-in support for balancing between multiple RPC endpoints.
npm install ethers-dynamic-provider
# or
yarn add ethers-dynamic-provider
# or
pnpm add ethers-dynamic-provider
DynamicProvider
is a powerful replacement for JsonRpcProvider
with the same API. This means that you can simply replace the initialization of the provider and all the rest of the code will work as before, but with added benefits such as:
import { DynamicProvider, FallbackStrategy } from 'ethers-dynamic-provider'
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"], // add another RPC as fallback
{
strategy: new FallbackStrategy(),
}
)
// Use like a normal ethers.js provider
const block = await provider.getBlockNumber()
Multicall functionality is provided by the ethers-multicall-provider and can be enabled if needed.
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"],
{
strategy: new RandomStrategy(),
multicall: true,
// or with addition settings
multicall: {
cache: true,
maxDataLength: 100_000,
},
}
);
When the RPC returns an error it is put into "jail" and can no longer be selected for some time. By default it is 10 seconds, but you can adjust this time (or disable it by setting 0)
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"],
{
strategy: new RandomStrategy(),
jailDuration: 5_000, // 5 seconds
}
);
You can use different strategies or RPCs for read and write operations. Read operations are used when reading data from contracts, while write operations are used when sending transactions.
For example, I want to send transactions always to the same RPC:
import {
DynamicProvider,
RandomStrategy,
FallbackStrategy,
} from "ethers-dynamic-provider";
const provider = new DynamicProvider(
{
read: ["https://rpc1.example.com", "https://rpc2.example.com"],
write: ["https://rpc3.example.com"],
},
{
strategy: {
read: new RandomStrategy(),
write: new FallbackStrategy(),
},
}
);
Uses a primary RPC endpoint and only switches to the next one when the current RPC fails
import { DynamicProvider, FallbackStrategy } from "ethers-dynamic-provider";
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"],
{
strategy: new FallbackStrategy(),
}
);
Randomly selects an RPC endpoint from the available list
import { DynamicProvider, RandomStrategy } from "ethers-dynamic-provider";
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"],
{
strategy: new RandomStrategy(),
}
);
Uses RPC endpoints in sequence, switching to the next one when number of requests to current RPC reaches requestsPerRpc
limit
import { DynamicProvider, SequentialStrategy } from "ethers-dynamic-provider";
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"],
{
strategy: new SequentialStrategy({
requestsPerRpc: 10, // default 5
}),
}
);
Selects the RPC endpoint with the highest block number. The block number is synchronized at the first request, and then every syncInterval
milliseconds
import { DynamicProvider, HighestBlockStrategy } from "ethers-dynamic-provider";
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"],
{
strategy: new HighestBlockStrategy({
syncInterval: 5_000, // default 10_000
}),
}
);
Sends requests to all RPCs simultaneously and returns the first successful response
import { DynamicProvider, FastestStrategy } from "ethers-dynamic-provider";
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"],
{
strategy: new FastestStrategy(),
}
);
Analyzes response time of each RPC and selects the fastest one based on average response time of the last historyDepth
requests
import { DynamicProvider, DynamicStrategy } from "ethers-dynamic-provider";
const provider = new DynamicProvider(
["https://rpc1.example.com", "https://rpc2.example.com"],
{
strategy: new DynamicStrategy({
historyDepth: 10, // default 5
}),
}
);
FAQs
ethers.js provider that supports multiple RPC endpoints with various routing strategies
We found that ethers-dynamic-provider demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.