stellar_base/operations/
inflation.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 InflationOperation {
8    source_account: Option<MuxedAccount>,
9}
10
11#[derive(Debug, Default)]
12pub struct InflationOperationBuilder {
13    source_account: Option<MuxedAccount>,
14}
15
16impl InflationOperation {
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::Inflation)
30    }
31
32    /// Creates from the xdr operation body.
33    pub fn from_xdr_operation_body(
34        source_account: Option<MuxedAccount>,
35    ) -> Result<InflationOperation> {
36        Ok(InflationOperation { source_account })
37    }
38}
39
40impl InflationOperationBuilder {
41    pub fn new() -> InflationOperationBuilder {
42        Default::default()
43    }
44
45    pub fn with_source_account<S>(mut self, source: S) -> InflationOperationBuilder
46    where
47        S: Into<MuxedAccount>,
48    {
49        self.source_account = Some(source.into());
50        self
51    }
52
53    pub fn build(self) -> Operation {
54        Operation::Inflation(InflationOperation {
55            source_account: self.source_account,
56        })
57    }
58}
59
60#[cfg(test)]
61mod tests {
62
63    use crate::network::Network;
64    use crate::operations::tests::*;
65    use crate::operations::Operation;
66    use crate::transaction::{Transaction, TransactionEnvelope, MIN_BASE_FEE};
67    use crate::xdr::{XDRDeserialize, XDRSerialize};
68
69    #[test]
70    fn test_inflation() {
71        let kp = keypair0();
72        let mut tx = Transaction::builder(kp.public_key(), 3556091187167235, MIN_BASE_FEE)
73            .add_operation(Operation::new_inflation().build())
74            .into_transaction()
75            .unwrap();
76        tx.sign(kp.as_ref(), &Network::new_test()).unwrap();
77        let envelope = tx.to_envelope();
78        let xdr = envelope.xdr_base64().unwrap();
79        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAAAAAAJAAAAAAAAAAHqLnLFAAAAQCvHHPKuTRaRXk9BH05oWii0PJRmVOoqMxxg+79MLO90n1ljVNoaQ1Fliy8Xe34yfUzjhMB/TCXH29T8dTYtBg4=";
80        assert_eq!(expected, xdr);
81        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
82        assert_eq!(envelope, back);
83    }
84
85    #[test]
86    fn test_inflation_with_source_account() {
87        let kp = keypair0();
88        let kp1 = keypair1();
89
90        let mut tx = Transaction::builder(kp.public_key(), 3556091187167235, MIN_BASE_FEE)
91            .add_operation(
92                Operation::new_inflation()
93                    .with_source_account(kp1.public_key())
94                    .build(),
95            )
96            .into_transaction()
97            .unwrap();
98        tx.sign(kp.as_ref(), &Network::new_test()).unwrap();
99        let envelope = tx.to_envelope();
100        let xdr = envelope.xdr_base64().unwrap();
101        let expected = "AAAAAgAAAADg3G3hclysZlFitS+s5zWyiiJD5B0STWy5LXCj6i5yxQAAAGQADKI/AAAAAwAAAAAAAAAAAAAAAQAAAAEAAAAAJcrx2g/Hbs/ohF5CVFG7B5JJSJR+OqDKzDGK7dKHZH4AAAAJAAAAAAAAAAHqLnLFAAAAQEMnHq75AT55x0OCFN8mQGbwamRmSdlZsOw0U9z5SbhZqug7qlrW4R7U4era7DDGwOv1bdIMCcGZ4FxSDMH8hwg=";
102        assert_eq!(expected, xdr);
103        let back = TransactionEnvelope::from_xdr_base64(&xdr).unwrap();
104        assert_eq!(envelope, back);
105    }
106}