-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put cast rpc implementation into its own file
And add stdin parsing Signed-off-by: Julian Popescu <[email protected]>
- Loading branch information
1 parent
4fb22a7
commit 8159b09
Showing
6 changed files
with
130 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
//! [`foundry_config::Config`]. | ||
|
||
pub mod find_block; | ||
pub mod rpc; | ||
pub mod run; | ||
pub mod wallet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use crate::{cmd::Cmd, utils::consume_config_rpc_url}; | ||
use cast::Cast; | ||
use clap::Parser; | ||
use ethers::prelude::*; | ||
use eyre::Result; | ||
use futures::future::BoxFuture; | ||
|
||
#[derive(Debug, Clone, Parser)] | ||
pub struct RpcArgs { | ||
#[clap(short, long, env = "ETH_RPC_URL", value_name = "URL")] | ||
rpc_url: Option<String>, | ||
#[clap( | ||
short = 'w', | ||
long, | ||
help = r#"Pass the "params" as is"#, | ||
long_help = r#"Pass the "params" as is | ||
If --raw is passed the first PARAM will be taken as the value of "params". If no params are given, stdin will be used. For example: | ||
rpc eth_getBlockByNumber '["0x123", false]' --raw | ||
=> {"method": "eth_getBlockByNumber", "params": ["0x123", false] ... }"# | ||
)] | ||
raw: bool, | ||
#[clap(value_name = "METHOD", help = "RPC method name")] | ||
method: String, | ||
#[clap( | ||
value_name = "PARAMS", | ||
help = "RPC parameters", | ||
long_help = r#"RPC parameters | ||
Parameters are interpreted as JSON and then fall back to string. For example: | ||
rpc eth_getBlockByNumber 0x123 false | ||
=> {"method": "eth_getBlockByNumber", "params": ["0x123", false] ... }"# | ||
)] | ||
params: Vec<String>, | ||
} | ||
|
||
impl Cmd for RpcArgs { | ||
type Output = BoxFuture<'static, Result<()>>; | ||
fn run(self) -> eyre::Result<Self::Output> { | ||
let RpcArgs { rpc_url, raw, method, params } = self; | ||
Ok(Box::pin(Self::do_rpc(rpc_url, raw, method, params))) | ||
} | ||
} | ||
|
||
impl RpcArgs { | ||
async fn do_rpc( | ||
rpc_url: Option<String>, | ||
raw: bool, | ||
method: String, | ||
params: Vec<String>, | ||
) -> Result<()> { | ||
let rpc_url = consume_config_rpc_url(rpc_url); | ||
let provider = Provider::try_from(rpc_url)?; | ||
let params = if raw { | ||
if params.is_empty() { | ||
serde_json::Deserializer::from_reader(std::io::stdin()) | ||
.into_iter() | ||
.next() | ||
.transpose()? | ||
.unwrap() | ||
} else { | ||
Self::to_json_or_string(params.into_iter().next().unwrap()) | ||
} | ||
} else { | ||
serde_json::Value::Array(params.into_iter().map(Self::to_json_or_string).collect()) | ||
}; | ||
println!("{}", Cast::new(provider).rpc(&method, params).await?); | ||
Ok(()) | ||
} | ||
fn to_json_or_string(value: String) -> serde_json::Value { | ||
serde_json::from_str(&value).unwrap_or(serde_json::Value::String(value)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters