Setting up a Provider

A Provider is an abstraction of a connection to the Ethereum network, providing a concise, consistent interface to standard Ethereum node functionality.

Builder

The correct way of creating a Provider is through the ProviderBuilder, a builder.

Alloy provides concrete transport implementations for HTTP, WS (WebSockets) and IPC (Inter-Process Communication), as well as higher level transports which wrap a single or multiple transports.

//! Example of using the HTTP provider using the `on_http` method.

use alloy::providers::{Provider, ProviderBuilder};
use eyre::Result;

#[tokio::main]
async fn main() -> eyre::Result<()> {
    // Set up the HTTP transport which is consumed by the RPC client.
    let rpc_url = "https://eth.merkle.io".parse()?;

    // Create a provider with the HTTP transport using the `reqwest` crate.
    let provider = ProviderBuilder::new().on_http(rpc_url);

    Ok(())
}

Next, lets look at the HTTP Provider.