1use crate::Cast;
2use clap::Parser;
3use eyre::Result;
4use foundry_cli::{opts::RpcOpts, utils, utils::LoadConfig};
5use foundry_common::shell;
6use itertools::Itertools;
7
8#[derive(Clone, Debug, Parser)]
10pub struct RpcArgs {
11 method: String,
13
14 params: Vec<String>,
21
22 #[arg(long, short = 'w')]
30 raw: bool,
31
32 #[command(flatten)]
33 rpc: RpcOpts,
34}
35
36impl RpcArgs {
37 pub async fn run(self) -> Result<()> {
38 let Self { raw, method, params, rpc } = self;
39
40 let config = rpc.load_config()?;
41
42 let params = if raw {
43 if params.is_empty() {
44 serde_json::Deserializer::from_reader(std::io::stdin())
45 .into_iter()
46 .next()
47 .transpose()?
48 .ok_or_else(|| eyre::format_err!("Empty JSON parameters"))?
49 } else {
50 value_or_string(params.into_iter().join(" "))
51 }
52 } else {
53 serde_json::Value::Array(params.into_iter().map(value_or_string).collect())
54 };
55
56 let provider = utils::get_provider_with_curl(&config, rpc.curl)?;
57 let result = Cast::new(provider).rpc(&method, params).await?;
58 if shell::is_json() {
59 let result: serde_json::Value = serde_json::from_str(&result)?;
60 sh_println!("{}", serde_json::to_string_pretty(&result)?)?;
61 } else {
62 sh_println!("{}", result)?;
63 }
64 Ok(())
65 }
66}
67
68fn value_or_string(value: String) -> serde_json::Value {
69 serde_json::from_str(&value).unwrap_or(serde_json::Value::String(value))
70}