Keyrxng

Dynamic Web3 RPC Handler

Ubiquity · 2024 · Production-grade handler that ranks RPC endpoints by latency, rotates on error via a Proxy-wrapped provider, and ships with dual CJS/ESM + types.

So what?
0 reported during my tenure failures · <=1500 ms selection
Role
Product Engineer
Year
2024
Stack
TypeScript, Node.js, Ethers.js (JsonRpcProvider), esbuild, GitHub Actions
Read narrative
0 reported during my tenure failures<=1500 ms selection

Problem

The ecosystem needed a dynamic RPC selection system for reliable Web3 operations. Manual blacklisting caused brittleness and UI flicker. The handler had to automatically detect failing endpoints, rank healthy ones by latency, and switch seamlessly without developer or user intervention.

Approach

System diagram

flowchart LR
  App[Client Application] --> GetInstance[RPCHandler getInstance]
  GetInstance --> Race[Promise race RPC endpoints]
  Race --> Rank[Latency ranking]
  Rank --> Select[JsonRpcProvider selection]
  Select --> Proxy[Proxy wrapper]
  Proxy --> Rotate[Error handling with rotation]
  Rotate --> Response[Response to client]

Outcome

Proof

Code excerpt — provider proxy (from the implemented handler)

private _createProxy(provider: JsonRpcProvider): JsonRpcProvider {
  return new Proxy(provider, {
    get: (target, prop) => {
      if (typeof (target as any)[prop] === 'function') {
        return async (...args: unknown[]) => {
          try {
            return await (target as any)[prop].apply(target, args);
          } catch (error: any) {
            // Rotate to next fastest healthy RPC and retry
            const nextProvider = await this.getFastestRpcProvider();
            return await (nextProvider as any)[prop].apply(nextProvider, args);
          }
        };
      }
      return (target as any)[prop];
    }
  });
}

Example usage — selecting a fastest RPC for a given network (original article)

import { getFastestRpc } from "web3-rpc-speed-racer";

// Supply your Network ID
const rpc = await getFastestRpc(1);
// Use rpc for your web3 provider

Adoption

@ubiquity-dao/rpc-handler — weekly downloads Weekly npm downloads chart 74 450 825 2024-08-14 to 2024-08-20 2025-04-23 to 2025-04-29

References