bitcoin_terminal_dashboard/app/
state.rs

1use std::thread::sleep;
2use std::time::Duration;
3
4use crate::inputs::{FetchEvent, InputEvent, Resource};
5
6#[derive(Clone)]
7pub enum FetchStatus<T> {
8    NotStarted,
9    InProgress(Option<T>),
10    Complete(T),
11}
12
13#[derive(Clone)]
14pub struct Stats {
15    pub bitcoin_price: FetchStatus<f64>,
16    pub block_height: FetchStatus<u64>,
17    pub last_block_time: u64,
18    pub average_block_time: u64,
19    pub seconds_since_last_block: FetchStatus<u64>,
20    pub transactions_count_over_last_30_days: FetchStatus<u64>,
21    pub average_block_time_for_last_2016_blocks: FetchStatus<u64>,
22    pub chain_size: FetchStatus<u64>,
23    pub utxo_set_size: FetchStatus<u64>,
24    pub total_money_supply: FetchStatus<f64>,
25    pub total_transactions_count: FetchStatus<u64>,
26    pub tps_for_last_30_days: FetchStatus<f64>,
27    pub total_fees_for_last_24_hours: FetchStatus<u64>,
28    pub difficulty: FetchStatus<f64>,
29    pub current_difficulty_epoch: FetchStatus<u64>,
30    pub block_count_until_retarget: FetchStatus<f64>,
31    pub estimated_seconds_until_retarget: FetchStatus<f64>,
32    pub average_block_time_since_last_difficulty_adjustement: FetchStatus<u64>,
33    pub estimated_hash_rate_per_second_for_last_2016_blocks: FetchStatus<f64>,
34    pub block_subsidy_of_most_recent_block: FetchStatus<u64>,
35    pub blocks_mined_over_last_24_hours: FetchStatus<u64>,
36    pub average_fees_per_block_over_last_24_hours: FetchStatus<u64>,
37    pub average_fees_per_block_over_last_2016_blocks: FetchStatus<u64>,
38    pub fees_as_a_percent_of_reward_for_last_2016_blocks: FetchStatus<f64>,
39    pub fees_as_a_percent_of_reward_for_last_24_hours: FetchStatus<f64>,
40    pub segwit_percent_last_24_hours: FetchStatus<f64>,
41    pub segwit_spending_payments_percent_last_24_hours: FetchStatus<f64>,
42    pub segwit_spending_transactions_percent_last_24_hours: FetchStatus<f64>,
43}
44
45#[derive(Clone)]
46pub struct InitializedData {
47    pub duration: Duration,
48    pub counter_sleep: u32,
49    pub counter_tick: u64,
50    pub stats: Stats,
51    pub newest_block_found_height: Option<u64>,
52}
53
54#[derive(Clone)]
55pub enum AppState {
56    Init,
57    Initialized(InitializedData),
58}
59
60impl AppState {
61    pub fn initialized() -> Self {
62        let duration = Duration::from_secs(1);
63        let counter_sleep = 0;
64        let counter_tick = 0;
65        let bitcoin_price = FetchStatus::NotStarted;
66        let block_height = FetchStatus::NotStarted;
67        let last_block_time = 38999993832;
68        let average_block_time = 9600;
69        let seconds_since_last_block = FetchStatus::NotStarted;
70        let transactions_count_over_last_30_days = FetchStatus::NotStarted;
71        let average_block_time_for_last_2016_blocks = FetchStatus::NotStarted;
72        let chain_size = FetchStatus::NotStarted;
73        let utxo_set_size = FetchStatus::NotStarted;
74        let total_money_supply = FetchStatus::NotStarted;
75        let total_transactions_count = FetchStatus::NotStarted;
76        let tps_for_last_30_days = FetchStatus::NotStarted;
77        let total_fees_for_last_24_hours = FetchStatus::NotStarted;
78        let difficulty = FetchStatus::NotStarted;
79        let current_difficulty_epoch = FetchStatus::NotStarted;
80        let block_count_until_retarget = FetchStatus::NotStarted;
81        let estimated_seconds_until_retarget = FetchStatus::NotStarted;
82        let average_block_time_since_last_difficulty_adjustement = FetchStatus::NotStarted;
83        let estimated_hash_rate_per_second_for_last_2016_blocks = FetchStatus::NotStarted;
84        let block_subsidy_of_most_recent_block = FetchStatus::NotStarted;
85        let blocks_mined_over_last_24_hours = FetchStatus::NotStarted;
86        let average_fees_per_block_over_last_24_hours = FetchStatus::NotStarted;
87        let average_fees_per_block_over_last_2016_blocks = FetchStatus::NotStarted;
88        let fees_as_a_percent_of_reward_for_last_2016_blocks = FetchStatus::NotStarted;
89        let fees_as_a_percent_of_reward_for_last_24_hours = FetchStatus::NotStarted;
90        let segwit_percent_last_24_hours = FetchStatus::NotStarted;
91        let segwit_spending_payments_percent_last_24_hours = FetchStatus::NotStarted;
92        let segwit_spending_transactions_percent_last_24_hours = FetchStatus::NotStarted;
93
94        Self::Initialized(InitializedData {
95            duration,
96            counter_sleep,
97            counter_tick,
98            stats: Stats {
99                bitcoin_price,
100                block_height,
101                last_block_time,
102                average_block_time,
103                chain_size,
104                seconds_since_last_block,
105                transactions_count_over_last_30_days,
106                average_block_time_for_last_2016_blocks,
107                total_money_supply,
108                utxo_set_size,
109                total_transactions_count,
110                tps_for_last_30_days,
111                total_fees_for_last_24_hours,
112                difficulty,
113                current_difficulty_epoch,
114                block_count_until_retarget,
115                estimated_seconds_until_retarget,
116                average_block_time_since_last_difficulty_adjustement,
117                estimated_hash_rate_per_second_for_last_2016_blocks,
118                block_subsidy_of_most_recent_block,
119                blocks_mined_over_last_24_hours,
120                average_fees_per_block_over_last_24_hours,
121                average_fees_per_block_over_last_2016_blocks,
122                fees_as_a_percent_of_reward_for_last_2016_blocks,
123                fees_as_a_percent_of_reward_for_last_24_hours,
124                segwit_percent_last_24_hours,
125                segwit_spending_payments_percent_last_24_hours,
126                segwit_spending_transactions_percent_last_24_hours,
127            },
128            newest_block_found_height: None,
129        })
130    }
131
132    pub fn is_initialized(&self) -> bool {
133        matches!(self, &Self::Initialized { .. })
134    }
135
136    pub fn incr_sleep(&mut self) {
137        if let Self::Initialized(InitializedData { counter_sleep, .. }) = self {
138            *counter_sleep += 1;
139        }
140    }
141
142    pub fn incr_tick(&mut self) {
143        if let Self::Initialized(InitializedData { counter_tick, .. }) = self {
144            *counter_tick += 1;
145        }
146    }
147    pub fn handle_new_block_found(&mut self, block_height: u64) {
148        if let Self::Initialized(InitializedData {
149            newest_block_found_height,
150            ..
151        }) = self
152        {
153            *newest_block_found_height = Some(block_height);
154        }
155    }
156
157    pub fn count_sleep(&self) -> Option<u32> {
158        if let Self::Initialized(InitializedData { counter_sleep, .. }) = self {
159            Some(*counter_sleep)
160        } else {
161            None
162        }
163    }
164
165    pub fn count_tick(&self) -> Option<u64> {
166        if let Self::Initialized(InitializedData { counter_tick, .. }) = self {
167            Some(*counter_tick)
168        } else {
169            None
170        }
171    }
172
173    pub fn duration(&self) -> Option<&Duration> {
174        if let Self::Initialized(InitializedData { duration, .. }) = self {
175            Some(duration)
176        } else {
177            None
178        }
179    }
180
181    pub fn increment_delay(&mut self) {
182        if let Self::Initialized(InitializedData { duration, .. }) = self {
183            // Set the duration, note that the duration is in 1s..10s
184            let secs = (duration.as_secs() + 1).clamp(1, 10);
185            *duration = Duration::from_secs(secs);
186        }
187    }
188
189    pub fn decrement_delay(&mut self) {
190        if let Self::Initialized(InitializedData { duration, .. }) = self {
191            // Set the duration, note that the duration is in 1s..10s
192            let secs = (duration.as_secs() - 1).clamp(1, 10);
193            *duration = Duration::from_secs(secs);
194        }
195    }
196    pub fn handle_fetch_resource(&mut self, resource: Resource) {
197        if let Self::Initialized(InitializedData {
198            stats:
199                Stats {
200                    bitcoin_price,
201                    block_height,
202                    seconds_since_last_block,
203                    transactions_count_over_last_30_days,
204                    average_block_time_for_last_2016_blocks,
205                    chain_size,
206                    utxo_set_size,
207                    total_money_supply,
208                    total_transactions_count,
209                    tps_for_last_30_days,
210                    total_fees_for_last_24_hours,
211                    difficulty,
212                    current_difficulty_epoch,
213                    block_count_until_retarget,
214                    estimated_seconds_until_retarget,
215                    average_block_time_since_last_difficulty_adjustement,
216                    estimated_hash_rate_per_second_for_last_2016_blocks,
217                    block_subsidy_of_most_recent_block,
218                    blocks_mined_over_last_24_hours,
219                    average_fees_per_block_over_last_24_hours,
220                    average_fees_per_block_over_last_2016_blocks,
221                    fees_as_a_percent_of_reward_for_last_2016_blocks,
222                    fees_as_a_percent_of_reward_for_last_24_hours,
223                    segwit_percent_last_24_hours,
224                    segwit_spending_payments_percent_last_24_hours,
225                    segwit_spending_transactions_percent_last_24_hours,
226                    ..
227                },
228            ..
229        }) = self
230        {
231            match resource {
232                Resource::BitcoinPrice(event) => match event {
233                    FetchEvent::Start => {
234                        *bitcoin_price = FetchStatus::InProgress(match bitcoin_price {
235                            FetchStatus::Complete(old_value) => Some(*old_value),
236                            FetchStatus::NotStarted => None,
237                            FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
238                                                                    // InProgress
239                        })
240                    }
241                    FetchEvent::Complete(new_bitcoin_price) => {
242                        *bitcoin_price = FetchStatus::Complete(new_bitcoin_price);
243                    }
244                },
245                Resource::NewBlockHeight(event) => match event {
246                    FetchEvent::Start => {
247                        *block_height = FetchStatus::InProgress(match block_height {
248                            FetchStatus::Complete(old_value) => Some(*old_value),
249                            FetchStatus::NotStarted => None,
250                            FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
251                                                                    // InProgress
252                        })
253                    }
254                    FetchEvent::Complete(new_block_height) => {
255                        *block_height = FetchStatus::Complete(new_block_height);
256                    }
257                },
258                Resource::SecondsSinceLastBlock(event) => match event {
259                    FetchEvent::Start => {
260                        *seconds_since_last_block =
261                            FetchStatus::InProgress(match seconds_since_last_block {
262                                FetchStatus::Complete(old_value) => Some(*old_value),
263                                FetchStatus::NotStarted => None,
264                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
265                                                                        // InProgress
266                            })
267                    }
268                    FetchEvent::Complete(new_seconds_since_last_block) => {
269                        *seconds_since_last_block =
270                            FetchStatus::Complete(new_seconds_since_last_block);
271                    }
272                },
273                Resource::TransactionsCountOverLast30Days(event) => match event {
274                    FetchEvent::Start => {
275                        *transactions_count_over_last_30_days =
276                            FetchStatus::InProgress(match transactions_count_over_last_30_days {
277                                FetchStatus::Complete(old_value) => Some(*old_value),
278                                FetchStatus::NotStarted => None,
279                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
280                                                                        // InProgress
281                            })
282                    }
283                    FetchEvent::Complete(new_transactions_count_over_last_30_days) => {
284                        *transactions_count_over_last_30_days =
285                            FetchStatus::Complete(new_transactions_count_over_last_30_days);
286                    }
287                },
288                Resource::AverageBlockTimeForLast2016Blocks(event) => match event {
289                    FetchEvent::Start => {
290                        *average_block_time_for_last_2016_blocks =
291                            FetchStatus::InProgress(match average_block_time_for_last_2016_blocks {
292                                FetchStatus::Complete(old_value) => Some(*old_value),
293                                FetchStatus::NotStarted => None,
294                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
295                                                                        // InProgress
296                            })
297                    }
298                    FetchEvent::Complete(new_average_block_time_for_last_2016_blocks) => {
299                        *average_block_time_for_last_2016_blocks =
300                            FetchStatus::Complete(new_average_block_time_for_last_2016_blocks);
301                    }
302                },
303                Resource::ChainSize(event) => match event {
304                    FetchEvent::Start => {
305                        *chain_size = FetchStatus::InProgress(match chain_size {
306                            FetchStatus::Complete(old_value) => Some(*old_value),
307                            FetchStatus::NotStarted => None,
308                            FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
309                                                                    // InProgress
310                        })
311                    }
312                    FetchEvent::Complete(new_chain_size) => {
313                        *chain_size = FetchStatus::Complete(new_chain_size);
314                    }
315                },
316                Resource::UtxoSetSize(event) => match event {
317                    FetchEvent::Start => {
318                        *utxo_set_size = FetchStatus::InProgress(match utxo_set_size {
319                            FetchStatus::Complete(old_value) => Some(*old_value),
320                            FetchStatus::NotStarted => None,
321                            FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
322                                                                    // InProgress
323                        })
324                    }
325                    FetchEvent::Complete(new_utxo_set_size) => {
326                        *utxo_set_size = FetchStatus::Complete(new_utxo_set_size);
327                    }
328                },
329                Resource::TotalMoneySupply(event) => match event {
330                    FetchEvent::Start => {
331                        *total_money_supply = FetchStatus::InProgress(match total_money_supply {
332                            FetchStatus::Complete(old_value) => Some(*old_value),
333                            FetchStatus::NotStarted => None,
334                            FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
335                                                                    // InProgress
336                        })
337                    }
338                    FetchEvent::Complete(new_total_money_supply) => {
339                        *total_money_supply = FetchStatus::Complete(new_total_money_supply);
340                    }
341                },
342                Resource::TotalTransactionCount(event) => match event {
343                    FetchEvent::Start => {
344                        *total_transactions_count =
345                            FetchStatus::InProgress(match total_transactions_count {
346                                FetchStatus::Complete(old_value) => Some(*old_value),
347                                FetchStatus::NotStarted => None,
348                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
349                                                                        // InProgress
350                            })
351                    }
352                    FetchEvent::Complete(new_total_transactions_count) => {
353                        *total_transactions_count =
354                            FetchStatus::Complete(new_total_transactions_count);
355                    }
356                },
357                Resource::TpsForLast30Days(event) => match event {
358                    FetchEvent::Start => {
359                        *tps_for_last_30_days =
360                            FetchStatus::InProgress(match tps_for_last_30_days {
361                                FetchStatus::Complete(old_value) => Some(*old_value),
362                                FetchStatus::NotStarted => None,
363                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
364                                                                        // InProgress
365                            })
366                    }
367                    FetchEvent::Complete(new_tps_for_last_30_days) => {
368                        *tps_for_last_30_days = FetchStatus::Complete(new_tps_for_last_30_days);
369                    }
370                },
371                Resource::TotalFeesForLast24Hours(event) => match event {
372                    FetchEvent::Start => {
373                        *total_fees_for_last_24_hours =
374                            FetchStatus::InProgress(match total_fees_for_last_24_hours {
375                                FetchStatus::Complete(old_value) => Some(*old_value),
376                                FetchStatus::NotStarted => None,
377                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
378                                                                        // InProgress
379                            })
380                    }
381                    FetchEvent::Complete(new_total_fees_for_last_24_hours) => {
382                        *total_fees_for_last_24_hours =
383                            FetchStatus::Complete(new_total_fees_for_last_24_hours);
384                    }
385                },
386                Resource::Difficulty(event) => match event {
387                    FetchEvent::Start => {
388                        *difficulty = FetchStatus::InProgress(match difficulty {
389                            FetchStatus::Complete(old_value) => Some(*old_value),
390                            FetchStatus::NotStarted => None,
391                            FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
392                                                                    // InProgress
393                        })
394                    }
395                    FetchEvent::Complete(new_difficulty) => {
396                        *difficulty = FetchStatus::Complete(new_difficulty);
397                    }
398                },
399                Resource::CurrentDifficultyEpoch(event) => match event {
400                    FetchEvent::Start => {
401                        *current_difficulty_epoch =
402                            FetchStatus::InProgress(match current_difficulty_epoch {
403                                FetchStatus::Complete(old_value) => Some(*old_value),
404                                FetchStatus::NotStarted => None,
405                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
406                                                                        // InProgress
407                            })
408                    }
409                    FetchEvent::Complete(new_current_difficulty_epoch) => {
410                        *current_difficulty_epoch =
411                            FetchStatus::Complete(new_current_difficulty_epoch);
412                    }
413                },
414                Resource::BlockCountUntilRetarget(event) => match event {
415                    FetchEvent::Start => {
416                        *block_count_until_retarget =
417                            FetchStatus::InProgress(match block_count_until_retarget {
418                                FetchStatus::Complete(old_value) => Some(*old_value),
419                                FetchStatus::NotStarted => None,
420                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
421                                                                        // InProgress
422                            })
423                    }
424                    FetchEvent::Complete(new_block_count_until_retarget) => {
425                        *block_count_until_retarget =
426                            FetchStatus::Complete(new_block_count_until_retarget);
427                    }
428                },
429                Resource::EstimatedSecondsUntilRetarget(event) => match event {
430                    FetchEvent::Start => {
431                        *estimated_seconds_until_retarget =
432                            FetchStatus::InProgress(match estimated_seconds_until_retarget {
433                                FetchStatus::Complete(old_value) => Some(*old_value),
434                                FetchStatus::NotStarted => None,
435                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
436                                                                        // InProgress
437                            })
438                    }
439                    FetchEvent::Complete(new_estimated_seconds_until_retarget) => {
440                        *estimated_seconds_until_retarget =
441                            FetchStatus::Complete(new_estimated_seconds_until_retarget);
442                    }
443                },
444                Resource::AverageBlockTimeSinceLastDifficultyAdjustment(event) => match event {
445                    FetchEvent::Start => {
446                        *average_block_time_since_last_difficulty_adjustement =
447                            FetchStatus::InProgress(
448                                match average_block_time_since_last_difficulty_adjustement {
449                                    FetchStatus::Complete(old_value) => Some(*old_value),
450                                    FetchStatus::NotStarted => None,
451                                    FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
452                                                                            // InProgress
453                                },
454                            )
455                    }
456                    FetchEvent::Complete(
457                        new_average_block_time_since_last_difficulty_adjustement,
458                    ) => {
459                        *average_block_time_since_last_difficulty_adjustement =
460                            FetchStatus::Complete(
461                                new_average_block_time_since_last_difficulty_adjustement,
462                            );
463                    }
464                },
465                Resource::EstimatedHashRatePerSecondForLast2016Blocks(event) => match event {
466                    FetchEvent::Start => {
467                        *estimated_hash_rate_per_second_for_last_2016_blocks =
468                            FetchStatus::InProgress(
469                                match estimated_hash_rate_per_second_for_last_2016_blocks {
470                                    FetchStatus::Complete(old_value) => Some(*old_value),
471                                    FetchStatus::NotStarted => None,
472                                    FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
473                                                                            // InProgress
474                                },
475                            )
476                    }
477                    FetchEvent::Complete(
478                        new_estimated_hash_rate_per_second_for_last_2016_blocks,
479                    ) => {
480                        *estimated_hash_rate_per_second_for_last_2016_blocks =
481                            FetchStatus::Complete(
482                                new_estimated_hash_rate_per_second_for_last_2016_blocks,
483                            );
484                    }
485                },
486                Resource::BlockSubsidyOfMostRecentBlock(event) => match event {
487                    FetchEvent::Start => {
488                        *block_subsidy_of_most_recent_block =
489                            FetchStatus::InProgress(match block_subsidy_of_most_recent_block {
490                                FetchStatus::Complete(old_value) => Some(*old_value),
491                                FetchStatus::NotStarted => None,
492                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
493                                                                        // InProgress
494                            })
495                    }
496                    FetchEvent::Complete(new_block_subsidy_of_most_recent_block) => {
497                        *block_subsidy_of_most_recent_block =
498                            FetchStatus::Complete(new_block_subsidy_of_most_recent_block);
499                    }
500                },
501                Resource::BlocksMinedOverLast24Hours(event) => match event {
502                    FetchEvent::Start => {
503                        *blocks_mined_over_last_24_hours =
504                            FetchStatus::InProgress(match blocks_mined_over_last_24_hours {
505                                FetchStatus::Complete(old_value) => Some(*old_value),
506                                FetchStatus::NotStarted => None,
507                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
508                                                                        // InProgress
509                            })
510                    }
511                    FetchEvent::Complete(new_blocks_mined_over_last_24_hours) => {
512                        *blocks_mined_over_last_24_hours =
513                            FetchStatus::Complete(new_blocks_mined_over_last_24_hours);
514                    }
515                },
516                Resource::AverageFeesPerBlockOverLast24Hours(event) => match event {
517                    FetchEvent::Start => {
518                        *average_fees_per_block_over_last_24_hours =
519                            FetchStatus::InProgress(match average_fees_per_block_over_last_24_hours
520                            {
521                                FetchStatus::Complete(old_value) => Some(*old_value),
522                                FetchStatus::NotStarted => None,
523                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
524                                                                        // InProgress
525                            })
526                    }
527                    FetchEvent::Complete(new_average_fees_per_block_over_last_24_hours) => {
528                        *average_fees_per_block_over_last_24_hours =
529                            FetchStatus::Complete(new_average_fees_per_block_over_last_24_hours);
530                    }
531                },
532                Resource::AverageFeesPerBlockOverLast2016Blocks(event) => match event {
533                    FetchEvent::Start => {
534                        *average_fees_per_block_over_last_2016_blocks = FetchStatus::InProgress(
535                            match average_fees_per_block_over_last_2016_blocks {
536                                FetchStatus::Complete(old_value) => Some(*old_value),
537                                FetchStatus::NotStarted => None,
538                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
539                                                                        // InProgress
540                            },
541                        )
542                    }
543                    FetchEvent::Complete(new_average_fees_per_block_over_last_2016_blocks) => {
544                        *average_fees_per_block_over_last_2016_blocks =
545                            FetchStatus::Complete(new_average_fees_per_block_over_last_2016_blocks);
546                    }
547                },
548                Resource::FeesAsAPercentOfRewardForLast2016Blocks(event) => match event {
549                    FetchEvent::Start => {
550                        *fees_as_a_percent_of_reward_for_last_2016_blocks = FetchStatus::InProgress(
551                            match fees_as_a_percent_of_reward_for_last_2016_blocks {
552                                FetchStatus::Complete(old_value) => Some(*old_value),
553                                FetchStatus::NotStarted => None,
554                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
555                                                                        // InProgress
556                            },
557                        )
558                    }
559                    FetchEvent::Complete(new_fees_as_a_percent_of_reward_for_last_2016_blocks) => {
560                        *fees_as_a_percent_of_reward_for_last_2016_blocks = FetchStatus::Complete(
561                            new_fees_as_a_percent_of_reward_for_last_2016_blocks,
562                        );
563                    }
564                },
565                Resource::FeesAsAPercentOfRewardForLast24Hours(event) => match event {
566                    FetchEvent::Start => {
567                        *fees_as_a_percent_of_reward_for_last_24_hours = FetchStatus::InProgress(
568                            match fees_as_a_percent_of_reward_for_last_24_hours {
569                                FetchStatus::Complete(old_value) => Some(*old_value),
570                                FetchStatus::NotStarted => None,
571                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
572                                                                        // InProgress
573                            },
574                        )
575                    }
576                    FetchEvent::Complete(new_fees_as_a_percent_of_reward_for_last_24_hours) => {
577                        *fees_as_a_percent_of_reward_for_last_24_hours = FetchStatus::Complete(
578                            new_fees_as_a_percent_of_reward_for_last_24_hours,
579                        );
580                    }
581                },
582                Resource::SegwitPercentLast24Hours(event) => match event {
583                    FetchEvent::Start => {
584                        *segwit_percent_last_24_hours =
585                            FetchStatus::InProgress(match segwit_percent_last_24_hours {
586                                FetchStatus::Complete(old_value) => Some(*old_value),
587                                FetchStatus::NotStarted => None,
588                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
589                                                                        // InProgress
590                            })
591                    }
592                    FetchEvent::Complete(new_segwit_percent_last_24_hours) => {
593                        *segwit_percent_last_24_hours =
594                            FetchStatus::Complete(new_segwit_percent_last_24_hours);
595                    }
596                },
597                Resource::SegwitSpendingPaymentsPercentLast24Hours(event) => match event {
598                    FetchEvent::Start => {
599                        *segwit_spending_payments_percent_last_24_hours = FetchStatus::InProgress(
600                            match segwit_spending_payments_percent_last_24_hours {
601                                FetchStatus::Complete(old_value) => Some(*old_value),
602                                FetchStatus::NotStarted => None,
603                                FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
604                                                                        // InProgress
605                            },
606                        )
607                    }
608                    FetchEvent::Complete(new_segwit_spending_payments_percent_last_24_hours) => {
609                        *segwit_spending_payments_percent_last_24_hours = FetchStatus::Complete(
610                            new_segwit_spending_payments_percent_last_24_hours,
611                        );
612                    }
613                },
614                Resource::SegwitSpendingTransactionsPercentLast24Hours(event) => match event {
615                    FetchEvent::Start => {
616                        *segwit_spending_transactions_percent_last_24_hours =
617                            FetchStatus::InProgress(
618                                match segwit_spending_transactions_percent_last_24_hours {
619                                    FetchStatus::Complete(old_value) => Some(*old_value),
620                                    FetchStatus::NotStarted => None,
621                                    FetchStatus::InProgress(_) => panic!(), // We should never go from InProgress to
622                                                                            // InProgress
623                                },
624                            )
625                    }
626                    FetchEvent::Complete(
627                        new_segwit_spending_transactions_percent_last_24_hours,
628                    ) => {
629                        *segwit_spending_transactions_percent_last_24_hours = FetchStatus::Complete(
630                            new_segwit_spending_transactions_percent_last_24_hours,
631                        );
632                    }
633                },
634            }
635        }
636    }
637}
638
639impl Default for AppState {
640    fn default() -> Self {
641        Self::Init
642    }
643}