Skip to content

Commit

Permalink
feat: block id convenience functions (alloy-rs#757)
Browse files Browse the repository at this point in the history
* feat: block id convenience functions

* fix: use B256
  • Loading branch information
prestwich authored and ben186 committed Jul 27, 2024
1 parent 62e5249 commit 02f8903
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions crates/provider/src/provider/with_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloy_eips::BlockId;
use alloy_json_rpc::{RpcError, RpcParam, RpcReturn};
use alloy_primitives::B256;
use alloy_rpc_client::{RpcCall, WeakClient};
use alloy_transport::{Transport, TransportErrorKind, TransportResult};
use futures::FutureExt;
Expand Down Expand Up @@ -229,6 +230,48 @@ where
self.block_id = block_id;
self
}

/// Set the block id to "pending".
pub fn pending(self) -> Self {
self.block_id(BlockId::pending())
}

/// Set the block id to "latest".
pub fn latest(self) -> Self {
self.block_id(BlockId::latest())
}

/// Set the block id to "earliest".
pub fn earliest(self) -> Self {
self.block_id(BlockId::earliest())
}

/// Set the block id to "finalized".
pub fn finalized(self) -> Self {
self.block_id(BlockId::finalized())
}

/// Set the block id to "safe".
pub fn safe(self) -> Self {
self.block_id(BlockId::safe())
}

/// Set the block id to a specific height.
pub fn number(self, number: u64) -> Self {
self.block_id(BlockId::number(number))
}

/// Set the block id to a specific hash, without requiring the hash be part
/// of the canonical chain.
pub fn hash(self, hash: B256) -> Self {
self.block_id(BlockId::hash(hash))
}

/// Set the block id to a specific hash and require the hash be part of the
/// canonical chain.
pub fn hash_canonical(self, hash: B256) -> Self {
self.block_id(BlockId::hash_canonical(hash))
}
}

impl<T, Params, Resp, Output, Map> IntoFuture for RpcWithBlock<T, Params, Resp, Output, Map>
Expand Down

0 comments on commit 02f8903

Please sign in to comment.