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 enablespubsub.ipc- Enable IPC support. Implicitly enablespubsub.
§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§
- Call
Item - A singular call type that is mapped into aggregate, aggregate3, aggregate3Value call structs via
the
CallInfoTraittrait. - Call
Item 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. - EthCall
Many - A builder for an
"eth_callMany"RPC request. - EthCall
Many Params - The parameters for an
"eth_callMany"RPC request. - EthCall
Params - The parameters for an
"eth_call"RPC request. - EthGet
Block - A builder for an
"eth_getBlockByHash"request. This type is returned by theProvider::callmethod. - EthGet
Block 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.
- Multicall
Builder - A Multicall3 builder
- Params
With Block - Helper struct that houses the params along with the BlockId.
- Pending
Transaction - Represents a transaction that is yet to be confirmed a specified number of times.
- Pending
Transaction Builder - A builder for configuring a pending transaction watcher.
- Pending
Transaction Config - Configuration for watching a pending transaction.
- Provider
Builder - A builder for constructing a
Providerfrom various layers. - Root
Provider - The root provider manages the RPC client and the heartbeat. It is at the base of every provider stack.
- RpcWith
Block - A struct that takes an optional
BlockIdparameter. - Sendable
TxErr - Error when converting a
SendableTx. - Stack
- A stack of two providers.
- Watch
Blocks - A builder type for polling new blocks using the
FilterPollerBuilder. - Web3
Signer - A remote signer that leverages the underlying provider to sign transactions using
"eth_signTransaction"requests.
Enums§
- Multicall
Error - Multicall errors.
- Pending
Transaction Error - Errors which may occur when watching a pending transaction.
- Provider
Call - The primary future type for the
Provider. - Sendable
Tx - A transaction that can be sent. This is either a builder or an envelope.
- Watch
TxError - Errors which may occur in heartbeat when watching a transaction.
Constants§
- MULTICAL
L3_ ADDRESS - Default address for the Multicall3 contract on most chains. See: https://github.com/mds1/multicall
Traits§
- Call
Info Trait - A trait for converting CallItem into relevant call types.
- Caller
- Trait that helpes convert
EthCallinto aProviderCall. - Multicall
Item - 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.
- Provider
Layer - A layering abstraction in the vein of
tower::Layer - Wallet
Provider - Trait for Providers, Fill stacks, etc, which contain
NetworkWallet.
Functions§
- builder
- Helper function to directly access
ProviderBuilderwith minimal generics.
Type Aliases§
- Boxed
Fut - Boxed future type used in
ProviderCallfor non-wasm targets. - Filter
Poller Builder - A task that polls the provider with
eth_getFilterChanges, returning a list ofR. - Reqwest
Provider Deprecated - Type alias for a
RootProviderusing theHttptransport and a reqwest client. - Result
- Result type for multicall operations.