Crate alloy

Crate alloy 

Source
Expand description

§Alloy

Alloy connects applications to blockchains.

Alloy is a rewrite of ethers-rs from the ground up, with exciting new features, high performance, and excellent docs.

We also have a book on all things Alloy and many examples to help you get started.

Telegram chat

§Installation

Alloy consists of a number of crates that provide a range of functionality essential for interfacing with any Ethereum-based blockchain.

The easiest way to get started is to add the alloy crate with the full feature flag from the command-line using Cargo:

cargo add alloy --features full

Alternatively, you can add the following to your Cargo.toml file:

alloy = { version = "1", features = ["full"] }

For a more fine-grained control over the features you wish to include, you can add the individual crates to your Cargo.toml file, or use the alloy crate with the features you need.

A comprehensive list of available features can be found on docs.rs or in the alloy crate’s Cargo.toml.

§Examples

§Connecting to a Provider

Here’s a simple example of connecting to an Ethereum node and querying the latest block:

use alloy::providers::{Provider, ProviderBuilder};

// Create a provider with the HTTP transport using the `reqwest` crate.
let rpc_url = "https://eth.llamarpc.com";
let provider = ProviderBuilder::new().connect(rpc_url).await?;

// Get the latest block number.
let latest_block = provider.get_block_number().await?;
println!("Latest block number: {latest_block}");

// Get chain ID.
let chain_id = provider.get_chain_id().await?;
println!("Chain ID: {chain_id}");

§Network generic

Alloy is network-generic, allowing you to work with any Ethereum-compatible chain. Here’s an example using Optimism (see op-alloy) to demonstrate this capability:

use alloy::providers::{Provider, ProviderBuilder};
use op_alloy::network::Optimism;

// Connect to Optimism mainnet.
let rpc_url = "https://mainnet.optimism.io";
let provider = ProviderBuilder::new_with_network::<Optimism>().connect(rpc_url).await?;

For more examples, check out the Alloy examples repository.

§Overview

This repository contains the following crates:

§Supported Rust Versions (MSRV)

The current MSRV (minimum supported rust version) is 1.88.

Alloy will keep a rolling MSRV policy of at least two versions behind the latest stable release (so if the latest stable release is 1.58, we would support 1.56).

Note that the MSRV is not increased automatically, and only as part of a patch (pre-1.0) or minor (post-1.0) release.

§Contributing

Thanks for your help improving the project! We are so happy to have you! We have a contributing guide to help you get involved in the Alloy project.

Pull requests will not be merged unless CI passes, so please ensure that your contribution follows the linting rules and passes clippy.

§Note on no_std

Because these crates are primarily network-focused, we do not intend to support no_std for most of them at this time.

The following crates support no_std:

CrateVersion Badge
alloy-eipsCrates.io
alloy-genesisCrates.io
alloy-serdeCrates.io
alloy-consensusCrates.io

If you would like to add no_std support to a crate, please make sure to update scripts/check_no_std.sh as well.

§Credits

None of these crates would have been possible without the great work done in:

ProjectDescription
ethers.jsA complete and compact JavaScript library for interacting with the Ethereum blockchain.
rust-web3Rust library for Ethereum JSON-RPC client communication, including support for async and WASM.
ruintA fast, no-std, const-friendly implementation of fixed-size unsigned integers in Rust.
ethabiEthereum ABI encoding/decoding in Rust for contracts and transactions.
ethcontract-rsRust library to generate type-safe bindings to Ethereum smart contracts.
guac_rsRust implementation of the GUAC protocol for Ethereum state channels.
§License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in these crates by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Re-exports§

pub use primitives::hex;
pub use primitives::hex;
pub use sol_types::sol;sol-types
pub use alloy_trie as trie;trie

Modules§

consensusconsensus
Ethereum consensus interface.
contractcontract
Interact with on-chain contracts.
dyn_abidyn-abi
Dynamic Solidity type encoder.
eipseips
Ethereum Improvement Proposal (EIP) implementations.
ensens
Ethereum Name Service utilities like namehash, forward & reverse lookups. ENS Name resolving utilities.
genesisgenesis
Ethereum genesis file definitions. Alloy genesis types
json_abijson-abi
Full Ethereum JSON-ABI implementation.
networknetwork
Ethereum blockchain RPC behavior abstraction.
node_bindingsnode-bindings
Ethereum execution-layer client bindings.
primitives
Low-level primitives for Ethereum.
providersproviders
Interface with an Ethereum blockchain.
pubsubpubsub
Ethereum JSON-RPC publish-subscribe tower service and type definitions.
rlprlp
A fast and simple Ethereum RLP implementation in Rust.
rpcrpc
Ethereum JSON-RPC client and types.
serdeserde
Serde related helpers for Alloy.
signerssigners
Ethereum signer abstraction and implementations.
sol_typessol-types
Solidity type modeling and ABI and EIP-712 codec implementation.
transportstransports
Low-level Ethereum JSON-RPC transport abstraction and implementations.