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
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Debug;
use std::marker::PhantomData;
pub use eth::*;
pub use net::*;
pub use personal::*;
pub use sub::*;
pub use txpool::*;
pub use web3::*;
mod eth;
mod net;
mod personal;
mod sub;
mod txpool;
mod web3;
#[repr(C)]
#[derive(Serialize, Debug)]
pub struct Rpc<T: DeserializeOwned + Debug> {
#[serde(rename = "jsonrpc")]
pub json_rpc: &'static str,
pub method: String,
pub params: Vec<Value>,
pub id: usize,
#[serde(skip_serializing)]
pub result_type: PhantomData<T>,
}
#[derive(Deserialize)]
pub struct RpcResponse<'a, T: 'a> {
pub id: usize,
pub jsonrpc: &'a str,
pub result: T,
}
impl<T: DeserializeOwned + Debug> Rpc<T> {
const JSON_RPC: &'static str = "2.0";
pub(crate) fn new(method: &str) -> Rpc<T> {
Rpc {
json_rpc: Self::JSON_RPC,
method: String::from(method),
params: Vec::new(),
id: Default::default(),
result_type: PhantomData,
}
}
pub(crate) fn add_param<U: Serialize + Debug>(&mut self, parameter: U) {
if let Ok(serialized_param) = serde_json::to_value(¶meter) {
self.params.push(serialized_param);
}
}
}