Skip to content
alloy · Rust Ethereum

Common Actions

read_contract.rs
//! Demonstrates reading a contract by fetching the WETH balance of an address.
use alloy::{primitives::address, providers::ProviderBuilder, sol};
use std::error::Error;
 
// Generate the contract bindings for the ERC20 interface.
sol! {
   // The `rpc` attribute enables contract interaction via the provider.
   #[sol(rpc)]
   contract ERC20 {
        function balanceOf(address owner) public view returns (uint256);
   }
}
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Initialize the provider.
    let provider = ProviderBuilder::new().connect("https://reth-ethereum.ithaca.xyz/rpc").await?;
 
    // Instantiate the contract instance.
    let weth = address!("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
    let erc20 = ERC20::new(weth, provider);
 
    // Fetch the balance of WETH for a given address.
    let owner = address!("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
    let balance = erc20.balanceOf(owner).call().await?;
 
    println!("WETH Balance of {owner}: {balance}");
 
    Ok(())
}

Used By the Best

RethFoundryRevmArbitrum StylusOptimism Kona

Features

Alloy supports the following out of the box for best in class developer experience

  • Fully Ethereum JSON-RPC compliant along with support for trace, debug and anvil endpoints
  • Seamless contract interactions using the sol! macro
  • Highly performant core primitives such as U256 Operations and ABI encoding
  • Override / Extend provider and transport behaviour using layers and fillers akin to tower layers
  • Node bindings for popular nodes such as Reth, Geth and Anvil for testing purposes

Community

Join the Community

Supporters

Collaborator
ParadigmIthaca