Skip to content

Commit

Permalink
Send RPC from CLI (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier authored Oct 30, 2022
1 parent df41a7b commit a961382
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bin/subalfred/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ vergen = { version = "7.4" }
anyhow = { version = "1.0" }
array-bytes = { version = "4.1" }
clap = { version = "4.0", features = ["cargo", "derive", "wrap_help"] }
serde_json = { version = "1.0" }
tokio = { version = "1.21", features = ["macros", "rt-multi-thread"] }
tracing-subscriber = { version = "0.3" }
unescaper = { version = "0.1" }
# hack-ink
cmd-impl = { version = "0.9.0-rc13", path = "src/command/impl" }
subalfred-core = { version = "0.9.0-rc13", path = "../../lib/core", features = ["clap"] }
subhasher = { version = "0.9.0-rc13", path = "../../substrate-minimal/subhasher" }
subrpcer = { version = "0.9.0-rc13", path = "../../substrate-minimal/subrpcer" }
substorager = { version = "0.9.0-rc13", path = "../../substrate-minimal/substorager" }
4 changes: 4 additions & 0 deletions bin/subalfred/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use hash::HashCmd;
mod key;
use key::KeyCmd;

mod rpc;
use rpc::RpcCmd;

mod state;
use state::StateCmd;

Expand All @@ -33,6 +36,7 @@ pub(crate) enum Cmd {
Get,
Hash,
Key,
Rpc,
#[command(subcommand)]
State,
StorageKey,
Expand Down
52 changes: 52 additions & 0 deletions bin/subalfred/src/command/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// crates.io
use clap::Args;
use serde_json::Value;
// hack-ink
use crate::prelude::*;
use subalfred_core::http::CLIENT;

/// Send a RPC request to the node's HTTP RPC endpoint.
///
/// Example:
/// Get the Polkadot's block zero's hash:
/// ```
/// # Normal output
/// subalfred rpc https://rpc.polkadot.io --method chain_getBlockHash --params '[[0,1,2]]'
/// # Beautiful output
/// subalfred rpc https://rpc.polkadot.io --method chain_getBlockHash --params '[[0,1,2]]' | jq
/// ```
#[derive(Debug, Args)]
pub(crate) struct RpcCmd {
/// Node's HTTP RPC endpoint.
#[arg(required = true, value_name = "URI", default_value = "http://localhost:9933")]
uri: String,
/// JSONRPC method name.
#[arg(long, required = true, value_name = "METHOD")]
method: String,
/// JSONRPC parameters.
#[arg(long, value_name = "PARAMETERS")]
params: Option<String>,
}
impl RpcCmd {
#[tokio::main]
pub(crate) async fn run(&self) -> Result<()> {
let Self { method, params, uri } = self;
let params = if let Some(params) = params.as_ref() {
serde_json::from_str(params)?
} else {
Value::Null
};
let result = CLIENT
.post(uri)
.json(&subrpcer::rpc(0, method, params))
.send()
.await?
.json::<Value>()
.await?;
let result = serde_json::to_string(&result)?;

println!("{result}");

Ok(())
}
}

0 comments on commit a961382

Please sign in to comment.