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