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.
//! Example of setting up a provider using the `.connect` method.
use alloy::providers::{Provider, ProviderBuilder};
use std::error::Error;
const RPC_URL: &str = "https://reth-ethereum.ithaca.xyz/rpc";
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Instanties a provider using a string.
let provider = ProviderBuilder::new().connect(RPC_URL).await?;
Ok(())
}
In order to instantiate a provider in synchronous settings use connect_http
.