openzeppelin_monitor/utils/tests/builders/solana/
block.rs1use crate::models::{SolanaBlock, SolanaConfirmedBlock, SolanaTransaction};
6
7#[derive(Debug, Default)]
9pub struct BlockBuilder {
10 slot: Option<u64>,
11 blockhash: Option<String>,
12 previous_blockhash: Option<String>,
13 parent_slot: Option<u64>,
14 block_time: Option<i64>,
15 block_height: Option<u64>,
16 transactions: Option<Vec<SolanaTransaction>>,
17}
18
19impl BlockBuilder {
20 pub fn new() -> Self {
22 Self::default()
23 }
24
25 pub fn slot(mut self, slot: u64) -> Self {
27 self.slot = Some(slot);
28 self
29 }
30
31 pub fn blockhash(mut self, blockhash: &str) -> Self {
33 self.blockhash = Some(blockhash.to_string());
34 self
35 }
36
37 pub fn previous_blockhash(mut self, previous_blockhash: &str) -> Self {
39 self.previous_blockhash = Some(previous_blockhash.to_string());
40 self
41 }
42
43 pub fn parent_slot(mut self, parent_slot: u64) -> Self {
45 self.parent_slot = Some(parent_slot);
46 self
47 }
48
49 pub fn block_time(mut self, block_time: i64) -> Self {
51 self.block_time = Some(block_time);
52 self
53 }
54
55 pub fn block_height(mut self, block_height: u64) -> Self {
57 self.block_height = Some(block_height);
58 self
59 }
60
61 pub fn transactions(mut self, transactions: Vec<SolanaTransaction>) -> Self {
63 self.transactions = Some(transactions);
64 self
65 }
66
67 pub fn add_transaction(mut self, transaction: SolanaTransaction) -> Self {
69 let mut transactions = self.transactions.unwrap_or_default();
70 transactions.push(transaction);
71 self.transactions = Some(transactions);
72 self
73 }
74
75 pub fn build(self) -> SolanaBlock {
77 let slot = self.slot.unwrap_or(123456789);
78 let confirmed_block = SolanaConfirmedBlock {
79 slot,
80 blockhash: self
81 .blockhash
82 .unwrap_or_else(|| "4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZAMdL4VZHirAn".to_string()),
83 previous_blockhash: self
84 .previous_blockhash
85 .unwrap_or_else(|| "3sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZAMdL4VZHirAn".to_string()),
86 parent_slot: self.parent_slot.unwrap_or(slot.saturating_sub(1)),
87 block_time: self.block_time,
88 block_height: self.block_height,
89 transactions: self.transactions.unwrap_or_default(),
90 };
91
92 SolanaBlock::from(confirmed_block)
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99 use crate::utils::tests::builders::solana::transaction::TransactionBuilder;
100
101 #[test]
102 fn test_default_block() {
103 let block = BlockBuilder::new().build();
104
105 assert_eq!(block.number(), Some(123456789));
106 assert_eq!(
107 block.blockhash(),
108 "4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZAMdL4VZHirAn"
109 );
110 }
111
112 #[test]
113 fn test_custom_block() {
114 let block = BlockBuilder::new()
115 .slot(999)
116 .blockhash("custom_blockhash")
117 .block_time(1234567890)
118 .block_height(100)
119 .build();
120
121 assert_eq!(block.number(), Some(999));
122 assert_eq!(block.blockhash(), "custom_blockhash");
123 assert_eq!(block.block_time(), Some(1234567890));
124 }
125
126 #[test]
127 fn test_block_with_transactions() {
128 let tx = TransactionBuilder::new().signature("test_sig").build();
129
130 let block = BlockBuilder::new().add_transaction(tx).build();
131
132 assert_eq!(block.transactions.len(), 1);
133 }
134
135 #[test]
136 fn test_block_with_multiple_transactions() {
137 let tx1 = TransactionBuilder::new().signature("sig1").build();
138 let tx2 = TransactionBuilder::new().signature("sig2").build();
139
140 let block = BlockBuilder::new().transactions(vec![tx1, tx2]).build();
141
142 assert_eq!(block.transactions.len(), 2);
143 }
144}