Expand description
§alloy-provider
Interface with an Ethereum blockchain.
This crate contains the Provider trait, which exposes Ethereum JSON-RPC
methods. Providers in alloy are similar to ethers.js providers. They manage
an RpcClient and allow other parts of the program to easily make RPC calls.
Unlike an ethers.js Provider, an alloy Provider is network-aware. It is
parameterized with a Network from alloy-networks. This allows the Provider
to expose a consistent interface to the rest of the program, while adjusting
request and response types to match the underlying blockchain.
Providers can be composed via stacking. For example, a Provider that tracks
the nonce for a given address can be stacked onto a Provider that signs
transactions to create a Provider that can send signed transactions with
correct nonces.
The ProviderBuilder struct can quickly create a stacked provider, similar to
tower::ServiceBuilder.
§Feature flags
- pubsub- Enable support for subscription methods.
- ws- Enable WebSocket support. Implicitly enables- pubsub.
- ipc- Enable IPC support. Implicitly enables- pubsub.
§Usage
use alloy_provider::{ProviderBuilder, RootProvider, Provider};
use alloy_network::Ethereum;
use alloy_primitives::address;
use std::str::FromStr;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a basic HTTP provider
    let provider = RootProvider::<Ethereum>::new_http("https://reth-ethereum.ithaca.xyz/rpc".parse()?);
    // Get the latest block number
    let block_number = provider.get_block_number().await?;
    println!("Latest block number: {block_number}");
    // Get balance of an address
    let address = address!("0x71C7656EC7ab88b098defB751B7401B5f6d8976F");
    let balance = provider.get_balance(address).await?;
    println!("Balance: {balance}");
    // Use the builder pattern to create a provider with recommended fillers
    let provider = ProviderBuilder::new().connect_http("https://reth-ethereum.ithaca.xyz/rpc".parse()?);
    Ok(())
}Re-exports§
- pub use alloy_network as network;
- pub use alloy_network::Network;
- pub use alloy_transport::mock;
- pub use tuple::CallTuple;
Modules§
- bindings
- Multicall bindings
- ext
- Extended APIs for the provider module.
- fillers
- Transaction Fillers
- layers
- Useful layer implementations for the provider.
- utils
- Provider-related utilities.
Structs§
- CallItem 
- A singular call type that is mapped into aggregate, aggregate3, aggregate3Value call structs via
the CallInfoTraittrait.
- CallItem Builder 
- Helper type to build a CallItem
- DynProvider
- A wrapper struct around a type erased Provider.
- Dynamic
- Marker for Dynamic Calls i.e where in SolCall type is locked to one specific type and multicall returns a Vec of the corresponding return type instead of a tuple.
- Empty
- Type indicating that the MulticallBuilderis empty.
- EthCall
- A builder for an "eth_call"request. This type is returned by theProvider::callmethod.
- EthCallMany 
- A builder for an "eth_callMany"RPC request.
- EthCallMany Params 
- The parameters for an "eth_callMany"RPC request.
- EthCallParams 
- The parameters for an "eth_call"RPC request.
- EthGetBlock 
- A builder for an "eth_getBlockByHash"request. This type is returned by theProvider::callmethod.
- EthGetBlock Params 
- The parameters for an eth_getBlockBy{Hash, Number}RPC request.
- Failure
- A struct representing a failure in a multicall
- Identity
- An identity layer that does nothing.
- MulticallBuilder 
- A Multicall3 builder
- ParamsWith Block 
- Helper struct that houses the params along with the BlockId.
- PendingTransaction 
- Represents a transaction that is yet to be confirmed a specified number of times.
- PendingTransaction Builder 
- A builder for configuring a pending transaction watcher.
- PendingTransaction Config 
- Configuration for watching a pending transaction.
- ProviderBuilder 
- A builder for constructing a Providerfrom various layers.
- RootProvider 
- The root provider manages the RPC client and the heartbeat. It is at the base of every provider stack.
- RpcWithBlock 
- A struct that takes an optional BlockIdparameter.
- SendableTxErr 
- Error when converting a SendableTx.
- Stack
- A stack of two providers.
- WatchBlocks 
- A builder type for polling new blocks using the FilterPollerBuilder.
- Web3Signer 
- A remote signer that leverages the underlying provider to sign transactions using
"eth_signTransaction"requests.
Enums§
- MulticallError 
- Multicall errors.
- PendingTransaction Error 
- Errors which may occur when watching a pending transaction.
- ProviderCall 
- The primary future type for the Provider.
- SendableTx 
- A transaction that can be sent. This is either a builder or an envelope.
- WatchTxError 
- Errors which may occur in heartbeat when watching a transaction.
Constants§
- MULTICALL3_ ADDRESS 
- Default address for the Multicall3 contract on most chains. See: https://github.com/mds1/multicall
Traits§
- CallInfo Trait 
- A trait for converting CallItem into relevant call types.
- Caller
- Trait that helpes convert EthCallinto aProviderCall.
- MulticallItem 
- A trait that is to be implemented by a type that can be distilled to a singular contract call item.
- Provider
- Ethereum JSON-RPC interface.
- ProviderLayer 
- A layering abstraction in the vein of tower::Layer
- WalletProvider 
- Trait for Providers, Fill stacks, etc, which contain NetworkWallet.
Functions§
- builder
- Helper function to directly access ProviderBuilderwith minimal generics.
Type Aliases§
- BoxedFut 
- Boxed future type used in ProviderCallfor non-wasm targets.
- FilterPoller Builder 
- A task that polls the provider with eth_getFilterChanges, returning a list ofR.
- ReqwestProvider Deprecated 
- Type alias for a RootProviderusing theHttptransport and a reqwest client.
- Result
- Result type for multicall operations.