Coder Social home page Coder Social logo

rust-web3-closest-provider's Introduction

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. or
cargo add 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.

rust-web3-closest-provider's People

Contributors

samuelsramko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.