substrate_api_client/rpc/
mod.rs

1/*
2   Copyright 2019 Supercomputing Systems AG
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8	   http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15
16*/
17
18use ac_primitives::RpcParams;
19#[cfg(all(not(feature = "sync-api"), not(feature = "std")))]
20use alloc::boxed::Box;
21use alloc::string::{String, ToString};
22use serde::de::DeserializeOwned;
23
24#[cfg(feature = "ws-client")]
25#[allow(deprecated)]
26pub use ws_client::WsRpcClient;
27#[cfg(feature = "ws-client")]
28#[allow(deprecated)]
29pub mod ws_client;
30
31#[cfg(feature = "tungstenite-client")]
32pub use tungstenite_client::TungsteniteRpcClient;
33#[cfg(feature = "tungstenite-client")]
34pub mod tungstenite_client;
35
36#[cfg(all(feature = "jsonrpsee-client", not(feature = "sync-api")))]
37pub use jsonrpsee_client::JsonrpseeClient;
38#[cfg(all(feature = "jsonrpsee-client", not(feature = "sync-api")))]
39#[allow(dead_code)]
40#[allow(unused_imports)]
41pub mod jsonrpsee_client;
42
43pub mod error;
44#[cfg(any(feature = "ws-client", feature = "tungstenite-client"))]
45mod helpers;
46
47pub use error::{Error, Result};
48
49#[cfg(test)]
50pub mod mocks;
51
52/// Trait to be implemented by the ws-client for sending rpc requests and extrinsic.
53#[maybe_async::maybe_async(?Send)]
54pub trait Request {
55	/// Sends a RPC request to the substrate node and returns the answer as string.
56	async fn request<R: DeserializeOwned>(&self, method: &str, params: RpcParams) -> Result<R>;
57}
58
59/// Trait to be implemented by the ws-client for subscribing to the substrate node.
60#[maybe_async::maybe_async(?Send)]
61pub trait Subscribe {
62	type Subscription<Notification>: HandleSubscription<Notification>
63	where
64		Notification: DeserializeOwned;
65
66	async fn subscribe<Notification: DeserializeOwned>(
67		&self,
68		sub: &str,
69		params: RpcParams,
70		unsub: &str,
71	) -> Result<Self::Subscription<Notification>>;
72}
73
74/// Trait to use the full functionality of jsonrpseee Subscription type
75/// without actually enforcing it.
76#[maybe_async::maybe_async(?Send)]
77pub trait HandleSubscription<Notification: DeserializeOwned> {
78	/// Returns the next notification from the stream.
79	/// This may return `None` if the subscription has been terminated,
80	/// which may happen if the channel becomes full or is dropped.
81	///
82	/// **Note:** This has an identical signature to the [`StreamExt::next`]
83	/// method (and delegates to that). Import [`StreamExt`] if you'd like
84	/// access to other stream combinator methods.
85	async fn next(&mut self) -> Option<Result<Notification>>;
86
87	/// Unsubscribe and consume the subscription.
88	async fn unsubscribe(self) -> Result<()>;
89}
90
91pub fn to_json_req(method: &str, params: RpcParams) -> Result<String> {
92	Ok(serde_json::json!({
93		"method": method,
94		"params": params.to_json_value()?,
95		"jsonrpc": "2.0",
96		"id": "1",
97	})
98	.to_string())
99}