Skip to content

Remove hardcoded nft moralis chains #2278

Open
@laruh

Description

@laruh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    improvement: configpriority: lowTasks with low importance that can be addressed when time permits.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions