mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 15:41:02 +00:00
connect to substrate using ws (#417)
This commit is contained in:
committed by
Bastian Köcher
parent
ede0ff8656
commit
55d22a0d1e
@@ -24,7 +24,7 @@ subcommands:
|
|||||||
- sub-port: &sub-port
|
- sub-port: &sub-port
|
||||||
long: sub-port
|
long: sub-port
|
||||||
value_name: SUB_PORT
|
value_name: SUB_PORT
|
||||||
help: Connect to Substrate node at given port.
|
help: Connect to Substrate node websocket server at given port.
|
||||||
takes_value: true
|
takes_value: true
|
||||||
- sub-tx-mode:
|
- sub-tx-mode:
|
||||||
long: sub-tx-mode
|
long: sub-tx-mode
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
async-trait = "0.1.40"
|
async-trait = "0.1.40"
|
||||||
codec = { package = "parity-scale-codec", version = "1.3.4" }
|
codec = { package = "parity-scale-codec", version = "1.3.4" }
|
||||||
jsonrpsee = { git = "https://github.com/svyatonik/jsonrpsee.git", branch = "shared-client-in-rpc-api", default-features = false, features = ["http"] }
|
jsonrpsee = { git = "https://github.com/svyatonik/jsonrpsee.git", branch = "shared-client-in-rpc-api", default-features = false, features = ["ws"] }
|
||||||
log = "0.4.11"
|
log = "0.4.11"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ use crate::{ConnectionParams, Result};
|
|||||||
|
|
||||||
use jsonrpsee::common::DeserializeOwned;
|
use jsonrpsee::common::DeserializeOwned;
|
||||||
use jsonrpsee::raw::RawClient;
|
use jsonrpsee::raw::RawClient;
|
||||||
use jsonrpsee::transport::http::HttpTransportClient;
|
use jsonrpsee::transport::ws::WsTransportClient;
|
||||||
use jsonrpsee::Client as RpcClient;
|
use jsonrpsee::Client as RpcClient;
|
||||||
use num_traits::Zero;
|
use num_traits::Zero;
|
||||||
use sp_core::Bytes;
|
use sp_core::Bytes;
|
||||||
@@ -49,10 +49,10 @@ impl<C: Chain> std::fmt::Debug for Client<C> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<C: Chain> Client<C> {
|
impl<C: Chain> Client<C> {
|
||||||
/// Returns client that is able to call RPCs on Substrate node.
|
/// Returns client that is able to call RPCs on Substrate node over websocket connection.
|
||||||
pub async fn new(params: ConnectionParams) -> Result<Self> {
|
pub async fn new(params: ConnectionParams) -> Result<Self> {
|
||||||
let uri = format!("http://{}:{}", params.host, params.port);
|
let uri = format!("ws://{}:{}", params.host, params.port);
|
||||||
let transport = HttpTransportClient::new(&uri);
|
let transport = WsTransportClient::new(&uri).await?;
|
||||||
let raw_client = RawClient::new(transport);
|
let raw_client = RawClient::new(transport);
|
||||||
let client: RpcClient = raw_client.into();
|
let client: RpcClient = raw_client.into();
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
//! Substrate node RPC errors.
|
//! Substrate node RPC errors.
|
||||||
|
|
||||||
use jsonrpsee::client::RequestError;
|
use jsonrpsee::client::RequestError;
|
||||||
|
use jsonrpsee::transport::ws::WsNewDnsError;
|
||||||
use relay_utils::MaybeConnectionError;
|
use relay_utils::MaybeConnectionError;
|
||||||
|
|
||||||
/// Result type used by Substrate client.
|
/// Result type used by Substrate client.
|
||||||
@@ -26,13 +27,21 @@ pub type Result<T> = std::result::Result<T, Error>;
|
|||||||
/// a Substrate node through RPC.
|
/// a Substrate node through RPC.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
/// Web socket connection error.
|
||||||
|
WsConnectionError(WsNewDnsError),
|
||||||
/// An error that can occur when making an HTTP request to
|
/// An error that can occur when making an HTTP request to
|
||||||
/// an JSON-RPC client.
|
/// an JSON-RPC server.
|
||||||
Request(RequestError),
|
Request(RequestError),
|
||||||
/// The response from the client could not be SCALE decoded.
|
/// The response from the server could not be SCALE decoded.
|
||||||
ResponseParseFailed(codec::Error),
|
ResponseParseFailed(codec::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<WsNewDnsError> for Error {
|
||||||
|
fn from(error: WsNewDnsError) -> Self {
|
||||||
|
Error::WsConnectionError(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<RequestError> for Error {
|
impl From<RequestError> for Error {
|
||||||
fn from(error: RequestError) -> Self {
|
fn from(error: RequestError) -> Self {
|
||||||
Error::Request(error)
|
Error::Request(error)
|
||||||
@@ -54,6 +63,7 @@ impl From<Error> for String {
|
|||||||
impl ToString for Error {
|
impl ToString for Error {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
|
Self::WsConnectionError(e) => e.to_string(),
|
||||||
Self::Request(e) => e.to_string(),
|
Self::Request(e) => e.to_string(),
|
||||||
Self::ResponseParseFailed(e) => e.what().to_string(),
|
Self::ResponseParseFailed(e) => e.what().to_string(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,12 +30,12 @@ pub use crate::client::{Client, OpaqueGrandpaAuthoritiesSet};
|
|||||||
pub use crate::error::{Error, Result};
|
pub use crate::error::{Error, Result};
|
||||||
pub use bp_runtime::{BlockNumberOf, Chain as ChainBase, HashOf, HeaderOf};
|
pub use bp_runtime::{BlockNumberOf, Chain as ChainBase, HashOf, HeaderOf};
|
||||||
|
|
||||||
/// Substrate connection params.
|
/// Substrate-over-websocket connection params.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ConnectionParams {
|
pub struct ConnectionParams {
|
||||||
/// Substrate RPC host.
|
/// Websocket server hostname.
|
||||||
pub host: String,
|
pub host: String,
|
||||||
/// Substrate RPC port.
|
/// Websocket server TCP port.
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ impl Default for ConnectionParams {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
ConnectionParams {
|
ConnectionParams {
|
||||||
host: "localhost".into(),
|
host: "localhost".into(),
|
||||||
port: 9933,
|
port: 9944,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ macro_rules! declare_chain_options {
|
|||||||
#[doc = "Connect to " $chain " node at given host."]
|
#[doc = "Connect to " $chain " node at given host."]
|
||||||
#[structopt(long)]
|
#[structopt(long)]
|
||||||
pub [<$chain_prefix _host>]: String,
|
pub [<$chain_prefix _host>]: String,
|
||||||
#[doc = "Connect to " $chain " node at given port."]
|
#[doc = "Connect to " $chain " node websocket server at given port."]
|
||||||
#[structopt(long)]
|
#[structopt(long)]
|
||||||
pub [<$chain_prefix _port>]: u16,
|
pub [<$chain_prefix _port>]: u16,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user