FABRKNT
Reth Fundamentals — Your First Steps with Alloy
Working with Alloy
Lesson 5 of 11·QUIZ15 min30 XP

Treat this page as a workbench, not a blog post. The goal is to extract a reusable mental model from the source and carry it into the rest of the Fabrknt stack.

Course
Reth Fundamentals — Your First Steps with Alloy
Lesson role
QUIZ
Sequence
5 / 11

Quiz — Balance Checker

Question

Write a function that returns true if a given address has zero ETH balance. Practical quiz combining Provider + get_balance + .await? + Option/Result in one function. Verify against Vitalik's address.

Principle (minimum model)

  • Function signature. async fn is_empty_wallet(provider: &impl Provider, address: Address) -> eyre::Result<bool>.
  • &impl Provider. Accepts any Provider implementation → trait-bound polymorphism. Works with HTTP / WebSocket / Anvil-fork alike.
  • get_balance(address).await?. Wait for the Future + propagate errors; extract the U256 balance.
  • .is_zero(). Idiomatic zero-check on U256. balance == 0u64 is a type mismatch and won't compile.
  • Wei units. get_balance returns wei as U256. ETH = 10¹⁸ wei. format_ether for display. Never use f64 for money — precision loss.

Worked example + steps

Quiz: balance checker

Goal: write a function that returns true when a given address has zero ETH balance, false otherwise.

What you'll need

Two pieces from Alloy you've already met:

  • Provider — created via ProviderBuilder::new().connect_http(url) (Provider lesson)
  • get_balance(address) — async method that returns the balance

Plus one bit from the previous Rust lesson: the ? operator for error propagation on async calls (x.await?).

Try it yourself

Create a new project locally (Rust Playground has no Alloy):

cargo new balance-check && cd balance-check

In Cargo.toml:

[dependencies]
alloy = { version = "1.0", features = ["full"] }
tokio = { version = "1", features = ["full"] }
eyre = "0.6"

In src/main.rs, write a function with this signature:

async fn is_empty_wallet(
    provider: &impl Provider,
    address: Address,
) -> eyre::Result<bool> {
    // your code
}

And exercise it from main:

#[tokio::main]
async fn main() -> eyre::Result<()> {
    let provider = ProviderBuilder::new()
        .connect_http("https://ethereum.reth.rs/rpc".parse()?);

    let vitalik = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".parse::<Address>()?;
    println!("vitalik empty? {}", is_empty_wallet(&provider, vitalik).await?);

    Ok(())
}

Hints if you get stuck:

  • provider.get_balance(address) returns a Future — use .await? to extract the balance
  • The balance type is U256. There's an idiomatic method on it for the "is this zero?" check — find it in the Alloy docs
  • You return Ok(...) from a function that returns Result<...>

cargo run will hit the public Reth RPC and tell you whether Vitalik's wallet is empty (it isn't).

Quiz

Summary (3 lines)

  • async fn is_empty_wallet(&impl Provider, Address) -> eyre::Result<bool> + get_balance(addr).await?.is_zero() — one line.
  • impl Provider = trait-bound polymorphism. is_zero() is the idiomatic zero-check. Wei in U256, not f64.
  • Next module: Inside the EVM, starting with the stack-machine basics.