@0x4007/ubiquity-rpc-provider
UbiquityRpcProvider
is a TypeScript class designed to efficiently manage RPC (Remote Procedure Call) endpoints across various blockchain networks. It intelligently selects the fastest available RPC endpoint for a given chain, caches endpoints for performance optimization, and gracefully handles request failures by retrying with alternative endpoints.
Install the package using the bun
package manager:
bun add @0x4007/ubiquity-rpc-provider
import { UbiquityRpcProvider } from "@0x4007/ubiquity-rpc-provider";
// Initialize the UbiquityRpcProvider
const ubiquityRpcProvider = new UbiquityRpcProvider();
// Define your RPC request payload
const payload = {
method: "eth_blockNumber",
params: [],
};
// Send the RPC request
ubiquityRpcProvider
.sendRequest(1, payload)
.then((response) => {
console.log("Block Number:", response.result);
})
.catch((error) => {
console.error("Error:", error.message);
});
UbiquityRpcProvider
new UbiquityRpcProvider(chainData?: ChainData[])
chainData
(optional): An array of ChainData
objects representing different blockchain networks and their RPC endpoints. If not provided, it uses a default set of chains.sendRequest
sendRequest(chainId: number, payload: { method: string; params: unknown[] }): Promise<JsonRpcResponse>
Sends an RPC request to the fastest available endpoint for the specified chain ID.
chainId
: The ID of the blockchain network.payload
: An object containing the RPC method and parameters.If a request to the fastest RPC fails, the UbiquityRpcProvider
automatically tries alternative RPC endpoints for the same chain.
The UbiquityRpcProvider
performs latency checks to determine the fastest RPC endpoint for each chain. It uses a test payload to verify the endpoint's responsiveness and correctness.
The fastest RPC endpoints are cached to improve performance on subsequent requests. The cache is saved and loaded using environment-appropriate storage mechanisms.
Small hex string results (less than 10 characters) are automatically converted to numbers for convenience. Larger hex strings (like contract bytecode) are left as-is.
We welcome contributions! If you have suggestions or encounter issues, please open an issue or submit a pull request.
This project is licensed under the MIT License.