stellar_base/operations/
mod.rs

1//! Operations that mutate the ledger state.
2use std::io::{Read, Write};
3
4use crate::crypto::MuxedAccount;
5use crate::error::Result;
6use crate::xdr;
7
8mod account_merge;
9mod allow_trust;
10mod begin_sponsoring_future_reserves;
11mod bump_sequence;
12mod change_trust;
13mod claim_claimable_balance;
14mod clawback;
15mod clawback_claimable_balance;
16mod create_account;
17mod create_claimable_balance;
18mod create_passive_sell_offer;
19mod end_sponsoring_future_reserves;
20mod extend_footprint_ttl;
21mod inflation;
22mod invoke_host_function;
23mod liquidity_pool_deposit;
24mod liquidity_pool_withdraw;
25mod manage_buy_offer;
26mod manage_data;
27mod manage_sell_offer;
28mod path_payment_strict_receive;
29mod path_payment_strict_send;
30mod payment;
31mod restore_footprint;
32mod revoke_sponsorship;
33mod set_options;
34mod set_trustline_flags;
35#[cfg(test)]
36pub mod tests;
37
38use crate::operations::clawback::ClawbackOperationBuilder;
39use crate::operations::clawback_claimable_balance::{
40    ClawbackClaimableBalanceOperation, ClawbackClaimableBalanceOperationBuilder,
41};
42use crate::operations::liquidity_pool_deposit::{
43    LiquidityPoolDepositOperation, LiquidityPoolDepositOperationBuilder,
44};
45use crate::operations::liquidity_pool_withdraw::{
46    LiquidityPoolWithdrawOperation, LiquidityPoolWithdrawOperationBuilder,
47};
48use crate::operations::set_trustline_flags::{
49    SetTrustLineFlagsOperation, SetTrustLineFlagsOperationBuilder,
50};
51pub use account_merge::{AccountMergeOperation, AccountMergeOperationBuilder};
52pub use allow_trust::{AllowTrustOperation, AllowTrustOperationBuilder};
53pub use begin_sponsoring_future_reserves::{
54    BeginSponsoringFutureReservesOperation, BeginSponsoringFutureReservesOperationBuilder,
55};
56pub use bump_sequence::{BumpSequenceOperation, BumpSequenceOperationBuilder};
57pub use change_trust::{ChangeTrustOperation, ChangeTrustOperationBuilder};
58pub use claim_claimable_balance::{
59    ClaimClaimableBalanceOperation, ClaimClaimableBalanceOperationBuilder,
60};
61pub use clawback::ClawbackOperation;
62pub use create_account::{CreateAccountOperation, CreateAccountOperationBuilder};
63pub use create_claimable_balance::{
64    CreateClaimableBalanceOperation, CreateClaimableBalanceOperationBuilder,
65};
66pub use create_passive_sell_offer::{
67    CreatePassiveSellOfferOperation, CreatePassiveSellOfferOperationBuilder,
68};
69pub use end_sponsoring_future_reserves::{
70    EndSponsoringFutureReservesOperation, EndSponsoringFutureReservesOperationBuilder,
71};
72pub use extend_footprint_ttl::{ExtendFootprintTtlOperation, ExtendFootprintTtlOperationBuilder};
73pub use inflation::{InflationOperation, InflationOperationBuilder};
74pub use invoke_host_function::{InvokeHostFunctionOperation, InvokeHostFunctionOperationBuilder};
75pub use manage_buy_offer::{ManageBuyOfferOperation, ManageBuyOfferOperationBuilder};
76pub use manage_data::{ManageDataOperation, ManageDataOperationBuilder};
77pub use manage_sell_offer::{ManageSellOfferOperation, ManageSellOfferOperationBuilder};
78pub use path_payment_strict_receive::{
79    PathPaymentStrictReceiveOperation, PathPaymentStrictReceiveOperationBuilder,
80};
81pub use path_payment_strict_send::{
82    PathPaymentStrictSendOperation, PathPaymentStrictSendOperationBuilder,
83};
84pub use payment::{PaymentOperation, PaymentOperationBuilder};
85pub use restore_footprint::{RestoreFootprintOperation, RestoreFootprintOperationBuilder};
86pub use revoke_sponsorship::{RevokeSponsorshipOperation, RevokeSponsorshipOperationBuilder};
87pub use set_options::{SetOptionsOperation, SetOptionsOperationBuilder};
88
89/// Operations on a Stellar network.
90#[derive(Debug, Clone, PartialEq, Eq)]
91pub enum Operation {
92    /// Create and fund a non existing account.
93    CreateAccount(CreateAccountOperation),
94    /// Create a payment.
95    Payment(PaymentOperation),
96    /// Send the specified amount of asset, optionally through a path.
97    PathPaymentStrictReceive(PathPaymentStrictReceiveOperation),
98    /// Create, update, or delete a sell offer.
99    ManageSellOffer(ManageSellOfferOperation),
100    /// Create a passive sell offer. This offer won't consume a counter offer that exactly matches this offer.
101    CreatePassiveSellOffer(CreatePassiveSellOfferOperation),
102    /// Change the account options.
103    SetOptions(SetOptionsOperation),
104    /// Add, remove, or update a trust line for the given asset.
105    ChangeTrust(ChangeTrustOperation),
106    /// Authorize another account to hold your account's credit aset.
107    AllowTrust(AllowTrustOperation),
108    /// Transfer all of account balance into destination account.
109    AccountMerge(AccountMergeOperation),
110    /// Generate the inflation.
111    Inflation(InflationOperation),
112    /// Add o remove data entry.
113    ManageData(ManageDataOperation),
114    /// Bumps the account sequence number.
115    BumpSequence(BumpSequenceOperation),
116    /// Create, update, or delete a buy offer.
117    ManageBuyOffer(ManageBuyOfferOperation),
118    /// Send the specified amount of asset, optionally through a path.
119    PathPaymentStrictSend(PathPaymentStrictSendOperation),
120    /// Create a new claimable balance.
121    CreateClaimableBalance(CreateClaimableBalanceOperation),
122    /// Claim a claimable balance.
123    ClaimClaimableBalance(ClaimClaimableBalanceOperation),
124    /// Begin sponsoring future reserves for an account.
125    /// Needs a matching EndSponsoringFutureReserves in the same transaction.
126    BeginSponsoringFutureReserves(BeginSponsoringFutureReservesOperation),
127    /// Ends a BeginSponsoringFutureReserves operation in the transaction.
128    EndSponsoringFutureReserves(EndSponsoringFutureReservesOperation),
129    /// Revoke a reserve sponsorship.
130    RevokeSponsorship(RevokeSponsorshipOperation),
131    Clawback(ClawbackOperation),
132    ClawbackClaimableBalance(ClawbackClaimableBalanceOperation),
133    SetTrustLineFlags(SetTrustLineFlagsOperation),
134    LiquidityPoolDeposit(LiquidityPoolDepositOperation),
135    LiquidityPoolWithdraw(LiquidityPoolWithdrawOperation),
136    InvokeHostFunction(InvokeHostFunctionOperation),
137    ExtendFootprintTtl(ExtendFootprintTtlOperation),
138    RestoreFootprint(RestoreFootprintOperation),
139}
140
141impl Operation {
142    /// Creates a new create account operation builder.
143    pub fn new_create_account() -> CreateAccountOperationBuilder {
144        CreateAccountOperationBuilder::new()
145    }
146
147    /// Creates a new payment operation builder.
148    pub fn new_payment() -> PaymentOperationBuilder {
149        PaymentOperationBuilder::new()
150    }
151
152    /// Creates a new path payment strict receive operation builder.
153    pub fn new_path_payment_strict_receive() -> PathPaymentStrictReceiveOperationBuilder {
154        PathPaymentStrictReceiveOperationBuilder::new()
155    }
156
157    /// Creates a new manage sell offer operation builder.
158    pub fn new_manage_sell_offer() -> ManageSellOfferOperationBuilder {
159        ManageSellOfferOperationBuilder::new()
160    }
161
162    /// Creates a new create passive sell offer operation builder.
163    pub fn new_create_passive_sell_offer() -> CreatePassiveSellOfferOperationBuilder {
164        CreatePassiveSellOfferOperationBuilder::new()
165    }
166
167    /// Creates a new set options operation builder.
168    pub fn new_set_options() -> SetOptionsOperationBuilder {
169        SetOptionsOperationBuilder::new()
170    }
171
172    /// Creates a new change trust operation builder.
173    pub fn new_change_trust() -> ChangeTrustOperationBuilder {
174        ChangeTrustOperationBuilder::new()
175    }
176
177    /// Creates a new allow trust operation builder.
178    pub fn new_allow_trust() -> AllowTrustOperationBuilder {
179        AllowTrustOperationBuilder::new()
180    }
181
182    /// Creates a new account merge operation builder.
183    pub fn new_account_merge() -> AccountMergeOperationBuilder {
184        AccountMergeOperationBuilder::new()
185    }
186
187    /// Creates a new inflation operation builder.
188    pub fn new_inflation() -> InflationOperationBuilder {
189        InflationOperationBuilder::new()
190    }
191
192    /// Creates a new manage data operation builder.
193    pub fn new_manage_data() -> ManageDataOperationBuilder {
194        ManageDataOperationBuilder::new()
195    }
196
197    /// Creates a new bump sequence operation builder.
198    pub fn new_bump_sequence() -> BumpSequenceOperationBuilder {
199        BumpSequenceOperationBuilder::new()
200    }
201
202    /// Creates a new manage buy offer operation builder.
203    pub fn new_manage_buy_offer() -> ManageBuyOfferOperationBuilder {
204        ManageBuyOfferOperationBuilder::new()
205    }
206
207    /// Creates a new path payment strict send operation builder.
208    pub fn new_path_payment_strict_send() -> PathPaymentStrictSendOperationBuilder {
209        PathPaymentStrictSendOperationBuilder::new()
210    }
211
212    /// Creates a new create claimable balance operation builder.
213    pub fn new_create_claimable_balance() -> CreateClaimableBalanceOperationBuilder {
214        CreateClaimableBalanceOperationBuilder::new()
215    }
216
217    /// Creates a new claim claimable balance operation builder.
218    pub fn new_claim_claimable_balance() -> ClaimClaimableBalanceOperationBuilder {
219        ClaimClaimableBalanceOperationBuilder::new()
220    }
221
222    /// Creates a new begin sponsoring future reserves operation builder.
223    pub fn new_begin_sponsoring_future_reserves() -> BeginSponsoringFutureReservesOperationBuilder {
224        BeginSponsoringFutureReservesOperationBuilder::new()
225    }
226
227    /// Creates a new end sponsoring future reserves operation builder.
228    pub fn new_end_sponsoring_future_reserves() -> EndSponsoringFutureReservesOperationBuilder {
229        EndSponsoringFutureReservesOperationBuilder::new()
230    }
231
232    /// Creates a new revoke sponsorship operation builder.
233    pub fn new_revoke_sponsorship() -> RevokeSponsorshipOperationBuilder {
234        RevokeSponsorshipOperationBuilder::new()
235    }
236
237    pub fn new_clawback() -> ClawbackOperationBuilder {
238        ClawbackOperationBuilder::new()
239    }
240
241    pub fn new_clawback_claimable_balance() -> ClawbackClaimableBalanceOperationBuilder {
242        ClawbackClaimableBalanceOperationBuilder::new()
243    }
244
245    pub fn new_set_trustline_flags() -> SetTrustLineFlagsOperationBuilder {
246        SetTrustLineFlagsOperationBuilder::new()
247    }
248
249    pub fn new_liquidity_pool_deposit() -> LiquidityPoolDepositOperationBuilder {
250        LiquidityPoolDepositOperationBuilder::new()
251    }
252
253    pub fn new_liquidity_pool_withdraw() -> LiquidityPoolWithdrawOperationBuilder {
254        LiquidityPoolWithdrawOperationBuilder::new()
255    }
256
257    pub fn new_invoke_host_function() -> InvokeHostFunctionOperationBuilder {
258        InvokeHostFunctionOperationBuilder::new()
259    }
260
261    /// Creates a new extend footprint ttl operation builder.
262    pub fn new_extend_footprint_ttl() -> ExtendFootprintTtlOperationBuilder {
263        ExtendFootprintTtlOperationBuilder::new()
264    }
265
266    pub fn new_restore_footprint() -> RestoreFootprintOperationBuilder {
267        RestoreFootprintOperationBuilder::new()
268    }
269
270    /// If the operation is a CreateAccount, returns its value. Returns None otherwise.
271    pub fn as_create_account(&self) -> Option<&CreateAccountOperation> {
272        match *self {
273            Operation::CreateAccount(ref op) => Some(op),
274            _ => None,
275        }
276    }
277
278    /// If the operation is a CreateAccount, returns its mutable value. Returns None otherwise.
279    pub fn as_create_account_mut(&mut self) -> Option<&mut CreateAccountOperation> {
280        match *self {
281            Operation::CreateAccount(ref mut op) => Some(op),
282            _ => None,
283        }
284    }
285
286    /// Returns true if the operation is a CreateAccount.
287    pub fn is_create_account(&self) -> bool {
288        self.as_create_account().is_some()
289    }
290
291    /// If the operation is a Payment, returns its value. Returns None otherwise.
292    pub fn as_payment(&self) -> Option<&PaymentOperation> {
293        match *self {
294            Operation::Payment(ref op) => Some(op),
295            _ => None,
296        }
297    }
298
299    /// If the operation is a Payment, returns its value. Returns None otherwise.
300    pub fn as_payment_mut(&mut self) -> Option<&mut PaymentOperation> {
301        match *self {
302            Operation::Payment(ref mut op) => Some(op),
303            _ => None,
304        }
305    }
306
307    /// Returns true if the operation is a Payment.
308    pub fn is_payment(&self) -> bool {
309        self.as_payment().is_some()
310    }
311
312    /// If the operation is a PathPaymentStrictReceive, returns its value. Returns None otherwise.
313    pub fn as_path_payment_strict_receive(&self) -> Option<&PathPaymentStrictReceiveOperation> {
314        match *self {
315            Operation::PathPaymentStrictReceive(ref op) => Some(op),
316            _ => None,
317        }
318    }
319
320    /// If the operation is a PathPaymentStrictReceive, returns its mutable value. Returns None otherwise.
321    pub fn as_path_payment_strict_receive_mut(
322        &mut self,
323    ) -> Option<&mut PathPaymentStrictReceiveOperation> {
324        match *self {
325            Operation::PathPaymentStrictReceive(ref mut op) => Some(op),
326            _ => None,
327        }
328    }
329
330    /// Returns true if the operation is a PathPaymentStrictReceive.
331    pub fn is_path_payment_strict_receive(&self) -> bool {
332        self.as_path_payment_strict_receive().is_some()
333    }
334
335    /// If the operation is a ManageSellOffer, returns its value. Returns None otherwise.
336    pub fn as_manage_sell_offer(&self) -> Option<&ManageSellOfferOperation> {
337        match *self {
338            Operation::ManageSellOffer(ref op) => Some(op),
339            _ => None,
340        }
341    }
342
343    /// If the operation is a ManageSellOffer, returns its mutable value. Returns None otherwise.
344    pub fn as_manage_sell_offer_mut(&mut self) -> Option<&mut ManageSellOfferOperation> {
345        match *self {
346            Operation::ManageSellOffer(ref mut op) => Some(op),
347            _ => None,
348        }
349    }
350
351    /// Returns true if the operation is a ManageSellOffer.
352    pub fn is_manage_sell_offer(&self) -> bool {
353        self.as_manage_sell_offer().is_some()
354    }
355
356    /// If the operation is a CreatePassiveSellOffer, returns its value. Returns None otherwise.
357    pub fn as_create_passive_sell_offer(&self) -> Option<&CreatePassiveSellOfferOperation> {
358        match *self {
359            Operation::CreatePassiveSellOffer(ref op) => Some(op),
360            _ => None,
361        }
362    }
363
364    /// If the operation is a CreatePassiveSellOffer, returns its mutable value. Returns None otherwise.
365    pub fn as_create_passive_sell_offer_mut(
366        &mut self,
367    ) -> Option<&mut CreatePassiveSellOfferOperation> {
368        match *self {
369            Operation::CreatePassiveSellOffer(ref mut op) => Some(op),
370            _ => None,
371        }
372    }
373
374    /// Returns true if the operation is a CreatePassiveSellOffer.
375    pub fn is_create_passive_sell_offer(&self) -> bool {
376        self.as_create_passive_sell_offer().is_some()
377    }
378
379    /// If the operation is a SetOptions, returns its value. Returns None otherwise.
380    pub fn as_set_options(&self) -> Option<&SetOptionsOperation> {
381        match *self {
382            Operation::SetOptions(ref op) => Some(op),
383            _ => None,
384        }
385    }
386
387    /// If the operation is a SetOptions, returns its mutable value. Returns None otherwise.
388    pub fn as_set_options_mut(&mut self) -> Option<&mut SetOptionsOperation> {
389        match *self {
390            Operation::SetOptions(ref mut op) => Some(op),
391            _ => None,
392        }
393    }
394
395    /// Returns true if the operation is a SetOptions.
396    pub fn is_set_options(&self) -> bool {
397        self.as_set_options().is_some()
398    }
399
400    /// If the operation is a ChangeTrust, returns its value. Returns None otherwise.
401    pub fn as_change_trust(&self) -> Option<&ChangeTrustOperation> {
402        match *self {
403            Operation::ChangeTrust(ref op) => Some(op),
404            _ => None,
405        }
406    }
407
408    /// If the operation is a ChangeTrust, returns its mutable value. Returns None otherwise.
409    pub fn as_change_trust_mut(&mut self) -> Option<&ChangeTrustOperation> {
410        match *self {
411            Operation::ChangeTrust(ref mut op) => Some(op),
412            _ => None,
413        }
414    }
415
416    /// Returns true if the operation is a ChangeTrust.
417    pub fn is_change_trust(&self) -> bool {
418        self.as_change_trust().is_some()
419    }
420
421    /// If the operation is a AllowTrust, returns its value. Returns None otherwise.
422    pub fn as_allow_trust(&self) -> Option<&AllowTrustOperation> {
423        match *self {
424            Operation::AllowTrust(ref op) => Some(op),
425            _ => None,
426        }
427    }
428
429    /// If the operation is a AllowTrust, returns its mutable value. Returns None otherwise.
430    pub fn as_allow_trust_mut(&mut self) -> Option<&mut AllowTrustOperation> {
431        match *self {
432            Operation::AllowTrust(ref mut op) => Some(op),
433            _ => None,
434        }
435    }
436
437    /// Returns true if the operation is a AllowTrust.
438    pub fn is_allow_trust(&self) -> bool {
439        self.as_allow_trust().is_some()
440    }
441
442    /// If the operation is a AccountMerge, returns its value. Returns None otherwise.
443    pub fn as_account_merge(&self) -> Option<&AccountMergeOperation> {
444        match *self {
445            Operation::AccountMerge(ref op) => Some(op),
446            _ => None,
447        }
448    }
449
450    /// If the operation is a AccountMerge, returns its mutable value. Returns None otherwise.
451    pub fn as_account_merge_mut(&mut self) -> Option<&mut AccountMergeOperation> {
452        match *self {
453            Operation::AccountMerge(ref mut op) => Some(op),
454            _ => None,
455        }
456    }
457
458    /// Returns true if the operation is a AccountMerge.
459    pub fn is_account_merge(&self) -> bool {
460        self.as_account_merge().is_some()
461    }
462
463    /// If the operation is a Inflation, returns its value. Returns None otherwise.
464    pub fn as_inflation(&self) -> Option<&InflationOperation> {
465        match *self {
466            Operation::Inflation(ref op) => Some(op),
467            _ => None,
468        }
469    }
470
471    /// If the operation is a Inflation, returns its mutable value. Returns None otherwise.
472    pub fn as_inflation_mut(&mut self) -> Option<&mut InflationOperation> {
473        match *self {
474            Operation::Inflation(ref mut op) => Some(op),
475            _ => None,
476        }
477    }
478
479    /// Returns true if the operation is a Inflation.
480    pub fn is_inflation(&self) -> bool {
481        self.as_inflation().is_some()
482    }
483
484    /// If the operation is a ManageData, returns its value. Returns None otherwise.
485    pub fn as_manage_data(&self) -> Option<&ManageDataOperation> {
486        match *self {
487            Operation::ManageData(ref op) => Some(op),
488            _ => None,
489        }
490    }
491
492    /// If the operation is a ManageData, returns its mutable value. Returns None otherwise.
493    pub fn as_manage_data_mut(&mut self) -> Option<&mut ManageDataOperation> {
494        match *self {
495            Operation::ManageData(ref mut op) => Some(op),
496            _ => None,
497        }
498    }
499
500    /// Returns true if the operation is a ManageData.
501    pub fn is_manage_data(&self) -> bool {
502        self.as_manage_data().is_some()
503    }
504
505    /// If the operation is a BumpSequence, returns its value. Returns None otherwise.
506    pub fn as_bump_sequence(&self) -> Option<&BumpSequenceOperation> {
507        match *self {
508            Operation::BumpSequence(ref op) => Some(op),
509            _ => None,
510        }
511    }
512
513    /// If the operation is a BumpSequence, returns its mutable value. Returns None otherwise.
514    pub fn as_bump_sequence_mut(&mut self) -> Option<&mut BumpSequenceOperation> {
515        match *self {
516            Operation::BumpSequence(ref mut op) => Some(op),
517            _ => None,
518        }
519    }
520
521    /// Returns true if the operation is a BumpSequence.
522    pub fn is_bump_sequence(&self) -> bool {
523        self.as_bump_sequence().is_some()
524    }
525
526    /// If the operation is a ManageBuyOffer, returns its value. Returns None otherwise.
527    pub fn as_manage_buy_offer(&self) -> Option<&ManageBuyOfferOperation> {
528        match *self {
529            Operation::ManageBuyOffer(ref op) => Some(op),
530            _ => None,
531        }
532    }
533
534    /// If the operation is a ManageBuyOffer, returns its mutable value. Returns None otherwise.
535    pub fn as_manage_buy_offer_mut(&mut self) -> Option<&mut ManageBuyOfferOperation> {
536        match *self {
537            Operation::ManageBuyOffer(ref mut op) => Some(op),
538            _ => None,
539        }
540    }
541
542    /// Returns true if the operation is a ManageBuyOffer.
543    pub fn is_manage_buy_offer(&self) -> bool {
544        self.as_manage_buy_offer().is_some()
545    }
546
547    /// If the operation is a PathPaymentStrictSend, returns its value. Returns None otherwise.
548    pub fn as_path_payment_strict_send(&self) -> Option<&PathPaymentStrictSendOperation> {
549        match *self {
550            Operation::PathPaymentStrictSend(ref op) => Some(op),
551            _ => None,
552        }
553    }
554
555    /// If the operation is a PathPaymentStrictSend, returns its mutable value. Returns None otherwise.
556    pub fn as_path_payment_strict_send_mut(
557        &mut self,
558    ) -> Option<&mut PathPaymentStrictSendOperation> {
559        match *self {
560            Operation::PathPaymentStrictSend(ref mut op) => Some(op),
561            _ => None,
562        }
563    }
564
565    /// Returns true if the operation is a PathPaymentStrictSend.
566    pub fn is_path_payment_strict_send(&self) -> bool {
567        self.as_path_payment_strict_send().is_some()
568    }
569
570    /// If the operation is a CreateClaimableBalance, returns its value. Returns None otherwise.
571    pub fn as_create_claimable_balance(&self) -> Option<&CreateClaimableBalanceOperation> {
572        match *self {
573            Operation::CreateClaimableBalance(ref op) => Some(op),
574            _ => None,
575        }
576    }
577
578    /// If the operation is a CreateClaimableBalance, returns its mutable value. Returns None otherwise.
579    pub fn as_create_claimable_balance_mut(
580        &mut self,
581    ) -> Option<&mut CreateClaimableBalanceOperation> {
582        match *self {
583            Operation::CreateClaimableBalance(ref mut op) => Some(op),
584            _ => None,
585        }
586    }
587
588    /// Returns true if the operation is a CreateClaimableBalance.
589    pub fn is_create_claimable_balance(&self) -> bool {
590        self.as_create_claimable_balance().is_some()
591    }
592
593    /// If the operation is a ClaimClaimableBalance, returns its value. Returns None otherwise.
594    pub fn as_claim_claimable_balance(&self) -> Option<&ClaimClaimableBalanceOperation> {
595        match *self {
596            Operation::ClaimClaimableBalance(ref op) => Some(op),
597            _ => None,
598        }
599    }
600
601    /// If the operation is a ClaimClaimableBalance, returns its mutable value. Returns None otherwise.
602    pub fn as_claim_claimable_balance_mut(
603        &mut self,
604    ) -> Option<&mut ClaimClaimableBalanceOperation> {
605        match *self {
606            Operation::ClaimClaimableBalance(ref mut op) => Some(op),
607            _ => None,
608        }
609    }
610
611    /// Returns true if the operation is a ClaimClaimableBalance.
612    pub fn is_claim_claimable_balance(&self) -> bool {
613        self.as_claim_claimable_balance().is_some()
614    }
615
616    /// If the operation is a BeginSponsoringFutureReserves, returns its value. Returns None otherwise.
617    pub fn as_begin_sponsoring_future_reserves(
618        &self,
619    ) -> Option<&BeginSponsoringFutureReservesOperation> {
620        match *self {
621            Operation::BeginSponsoringFutureReserves(ref op) => Some(op),
622            _ => None,
623        }
624    }
625
626    /// If the operation is a BeginSponsoringFutureReserves, returns its mutable value. Returns None otherwise.
627    pub fn as_begin_sponsoring_future_reserves_mut(
628        &mut self,
629    ) -> Option<&mut BeginSponsoringFutureReservesOperation> {
630        match *self {
631            Operation::BeginSponsoringFutureReserves(ref mut op) => Some(op),
632            _ => None,
633        }
634    }
635
636    /// Returns true if the operation is a BeginSponsoringFutureReserves.
637    pub fn is_begin_sponsoring_future_reserves(&self) -> bool {
638        self.as_begin_sponsoring_future_reserves().is_some()
639    }
640
641    /// If the operation is a EndSponsoringFutureReserves, returns its value. Returns None otherwise.
642    pub fn as_end_sponsoring_future_reserves(
643        &self,
644    ) -> Option<&EndSponsoringFutureReservesOperation> {
645        match *self {
646            Operation::EndSponsoringFutureReserves(ref op) => Some(op),
647            _ => None,
648        }
649    }
650
651    /// If the operation is a EndSponsoringFutureReserves, returns its mutable value. Returns None otherwise.
652    pub fn as_end_sponsoring_future_reserves_mut(
653        &mut self,
654    ) -> Option<&mut EndSponsoringFutureReservesOperation> {
655        match *self {
656            Operation::EndSponsoringFutureReserves(ref mut op) => Some(op),
657            _ => None,
658        }
659    }
660
661    /// Returns true if the operation is a EndSponsoringFutureReserves.
662    pub fn is_end_sponsoring_future_reserves(&self) -> bool {
663        self.as_end_sponsoring_future_reserves().is_some()
664    }
665
666    /// If the operation is a RevokeSponsorship, returns its value. Returns None otherwise.
667    pub fn as_revoke_sponsorship(&self) -> Option<&RevokeSponsorshipOperation> {
668        match *self {
669            Operation::RevokeSponsorship(ref op) => Some(op),
670            _ => None,
671        }
672    }
673
674    /// If the operation is a RevokeSponsorship, returns its mutable value. Returns None otherwise.
675    pub fn as_revoke_sponsorship_mut(&mut self) -> Option<&mut RevokeSponsorshipOperation> {
676        match *self {
677            Operation::RevokeSponsorship(ref mut op) => Some(op),
678            _ => None,
679        }
680    }
681
682    /// Returns true if the operation is a RevokeSponsorship.
683    pub fn is_revoke_sponsorship(&self) -> bool {
684        self.as_revoke_sponsorship().is_some()
685    }
686
687    pub fn as_clawback(&self) -> Option<&ClawbackOperation> {
688        match *self {
689            Operation::Clawback(ref op) => Some(op),
690            _ => None,
691        }
692    }
693
694    pub fn as_clawback_mut(&mut self) -> Option<&mut ClawbackOperation> {
695        match *self {
696            Operation::Clawback(ref mut op) => Some(op),
697            _ => None,
698        }
699    }
700
701    pub fn is_clawback(&self) -> bool {
702        self.as_clawback().is_some()
703    }
704
705    pub fn as_clawback_claimable_balance(&self) -> Option<&ClawbackClaimableBalanceOperation> {
706        match *self {
707            Operation::ClawbackClaimableBalance(ref op) => Some(op),
708            _ => None,
709        }
710    }
711
712    pub fn as_clawback_claimable_balance_mut(
713        &mut self,
714    ) -> Option<&mut ClawbackClaimableBalanceOperation> {
715        match *self {
716            Operation::ClawbackClaimableBalance(ref mut op) => Some(op),
717            _ => None,
718        }
719    }
720
721    pub fn is_clawback_claimable_balance(&self) -> bool {
722        self.as_clawback_claimable_balance().is_some()
723    }
724
725    pub fn as_set_trustline_flags(&self) -> Option<&SetTrustLineFlagsOperation> {
726        match *self {
727            Operation::SetTrustLineFlags(ref op) => Some(op),
728            _ => None,
729        }
730    }
731
732    pub fn as_set_trustline_flags_mut(&mut self) -> Option<&mut SetTrustLineFlagsOperation> {
733        match *self {
734            Operation::SetTrustLineFlags(ref mut op) => Some(op),
735            _ => None,
736        }
737    }
738
739    pub fn is_set_trustline_flags(&self) -> bool {
740        self.as_set_trustline_flags().is_some()
741    }
742
743    pub fn as_liquidity_pool_deposit(&self) -> Option<&LiquidityPoolDepositOperation> {
744        match *self {
745            Operation::LiquidityPoolDeposit(ref op) => Some(op),
746            _ => None,
747        }
748    }
749
750    pub fn as_liquidity_pool_deposit_mut(&mut self) -> Option<&mut LiquidityPoolDepositOperation> {
751        match *self {
752            Operation::LiquidityPoolDeposit(ref mut op) => Some(op),
753            _ => None,
754        }
755    }
756
757    pub fn is_liquidity_pool_deposit(&self) -> bool {
758        self.as_liquidity_pool_deposit().is_some()
759    }
760
761    pub fn as_liquidity_pool_withdraw(&self) -> Option<&LiquidityPoolWithdrawOperation> {
762        match *self {
763            Operation::LiquidityPoolWithdraw(ref op) => Some(op),
764            _ => None,
765        }
766    }
767
768    pub fn as_liquidity_pool_withdraw_mut(
769        &mut self,
770    ) -> Option<&mut LiquidityPoolWithdrawOperation> {
771        match *self {
772            Operation::LiquidityPoolWithdraw(ref mut op) => Some(op),
773            _ => None,
774        }
775    }
776
777    pub fn is_liquidity_pool_withdraw(&self) -> bool {
778        self.as_liquidity_pool_withdraw().is_some()
779    }
780
781    /// Retrieves the operation source account.
782    pub fn source_account(&self) -> &Option<MuxedAccount> {
783        match self {
784            Operation::CreateAccount(op) => op.source_account(),
785            Operation::Payment(op) => op.source_account(),
786            Operation::PathPaymentStrictReceive(op) => op.source_account(),
787            Operation::ManageSellOffer(op) => op.source_account(),
788            Operation::CreatePassiveSellOffer(op) => op.source_account(),
789            Operation::SetOptions(op) => op.source_account(),
790            Operation::ChangeTrust(op) => op.source_account(),
791            Operation::AllowTrust(op) => op.source_account(),
792            Operation::AccountMerge(op) => op.source_account(),
793            Operation::Inflation(op) => op.source_account(),
794            Operation::ManageData(op) => op.source_account(),
795            Operation::BumpSequence(op) => op.source_account(),
796            Operation::ManageBuyOffer(op) => op.source_account(),
797            Operation::PathPaymentStrictSend(op) => op.source_account(),
798            Operation::CreateClaimableBalance(op) => op.source_account(),
799            Operation::ClaimClaimableBalance(op) => op.source_account(),
800            Operation::BeginSponsoringFutureReserves(op) => op.source_account(),
801            Operation::EndSponsoringFutureReserves(op) => op.source_account(),
802            Operation::RevokeSponsorship(op) => op.source_account(),
803            Operation::Clawback(op) => op.source_account(),
804            Operation::ClawbackClaimableBalance(op) => op.source_account(),
805            Operation::SetTrustLineFlags(op) => op.source_account(),
806            Operation::LiquidityPoolDeposit(op) => op.source_account(),
807            Operation::LiquidityPoolWithdraw(op) => op.source_account(),
808            Operation::InvokeHostFunction(op) => op.source_account(),
809            Operation::ExtendFootprintTtl(op) => op.source_account(),
810            Operation::RestoreFootprint(op) => op.source_account(),
811        }
812    }
813
814    /// Retrieves a mutable reference to the operation source account.
815    pub fn source_account_mut(&mut self) -> &mut Option<MuxedAccount> {
816        match self {
817            Operation::CreateAccount(op) => op.source_account_mut(),
818            Operation::Payment(op) => op.source_account_mut(),
819            Operation::PathPaymentStrictReceive(op) => op.source_account_mut(),
820            Operation::ManageSellOffer(op) => op.source_account_mut(),
821            Operation::CreatePassiveSellOffer(op) => op.source_account_mut(),
822            Operation::SetOptions(op) => op.source_account_mut(),
823            Operation::ChangeTrust(op) => op.source_account_mut(),
824            Operation::AllowTrust(op) => op.source_account_mut(),
825            Operation::AccountMerge(op) => op.source_account_mut(),
826            Operation::Inflation(op) => op.source_account_mut(),
827            Operation::ManageData(op) => op.source_account_mut(),
828            Operation::BumpSequence(op) => op.source_account_mut(),
829            Operation::ManageBuyOffer(op) => op.source_account_mut(),
830            Operation::PathPaymentStrictSend(op) => op.source_account_mut(),
831            Operation::CreateClaimableBalance(op) => op.source_account_mut(),
832            Operation::ClaimClaimableBalance(op) => op.source_account_mut(),
833            Operation::BeginSponsoringFutureReserves(op) => op.source_account_mut(),
834            Operation::EndSponsoringFutureReserves(op) => op.source_account_mut(),
835            Operation::RevokeSponsorship(op) => op.source_account_mut(),
836            Operation::Clawback(op) => op.source_account_mut(),
837            Operation::ClawbackClaimableBalance(op) => op.source_account_mut(),
838            Operation::SetTrustLineFlags(op) => op.source_account_mut(),
839            Operation::LiquidityPoolDeposit(op) => op.source_account_mut(),
840            Operation::LiquidityPoolWithdraw(op) => op.source_account_mut(),
841            Operation::InvokeHostFunction(op) => op.source_account_mut(),
842            Operation::ExtendFootprintTtl(op) => op.source_account_mut(),
843            Operation::RestoreFootprint(op) => op.source_account_mut(),
844        }
845    }
846
847    /// Returns the xdr object.
848    pub fn to_xdr(&self) -> Result<xdr::Operation> {
849        let source_account = match self.source_account() {
850            None => None,
851            Some(account) => Some(account.to_xdr()?),
852        };
853        let body = match self {
854            Operation::CreateAccount(op) => op.to_xdr_operation_body()?,
855            Operation::Payment(op) => op.to_xdr_operation_body()?,
856            Operation::PathPaymentStrictReceive(op) => op.to_xdr_operation_body()?,
857            Operation::ManageSellOffer(op) => op.to_xdr_operation_body()?,
858            Operation::CreatePassiveSellOffer(op) => op.to_xdr_operation_body()?,
859            Operation::SetOptions(op) => op.to_xdr_operation_body()?,
860            Operation::ChangeTrust(op) => op.to_xdr_operation_body()?,
861            Operation::AllowTrust(op) => op.to_xdr_operation_body()?,
862            Operation::AccountMerge(op) => op.to_xdr_operation_body()?,
863            Operation::Inflation(op) => op.to_xdr_operation_body()?,
864            Operation::ManageData(op) => op.to_xdr_operation_body()?,
865            Operation::BumpSequence(op) => op.to_xdr_operation_body()?,
866            Operation::ManageBuyOffer(op) => op.to_xdr_operation_body()?,
867            Operation::PathPaymentStrictSend(op) => op.to_xdr_operation_body()?,
868            Operation::CreateClaimableBalance(op) => op.to_xdr_operation_body()?,
869            Operation::ClaimClaimableBalance(op) => op.to_xdr_operation_body()?,
870            Operation::BeginSponsoringFutureReserves(op) => op.to_xdr_operation_body()?,
871            Operation::EndSponsoringFutureReserves(op) => op.to_xdr_operation_body()?,
872            Operation::RevokeSponsorship(op) => op.to_xdr_operation_body()?,
873            Operation::Clawback(op) => op.to_xdr_operation_body()?,
874            Operation::ClawbackClaimableBalance(op) => op.to_xdr_operation_body()?,
875            Operation::SetTrustLineFlags(op) => op.to_xdr_operation_body()?,
876            Operation::LiquidityPoolDeposit(op) => op.to_xdr_operation_body()?,
877            Operation::LiquidityPoolWithdraw(op) => op.to_xdr_operation_body()?,
878            Operation::InvokeHostFunction(op) => op.to_xdr_operation_body()?,
879            Operation::ExtendFootprintTtl(op) => op.to_xdr_operation_body()?,
880            Operation::RestoreFootprint(op) => op.to_xdr_operation_body()?,
881        };
882        Ok(xdr::Operation {
883            source_account,
884            body,
885        })
886    }
887
888    /// Creates from the xdr object.
889    pub fn from_xdr(x: &xdr::Operation) -> Result<Operation> {
890        let source_account = match &x.source_account {
891            None => None,
892            Some(sa) => Some(MuxedAccount::from_xdr(sa)?),
893        };
894        match &x.body {
895            xdr::OperationBody::CreateAccount(op) => {
896                let inner = CreateAccountOperation::from_xdr_operation_body(source_account, op)?;
897                Ok(Operation::CreateAccount(inner))
898            }
899            xdr::OperationBody::Payment(op) => {
900                let inner = PaymentOperation::from_xdr_operation_body(source_account, op)?;
901                Ok(Operation::Payment(inner))
902            }
903            xdr::OperationBody::PathPaymentStrictReceive(op) => {
904                let inner =
905                    PathPaymentStrictReceiveOperation::from_xdr_operation_body(source_account, op)?;
906                Ok(Operation::PathPaymentStrictReceive(inner))
907            }
908            xdr::OperationBody::ManageSellOffer(op) => {
909                let inner = ManageSellOfferOperation::from_xdr_operation_body(source_account, op)?;
910                Ok(Operation::ManageSellOffer(inner))
911            }
912            xdr::OperationBody::CreatePassiveSellOffer(op) => {
913                let inner =
914                    CreatePassiveSellOfferOperation::from_xdr_operation_body(source_account, op)?;
915                Ok(Operation::CreatePassiveSellOffer(inner))
916            }
917            xdr::OperationBody::SetOptions(op) => {
918                let inner = SetOptionsOperation::from_xdr_operation_body(source_account, op)?;
919                Ok(Operation::SetOptions(inner))
920            }
921            xdr::OperationBody::ChangeTrust(op) => {
922                let inner = ChangeTrustOperation::from_xdr_operation_body(source_account, op)?;
923                Ok(Operation::ChangeTrust(inner))
924            }
925            xdr::OperationBody::AllowTrust(op) => {
926                let inner = AllowTrustOperation::from_xdr_operation_body(source_account, op)?;
927                Ok(Operation::AllowTrust(inner))
928            }
929            xdr::OperationBody::AccountMerge(op) => {
930                let inner = AccountMergeOperation::from_xdr_operation_body(source_account, op)?;
931                Ok(Operation::AccountMerge(inner))
932            }
933            xdr::OperationBody::Inflation => {
934                let inner = InflationOperation::from_xdr_operation_body(source_account)?;
935                Ok(Operation::Inflation(inner))
936            }
937            xdr::OperationBody::ManageData(op) => {
938                let inner = ManageDataOperation::from_xdr_operation_body(source_account, op)?;
939                Ok(Operation::ManageData(inner))
940            }
941            xdr::OperationBody::BumpSequence(op) => {
942                let inner = BumpSequenceOperation::from_xdr_operation_body(source_account, op)?;
943                Ok(Operation::BumpSequence(inner))
944            }
945            xdr::OperationBody::ManageBuyOffer(op) => {
946                let inner = ManageBuyOfferOperation::from_xdr_operation_body(source_account, op)?;
947                Ok(Operation::ManageBuyOffer(inner))
948            }
949            xdr::OperationBody::PathPaymentStrictSend(op) => {
950                let inner =
951                    PathPaymentStrictSendOperation::from_xdr_operation_body(source_account, op)?;
952                Ok(Operation::PathPaymentStrictSend(inner))
953            }
954            xdr::OperationBody::CreateClaimableBalance(op) => {
955                let inner =
956                    CreateClaimableBalanceOperation::from_xdr_operation_body(source_account, op)?;
957                Ok(Operation::CreateClaimableBalance(inner))
958            }
959            xdr::OperationBody::ClaimClaimableBalance(op) => {
960                let inner =
961                    ClaimClaimableBalanceOperation::from_xdr_operation_body(source_account, op)?;
962                Ok(Operation::ClaimClaimableBalance(inner))
963            }
964            xdr::OperationBody::BeginSponsoringFutureReserves(op) => {
965                let inner = BeginSponsoringFutureReservesOperation::from_xdr_operation_body(
966                    source_account,
967                    op,
968                )?;
969                Ok(Operation::BeginSponsoringFutureReserves(inner))
970            }
971            xdr::OperationBody::EndSponsoringFutureReserves => {
972                let inner =
973                    EndSponsoringFutureReservesOperation::from_xdr_operation_body(source_account)?;
974                Ok(Operation::EndSponsoringFutureReserves(inner))
975            }
976            xdr::OperationBody::RevokeSponsorship(op) => {
977                let inner =
978                    RevokeSponsorshipOperation::from_xdr_operation_body(source_account, op)?;
979                Ok(Operation::RevokeSponsorship(inner))
980            }
981            xdr::OperationBody::Clawback(op) => {
982                let inner = ClawbackOperation::from_xdr_operation_body(source_account, op)?;
983                Ok(Operation::Clawback(inner))
984            }
985            xdr::OperationBody::ClawbackClaimableBalance(op) => {
986                let inner =
987                    ClawbackClaimableBalanceOperation::from_xdr_operation_body(source_account, op)?;
988                Ok(Operation::ClawbackClaimableBalance(inner))
989            }
990            xdr::OperationBody::SetTrustLineFlags(op) => {
991                let inner =
992                    SetTrustLineFlagsOperation::from_xdr_operation_body(source_account, op)?;
993                Ok(Operation::SetTrustLineFlags(inner))
994            }
995            xdr::OperationBody::LiquidityPoolDeposit(op) => {
996                let inner =
997                    LiquidityPoolDepositOperation::from_xdr_operation_body(source_account, op)?;
998                Ok(Operation::LiquidityPoolDeposit(inner))
999            }
1000            xdr::OperationBody::LiquidityPoolWithdraw(op) => {
1001                let inner =
1002                    LiquidityPoolWithdrawOperation::from_xdr_operation_body(source_account, op)?;
1003                Ok(Operation::LiquidityPoolWithdraw(inner))
1004            }
1005            xdr::OperationBody::InvokeHostFunction(op) => {
1006                let inner =
1007                    InvokeHostFunctionOperation::from_xdr_operation_body(source_account, op)?;
1008                Ok(Operation::InvokeHostFunction(inner))
1009            }
1010            xdr::OperationBody::ExtendFootprintTtl(op) => {
1011                let inner =
1012                    ExtendFootprintTtlOperation::from_xdr_operation_body(source_account, op)?;
1013                Ok(Operation::ExtendFootprintTtl(inner))
1014            }
1015            xdr::OperationBody::RestoreFootprint(op) => {
1016                let inner = RestoreFootprintOperation::from_xdr_operation_body(source_account, op)?;
1017                Ok(Operation::RestoreFootprint(inner))
1018            }
1019        }
1020    }
1021}
1022
1023impl xdr::WriteXdr for Operation {
1024    fn write_xdr<W: Write>(&self, w: &mut xdr::Limited<W>) -> xdr::Result<()> {
1025        let xdr_operation = self.to_xdr().map_err(|_| xdr::Error::Invalid)?;
1026        xdr_operation.write_xdr(w)
1027    }
1028}
1029
1030impl xdr::ReadXdr for Operation {
1031    fn read_xdr<R: Read>(r: &mut xdr::Limited<R>) -> xdr::Result<Self> {
1032        let xdr_result = xdr::Operation::read_xdr(r)?;
1033        Self::from_xdr(&xdr_result).map_err(|_| xdr::Error::Invalid)
1034    }
1035}