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 = "tungstenite-client")]
25pub use tungstenite_client::TungsteniteRpcClient;
26#[cfg(feature = "tungstenite-client")]
27pub mod tungstenite_client;
28
29#[cfg(all(feature = "jsonrpsee-client", not(feature = "sync-api")))]
30pub use jsonrpsee_client::JsonrpseeClient;
31#[cfg(all(feature = "jsonrpsee-client", not(feature = "sync-api")))]
32#[allow(dead_code)]
33#[allow(unused_imports)]
34pub mod jsonrpsee_client;
35
36pub mod error;
37#[cfg(feature = "tungstenite-client")]
38mod helpers;
39
40pub use error::{Error, Result};
41
42#[cfg(test)]
43pub mod mocks;
44
45/// Trait to be implemented by the rpc-client for sending rpc requests and extrinsic.
46#[maybe_async::maybe_async(?Send)]
47pub trait Request {
48	/// Sends a RPC request to the substrate node and returns the answer as string.
49	async fn request<R: DeserializeOwned>(&self, method: &str, params: RpcParams) -> Result<R>;
50}
51
52/// Trait to be implemented by the rpc-client for subscribing to the substrate node.
53#[maybe_async::maybe_async(?Send)]
54pub trait Subscribe {
55	type Subscription<Notification>: HandleSubscription<Notification>
56	where
57		Notification: DeserializeOwned;
58
59	async fn subscribe<Notification: DeserializeOwned>(
60		&self,
61		sub: &str,
62		params: RpcParams,
63		unsub: &str,
64	) -> Result<Self::Subscription<Notification>>;
65}
66
67/// Trait to use the full functionality of jsonrpseee Subscription type
68/// without actually enforcing it.
69#[maybe_async::maybe_async(?Send)]
70pub trait HandleSubscription<Notification: DeserializeOwned> {
71	/// Returns the next notification from the stream.
72	/// This may return `None` if the subscription has been terminated,
73	/// which may happen if the channel becomes full or is dropped.
74	///
75	/// **Note:** This has an identical signature to the [`StreamExt::next`]
76	/// method (and delegates to that). Import [`StreamExt`] if you'd like
77	/// access to other stream combinator methods.
78	async fn next(&mut self) -> Option<Result<Notification>>;
79
80	/// Unsubscribe and consume the subscription.
81	async fn unsubscribe(self) -> Result<()>;
82}
83
84pub fn to_json_req(method: &str, params: RpcParams) -> Result<String> {
85	Ok(serde_json::json!({
86		"method": method,
87		"params": params.to_json_value()?,
88		"jsonrpc": "2.0",
89		"id": "1",
90	})
91	.to_string())
92}