-
Notifications
You must be signed in to change notification settings - Fork 123
/
mod.rs
93 lines (82 loc) · 2.62 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
92
93
/*
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 crate::rpc::{Error, Request, Result, RpcParams, Subscribe};
use jsonrpsee::{
client_transport::ws::{Uri, WsTransportClientBuilder},
core::{
client::{Client, ClientBuilder, ClientT, SubscriptionClientT},
traits::ToRpcParams,
},
};
use serde::de::DeserializeOwned;
use serde_json::value::RawValue;
use std::sync::Arc;
pub use subscription::SubscriptionWrapper;
mod subscription;
#[derive(Clone)]
pub struct JsonrpseeClient {
inner: Arc<Client>,
}
impl JsonrpseeClient {
pub async fn with_default_url() -> Result<Self> {
Self::new("ws://127.0.0.1:9944").await
}
pub async fn new(url: &str) -> Result<Self> {
let uri: Uri = url.parse().map_err(|e| Error::Client(Box::new(e)))?;
let (tx, rx) = WsTransportClientBuilder::default()
.build(uri)
.await
.map_err(|e| Error::Client(Box::new(e)))?;
let client = ClientBuilder::default()
.max_notifs_per_subscription(4096)
.build_with_tokio(tx, rx);
Ok(Self { inner: Arc::new(client) })
}
}
#[maybe_async::async_impl(?Send)]
impl Request for JsonrpseeClient {
async fn request<R: DeserializeOwned>(&self, method: &str, params: RpcParams) -> Result<R> {
self.inner
.request(method, RpcParamsWrapper(params))
.await
.map_err(|e| Error::Client(Box::new(e)))
}
}
#[maybe_async::async_impl(?Send)]
impl Subscribe for JsonrpseeClient {
type Subscription<Notification> = SubscriptionWrapper<Notification> where Notification: DeserializeOwned;
async fn subscribe<Notification: DeserializeOwned>(
&self,
sub: &str,
params: RpcParams,
unsub: &str,
) -> Result<Self::Subscription<Notification>> {
self.inner
.subscribe(sub, RpcParamsWrapper(params), unsub)
.await
.map(|sub| sub.into())
.map_err(|e| Error::Client(Box::new(e)))
}
}
struct RpcParamsWrapper(RpcParams);
impl ToRpcParams for RpcParamsWrapper {
fn to_rpc_params(self) -> core::result::Result<Option<Box<RawValue>>, jsonrpsee::core::Error> {
if let Some(json) = self.0.build() {
RawValue::from_string(json)
.map(Some)
.map_err(jsonrpsee::core::Error::ParseError)
} else {
Ok(None)
}
}
}