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

Remove hardcoded nft moralis chains #2278

Open
laruh opened this issue Nov 20, 2024 · 0 comments
Open

Remove hardcoded nft moralis chains #2278

laruh opened this issue Nov 20, 2024 · 0 comments

Comments

@laruh
Copy link
Member

laruh commented Nov 20, 2024

Right now we hardcode supported nft chains in enum, which require us to implement from to convertations for every new chain variant.

pub enum Chain {
Avalanche,
Bsc,
Eth,
Fantom,
Polygon,
}
pub trait ConvertChain {
fn to_ticker(&self) -> &'static str;
fn from_ticker(s: &str) -> Result<Chain, ParseChainTypeError>;
fn to_nft_ticker(&self) -> &'static str;
fn from_nft_ticker(s: &str) -> Result<Chain, ParseChainTypeError>;
}
impl ConvertChain for Chain {
#[inline(always)]
fn to_ticker(&self) -> &'static str {
match self {
Chain::Avalanche => "AVAX",
Chain::Bsc => "BNB",
Chain::Eth => "ETH",
Chain::Fantom => "FTM",
Chain::Polygon => "MATIC",
}
}
/// Converts a coin ticker string to a `Chain` enum.
#[inline(always)]
fn from_ticker(s: &str) -> Result<Chain, ParseChainTypeError> {
match s {
"AVAX" | "avax" => Ok(Chain::Avalanche),
"BNB" | "bnb" => Ok(Chain::Bsc),
"ETH" | "eth" => Ok(Chain::Eth),
"FTM" | "ftm" => Ok(Chain::Fantom),
"MATIC" | "matic" => Ok(Chain::Polygon),
_ => Err(ParseChainTypeError::UnsupportedChainType),
}
}
#[inline(always)]
fn to_nft_ticker(&self) -> &'static str {
match self {
Chain::Avalanche => "NFT_AVAX",
Chain::Bsc => "NFT_BNB",
Chain::Eth => "NFT_ETH",
Chain::Fantom => "NFT_FTM",
Chain::Polygon => "NFT_MATIC",
}
}
/// Converts a NFT ticker string to a `Chain` enum.
#[inline(always)]
fn from_nft_ticker(s: &str) -> Result<Chain, ParseChainTypeError> {
match s.to_uppercase().as_str() {
"NFT_AVAX" => Ok(Chain::Avalanche),
"NFT_BNB" => Ok(Chain::Bsc),
"NFT_ETH" => Ok(Chain::Eth),
"NFT_FTM" => Ok(Chain::Fantom),
"NFT_MATIC" => Ok(Chain::Polygon),
_ => Err(ParseChainTypeError::UnsupportedChainType),
}
}
}
impl fmt::Display for Chain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Chain::Avalanche => write!(f, "AVALANCHE"),
Chain::Bsc => write!(f, "BSC"),
Chain::Eth => write!(f, "ETH"),
Chain::Fantom => write!(f, "FANTOM"),
Chain::Polygon => write!(f, "POLYGON"),
}
}
}

Its better to have more generic approach in the code. We could use coins config to notify that chain supports nft feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant