ethers-rs

Connect to 256 Blocks using ethers-rs for Rust

ethers-rs is a complete Ethereum and Celo library for Rust. While new projects should consider using Alloy, ethers-rs remains a solid choice with comprehensive features and widespread adoption.

Installation

Add to your Cargo.toml:

[dependencies]
ethers = "2"
tokio = { version = "1", features = ["full"] }
eyre = "0.6"

Quick Start

Connect to 256 Blocks using the X-API-Key header:

use ethers::providers::{Http, Provider};
use eyre::Result;
use reqwest::{
    header::{HeaderMap, HeaderValue},
    Client,
};
use url::Url;
 
#[tokio::main]
async fn main() -> Result<()> {
    // Set the X-API-Key header
    let mut headers = HeaderMap::new();
    headers.insert("X-API-Key", HeaderValue::from_static("your-api-key"));
 
    // Create the reqwest client with custom headers
    let client = Client::builder().default_headers(headers).build()?;
 
    // Create the HTTP provider with custom client
    let url = Url::parse("https://rpc.256blocks.com/ethereum")?;
    let http = Http::new_with_client(url, client);
    let provider = Provider::new(http);
 
    // Get the latest block number
    let block_number = provider.get_block_number().await?;
    println!("Latest block: {}", block_number);
 
    Ok(())
}

Reading Account Balance

use ethers::{
    providers::{Http, Middleware, Provider},
    types::Address,
    utils::format_ether,
};
use eyre::Result;
use reqwest::{
    header::{HeaderMap, HeaderValue},
    Client,
};
use url::Url;
 
#[tokio::main]
async fn main() -> Result<()> {
    let mut headers = HeaderMap::new();
    headers.insert("X-API-Key", HeaderValue::from_static("your-api-key"));
 
    let client = Client::builder().default_headers(headers).build()?;
    let url = Url::parse("https://rpc.256blocks.com/ethereum")?;
    let http = Http::new_with_client(url, client);
    let provider = Provider::new(http);
 
    // Get account balance
    let address: Address = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb".parse()?;
    let balance = provider.get_balance(address, None).await?;
 
    println!("Balance: {} ETH", format_ether(balance));
 
    Ok(())
}

Reading Contract Data

use ethers::{
    contract::abigen,
    providers::{Http, Provider},
    types::Address,
};
use eyre::Result;
use reqwest::{
    header::{HeaderMap, HeaderValue},
    Client,
};
use std::sync::Arc;
use url::Url;
 
// Generate the ERC-20 contract bindings
abigen!(
    IERC20,
    r#"[
        function balanceOf(address account) external view returns (uint256)
        function decimals() external view returns (uint8)
        function symbol() external view returns (string)
    ]"#
);
 
#[tokio::main]
async fn main() -> Result<()> {
    let mut headers = HeaderMap::new();
    headers.insert("X-API-Key", HeaderValue::from_static("your-api-key"));
 
    let client = Client::builder().default_headers(headers).build()?;
    let url = Url::parse("https://rpc.256blocks.com/ethereum")?;
    let http = Http::new_with_client(url, client);
    let provider = Arc::new(Provider::new(http));
 
    // USDC contract on Ethereum
    let usdc_address: Address = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48".parse()?;
    let account: Address = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb".parse()?;
 
    // Create contract instance
    let contract = IERC20::new(usdc_address, provider);
 
    // Call contract methods
    let balance = contract.balance_of(account).call().await?;
    let decimals = contract.decimals().call().await?;
    let symbol = contract.symbol().call().await?;
 
    println!("{} Balance: {}", symbol, balance);
    println!("Decimals: {}", decimals);
 
    Ok(())
}

Multi-Chain Support

Connect to different chains by changing the endpoint:

use ethers::providers::{Http, Provider};
use eyre::Result;
use reqwest::{
    header::{HeaderMap, HeaderValue},
    Client,
};
use url::Url;
 
fn create_provider(chain: &str, api_key: &str) -> Result<Provider<Http>> {
    let mut headers = HeaderMap::new();
    headers.insert("X-API-Key", HeaderValue::from_str(api_key)?);
 
    let client = Client::builder().default_headers(headers).build()?;
    let url = Url::parse(&format!("https://rpc.256blocks.com/{}", chain))?;
    let http = Http::new_with_client(url, client);
 
    Ok(Provider::new(http))
}
 
#[tokio::main]
async fn main() -> Result<()> {
    // Create providers for different chains
    let eth_provider = create_provider("ethereum", "your-api-key")?;
    let base_provider = create_provider("base", "your-api-key")?;
    let arb_provider = create_provider("arbitrum", "your-api-key")?;
    let polygon_provider = create_provider("polygon", "your-api-key")?;
 
    // Use providers...
    let eth_block = eth_provider.get_block_number().await?;
    println!("Ethereum block: {}", eth_block);
 
    Ok(())
}

Resources

ethers-rs | 256 Blocks