Lesson 4 — Provider — connecting to a node
Question
Three lessons in, you can read Rust + Alloy types. Next: talking to an Ethereum node — Provider is the interface. HTTP / WebSocket / IPC / Anvil-fork all use the same trait and the same methods.
Principle (minimum model)
Providertrait. The RPC-client abstraction. Verbs:get_block_number/get_balance/call/send_transactionetc.ProviderBuilder::new().connect_http(url). Creates an HTTP provider.url.parse()?produces aUrltype.- Public RPC endpoints.
https://ethereum.reth.rs/rpc(official, free) /https://eth.llamarpc.com(llamarpc) / Alchemy / Infura (API key needed). - async methods.
provider.get_block_number().await?= a network round trip; runs on a Tokio runtime. #[tokio::main]. Makesmain()async, sets up the runtime, blocks until done. Beginner boilerplate.
Worked example + steps
Provider — connecting to a node
A Provider is your gateway to a node. Block numbers, balances, transactions — everything goes through it.
Minimal example — verbatim
This is a minimal project built around the alloy-rs/examples http.rs example.
First Cargo.toml:
[package]
name = "hello_provider"
version = "0.1.0"
edition = "2024"
[dependencies]
alloy = "2.0.5"
alloy-provider = "2.0.5"
eyre = "0.6.12"
tokio = { version = "1", features = ["full"] }
Then src/main.rs:
use alloy::providers::{Provider, ProviderBuilder};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
let rpc_url = "https://ethereum.reth.rs/rpc".parse()?;
let provider = ProviderBuilder::new().connect_http(rpc_url);
let latest_block = provider.get_block_number().await?;
println!("Latest block number: {}", latest_block);
Ok(())
}
cargo run and you should see the current mainnet block number. This is the seed of every monitoring bot you'll ever write.
Note the URL: https://ethereum.reth.rs/rpc is the public RPC endpoint operated by the Reth project — your code is literally talking to a Reth node. You're already inside the stack.
Common Provider methods
| Method | Role |
|---|---|
get_block_number | Latest block number |
get_balance(address) | ETH balance |
get_block(...) | Full block data |
get_transaction_by_hash(hash) | Transaction details |
get_logs(filter) | Event logs |
Pointing at any EVM chain
Just swap the URL. HyperEVM, Optimism, your local Anvil — same code:
let provider = ProviderBuilder::new()
.connect_http("https://api.hyperliquid.xyz/evm".parse()?); // HyperEVM
let provider = ProviderBuilder::new()
.connect_http("http://127.0.0.1:8545".parse()?); // Anvil
Local dev with Anvil
Anvil (part of Foundry) runs a local Ethereum node. No real money, no rate limits — perfect for learning.
anvil
What's next
You can now read state. The next module dives into how the EVM actually executes — stack, memory, opcodes — which prepares you for Revm.
Summary (3 lines)
Provider= the RPC-client abstraction. HTTP / WebSocket / IPC / Anvil-fork all use the same trait and same methods.ProviderBuilder::new().connect_http(url)creates one. Public RPCs includeethereum.reth.rs/rpc. async methods do network round trips.#[tokio::main]makes main async. Next: a balance-checker quiz combining everything so far.