openzeppelin_monitor/utils/tests/builders/midnight/
transaction.rs1use crate::models::{MidnightBaseTransaction, MidnightOperation, MidnightTransaction};
2
3#[derive(Debug)]
5pub struct TransactionBuilder {
6	tx_hash: String,
7	operations: Vec<MidnightOperation>,
8	identifiers: Vec<String>,
9}
10
11impl Default for TransactionBuilder {
12	fn default() -> Self {
14		Self {
15			tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
16				.to_string(),
17			operations: vec![],
18			identifiers: vec![],
19		}
20	}
21}
22
23impl TransactionBuilder {
24	pub fn new() -> Self {
26		Self::default()
27	}
28
29	pub fn hash(mut self, hash: String) -> Self {
31		self.tx_hash = hash;
32		self
33	}
34
35	pub fn operations(mut self, operations: Vec<MidnightOperation>) -> Self {
37		self.operations = operations;
38		self
39	}
40
41	pub fn add_operation(mut self, operation: MidnightOperation) -> Self {
43		self.operations.push(operation);
44		self
45	}
46
47	pub fn add_call_operation(mut self, address: String, entry_point: String) -> Self {
49		self.operations.push(MidnightOperation::Call {
50			address,
51			entry_point: hex::encode(entry_point.as_bytes()),
52		});
53		self
54	}
55
56	pub fn add_deploy_operation(mut self, address: String) -> Self {
58		self.operations.push(MidnightOperation::Deploy { address });
59		self
60	}
61
62	pub fn add_fallible_coins_operation(mut self) -> Self {
64		self.operations.push(MidnightOperation::FallibleCoins);
65		self
66	}
67
68	pub fn add_guaranteed_coins_operation(mut self) -> Self {
70		self.operations.push(MidnightOperation::GuaranteedCoins);
71		self
72	}
73
74	pub fn add_maintain_operation(mut self, address: String) -> Self {
76		self.operations
77			.push(MidnightOperation::Maintain { address });
78		self
79	}
80
81	pub fn add_claim_mint_operation(mut self, value: u128, coin_type: String) -> Self {
83		self.operations
84			.push(MidnightOperation::ClaimMint { value, coin_type });
85		self
86	}
87
88	pub fn identifiers(mut self, identifiers: Vec<String>) -> Self {
90		self.identifiers = identifiers;
91		self
92	}
93
94	pub fn build(self) -> MidnightTransaction {
96		let base_tx = MidnightBaseTransaction {
97			tx_hash: self.tx_hash,
98			operations: self.operations,
99			identifiers: self.identifiers,
100		};
101
102		MidnightTransaction::from(base_tx)
103	}
104}
105
106#[cfg(test)]
107mod tests {
108	use super::*;
109
110	#[test]
111	fn test_builder_default() {
112		let tx = TransactionBuilder::new().build();
113		assert_eq!(
114			tx.tx_hash,
115			"0x0000000000000000000000000000000000000000000000000000000000000000"
116		);
117		assert!(tx.operations.is_empty());
118		assert!(tx.identifiers.is_empty());
119	}
120
121	#[test]
122	fn test_builder_with_hash() {
123		let hash = "0x123abc".to_string();
124		let tx = TransactionBuilder::new().hash(hash.clone()).build();
125		assert_eq!(tx.tx_hash, hash);
126	}
127
128	#[test]
129	fn test_builder_with_operations() {
130		let operations = vec![
131			MidnightOperation::Call {
132				address: "0x123".to_string(),
133				entry_point: "main".to_string(),
134			},
135			MidnightOperation::Deploy {
136				address: "0x456".to_string(),
137			},
138		];
139		let tx = TransactionBuilder::new()
140			.operations(operations.clone())
141			.build();
142		assert_eq!(tx.operations, operations);
143	}
144
145	#[test]
146	fn test_builder_with_identifiers() {
147		let identifiers = vec!["id1".to_string(), "id2".to_string()];
148		let tx = TransactionBuilder::new()
149			.identifiers(identifiers.clone())
150			.build();
151		assert_eq!(tx.identifiers, identifiers);
152	}
153
154	#[test]
155	fn test_builder_add_operation() {
156		let operation = MidnightOperation::Call {
157			address: "0x123".to_string(),
158			entry_point: "main".to_string(),
159		};
160		let tx = TransactionBuilder::new()
161			.add_operation(operation.clone())
162			.build();
163		assert_eq!(tx.operations, vec![operation]);
164	}
165
166	#[test]
167	fn test_builder_add_call_operation() {
168		let address = "0x123".to_string();
169		let entry_point = "main".to_string();
170		let tx = TransactionBuilder::new()
171			.add_call_operation(address.clone(), entry_point.clone())
172			.build();
173		assert_eq!(
174			tx.operations,
175			vec![MidnightOperation::Call {
176				address,
177				entry_point: hex::encode(entry_point.as_bytes()),
178			}]
179		);
180	}
181
182	#[test]
183	fn test_builder_add_deploy_operation() {
184		let address = "0x123".to_string();
185		let tx = TransactionBuilder::new()
186			.add_deploy_operation(address.clone())
187			.build();
188		assert_eq!(tx.operations, vec![MidnightOperation::Deploy { address }]);
189	}
190
191	#[test]
192	fn test_builder_add_fallible_coins_operation() {
193		let tx = TransactionBuilder::new()
194			.add_fallible_coins_operation()
195			.build();
196		assert_eq!(tx.operations, vec![MidnightOperation::FallibleCoins]);
197	}
198
199	#[test]
200	fn test_builder_add_guaranteed_coins_operation() {
201		let tx = TransactionBuilder::new()
202			.add_guaranteed_coins_operation()
203			.build();
204		assert_eq!(tx.operations, vec![MidnightOperation::GuaranteedCoins]);
205	}
206
207	#[test]
208	fn test_builder_add_maintain_operation() {
209		let address = "0x123".to_string();
210		let tx = TransactionBuilder::new()
211			.add_maintain_operation(address.clone())
212			.build();
213		assert_eq!(tx.operations, vec![MidnightOperation::Maintain { address }]);
214	}
215
216	#[test]
217	fn test_builder_add_claim_mint_operation() {
218		let value = 100u128;
219		let coin_type = "ETH".to_string();
220		let tx = TransactionBuilder::new()
221			.add_claim_mint_operation(value, coin_type.clone())
222			.build();
223		assert_eq!(
224			tx.operations,
225			vec![MidnightOperation::ClaimMint { value, coin_type }]
226		);
227	}
228
229	#[test]
230	fn test_builder_complete_transaction() {
231		let tx = TransactionBuilder::new()
232			.hash("0x123abc".to_string())
233			.add_call_operation("0x123".to_string(), "main".to_string())
234			.add_deploy_operation("0x456".to_string())
235			.add_fallible_coins_operation()
236			.add_guaranteed_coins_operation()
237			.add_maintain_operation("0x789".to_string())
238			.add_claim_mint_operation(100u128, "ETH".to_string())
239			.identifiers(vec!["id1".to_string(), "id2".to_string()])
240			.build();
241
242		assert_eq!(tx.tx_hash, "0x123abc");
243		assert_eq!(tx.operations.len(), 6);
244		assert_eq!(tx.identifiers.len(), 2);
245	}
246}