stellar_base/operations/
end_sponsoring_future_reserves.rs

1use crate::crypto::MuxedAccount;
2use crate::error::Result;
3use crate::operations::Operation;
4use crate::xdr;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct EndSponsoringFutureReservesOperation {
8    source_account: Option<MuxedAccount>,
9}
10
11#[derive(Debug, Default)]
12pub struct EndSponsoringFutureReservesOperationBuilder {
13    source_account: Option<MuxedAccount>,
14}
15
16impl EndSponsoringFutureReservesOperation {
17    /// Retrieves the operation source account.
18    pub fn source_account(&self) -> &Option<MuxedAccount> {
19        &self.source_account
20    }
21
22    /// Retrieves a reference to the operation source account.
23    pub fn source_account_mut(&mut self) -> &mut Option<MuxedAccount> {
24        &mut self.source_account
25    }
26
27    /// Returns the xdr operation body.
28    pub fn to_xdr_operation_body(&self) -> Result<xdr::OperationBody> {
29        Ok(xdr::OperationBody::EndSponsoringFutureReserves)
30    }
31
32    /// Creates from the xdr operation body.
33    pub fn from_xdr_operation_body(
34        source_account: Option<MuxedAccount>,
35    ) -> Result<EndSponsoringFutureReservesOperation> {
36        Ok(EndSponsoringFutureReservesOperation { source_account })
37    }
38}
39
40impl EndSponsoringFutureReservesOperationBuilder {
41    pub fn new() -> EndSponsoringFutureReservesOperationBuilder {
42        Default::default()
43    }
44
45    pub fn with_source_account<S>(
46        mut self,
47        source: S,
48    ) -> EndSponsoringFutureReservesOperationBuilder
49    where
50        S: Into<MuxedAccount>,
51    {
52        self.source_account = Some(source.into());
53        self
54    }
55
56    pub fn build(self) -> Operation {
57        Operation::EndSponsoringFutureReserves(EndSponsoringFutureReservesOperation {
58            source_account: self.source_account,
59        })
60    }
61}
62
63#[cfg(test)]
64mod tests {
65
66    use crate::network::Network;
67    use crate::operations::tests::*;
68    use crate::operations::Operation;
69    use crate::transaction::{Transaction, TransactionEnvelope, MIN_BASE_FEE};
70    use crate::xdr::{XDRDeserialize, XDRSerialize};
71
72    #[test]
73    fn test_end_sponsoring_future_reserves() {
74        let kp = keypair0();
75        let mut tx = Transaction::builder(kp.public_key(), 3556091187167235, MIN_BASE_FEE)
76            .add_operation(Operation::new_end_sponsoring_future_reserves().build())
77            .into_transaction()
78            .unwrap();
79        tx.sign(kp.as_ref(), &Network::new_test()).unwrap();
80        let envelope = tx.to_envelope();
81        let xdr = envelope.xdr_base64().unwrap();
82        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAAAAAARAAAAAAAAAAHqLnLFAAAAQOQlXWOVDQVXDDwpbjv6EpROZHQ77tzmTMCrczR20UFqzPBZHc6lKv9tTLzz/esVDbwYbaa+m/y8HqH7MqPjuQw=";
83        assert_eq!(expected, xdr);
84        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
85        assert_eq!(envelope, back);
86    }
87
88    #[test]
89    fn test_end_sponsoring_future_reserves_with_source_account() {
90        let kp = keypair0();
91        let kp1 = keypair1();
92
93        let mut tx = Transaction::builder(kp.public_key(), 3556091187167235, MIN_BASE_FEE)
94            .add_operation(
95                Operation::new_end_sponsoring_future_reserves()
96                    .with_source_account(kp1.public_key())
97                    .build(),
98            )
99            .into_transaction()
100            .unwrap();
101        tx.sign(kp.as_ref(), &Network::new_test()).unwrap();
102        let envelope = tx.to_envelope();
103        let xdr = envelope.xdr_base64().unwrap();
104        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAEAAAAAJcrx2g/Hbs/ohF5CVFG7B5JJSJR+OqDKzDGK7dKHZH4AAAARAAAAAAAAAAHqLnLFAAAAQBjITBRZCRbpp2adVQSTC8l9NB/UoTFtHpHkZYEEWl0hSGD7U4N3m2WhFD+kmoBnyy1jWbtPMkyL3SriGvrSCwM=";
105        assert_eq!(expected, xdr);
106        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
107        assert_eq!(envelope, back);
108    }
109}