Skip to content

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.

The connect method on the ProviderBuilder will automatically determine the connection type (Http, Ws or Ipc) depending on the format of the URL.

//! Example of `.connect` to setup a simple provider
 
use alloy::providers::{Provider, ProviderBuilder};
use std::error::Error;
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Set up the RPC URL.
    let rpc_url = "https://reth-ethereum.ithaca.xyz/rpc";
 
    // Instanties a provider using a rpc_url string.
    let provider = ProviderBuilder::new().connect(rpc_url).await?;
 
    Ok(())
}

In order to instantiate a provider in synchronous settings use connect_http.