Transports
The @ponder/utils
package exports two new Viem Transports, loadBalance
and rateLimit
.
These Transports are useful for managing the RPC request workloads that Ponder apps commonly encounter.
Load balance
The loadBalance
Transport distributes requests across a list of inner Transports in a simple round-robin scheme.
Usage
import { loadBalance } from "@ponder/utils";
import { createPublicClient, fallback, http, webSocket } from "viem";
import { mainnet } from "viem/chains";
const client = createPublicClient({
chain: mainnet,
transport: loadBalance([
http("https://cloudflare-eth.com"),
http("https://eth-mainnet.public.blastapi.io"),
webSocket("wss://ethereum-rpc.publicnode.com"),
rateLimit(http("https://rpc.ankr.com/eth"), { requestsPerSecond: 5 }),
]),
});
Usage in Ponder
For convenience, @ponder/core
re-exports loadBalance
from @ponder/utils
.
import { createConfig, loadBalance } from "@ponder/core";
import { http } from "viem";
export default createConfig({
networks: {
mainnet: {
chainId: 1,
transport: loadBalance([
http("https://cloudflare-eth.com"),
http("https://eth-mainnet.public.blastapi.io"),
webSocket("wss://ethereum-rpc.publicnode.com"),
rateLimit(http("https://rpc.ankr.com/eth"), { requestsPerSecond: 5 }),
]),
},
},
contracts: {
// ...
},
});
Parameters
Transports
Type: Transport[]
(required)
A list of Transports to load balance requests across.
Rate limit
The rateLimit
Transport limits the number of requests per second submitted to an inner Transport using a first-in-first-out queue.
Usage
import { rateLimit } from "@ponder/utils";
import { createPublicClient, fallback, http } from "viem";
import { mainnet } from "viem/chains";
const client = createPublicClient({
chain: mainnet,
transport: rateLimit(http("https://eth-mainnet.g.alchemy.com/v2/..."), {
requestsPerSecond: 25,
}),
});
Usage in Ponder
For convenience, @ponder/core
re-exports rateLimit
from @ponder/utils
.
import { createConfig, rateLimit } from "@ponder/core";
import { http } from "viem";
export default createConfig({
networks: {
mainnet: {
chainId: 1,
transport: rateLimit(http(process.env.PONDER_RPC_URL_1), {
requestsPerSecond: 25,
}),
},
},
contracts: {
// ...
},
});
Parameters
Transport
Type: Transport
(required)
An inner transport to rate limit.
requestsPerSecond
Type: number
(required)
browser
Type: boolean
(default: true
)
If false
, the internal queue will use the Node.js-specific process.nextTick()
API to schedule requests. This leads to more predictable behavior in Node.js, but is not available in the browser.