-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a cast rpc
method for raw JSON-RPC reqs
#2030
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f792815
Add a request method to cast for raw JSON-RPC reqs
jpopesculian a505f66
Change name from cast request to cast rpc
jpopesculian 4fb22a7
Change how params are parsed in cast rpc
jpopesculian 8159b09
Put cast rpc implementation into its own file
jpopesculian 8b88c23
Better handling of raw parameters for cast rpc
jpopesculian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,76 @@ | ||
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; | ||
use itertools::Itertools; | ||
|
||
#[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()? | ||
.ok_or_else(|| eyre::format_err!("Empty JSON parameters"))? | ||
} else { | ||
Self::to_json_or_string(params.into_iter().join(" ")) | ||
} | ||
} 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is quite neat!