shuttle_core/
transaction_builder.rs1use account::Account;
2use time_bounds::TimeBounds;
3use memo::Memo;
4use transaction::Transaction;
5use operation::Operation;
6
7#[derive(Debug)]
9pub struct TransactionBuilder<'a> {
10 source: &'a mut Account,
11 time_bounds: Option<TimeBounds>,
12 memo: Memo,
13 operations: Vec<Operation>,
14}
15
16impl<'a> TransactionBuilder<'a> {
17 pub fn new(source: &'a mut Account) -> TransactionBuilder<'a> {
19 TransactionBuilder {
20 source: source,
21 time_bounds: None,
22 memo: Memo::None,
23 operations: Vec::new(),
24 }
25 }
26
27 pub fn with_time_bounds(mut self, time_bounds: TimeBounds) -> Self {
29 self.time_bounds = Some(time_bounds);
30 self
31 }
32
33 pub fn with_memo(mut self, memo: Memo) -> Self {
35 self.memo = memo;
36 self
37 }
38
39 pub fn operation(mut self, op: Operation) -> Self {
41 self.operations.push(op);
42 self
43 }
44
45 pub fn operations_len(&self) -> usize {
47 self.operations.len()
48 }
49
50 pub fn build(self) -> Transaction {
52 let keypair = self.source.account_id().clone();
53 let sequence = self.source.increment_sequence();
54 Transaction::new(
55 keypair,
56 sequence,
57 self.time_bounds,
58 self.memo,
59 self.operations,
60 )
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use Account;
67 use KeyPair;
68 use Memo;
69 use TransactionBuilder;
70 use OperationBuilder;
71
72 #[test]
73 fn test_builder_success() {
74 let kp = KeyPair::random().unwrap();
75 let mut account = Account::new(kp.public_key().clone(), 999);
76
77 let tx0 = TransactionBuilder::new(&mut account)
78 .operation(OperationBuilder::inflation().build())
79 .build();
80
81 let tx1 = TransactionBuilder::new(&mut account)
82 .operation(OperationBuilder::inflation().build())
83 .build();
84
85 assert_eq!(tx0.operations().len(), 1);
86 assert_eq!(tx0.sequence(), 1000);
87 assert_eq!(tx1.sequence(), 1001);
88 }
89
90 #[test]
91 fn test_builder_memo() {
92 let kp = KeyPair::random().unwrap();
93 let mut account = Account::new(kp.public_key().clone(), 999);
94
95 let tx = TransactionBuilder::new(&mut account)
96 .operation(OperationBuilder::inflation().build())
97 .with_memo(Memo::text("TEST STRING").unwrap())
98 .build();
99 assert_eq!(*tx.memo(), Memo::Text("TEST STRING".to_string()));
100 }
101}