fix some nits

This commit is contained in:
Niklas Adolfsson
2021-12-01 10:48:32 +01:00
parent 1a3b5a9fe1
commit f9860e7d30
3 changed files with 19 additions and 17 deletions
+8 -6
View File
@@ -35,6 +35,7 @@ use core::{
use frame_metadata::RuntimeMetadataPrefixed;
pub use jsonrpsee::{
client_transport::ws::{
InvalidUri,
Receiver as WsReceiver,
Sender as WsSender,
Uri,
@@ -650,23 +651,24 @@ impl<T: Config> ExtrinsicSuccess<T> {
/// Example to check that `From<(Sender, Receiver) for RpcClient` works.
pub async fn build_ws_client_default(url: &str) -> Result<RpcClient, RpcError> {
let client = ws_transport(url).await.into();
let client = ws_transport(url).await?.into();
Ok(client)
}
/// Build WS RPC client from URL
pub async fn build_ws_client(url: &str) -> Result<RpcClient, RpcError> {
let (sender, receiver) = ws_transport(url).await;
let (sender, receiver) = ws_transport(url).await?;
Ok(RpcClientBuilder::default()
.max_notifs_per_subscription(4096)
.build(sender, receiver))
}
async fn ws_transport(url: &str) -> (WsSender, WsReceiver) {
// fix unwraps because I'm lazy.
let url: Uri = url.parse().unwrap();
async fn ws_transport(url: &str) -> Result<(WsSender, WsReceiver), RpcError> {
let url: Uri = url
.parse()
.map_err(|e: InvalidUri| RpcError::Transport(e.into()))?;
WsTransportClientBuilder::default()
.build(url)
.await
.unwrap()
.map_err(|e| RpcError::Transport(e.into()))
}
+1
View File
@@ -12,3 +12,4 @@ codec = { package = "parity-scale-codec", version = "2", default-features = fals
subxt = { path = ".." }
async-std = { version = "1.9.0", features = ["attributes"] }
sp-core = { package = "sp-core", git = "https://github.com/paritytech/substrate/", branch = "master" }
jsonrpsee-http-client = { git = "https://github.com/paritytech/jsonrpsee/", branch = "extract-async-client" }
+10 -11
View File
@@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.
use jsonrpsee_http_client::{
types::traits::Client,
HttpClientBuilder,
};
use std::{
env,
fs,
@@ -27,11 +31,6 @@ use std::{
thread,
time,
};
use subxt::rpc::{
build_ws_client,
rpc_params,
Client as _,
};
static SUBSTRATE_BIN_ENV_VAR: &str = "SUBSTRATE_NODE_PATH";
@@ -47,7 +46,7 @@ async fn main() {
let cmd = Command::new(&substrate_bin)
.arg("--dev")
.arg("--tmp")
.arg(format!("--ws-port={}", port))
.arg(format!("--rpc-port={}", port))
.spawn();
let mut cmd = match cmd {
Ok(cmd) => cmd,
@@ -61,15 +60,15 @@ async fn main() {
const MAX_RETRIES: usize = 20;
let mut retries = 0;
let mut wait_secs = 1;
let rpc_client = HttpClientBuilder::default()
.build(&format!("http://localhost:{}", port))
.expect("valid URL; qed");
loop {
if retries >= MAX_RETRIES {
panic!("Cannot connect to substrate node after {} retries", retries);
}
let res = build_ws_client(&format!("ws://localhost:{}", port))
.await
.expect("should only error if malformed URL for an HTTP connection")
.request("state_getMetadata", rpc_params![])
.await;
let res = rpc_client.request("state_getMetadata", None).await;
match res {
Ok(res) => {
let _ = cmd.kill();