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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use super::deserialize;
use resources::{asset::Flags, offer::PriceRatio, Amount, AssetIdentifier};
use serde::{de, Deserialize, Deserializer};
mod account_merge;
mod allow_trust;
mod change_trust;
mod create_account;
mod create_passive_offer;
mod manage_data;
mod manage_offer;
mod path_payment;
mod payment;
mod set_options;
pub use self::account_merge::AccountMerge;
pub use self::allow_trust::AllowTrust;
pub use self::change_trust::ChangeTrust;
pub use self::create_account::CreateAccount;
pub use self::create_passive_offer::CreatePassiveOffer;
pub use self::manage_data::ManageData;
pub use self::manage_offer::ManageOffer;
pub use self::path_payment::PathPayment;
pub use self::payment::Payment;
pub use self::set_options::SetOptions;
pub mod prelude {
pub use super::{
account_merge::AccountMerge, allow_trust::AllowTrust, change_trust::ChangeTrust,
create_account::CreateAccount, create_passive_offer::CreatePassiveOffer,
manage_data::ManageData, manage_offer::ManageOffer, path_payment::PathPayment,
payment::Payment, set_options::SetOptions, Operation, OperationKind,
};
}
#[cfg(test)]
mod test;
#[derive(Debug, Clone)]
pub struct Operation {
id: i64,
paging_token: String,
transaction_hash: String,
kind: Kind,
}
#[derive(Debug, Clone)]
pub enum OperationKind {
CreateAccount(CreateAccount),
Payment(Payment),
PathPayment(PathPayment),
ManageOffer(ManageOffer),
CreatePassiveOffer(CreatePassiveOffer),
SetOptions(SetOptions),
ChangeTrust(ChangeTrust),
AllowTrust(AllowTrust),
AccountMerge(AccountMerge),
Inflation,
ManageData(ManageData),
}
use self::OperationKind as Kind;
impl Operation {
pub fn id(&self) -> i64 {
self.id
}
pub fn paging_token(&self) -> &str {
&self.paging_token
}
pub fn transaction(&self) -> &str {
&self.transaction_hash
}
pub fn type_i(&self) -> u32 {
match self.kind {
Kind::CreateAccount(_) => 0,
Kind::Payment(_) => 1,
Kind::PathPayment(_) => 2,
Kind::ManageOffer(_) => 3,
Kind::CreatePassiveOffer(_) => 4,
Kind::SetOptions(_) => 5,
Kind::ChangeTrust(_) => 6,
Kind::AllowTrust(_) => 7,
Kind::AccountMerge(_) => 8,
Kind::Inflation => 9,
Kind::ManageData(_) => 10,
}
}
pub fn kind(&self) -> &Kind {
&self.kind
}
pub fn kind_name(&self) -> &str {
match self.kind {
Kind::CreateAccount(_) => "Create Account",
Kind::Payment(_) => "Payment",
Kind::PathPayment(_) => "Path Payment",
Kind::ManageOffer(_) => "Manage Offer",
Kind::CreatePassiveOffer(_) => "Create Passive Offer",
Kind::SetOptions(_) => "Set Options",
Kind::ChangeTrust(_) => "Change Trust",
Kind::AllowTrust(_) => "Allow Trust",
Kind::AccountMerge(_) => "Account Merge",
Kind::Inflation => "Inflation",
Kind::ManageData(_) => "Manage Data",
}
}
pub fn is_create_account(&self) -> bool {
match self.kind {
Kind::CreateAccount(_) => true,
_ => false,
}
}
pub fn is_payment(&self) -> bool {
match self.kind {
Kind::Payment(_) => true,
_ => false,
}
}
pub fn is_path_payment(&self) -> bool {
match self.kind {
Kind::PathPayment(_) => true,
_ => false,
}
}
pub fn is_manage_offer(&self) -> bool {
match self.kind {
Kind::ManageOffer(_) => true,
_ => false,
}
}
pub fn is_create_passive_offer(&self) -> bool {
match self.kind {
Kind::CreatePassiveOffer(_) => true,
_ => false,
}
}
pub fn is_set_options(&self) -> bool {
match self.kind {
Kind::SetOptions(_) => true,
_ => false,
}
}
pub fn is_inflation(&self) -> bool {
match self.kind {
Kind::Inflation => true,
_ => false,
}
}
pub fn is_change_trust(&self) -> bool {
match self.kind {
Kind::ChangeTrust(_) => true,
_ => false,
}
}
pub fn is_allow_trust(&self) -> bool {
match self.kind {
Kind::AllowTrust(_) => true,
_ => false,
}
}
pub fn is_account_merge(&self) -> bool {
match self.kind {
Kind::AccountMerge(_) => true,
_ => false,
}
}
pub fn is_manage_data(&self) -> bool {
match self.kind {
Kind::ManageData(_) => true,
_ => false,
}
}
}
#[derive(Debug, Deserialize, Clone)]
struct Intermediate {
#[serde(deserialize_with = "deserialize::from_str")]
id: i64,
paging_token: String,
transaction_hash: String,
#[serde(rename = "type")]
kind: String,
account: Option<String>,
funder: Option<String>,
starting_balance: Option<Amount>,
from: Option<String>,
to: Option<String>,
asset_type: Option<String>,
asset_code: Option<String>,
asset_issuer: Option<String>,
source_asset_type: Option<String>,
source_asset_code: Option<String>,
source_asset_issuer: Option<String>,
amount: Option<Amount>,
source_amount: Option<Amount>,
source_max: Option<Amount>,
buying_asset_type: Option<String>,
buying_asset_code: Option<String>,
buying_asset_issuer: Option<String>,
selling_asset_type: Option<String>,
selling_asset_code: Option<String>,
selling_asset_issuer: Option<String>,
offer_id: Option<i64>,
#[serde(rename = "price_r")]
price_ratio: Option<PriceRatio>,
price: Option<Amount>,
signer_key: Option<String>,
signer_weight: Option<u8>,
master_key_weight: Option<u8>,
low_threshold: Option<u32>,
med_threshold: Option<u32>,
high_threshold: Option<u32>,
home_domain: Option<String>,
set_flags: Option<Vec<u32>>,
set_flags_s: Option<Vec<String>>,
clear_flags: Option<Vec<u32>>,
clear_flags_s: Option<Vec<String>>,
trustor: Option<String>,
trustee: Option<String>,
authorize: Option<bool>,
limit: Option<Amount>,
into: Option<String>,
name: Option<String>,
value: Option<String>,
}
impl<'de> Deserialize<'de> for Operation {
fn deserialize<D>(d: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let rep = Intermediate::deserialize(d)?;
let kind: Kind = match rep.kind.as_str() {
"create_account" => match rep {
Intermediate {
account: Some(account),
funder: Some(funder),
starting_balance: Some(starting_balance),
..
} => Kind::CreateAccount(CreateAccount::new(account, funder, starting_balance)),
_ => {
return Err(de::Error::custom(
"Missing fields for create_account operation.",
))
}
},
"path_payment" => match rep {
Intermediate {
from: Some(from),
to: Some(to),
asset_code,
asset_issuer,
asset_type: Some(asset_type),
amount: Some(amount),
source_asset_code,
source_asset_issuer,
source_asset_type: Some(source_asset_type),
source_max: Some(source_max),
..
} => {
let destination_asset_identifier =
AssetIdentifier::new(&asset_type, asset_code, asset_issuer)
.map_err(de::Error::custom)?;
let source_asset_identifier = AssetIdentifier::new(
&source_asset_type,
source_asset_code,
source_asset_issuer,
).map_err(de::Error::custom)?;
Kind::PathPayment(PathPayment::new(
from,
to,
destination_asset_identifier,
amount,
source_asset_identifier,
source_max,
))
}
_ => {
return Err(de::Error::custom(
"Missing fields for path_payment operation.",
))
}
},
"payment" => match rep {
Intermediate {
from: Some(from),
to: Some(to),
asset_code,
asset_issuer,
asset_type: Some(asset_type),
amount: Some(amount),
..
} => {
let asset_identifier =
AssetIdentifier::new(&asset_type, asset_code, asset_issuer)
.map_err(de::Error::custom)?;
Kind::Payment(Payment::new(from, to, asset_identifier, amount))
}
_ => return Err(de::Error::custom("Missing fields for payment operation.")),
},
"create_passive_offer" => match rep {
Intermediate {
offer_id: Some(offer_id),
buying_asset_code,
buying_asset_issuer,
buying_asset_type: Some(buying_asset_type),
selling_asset_code,
selling_asset_issuer,
selling_asset_type: Some(selling_asset_type),
amount: Some(amount),
price_ratio: Some(price_ratio),
price: Some(price),
..
} => {
let buying_asset_identifier = AssetIdentifier::new(
&buying_asset_type,
buying_asset_code,
buying_asset_issuer,
).map_err(de::Error::custom)?;
let selling_asset_identifier = AssetIdentifier::new(
&selling_asset_type,
selling_asset_code,
selling_asset_issuer,
).map_err(de::Error::custom)?;
Kind::CreatePassiveOffer(CreatePassiveOffer::new(
offer_id,
selling_asset_identifier,
buying_asset_identifier,
amount,
price_ratio,
price,
))
}
_ => {
return Err(de::Error::custom(
"Missing fields for create_passive_offer operation.",
))
}
},
"manage_offer" => match rep {
Intermediate {
offer_id: Some(offer_id),
buying_asset_code,
buying_asset_issuer,
buying_asset_type: Some(buying_asset_type),
selling_asset_code,
selling_asset_issuer,
selling_asset_type: Some(selling_asset_type),
amount: Some(amount),
price_ratio: Some(price_ratio),
price: Some(price),
..
} => {
let buying_asset_identifier = AssetIdentifier::new(
&buying_asset_type,
buying_asset_code,
buying_asset_issuer,
).map_err(de::Error::custom)?;
let selling_asset_identifier = AssetIdentifier::new(
&selling_asset_type,
selling_asset_code,
selling_asset_issuer,
).map_err(de::Error::custom)?;
Kind::ManageOffer(ManageOffer::new(
offer_id,
selling_asset_identifier,
buying_asset_identifier,
amount,
price_ratio,
price,
))
}
_ => {
return Err(de::Error::custom(
"Missing fields for manage_offer operation.",
))
}
},
"set_options" => match rep {
Intermediate {
set_flags_s,
clear_flags_s,
signer_key: Some(signer_key),
signer_weight: Some(signer_weight),
master_key_weight: Some(master_key_weight),
low_threshold: Some(low),
med_threshold: Some(med),
high_threshold: Some(high),
home_domain: Some(home_domain),
..
} => {
let set_flags: Option<Flags> = match set_flags_s {
Some(vec_strings) => {
let auth_required =
vec_strings.iter().any(|e| e == "auth_required_flag");
let auth_revocable =
vec_strings.iter().any(|e| e == "auth_revocable_flag");
Some(Flags::new(auth_required, auth_revocable))
}
None => None,
};
let clear_flags: Option<Flags> = match clear_flags_s {
Some(vec_strings) => {
let auth_required =
vec_strings.iter().any(|e| e == "auth_required_flag");
let auth_revocable =
vec_strings.iter().any(|e| e == "auth_revocable_flag");
Some(Flags::new(auth_required, auth_revocable))
}
None => None,
};
Kind::SetOptions(SetOptions::new(
signer_key,
signer_weight,
master_key_weight,
(low, med, high),
home_domain,
set_flags,
clear_flags,
))
}
_ => {
return Err(de::Error::custom(
"Missing fields for set_options operation.",
))
}
},
"change_trust" => match rep {
Intermediate {
limit: Some(limit),
asset_code,
asset_issuer,
asset_type: Some(asset_type),
trustor: Some(trustor),
trustee: Some(trustee),
..
} => {
let asset = AssetIdentifier::new(&asset_type, asset_code, asset_issuer)
.map_err(de::Error::custom)?;
Kind::ChangeTrust(ChangeTrust::new(trustee, trustor, asset, limit))
}
_ => {
return Err(de::Error::custom(
"Missing fields for change_trust operation.",
))
}
},
"allow_trust" => match rep {
Intermediate {
authorize: Some(authorize),
asset_code,
asset_issuer,
asset_type: Some(asset_type),
trustor: Some(trustor),
trustee: Some(trustee),
..
} => {
let asset = AssetIdentifier::new(&asset_type, asset_code, asset_issuer)
.map_err(de::Error::custom)?;
Kind::AllowTrust(AllowTrust::new(trustee, trustor, asset, authorize))
}
_ => {
return Err(de::Error::custom(
"Missing fields for allow_trust operation.",
))
}
},
"account_merge" => match rep {
Intermediate {
account: Some(account),
into: Some(into),
..
} => Kind::AccountMerge(AccountMerge::new(account, into)),
_ => {
return Err(de::Error::custom(
"Missing fields for account_merge operation.",
))
}
},
"manage_data" => match rep {
Intermediate {
name: Some(name),
value: Some(value),
..
} => Kind::ManageData(ManageData::new(name, value)),
_ => {
return Err(de::Error::custom(
"Missing fields for manage_data operation.",
))
}
},
"inflation" => Kind::Inflation,
_ => return Err(de::Error::custom("Unknown operation type.")),
};
Ok(Operation {
id: rep.id,
paging_token: rep.paging_token,
transaction_hash: rep.transaction_hash,
kind,
})
}
}