Crates.io | web3_closest_provider |
lib.rs | web3_closest_provider |
version | 1.0.0 |
source | src |
created_at | 2024-02-26 12:12:34.939055 |
updated_at | 2024-02-26 12:12:34.939055 |
description | A library for dynamically selecting the fastest Web3 provider based on response time. |
homepage | |
repository | https://github.com/samuelsramko/rust-web3-closest-provider |
max_upload_size | |
id | 1153493 |
size | 26,874 |
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.
Using Cargo:
web3_closest_provider = "1.0.0"
to your Cargo.toml
dependencies.cargo install web3_closest_provider
.use web3_closest_provider::{ClosestWeb3Provider, ClosestWeb3RpcProviderSelector};
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(), std::time::Duration::from_secs(10));
if balancer.is_ready() {
println!("Balancer is ready to use!");
} else {
balancer.wait_until_ready().await;
println!("Balancer is ready to use!");
}
let fastest_provider = balancer.get_fastest_provider();
println!("Fastest provider: {}", fastest_provider);
// ... use the fastest provider for your Web3 operations ...
balancer.destroy(); // **This step is essential!**
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();
}
We welcome contributions! Please refer to the CONTRIBUTING.md file for guidelines.
This library is licensed under the MIT License. See the LICENSE file for details.