templar_proxy_oracle_kernel/proxy/circuit_breaker/rule/
stepwise_change.rs

1#[cfg(feature = "schemars")]
2use alloc::borrow::ToOwned;
3#[cfg(any(feature = "borsh", feature = "schemars"))]
4use alloc::string::ToString;
5use templar_primitives::Decimal;
6
7use crate::proxy::circuit_breaker::{
8    math::relative_abs_change_exceeds, CircuitBreakerRule, Observation, RingBuffer,
9};
10
11serialize! {
12    #[derive(Debug, Clone, PartialEq, Eq)]
13    /// Trips when the relative absolute change between adjacent observations
14    /// exceeds the configured maximum.
15    pub struct StepwiseChange {
16        /// Maximum allowed relative absolute change between adjacent prices.
17        pub max_relative_change: Decimal,
18    }
19}
20
21impl CircuitBreakerRule for StepwiseChange {
22    fn should_trip(&self, history: &RingBuffer<Observation>) -> bool {
23        if history.len() < 2 {
24            return false;
25        }
26        let Some(previous) = history.get(history.len() - 2) else {
27            return false;
28        };
29        let Some(current) = history.get(history.len() - 1) else {
30            return false;
31        };
32
33        relative_abs_change_exceeds(&previous.price, &current.price, self.max_relative_change)
34    }
35}