const DEFAULT_SORT_FIELD: &str = "volume_usd_24h";
pub fn map_pool_sort_field(field: &str) -> &'static str {
match field {
"volume_usd_24h" => "volume_usd_24h",
"volume_usd_7d" => "volume_usd_7d",
"volume_usd_30d" => "volume_usd_30d",
"liquidity_usd" => "liquidity_usd",
"txns_24h" => "txns_24h",
"created_at" => "created_at",
"price_usd" => "price_usd",
"price_change_percentage_24h" => "price_change_percentage_24h",
"volume_usd" => "volume_usd_24h",
"volume_24h" => "volume_usd_24h",
"volume_7d" => "volume_usd_7d",
"volume_30d" => "volume_usd_30d",
"transactions" => "txns_24h",
"last_price_change_usd_24h" => "price_change_percentage_24h",
"liquidity" => "liquidity_usd",
_ => DEFAULT_SORT_FIELD,
}
}
pub fn map_token_sort_field(field: &str) -> &'static str {
match field {
"volume_usd_24h" => "volume_usd_24h",
"volume_usd_7d" => "volume_usd_7d",
"volume_usd_30d" => "volume_usd_30d",
"liquidity_usd" => "liquidity_usd",
"txns_24h" => "txns_24h",
"fdv_usd" => "fdv_usd",
"created_at" => "created_at",
"price_change_percentage_24h" => "price_change_percentage_24h",
"volume_24h" => "volume_usd_24h",
"volume_7d" => "volume_usd_7d",
"volume_30d" => "volume_usd_30d",
"txns" => "txns_24h",
"price_change" => "price_change_percentage_24h",
"fdv" => "fdv_usd",
"price_usd" => "volume_usd_24h",
_ => DEFAULT_SORT_FIELD,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pool_legacy_values_map_to_canonical() {
assert_eq!(map_pool_sort_field("volume_usd"), "volume_usd_24h");
assert_eq!(map_pool_sort_field("volume_24h"), "volume_usd_24h");
assert_eq!(map_pool_sort_field("volume_7d"), "volume_usd_7d");
assert_eq!(map_pool_sort_field("volume_30d"), "volume_usd_30d");
assert_eq!(map_pool_sort_field("transactions"), "txns_24h");
assert_eq!(
map_pool_sort_field("last_price_change_usd_24h"),
"price_change_percentage_24h"
);
assert_eq!(map_pool_sort_field("liquidity"), "liquidity_usd");
}
#[test]
fn pool_canonical_values_pass_through() {
for v in [
"volume_usd_24h",
"volume_usd_7d",
"volume_usd_30d",
"liquidity_usd",
"txns_24h",
"created_at",
"price_usd",
"price_change_percentage_24h",
] {
assert_eq!(map_pool_sort_field(v), v);
}
}
#[test]
fn pool_unknown_value_falls_back_to_default() {
assert_eq!(map_pool_sort_field("nonsense"), "volume_usd_24h");
assert_eq!(map_pool_sort_field(""), "volume_usd_24h");
}
#[test]
fn token_legacy_values_map_to_canonical() {
assert_eq!(map_token_sort_field("volume_24h"), "volume_usd_24h");
assert_eq!(map_token_sort_field("volume_7d"), "volume_usd_7d");
assert_eq!(map_token_sort_field("volume_30d"), "volume_usd_30d");
assert_eq!(map_token_sort_field("txns"), "txns_24h");
assert_eq!(
map_token_sort_field("price_change"),
"price_change_percentage_24h"
);
assert_eq!(map_token_sort_field("fdv"), "fdv_usd");
}
#[test]
fn token_price_ordering_falls_back_to_volume() {
assert_eq!(map_token_sort_field("price_usd"), "volume_usd_24h");
}
#[test]
fn token_canonical_values_pass_through() {
for v in [
"volume_usd_24h",
"volume_usd_7d",
"volume_usd_30d",
"liquidity_usd",
"txns_24h",
"fdv_usd",
"created_at",
"price_change_percentage_24h",
] {
assert_eq!(map_token_sort_field(v), v);
}
}
#[test]
fn token_unknown_value_falls_back_to_default() {
assert_eq!(map_token_sort_field("nonsense"), "volume_usd_24h");
assert_eq!(map_token_sort_field(""), "volume_usd_24h");
}
}