cast/cmd/bal.rs
1use crate::cmd::rpc_provider;
2use alloy_primitives::Bytes;
3use alloy_provider::Provider;
4use alloy_rpc_types::BlockId;
5use clap::Parser;
6use eyre::Result;
7use foundry_cli::{
8 json::{print_json_object, print_scalar},
9 opts::RpcOpts,
10};
11
12/// CLI arguments for `cast bal`.
13#[derive(Debug, Parser)]
14pub struct BalArgs {
15 /// The block height or hash to query at.
16 ///
17 /// Can also be the tags earliest, finalized, safe, latest, or pending.
18 block: Option<BlockId>,
19
20 /// Print the RLP encoded block access list.
21 #[arg(long)]
22 raw: bool,
23
24 #[command(flatten)]
25 rpc: RpcOpts,
26}
27
28impl BalArgs {
29 pub async fn run(self) -> Result<()> {
30 let provider = rpc_provider(&self.rpc)?;
31 let block = self.block.unwrap_or_default();
32 let bal = provider.get_block_access_list(block).await?.ok_or_else(|| missing_bal(block))?;
33
34 // `eth_getBlockAccessListRaw` is not a specified method, so the encoding is done here
35 // rather than asked of the node.
36 if self.raw {
37 print_scalar(Bytes::from(alloy_rlp::encode(&bal)))
38 } else {
39 print_json_object(bal)
40 }
41 }
42}
43
44/// The error for a block whose access list the node did not return.
45fn missing_bal(block: BlockId) -> eyre::Report {
46 eyre::eyre!("block access list for {block} not found")
47}