Crates.io | alloy-provider |
lib.rs | alloy-provider |
version | 1.0.27 |
created_at | 2023-12-18 08:42:21.231706+00 |
updated_at | 2025-08-26 16:38:24.486374+00 |
description | Interface with an Ethereum blockchain |
homepage | https://github.com/alloy-rs/alloy |
repository | https://github.com/alloy-rs/alloy |
max_upload_size | |
id | 1073195 |
size | 762,673 |
Interface with an Ethereum blockchain.
This crate contains the Provider
trait, which exposes Ethereum JSON-RPC
methods. Providers in alloy are similar to ethers.js
providers. They manage
an RpcClient
and allow other parts of the program to easily make RPC calls.
Unlike an ethers.js
Provider, an alloy Provider is network-aware. It is
parameterized with a Network
from alloy-networks
. This allows the Provider
to expose a consistent interface to the rest of the program, while adjusting
request and response types to match the underlying blockchain.
Providers can be composed via stacking. For example, a Provider
that tracks
the nonce for a given address can be stacked onto a Provider
that signs
transactions to create a Provider
that can send signed transactions with
correct nonces.
The ProviderBuilder
struct can quickly create a stacked provider, similar to
tower::ServiceBuilder
.
pubsub
- Enable support for subscription methods.ws
- Enable WebSocket support. Implicitly enables pubsub
.ipc
- Enable IPC support. Implicitly enables pubsub
.use alloy_provider::{ProviderBuilder, RootProvider, Provider};
use alloy_network::Ethereum;
use alloy_primitives::address;
use std::str::FromStr;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a basic HTTP provider
let provider = RootProvider::<Ethereum>::new_http("https://reth-ethereum.ithaca.xyz/rpc".parse()?);
// Get the latest block number
let block_number = provider.get_block_number().await?;
println!("Latest block number: {block_number}");
// Get balance of an address
let address = address!("0x71C7656EC7ab88b098defB751B7401B5f6d8976F");
let balance = provider.get_balance(address).await?;
println!("Balance: {balance}");
// Use the builder pattern to create a provider with recommended fillers
let provider = ProviderBuilder::new().connect_http("https://reth-ethereum.ithaca.xyz/rpc".parse()?);
Ok(())
}