Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apps/hermes/server): add crypto redemption rate asset type #2112

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/hermes/server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/hermes/server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hermes"
version = "0.7.1"
version = "0.7.2"
description = "Hermes is an agent that provides Verified Prices from the Pythnet Pyth Oracle."
edition = "2021"

Expand Down
59 changes: 56 additions & 3 deletions apps/hermes/server/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,70 @@ pub struct PriceFeedMetadata {
}

#[derive(Debug, Serialize, Deserialize, PartialEq, ToSchema)]
#[serde(rename_all = "lowercase")]
#[serde(rename_all = "snake_case")]
pub enum AssetType {
Crypto,
FX,
Equity,
Metals,
Metal,
Rates,
CryptoRedemptionRate,
}

impl Display for AssetType {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{:?}", self)
match self {
AssetType::Crypto => write!(f, "crypto"),
AssetType::FX => write!(f, "fx"),
AssetType::Equity => write!(f, "equity"),
AssetType::Metal => write!(f, "metal"),
AssetType::Rates => write!(f, "rates"),
AssetType::CryptoRedemptionRate => write!(f, "crypto_redemption_rate"),
}
}
}
Comment on lines 340 to +351
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe implement it using serde? It's an extra allocation but that shouldn't matter too much.

impl Display for AssetType {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        let value = serde_json::to_value(self).unwrap();
        write!(f, "{}", value.as_str().unwrap())
    }
}

(You can also use serde_value instead of serde_json).

Copy link
Collaborator Author

@ali-bahjati ali-bahjati Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that works but i wanted to be explicit here. and being explicit here actually helped me capture a bug :D (in snake_case FX becomes f_x). I could possibly have explicit tests though but the benefit here is that if i add a new field i won't miss adding this explicit definition.


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_serialize_matches_display() {
assert_eq!(
AssetType::Crypto.to_string(),
serde_json::to_string(&AssetType::Crypto)
.unwrap()
.trim_matches('"')
);
assert_eq!(
AssetType::FX.to_string(),
serde_json::to_string(&AssetType::FX)
.unwrap()
.trim_matches('"')
);
assert_eq!(
AssetType::Equity.to_string(),
serde_json::to_string(&AssetType::Equity)
.unwrap()
.trim_matches('"')
);
assert_eq!(
AssetType::Metal.to_string(),
serde_json::to_string(&AssetType::Metal)
.unwrap()
.trim_matches('"')
);
assert_eq!(
AssetType::Rates.to_string(),
serde_json::to_string(&AssetType::Rates)
.unwrap()
.trim_matches('"')
);
assert_eq!(
AssetType::CryptoRedemptionRate.to_string(),
serde_json::to_string(&AssetType::CryptoRedemptionRate)
.unwrap()
.trim_matches('"')
);
}
}
3 changes: 2 additions & 1 deletion apps/hermes/server/src/state/price_feeds_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ where
if let Some(asset_type) = &asset_type {
price_feeds_metadata.retain(|feed| {
feed.attributes.get("asset_type").map_or(false, |type_str| {
type_str.to_lowercase() == asset_type.to_string().to_lowercase()
type_str.to_lowercase().trim().replace(" ", "_")
== asset_type.to_string().to_lowercase()
})
});
}
Expand Down
Loading