soroban_cli/tx/builder/
transaction.rs

1use crate::xdr::{self, Memo, SequenceNumber, TransactionExt};
2
3use super::Error;
4
5pub trait TxExt {
6    fn new_tx(
7        source: xdr::MuxedAccount,
8        fee: u32,
9        seq_num: impl Into<SequenceNumber>,
10        operation: xdr::Operation,
11    ) -> xdr::Transaction;
12
13    fn add_operation(self, operation: xdr::Operation) -> Result<xdr::Transaction, Error>;
14
15    fn add_memo(self, memo: Memo) -> xdr::Transaction;
16
17    fn add_cond(self, cond: xdr::Preconditions) -> xdr::Transaction;
18}
19
20impl TxExt for xdr::Transaction {
21    fn new_tx(
22        source_account: xdr::MuxedAccount,
23        fee: u32,
24        seq_num: impl Into<SequenceNumber>,
25        operation: xdr::Operation,
26    ) -> xdr::Transaction {
27        xdr::Transaction {
28            source_account,
29            fee,
30            seq_num: seq_num.into(),
31            cond: crate::xdr::Preconditions::None,
32            memo: Memo::None,
33            operations: [operation].try_into().unwrap(),
34            ext: TransactionExt::V0,
35        }
36    }
37
38    fn add_operation(mut self, operation: xdr::Operation) -> Result<Self, Error> {
39        let mut ops = self.operations.to_vec();
40        ops.push(operation);
41        self.operations = ops.try_into().map_err(|_| Error::TooManyOperations)?;
42        Ok(self)
43    }
44
45    fn add_memo(mut self, memo: Memo) -> Self {
46        self.memo = memo;
47        self
48    }
49
50    fn add_cond(self, cond: xdr::Preconditions) -> xdr::Transaction {
51        xdr::Transaction { cond, ..self }
52    }
53}