Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelsramko committed Feb 26, 2024
0 parents commit dc71a5c
Show file tree
Hide file tree
Showing 8 changed files with 708 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/rust_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Rust CI

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Install cargo-make
run: cargo install cargo-make

- name: Build
run: cargo make build

- name: Test
run: cargo make test

- name: Generate documentation
run: cargo doc --no-deps --document-private-items

# - name: Publish documentation as GitHub page
# uses: peaceiris/actions-gh-pages@v3
# with:
# github_token: ${{ secrets.GITHUB_TOKEN }}
# publish_dir: ./target/doc

- name: Publish to crates.io
run: cargo publish
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb


# Added by cargo

/target
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'web3_closest_provider'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=web3_closest_provider"
],
"filter": {
"name": "web3_closest_provider",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
41 changes: 41 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Contributing to rust-web3-closest-provider

We appreciate your interest in contributing to the `rust-web3-closest-provider` project! We welcome contributions of all kinds, from bug fixes and documentation improvements to new features and enhancements.

## Getting Started

Here are some resources to help you get started:

- Project repository: [rust-web3-closest-provider](https://github.com/samuelsramko/rust-web3-closest-provider)
- Issue tracker: [GitHub Issues](https://github.com/samuelsramko/rust-web3-closest-provider/issues)
- Contributing guidelines: This document

## Code Style and Formatting

We follow the [Rust style guide](https://doc.rust-lang.org/nightly/edition-guide/rust-2018/style/). Please make sure your code adheres to these guidelines before submitting a pull request.

## Making Changes

1. **Fork the repository**: Create a fork of the `rust-web3-closest-provider` repository on GitHub.
2. **Create a branch**: Create a new branch for your feature or fix.
3. **Make your changes**: Make your changes to the codebase, ensuring they follow the style guide and are well-documented.
4. **Test your changes**: Run the tests to ensure your changes don't introduce any regressions.
5. **Commit your changes**: Commit your changes to your branch with a descriptive message.
6. **Push your changes**: Push your changes to your fork.
7. **Create a pull request**: Create a pull request to the main branch of the upstream repository.

## Pull Request Guidelines

- Include a clear description of your changes. This should explain what you changed, why you changed it, and how it addresses any issues or improves the project.
- Write well-formatted code. Your code should be easy to read and understand.
- Add unit tests for new code. This helps ensure that your changes don't break anything.
- Follow the existing code style. This makes your code look consistent with the rest of the project.

## Additional Notes

- We encourage you to submit your changes early and often. This allows us to provide feedback and help you merge your changes more quickly.
- We appreciate any contributions you make to the `rust-web3-closest-provider` project!

## License

This project is licensed under the MIT License. See the LICENSE file for details.
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "web3_closest_provider"
version = "1.0.0"
edition = "2021"
authors = ["Samuel Sramko <[email protected]>"]
repository = "https://github.com/samuelsramko/rust-web3-closest-provider"

description = "A library for dynamically selecting the fastest Web3 provider based on response time."
license = "MIT"
license-file = "LICENSE"

[dependencies]
reqwest = {version="0.11.24", features=["json"]}
serde = { version="1.0.196", features=["derive"]}
serde_json = "1.0.113"
tokio = { version = "1.36.0", features = ["full"] }
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 SamOnChain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Get the closest Web3 RPC provider in Rust

## 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:**

```rust
use web3_closest_provider::{ClosestWeb3Provider, ClosestWeb3RpcProviderSelector};
```

2. **Create a list of potential providers:**
```rust
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(),
];
```

3. **Initialize the load balancer:**
```rust
let balancer = ClosestWeb3RpcProviderSelector::init(providers.clone(), std::time::Duration::from_secs(10));
```

4. **Check if the balancer is ready:**
```rust
if balancer.is_ready() {
println!("Balancer is ready to use!");
} else {
balancer.wait_until_ready().await;
println!("Balancer is ready to use!");
}
```

5. **Get the URL of the fastest provider:**
```rust
let fastest_provider = balancer.get_fastest_provider();
println!("Fastest provider: {}", fastest_provider);

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

6. **Crucially, remember to always destroy the balancer when you're done:**
```rust
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
```rust
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](./CONTRIBUTING.md) file for guidelines.

## License
This library is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
Loading

0 comments on commit dc71a5c

Please sign in to comment.