Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.

Commit 8655615

Browse files
committed
Add all-time high/low change percentage to AssetMarket
Introduces fields for all_time_high_change_percentage and all_time_low_change_percentage in AssetMarket. Calculates these values in AssetPriceInfo and PriceRow, and adds a test to verify correct computation.
1 parent c2db8ca commit 8655615

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎crates/primitives/src/asset_price.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ pub struct AssetMarket {
3838
pub max_supply: Option<f64>,
3939
pub all_time_high: Option<f64>,
4040
pub all_time_high_date: Option<DateTime<Utc>>,
41+
pub all_time_high_change_percentage: Option<f64>,
4142
pub all_time_low: Option<f64>,
4243
pub all_time_low_date: Option<DateTime<Utc>>,
44+
pub all_time_low_change_percentage: Option<f64>,
4345
}
4446

4547
#[derive(Debug, Clone, Serialize, Deserialize)]

‎crates/primitives/src/asset_price_info.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl AssetPriceInfo {
4040
}
4141

4242
pub fn as_market_with_rate(&self, rate: f64) -> AssetMarket {
43+
let current_price = self.price.price;
4344
AssetMarket {
4445
market_cap: self.market.market_cap.map(|x| x * rate),
4546
market_cap_fdv: self.market.market_cap_fdv.map(|x| x * rate),
@@ -50,8 +51,45 @@ impl AssetPriceInfo {
5051
max_supply: self.market.max_supply,
5152
all_time_high: self.market.all_time_high.map(|x| x * rate),
5253
all_time_high_date: self.market.all_time_high_date,
54+
all_time_high_change_percentage: self.market.all_time_high.map(|ath| (current_price - ath) / ath * 100.0),
5355
all_time_low: self.market.all_time_low.map(|x| x * rate),
5456
all_time_low_date: self.market.all_time_low_date,
57+
all_time_low_change_percentage: self.market.all_time_low.map(|atl| (current_price - atl) / atl * 100.0),
5558
}
5659
}
5760
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
use crate::Chain;
66+
use chrono::Utc;
67+
68+
#[test]
69+
fn test_all_time_change_percentage() {
70+
let info = AssetPriceInfo {
71+
asset_id: AssetId::from(Chain::Bitcoin, None),
72+
price: Price::new(80.0, 5.0, Utc::now()),
73+
market: AssetMarket {
74+
market_cap: None,
75+
market_cap_fdv: None,
76+
market_cap_rank: None,
77+
total_volume: None,
78+
circulating_supply: None,
79+
total_supply: None,
80+
max_supply: None,
81+
all_time_high: Some(100.0),
82+
all_time_high_date: None,
83+
all_time_high_change_percentage: None,
84+
all_time_low: Some(40.0),
85+
all_time_low_date: None,
86+
all_time_low_change_percentage: None,
87+
},
88+
};
89+
90+
let market = info.as_market();
91+
92+
assert_eq!(market.all_time_high_change_percentage, Some(-20.0));
93+
assert_eq!(market.all_time_low_change_percentage, Some(100.0));
94+
}
95+
}

‎crates/storage/src/models/price.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ impl PriceRow {
145145
max_supply: Some(self.max_supply),
146146
all_time_high: Some(self.all_time_high),
147147
all_time_high_date: self.all_time_high_date.map(|d| d.and_utc()),
148+
all_time_high_change_percentage: Some((self.price - self.all_time_high) / self.all_time_high * 100.0),
148149
all_time_low: Some(self.all_time_low),
149150
all_time_low_date: self.all_time_low_date.map(|d| d.and_utc()),
151+
all_time_low_change_percentage: Some((self.price - self.all_time_low) / self.all_time_low * 100.0),
150152
}
151153
}
152154

0 commit comments

Comments
 (0)