Skip to content

Commit

Permalink
expose jsonrpc-core client (paritytech#672)
Browse files Browse the repository at this point in the history
* expose jsonrpc-core client

* use shared reference to RpcClienT

* don't expose Arc<dyn RpcClientT>

* cargo fmt
  • Loading branch information
seunlanlege committed Oct 4, 2022
1 parent 6cfd3bb commit 81175b2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/examples/custom_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Pass this into our OnlineClient to instantiate it. This will lead to some
// RPC calls being made to fetch chain details/metadata, which will immediately
// fail..
let _ = OnlineClient::<PolkadotConfig>::from_rpc_client(rpc_client).await;
let _ = OnlineClient::<PolkadotConfig>::from_rpc_client(Arc::new(rpc_client)).await;

// But, we can see that the calls were made via our custom RPC client:
println!("Log of calls made:\n\n{}", log.lock().unwrap().as_str());
Expand Down
4 changes: 2 additions & 2 deletions subxt/src/client/online_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ impl<T: Config> OnlineClient<T> {
let client = jsonrpsee_helpers::ws_client(url.as_ref())
.await
.map_err(|e| crate::error::RpcError(e.to_string()))?;
OnlineClient::from_rpc_client(client).await
OnlineClient::from_rpc_client(Arc::new(client)).await
}
}

impl<T: Config> OnlineClient<T> {
/// Construct a new [`OnlineClient`] by providing an underlying [`RpcClientT`]
/// implementation to drive the connection.
pub async fn from_rpc_client<R: RpcClientT>(
rpc_client: R,
rpc_client: Arc<R>,
) -> Result<OnlineClient<T>, Error> {
let rpc = Rpc::new(rpc_client);

Expand Down
1 change: 0 additions & 1 deletion subxt/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub use event_subscription::{
FinalizedEventSub,
};
pub use events_client::{
// Exposed only for testing:
subscribe_to_block_headers_filling_in_gaps,
EventsClient,
};
Expand Down
7 changes: 5 additions & 2 deletions subxt/src/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ use sp_runtime::{
},
ApplyExtrinsicResult,
};
use std::collections::HashMap;
use std::{
collections::HashMap,
sync::Arc,
};

/// A number type that can be serialized both as a number or a string that encodes a number in a
/// string.
Expand Down Expand Up @@ -342,7 +345,7 @@ impl<T: Config> std::ops::Deref for Rpc<T> {

impl<T: Config> Rpc<T> {
/// Create a new [`Rpc`]
pub fn new<R: RpcClientT>(client: R) -> Self {
pub fn new<R: RpcClientT>(client: Arc<R>) -> Self {
Self {
client: RpcClient::new(client),
_marker: PhantomDataSendSync::new(),
Expand Down
4 changes: 2 additions & 2 deletions subxt/src/rpc/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use std::{
pub struct RpcClient(Arc<dyn RpcClientT>);

impl RpcClient {
pub(crate) fn new<R: RpcClientT>(client: R) -> Self {
RpcClient(Arc::new(client))
pub(crate) fn new<R: RpcClientT>(client: Arc<R>) -> Self {
RpcClient(client)
}

/// Make an RPC request, given a method name and some parameters.
Expand Down

0 comments on commit 81175b2

Please sign in to comment.