Alloy Rust Library

repository·main·Indexed 23 days ago

https://github.com/alloy-rs/alloy

A high-performance, modular Rust library for connecting applications to Ethereum-based blockchains. A ground-up rewrite of ethers-rs, alloy provides a suite of crates including alloy-contract for on-chain interactions via the sol! macro, alloy-provider for blockchain interfacing, alloy-networks for RPC abstractions, and specialized support for ENS, JSON-RPC 2.0, and various Ethereum Improvement Proposals (EIPs).

Tokens
119.4K
Snippets
175
Records
688
Agent score
79%

What's inside alloy

  1. Overview of Alloy Crates

    main

    Alloy is modular and composed of several specialized crates:

    • alloy: Meta-crate for the entire project.
    • alloy-consensus: Ethereum consensus interface.
    • alloy-contract: Interact with on-chain contracts.
    • alloy-ens: Ethereum Name Service (ENS) utilities.
    • alloy-json-rpc: Core data types for JSON-RPC 2.0 clients.
    • alloy-network: Network abstraction for RPC types.
    • alloy-provider: Interface with an Ethereum blockchain.
    • alloy-signer: Ethereum signer abstraction (includes implementations for AWS KMS, GCP KMS, Ledger, Trezor, Turnkey, and local keys).
    • alloy-transport: Low-level JSON-RPC transport (HTTP, IPC, WS).
    • alloy-rpc-types: Meta-crate for all Ethereum JSON-RPC types (includes namespaces like eth, debug, trace, anvil, etc.).
  2. Overview of alloy-consensus

    main

    The alloy-consensus crate provides the Ethereum consensus interface. It contains the constants, types, and functions required to implement Ethereum Execution Layer (EL) consensus and communication.

    This crate is the primary location for types that are committed to in the EL block header, including:

    • Transactions
    • Blocks
    • Headers
    • Receipts
    • [EIP-2718] envelopes
    • [EIP-2930] support
    • [EIP-4844] support
  3. Use alloy-rpc-types for Ethereum JSON-RPC types

    main
    The alloy-rpc-types crate serves as a meta-crate that aggregates all Ethereum JSON-RPC type definitions used across the Alloy ecosystem. Use this crate when you need access to the standardized data structures and types that define Ethereum JSON-RPC requests and responses.
  4. Available local signer implementations in alloy-signer-local

    main

    The alloy-signer-local crate provides several ways to implement local signing for Ethereum-compatible operations:

    • PrivateKeySigner: A default signer using the k256 pure Rust implementation.
    • Secp256k1Signer: An alternative signer using the secp256k1 crate (requires the secp256k1 feature). This uses Rust bindings to the optimized C libsecp256k1 library.
    • MnemonicBuilder: Used to build PrivateKeySigners from BIP-39 mnemonic phrases (requires the mnemonic feature).
    • YubiSigner: Provides support for YubiHSM2 hardware security modules (requires the yubihsm feature).
  5. Use alloy-transport for low-level JSON-RPC management

    main

    The alloy-transport crate provides a low-level abstraction for Ethereum JSON-RPC connections and request management. It implements the tower::Service interface and builds an RpcClient to handle simple and batch RPC requests.

    Note: For most use cases, you should use higher-level crates instead:

    • Use alloy-provider for a high-level API interacting with standard Ethereum RPC endpoints.
    • Use alloy-rpc-client for a low-level JSON-RPC API that does not include specific Ethereum endpoints.

    Key features include:

    • Unified TransportError type.
    • Support for simple and batch RPC requests via futures.
    • Integration with the tower::Service abstraction.
  6. Use alloy-rpc-types-anvil for Anvil JSON-RPC types

    main
    The alloy-rpc-types-anvil crate provides the Rust type definitions for the custom Ethereum JSON-RPC namespace used by Anvil, the development node from the Foundry toolkit. Use these types when you need to interact with Anvil-specific RPC methods that are not part of the standard Ethereum JSON-RPC specification.
  7. Interact with on-chain contracts using alloy-contract

    main

    The alloy-contract crate provides the CallBuilder type, which is used to construct, encode, and decode data for on-chain contract calls. It allows you to either perform read-only calls (using .call()) or broadcast calls as transactions (using .send()).

    To simplify interaction, use the sol! macro with the #[sol(rpc)] attribute. This attribute automatically generates a contract struct and methods for every function defined in the contract, where each method returns a CallBuilder configured for that specific function.