Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: block id convenience functions #757

Merged
merged 2 commits into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading