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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
pub mod account;
pub mod chain;
pub mod info;
pub mod state;
use std::str;
use futures::{future::BoxFuture, TryFutureExt};
use http::Response;
use hyper::Body;
use serde::{Deserialize, Serialize};
use warp::{
filters::BoxedFilter,
reject::{self, Reject},
Filter,
};
use warp_json_rpc::{filters, Builder};
use super::{ApiRequest, ReactorEventT};
use crate::effect::EffectBuilder;
pub const RPC_API_PATH: &str = "rpc";
#[repr(i64)]
enum ErrorCode {
NoSuchDeploy = 32000,
NoSuchBlock = 32001,
ParseQueryKey = 32002,
QueryFailed = 32003,
QueryFailedToExecute = 32004,
ParseGetBalanceURef = 32005,
GetBalanceFailed = 32006,
GetBalanceFailedToExecute = 32007,
}
#[derive(Debug)]
pub(super) struct Error(String);
impl Reject for Error {}
impl From<anyhow::Error> for Error {
fn from(error: anyhow::Error) -> Self {
Error(error.to_string())
}
}
pub trait RpcWithParams {
const METHOD: &'static str;
type RequestParams: Serialize + for<'de> Deserialize<'de> + Send + 'static;
type ResponseResult: Serialize + for<'de> Deserialize<'de> + Send + 'static;
}
pub(super) trait RpcWithParamsExt: RpcWithParams {
fn create_filter<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
) -> BoxedFilter<(Response<Body>,)> {
warp::path(RPC_API_PATH)
.and(filters::json_rpc())
.and(filters::method(Self::METHOD))
.and(filters::params::<Self::RequestParams>())
.and_then(
move |response_builder: Builder, params: Self::RequestParams| {
Self::handle_request(effect_builder, response_builder, params)
.map_err(reject::custom)
},
)
.boxed()
}
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
params: Self::RequestParams,
) -> BoxFuture<'static, Result<Response<Body>, Error>>;
}
pub trait RpcWithoutParams {
const METHOD: &'static str;
type ResponseResult: Serialize + for<'de> Deserialize<'de> + Send + 'static;
}
pub(super) trait RpcWithoutParamsExt: RpcWithoutParams {
fn create_filter<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
) -> BoxedFilter<(Response<Body>,)> {
warp::path(RPC_API_PATH)
.and(filters::json_rpc())
.and(filters::method(Self::METHOD))
.and_then(move |response_builder: Builder| {
Self::handle_request(effect_builder, response_builder).map_err(reject::custom)
})
.boxed()
}
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
) -> BoxFuture<'static, Result<Response<Body>, Error>>;
}
pub trait RpcWithOptionalParams {
const METHOD: &'static str;
type OptionalRequestParams: Serialize + for<'de> Deserialize<'de> + Send + 'static;
type ResponseResult: Serialize + for<'de> Deserialize<'de> + Send + 'static;
}
pub(super) trait RpcWithOptionalParamsExt: RpcWithOptionalParams {
fn create_filter<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
) -> BoxedFilter<(Response<Body>,)> {
let with_params = warp::path(RPC_API_PATH)
.and(filters::json_rpc())
.and(filters::method(Self::METHOD))
.and(filters::params::<Self::OptionalRequestParams>())
.and_then(
move |response_builder: Builder, params: Self::OptionalRequestParams| {
Self::handle_request(effect_builder, response_builder, Some(params))
.map_err(reject::custom)
},
);
let without_params = warp::path(RPC_API_PATH)
.and(filters::json_rpc())
.and(filters::method(Self::METHOD))
.and_then(move |response_builder: Builder| {
Self::handle_request(effect_builder, response_builder, None).map_err(reject::custom)
});
with_params.or(without_params).unify().boxed()
}
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
maybe_params: Option<Self::OptionalRequestParams>,
) -> BoxFuture<'static, Result<Response<Body>, Error>>;
}