This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
routes.rs
369 lines (335 loc) · 12.2 KB
/
routes.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
pub mod index;
pub mod peers;
pub mod post;
pub mod rfc003;
use crate::{
http_api::{
action::ActionResponseBody,
halight::HalightFinalized,
herc20::Herc20Finalized,
problem,
protocol::{GetAlphaEvents, GetAlphaParams, GetBetaEvents, GetBetaParams, GetRole},
route_factory, ActionNotFound, AliceSwap, BobSwap, Http, Swap,
},
storage::Load,
swap_protocols::{
DeployAction, Facade, FundAction, InitAction, LocalSwapId, RedeemAction, RefundAction, Role,
},
};
use ::comit::Protocol;
use comit::asset;
use http_api_problem::HttpApiProblem;
use serde::Serialize;
use warp::{http, http::StatusCode, Rejection, Reply};
pub fn into_rejection(problem: HttpApiProblem) -> Rejection {
warp::reject::custom(problem)
}
#[allow(clippy::needless_pass_by_value)]
pub async fn get_swap(swap_id: LocalSwapId, facade: Facade) -> Result<impl Reply, Rejection> {
handle_get_swap(facade, swap_id)
.await
.map(|swap_resource| warp::reply::json(&swap_resource))
.map_err(problem::from_anyhow)
.map_err(into_rejection)
}
pub async fn handle_get_swap(
facade: Facade,
swap_id: LocalSwapId,
) -> anyhow::Result<siren::Entity> {
match facade.load(swap_id).await? {
Swap {
alpha: Protocol::Herc20,
beta: Protocol::Halight,
role: Role::Alice,
} => {
let swap: AliceSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(swap_id).await?;
make_swap_entity(swap_id, swap)
}
Swap {
alpha: Protocol::Herc20,
beta: Protocol::Halight,
role: Role::Bob,
} => {
let swap: BobSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(swap_id).await?;
make_swap_entity(swap_id, swap)
}
Swap {
alpha: Protocol::Halight,
beta: Protocol::Herc20,
role: Role::Alice,
} => {
let swap: AliceSwap<asset::Bitcoin, asset::Erc20, HalightFinalized, Herc20Finalized> =
facade.load(swap_id).await?;
make_swap_entity(swap_id, swap)
}
Swap {
alpha: Protocol::Halight,
beta: Protocol::Herc20,
role: Role::Bob,
} => {
let swap: BobSwap<asset::Bitcoin, asset::Erc20, HalightFinalized, Herc20Finalized> =
facade.load(swap_id).await?;
make_swap_entity(swap_id, swap)
}
_ => unimplemented!("other combinations not suported yet"),
}
}
fn make_swap_entity<S>(swap_id: LocalSwapId, swap: S) -> anyhow::Result<siren::Entity>
where
S: GetRole
+ GetAlphaParams
+ GetBetaParams
+ GetAlphaEvents
+ GetBetaEvents
+ DeployAction
+ InitAction
+ FundAction
+ RedeemAction
+ RefundAction
+ Clone,
{
let role = swap.get_role();
let swap_resource = SwapResource { role: Http(role) };
let mut entity = siren::Entity::default()
.with_class_member("swap")
.with_properties(swap_resource)
.map_err(|e| {
tracing::error!("failed to set properties of entity: {:?}", e);
HttpApiProblem::with_title_and_type_from_status(StatusCode::INTERNAL_SERVER_ERROR)
})?
.with_link(siren::NavigationalLink::new(
&["self"],
route_factory::swap_path(swap_id),
));
let alpha_params = swap.get_alpha_params();
let alpha_params_sub = siren::SubEntity::from_entity(
siren::Entity::default()
.with_class_member("parameters")
.with_properties(alpha_params)
.map_err(|e| {
tracing::error!("failed to set properties of entity: {:?}", e);
HttpApiProblem::with_title_and_type_from_status(StatusCode::INTERNAL_SERVER_ERROR)
})?,
&["alpha"],
);
entity.push_sub_entity(alpha_params_sub);
let beta_params = swap.get_beta_params();
let beta_params_sub = siren::SubEntity::from_entity(
siren::Entity::default()
.with_class_member("parameters")
.with_properties(beta_params)
.map_err(|e| {
tracing::error!("failed to set properties of entity: {:?}", e);
HttpApiProblem::with_title_and_type_from_status(StatusCode::INTERNAL_SERVER_ERROR)
})?,
&["beta"],
);
entity.push_sub_entity(beta_params_sub);
match (swap.get_alpha_events(), swap.get_beta_events()) {
(Some(alpha_tx), Some(beta_tx)) => {
let alpha_state_sub = siren::SubEntity::from_entity(
siren::Entity::default()
.with_class_member("state")
.with_properties(alpha_tx)
.map_err(|e| {
tracing::error!("failed to set properties of entity: {:?}", e);
HttpApiProblem::with_title_and_type_from_status(
StatusCode::INTERNAL_SERVER_ERROR,
)
})?,
&["alpha"],
);
entity.push_sub_entity(alpha_state_sub);
let beta_state_sub = siren::SubEntity::from_entity(
siren::Entity::default()
.with_class_member("state")
.with_properties(beta_tx)
.map_err(|e| {
tracing::error!("failed to set properties of entity: {:?}", e);
HttpApiProblem::with_title_and_type_from_status(
StatusCode::INTERNAL_SERVER_ERROR,
)
})?,
&["beta"],
);
entity.push_sub_entity(beta_state_sub);
let maybe_action_names = vec![
swap.init_action().map(|_| "init"),
swap.deploy_action().map(|_| "deploy"),
swap.fund_action().map(|_| "fund"),
swap.redeem_action().map(|_| "redeem"),
swap.refund_action().map(|_| "refund"),
];
Ok(maybe_action_names
.into_iter()
.filter_map(|action| action.ok())
.fold(entity, |acc, action_name| {
let siren_action = make_siren_action(swap_id, action_name);
acc.with_action(siren_action)
}))
}
_ => Ok(entity),
}
}
fn make_siren_action(swap_id: LocalSwapId, action_name: &str) -> siren::Action {
siren::Action {
name: action_name.to_owned(),
class: vec![],
method: Some(http::Method::GET),
href: format!("/swaps/{}/{}", swap_id, action_name),
title: None,
_type: None,
fields: vec![],
}
}
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, Serialize, PartialEq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum SwapStatus {
Created,
InProgress,
Swapped,
NotSwapped,
}
#[derive(Debug, Serialize)]
struct SwapResource {
pub role: Http<Role>,
}
#[allow(clippy::needless_pass_by_value)]
pub async fn action_init(swap_id: LocalSwapId, facade: Facade) -> Result<impl Reply, Rejection> {
handle_action_init(swap_id, facade)
.await
.map(|body| warp::reply::json(&body))
.map_err(problem::from_anyhow)
.map_err(into_rejection)
}
#[allow(clippy::unit_arg, clippy::let_unit_value, clippy::cognitive_complexity)]
async fn handle_action_init(id: LocalSwapId, facade: Facade) -> anyhow::Result<ActionResponseBody> {
let swap: AliceSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(id).await?;
let action = swap.init_action()?;
let response = ActionResponseBody::from(action);
Ok(response)
}
#[allow(clippy::needless_pass_by_value)]
pub async fn action_deploy(swap_id: LocalSwapId, facade: Facade) -> Result<impl Reply, Rejection> {
handle_action_deploy(swap_id, facade)
.await
.map(|body| warp::reply::json(&body))
.map_err(problem::from_anyhow)
.map_err(into_rejection)
}
#[allow(clippy::unit_arg, clippy::let_unit_value, clippy::cognitive_complexity)]
async fn handle_action_deploy(
id: LocalSwapId,
facade: Facade,
) -> anyhow::Result<ActionResponseBody> {
let swap: AliceSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(id).await?;
let action = swap.deploy_action()?;
let response = ActionResponseBody::from(action);
Ok(response)
}
#[allow(clippy::needless_pass_by_value)]
pub async fn action_fund(swap_id: LocalSwapId, facade: Facade) -> Result<impl Reply, Rejection> {
handle_action_fund(swap_id, facade)
.await
.map(|body| warp::reply::json(&body))
.map_err(problem::from_anyhow)
.map_err(into_rejection)
}
#[allow(clippy::unit_arg, clippy::let_unit_value, clippy::cognitive_complexity)]
async fn handle_action_fund(id: LocalSwapId, facade: Facade) -> anyhow::Result<ActionResponseBody> {
let response = match facade.load(id).await? {
Swap {
alpha: Protocol::Herc20,
beta: Protocol::Halight,
role: Role::Alice,
} => {
let swap: AliceSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(id).await?;
let action = swap.fund_action()?;
ActionResponseBody::from(action)
}
Swap {
alpha: Protocol::Herc20,
beta: Protocol::Halight,
role: Role::Bob,
} => {
let swap: BobSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(id).await?;
let action = swap.fund_action()?;
ActionResponseBody::from(action)
}
_ => anyhow::bail!(ActionNotFound),
};
Ok(response)
}
#[allow(clippy::needless_pass_by_value)]
pub async fn action_redeem(swap_id: LocalSwapId, facade: Facade) -> Result<impl Reply, Rejection> {
handle_action_redeem(swap_id, facade)
.await
.map(|body| warp::reply::json(&body))
.map_err(problem::from_anyhow)
.map_err(into_rejection)
}
#[allow(clippy::unit_arg, clippy::let_unit_value, clippy::cognitive_complexity)]
async fn handle_action_redeem(
id: LocalSwapId,
facade: Facade,
) -> anyhow::Result<ActionResponseBody> {
let response = match facade.load(id).await? {
Swap {
alpha: Protocol::Herc20,
beta: Protocol::Halight,
role: Role::Alice,
} => {
let swap: AliceSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(id).await?;
let action = swap.redeem_action()?;
ActionResponseBody::from(action)
}
Swap {
alpha: Protocol::Herc20,
beta: Protocol::Halight,
role: Role::Bob,
} => {
let swap: BobSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(id).await?;
let action = swap.redeem_action()?;
ActionResponseBody::from(action)
}
_ => return Err(ActionNotFound.into()),
};
Ok(response)
}
#[allow(clippy::needless_pass_by_value)]
pub async fn action_refund(swap_id: LocalSwapId, facade: Facade) -> Result<impl Reply, Rejection> {
handle_action_refund(swap_id, facade)
.await
.map(|body| warp::reply::json(&body))
.map_err(problem::from_anyhow)
.map_err(into_rejection)
}
#[allow(clippy::unit_arg, clippy::let_unit_value, clippy::cognitive_complexity)]
async fn handle_action_refund(
id: LocalSwapId,
facade: Facade,
) -> anyhow::Result<ActionResponseBody> {
match facade.load(id).await? {
Swap {
alpha: Protocol::Herc20,
beta: Protocol::Halight,
role: Role::Alice,
} => {
let swap: AliceSwap<asset::Erc20, asset::Bitcoin, Herc20Finalized, HalightFinalized> =
facade.load(id).await?;
let action = swap.refund_action()?;
let response = ActionResponseBody::from(action);
Ok(response)
}
_ => Err(ActionNotFound.into()),
}
}