From 02f8903d172f4abb8439f189e3da112f363d9989 Mon Sep 17 00:00:00 2001 From: James Prestwich Date: Fri, 17 May 2024 02:20:39 -0400 Subject: [PATCH] feat: block id convenience functions (#757) * feat: block id convenience functions * fix: use B256 --- crates/provider/src/provider/with_block.rs | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/crates/provider/src/provider/with_block.rs b/crates/provider/src/provider/with_block.rs index 7da33df2665..ea025216411 100644 --- a/crates/provider/src/provider/with_block.rs +++ b/crates/provider/src/provider/with_block.rs @@ -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; @@ -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 IntoFuture for RpcWithBlock