web3_closest_provider

Crates.ioweb3_closest_provider
lib.rsweb3_closest_provider
version1.0.0
sourcesrc
created_at2024-02-26 12:12:34.939055
updated_at2024-02-26 12:12:34.939055
descriptionA library for dynamically selecting the fastest Web3 provider based on response time.
homepage
repositoryhttps://github.com/samuelsramko/rust-web3-closest-provider
max_upload_size
id1153493
size26,874
SamOnChain (samuelsramko)

documentation

README

Get the closest Web3 RPC provider in Rust

Rust CI

Introduction

Tired of manually switching between Web3 RPC providers to find the fastest one? This library takes the hassle out of it by automatically load balancing between your chosen providers based on their response times. Simply provide a list of potential providers, and the library will dynamically select the fastest option for your local setup, ensuring optimal performance for your Web3 applications.

Key Features

  • Automatic Response Time Checks: The library periodically checks the response times of each provider in your list, keeping your selection up-to-date.
  • Dynamic Selection: Based on the latest response times, the library seamlessly chooses the fastest provider, ensuring you're always using the best option.
  • Easy Integration: Integrate this library into your Web3 applications quickly and effortlessly using its straightforward API.
  • Customizable Interval: Adjust the frequency of response time checks to fit your specific needs and network conditions.
  • Clear Communication: The library logs information about selected providers and encountered errors, keeping you informed.

Installation

Using Cargo:

  1. Add web3_closest_provider = "1.0.0" to your Cargo.toml dependencies.
  2. Run cargo install web3_closest_provider.

Usage

  1. Import the library:
use web3_closest_provider::{ClosestWeb3Provider, ClosestWeb3RpcProviderSelector};
  1. Create a list of potential providers:
let providers = vec![
    "[https://mainnet.infura.io/v3/your_api_key](https://mainnet.infura.io/v3/your_api_key)".to_string(),
    "[https://rpc.ankr.com/eth](https://rpc.ankr.com/eth)".to_string(),
    "[https://api.mycryptoapi.com/v1/eth](https://api.mycryptoapi.com/v1/eth)".to_string(),
];
  1. Initialize the load balancer:
let balancer = ClosestWeb3RpcProviderSelector::init(providers.clone(), std::time::Duration::from_secs(10));
  1. Check if the balancer is ready:
if balancer.is_ready() {
    println!("Balancer is ready to use!");
} else {
    balancer.wait_until_ready().await;
    println!("Balancer is ready to use!");
}
  1. Get the URL of the fastest provider:
let fastest_provider = balancer.get_fastest_provider();
println!("Fastest provider: {}", fastest_provider);

// ... use the fastest provider for your Web3 operations ...
  1. Crucially, remember to always destroy the balancer when you're done:
balancer.destroy(); // **This step is essential!**

Customization

  • You can change the interval_duration to adjust the frequency of response time checks.
  • You can implement specific checks or logic for provider selection beyond response time.

Example Usage

use web3_closest_provider::{ClosestWeb3Provider, ClosestWeb3RpcProviderSelector};
use std::time::Duration;

#[tokio::main]
async fn main() {
    let providers = vec![
        "[https://mainnet.infura.io/v3/your_api_key](https://mainnet.infura.io/v3/your_api_key)".to_string(),
        "[https://rpc.ankr.com/eth](https://rpc.ankr.com/eth)".to_string(),
        "[https://api.mycryptoapi.com/v1/eth](https://api.mycryptoapi.com/v1/eth)".to_string(),
    ];

    let balancer = ClosestWeb3RpcProviderSelector::init(providers.clone(), Duration::from_secs(10));

    balancer.wait_until_ready().await;
    let fastest_provider = balancer.get_fastest_provider();
    println!("Using fastest provider: {}", fastest_provider);

    // ... use the fastest provider for your Web3 operations ...

    // **Remember to destroy the balancer!**
    balancer.destroy();
}

Contribution

We welcome contributions! Please refer to the CONTRIBUTING.md file for guidelines.

License

This library is licensed under the MIT License. See the LICENSE file for details.

Commit count: 0

cargo fmt