anvil/eth/backend/info.rs
1//! Handler that can get current storage related data
2
3use crate::mem::Backend;
4use alloy_network::AnyRpcBlock;
5use alloy_primitives::B256;
6use anvil_core::eth::{block::Block, transaction::TypedReceipt};
7use std::{fmt, sync::Arc};
8
9/// A type that can fetch data related to the ethereum storage.
10///
11/// This is simply a wrapper type for the [`Backend`] but exposes a limited set of functions to
12/// fetch ethereum storage related data
13// TODO(mattsee): once we have multiple Backend types, this should be turned into a trait
14#[derive(Clone)]
15pub struct StorageInfo {
16 backend: Arc<Backend>,
17}
18
19impl StorageInfo {
20 pub(crate) fn new(backend: Arc<Backend>) -> Self {
21 Self { backend }
22 }
23
24 /// Returns the receipts of the current block
25 pub fn current_receipts(&self) -> Option<Vec<TypedReceipt>> {
26 self.backend.mined_receipts(self.backend.best_hash())
27 }
28
29 /// Returns the current block
30 pub fn current_block(&self) -> Option<Block> {
31 self.backend.get_block(self.backend.best_number())
32 }
33
34 /// Returns the receipts of the block with the given hash
35 pub fn receipts(&self, hash: B256) -> Option<Vec<TypedReceipt>> {
36 self.backend.mined_receipts(hash)
37 }
38
39 /// Returns the block with the given hash
40 pub fn block(&self, hash: B256) -> Option<Block> {
41 self.backend.get_block_by_hash(hash)
42 }
43
44 /// Returns the block with the given hash in the format of the ethereum API
45 pub fn eth_block(&self, hash: B256) -> Option<AnyRpcBlock> {
46 let block = self.block(hash)?;
47 Some(self.backend.convert_block(block))
48 }
49}
50
51impl fmt::Debug for StorageInfo {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 f.debug_struct("StorageInfo").finish_non_exhaustive()
54 }
55}