fedimovies/src/utils/currencies.rs

79 lines
1.8 KiB
Rust
Raw Normal View History

2022-08-04 12:07:57 +00:00
use std::convert::TryFrom;
use crate::errors::ConversionError;
use super::caip2::ChainId;
2022-08-04 12:07:57 +00:00
#[derive(Debug, PartialEq)]
pub enum Currency {
Ethereum,
}
impl Currency {
fn code(&self) -> String {
match self {
Self::Ethereum => "ETH",
}.to_string()
}
2022-08-04 15:44:48 +00:00
pub fn field_name(&self) -> String {
format!("${}", self.code())
}
2022-08-04 12:07:57 +00:00
/// Returns CAIP-2 chain ID
pub fn chain_id(&self) -> ChainId {
self.into()
}
pub fn normalize_address(&self, address: &str) -> String {
match self {
Self::Ethereum => address.to_lowercase(),
}
}
}
2022-08-04 12:07:57 +00:00
impl From<&Currency> for ChainId {
fn from(value: &Currency) -> Self {
let (namespace, reference) = match value {
Currency::Ethereum => ("eip155", "1"),
};
Self {
namespace: namespace.to_string(),
reference: reference.to_string(),
}
}
}
impl TryFrom<&ChainId> for Currency {
type Error = ConversionError;
fn try_from(value: &ChainId) -> Result<Self, Self::Error> {
let currency = match value.namespace.as_str() {
"eip155" => match value.reference.as_str() {
"1" => Self::Ethereum,
_ => return Err(ConversionError),
},
_ => return Err(ConversionError),
};
Ok(currency)
}
}
#[cfg(test)]
mod tests {
use super::*;
2022-08-04 12:07:57 +00:00
#[test]
fn test_chain_id_conversion() {
let ethereum = Currency::Ethereum;
let ethereum_chain_id = ChainId::from(&ethereum);
let currency = Currency::try_from(&ethereum_chain_id).unwrap();
assert_eq!(currency, ethereum);
}
#[test]
fn test_get_currency_field_name() {
let ethereum = Currency::Ethereum;
2022-08-04 15:44:48 +00:00
assert_eq!(ethereum.field_name(), "$ETH");
}
}