ethers-rs

repository·master·Indexed 25 days ago

https://github.com/gakonst/ethers-rs

A complete Rust library for interacting with Ethereum and Celo. It provides tools for smart contract interaction, deployment, event monitoring, and JSON-RPC client capabilities. The library includes crates for type-safe contract bindings (ethers-contract), fundamental data types and cryptography (ethers-core), and a layered middleware architecture (ethers-middleware) for managing signers, nonces, and gas estimation.

Tokens
91.2K
Snippets
192
Records
518
Agent score
75%

What's inside ethers-rs

  1. Overview of ethers-contract

    master

    The ethers-contract crate provides type-safe abstractions for interacting with Ethereum smart contracts. Instead of manually constructing ethers_core::types::TransactionRequest objects with hand-crafted data fields (containing function selectors and encoded arguments), you can use high-level abstractions to manage contract interactions.

    Note: This library is currently in the process of being deprecated. Refer to issue #2667 for details.

  2. Overview of the ethers-contracts module

    master

    The ethers-contracts module is the primary interface for interacting with Ethereum smart contracts in Rust. It provides a robust API for managing the entire lifecycle of a contract, including generation, compilation, deployment, and interaction.

    Key capabilities include:

    • Code Generation: Using Abigen to create Rust bindings from Solidity.
    • Compilation: Compiling Solidity source code into bytecode and ABI files.
    • Deployment: Deploying contracts to various networks (including Anvil and Moonbeam) or via raw ABI and bytecode.
    • Interaction: Calling smart contract methods and listening to contract events (with or without metadata).
  3. Overview of ethers-core

    master

    The ethers-core crate provides fundamental Ethereum data types, cryptography, and utilities. It includes type definitions for Ethereum's main datatypes and tools for interacting with the Ethereum ecosystem.

    Note on Deprecation: This library is currently in the process of being deprecated.

    Import Recommendation: To simplify imports, it is recommended to use the utils, types, and abi re-exports directly instead of the core module.

  4. Use ethers-contract-abigen for type-safe smart contract bindings

    master
    The ethers-contract-abigen crate provides a code generation tool to create type-safe Rust bindings for Ethereum smart contracts. It is adapted from the original ethcontract-rs repository by Gnosis. This allows you to interact with smart contracts using native Rust types instead of manually constructing ABI-encoded calls.
  5. Deploy smart contracts

    master

    The ethers-contracts module supports multiple deployment workflows:

    • Via ABI and Bytecode: Deploying using existing compiled artifacts.
    • Network Specific: Specialized modules for deploying to the Anvil network or the Moonbeam network.
    • General Deployment: Creating contract instances on a target network.
  6. Use `CallBuilder` to create complex `eth_call` requests

    master

    The CallBuilder is an enum returned by provider.call_raw(&tx) that allows you to override parameters for the eth_call RPC method. It implements the RawCall trait, which provides methods to customize the execution environment of the call.

    By default, calling .await on a CallBuilder behaves identically to provider.call(). However, you can use RawCall methods to:

    • Specify a specific block number for the call to execute on using .block(block_id).
    • Provide a state override set using .state(state) to simulate specific account states (balances, nonces, storage, etc.).
  7. Resolve Ethereum Name Service (ENS) names

    master

    The Provider can resolve ENS names to addresses, lookup names from addresses, and resolve specific ENS fields or avatars. The default ENS address is 0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e. You can override this using the .ens() method on the provider.

    # use ethers_providers::{Provider, Http, Middleware};
    # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
    let provider = Provider::<Http>::try_from("https://eth.llamarpc.com")?;
    
    // Resolve ENS name to Address
    let name = "vitalik.eth";
    let address = provider.resolve_name(name).await?;
    
    // Lookup ENS name given Address
    let resolved_name = provider.lookup_address(address).await?;
    assert_eq!(name, resolved_name);
    
    /// Lookup ENS field
    let url = "https://vitalik.ca".to_string();
    let resolved_url = provider.resolve_field(name, "url").await?;
    assert_eq!(url, resolved_url);
    
    /// Lookup and resolve ENS avatar
    let avatar = "https://ipfs.io/ipfs/QmSP4nq9fnN9dAiCj42ug9Wa79rqmQerZXZch82VqpiH7U/image.gif".to_string();
    let resolved_avatar = provider.resolve_avatar(name).await?;
    assert_eq!(avatar, resolved_avatar.to_string());
    # Ok(())
    # }
  8. How events, EventWatcher, and SubscriptionStream work together

    master

    Working with smart contract events in ethers-rs involves three main abstractions:

    1. Event: A struct representing a specific event emitted by a smart contract. You create this by calling .event::<T>() on a Contract instance, where T is a struct implementing the EthEvent trait.
    2. EventWatcher: A configuration object used to define the scope of event monitoring. You create it by calling .watcher() on an Event. It allows you to set filters like block ranges.
    3. SubscriptionStream: A real-time stream of events. You obtain this by calling .subscribe().await on an EventWatcher. This stream can be iterated over to receive updates as they occur on the blockchain.