Skip to content
Logo

RPC Provider

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

Provider Builder Pattern

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.

The examples use environment variables for endpoints so credentials are not committed to source control. Set RPC_URL to an HTTP endpoint or WS_URL to a WebSocket endpoint before running them.

//! Example of setting up a provider using the `.connect` method.
 
use alloy::providers::{Provider, ProviderBuilder}; 
use std::error::Error;
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Instantiate a provider using a caller-supplied endpoint.
    let rpc_url = std::env::var("RPC_URL")?; 
    let provider = ProviderBuilder::new().connect(&rpc_url).await?; 
 
    Ok(())
}

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