forked from scs/substrate-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_custom_client.rs
51 lines (46 loc) · 1.34 KB
/
example_custom_client.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
use substrate_api_client::rpc::json_req::author_submit_extrinsic;
use substrate_api_client::{
Api, ApiClientError, ApiResult, FromHexString, Hash, RpcClient, Value, XtStatus,
};
struct MyClient {
// pick any request crate, such as ureq::Agent
_inner: (),
}
impl MyClient {
pub fn new() -> Self {
Self {
// ureq::agent()
_inner: (),
}
}
pub fn send_json<R>(
&self,
_path: String,
_json: Value,
) -> Result<R, Box<dyn std::error::Error>> {
// you can figure this out...self.inner...send_json...
todo!()
}
}
impl RpcClient for MyClient {
fn get_request(&self, jsonreq: serde_json::Value) -> ApiResult<String> {
self.send_json::<Value>("".into(), jsonreq)
.map(|v| v.to_string())
.map_err(|err| ApiClientError::RpcClient(err.to_string()))
}
fn send_extrinsic(
&self,
xthex_prefixed: String,
_exit_on: XtStatus,
) -> ApiResult<Option<Hash>> {
let jsonreq = author_submit_extrinsic(&xthex_prefixed);
let res: String = self
.send_json("".into(), jsonreq)
.map_err(|err| ApiClientError::RpcClient(err.to_string()))?;
Ok(Some(Hash::from_hex(res)?))
}
}
fn main() {
let client = MyClient::new();
let _api = Api::<(), _>::new(client);
}