Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Make Chain's to_string and from_str inverse functions #897

Closed
meetmangukiya opened this issue Feb 11, 2022 · 1 comment · Fixed by #903
Closed

Make Chain's to_string and from_str inverse functions #897

meetmangukiya opened this issue Feb 11, 2022 · 1 comment · Fixed by #903

Comments

@meetmangukiya
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently, to_string and from_str are not mirroring each other, maybe they should.

Describe the solution you'd like
Make to_string and from_str inverse functions

let chain_name = "mainnet";

let chain = Chain::from_str(chain_name)?;
assert_eq!(chain.to_string(), chain_name);
@meetmangukiya meetmangukiya changed the title Make to_string and from_str inverse functions Make Chain's to_string and from_str inverse functions Feb 11, 2022
@mattsse
Copy link
Collaborator

mattsse commented Feb 11, 2022

this is done in the display impl

impl fmt::Display for Chain {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{:?}", self)
}
}

which by default prints the Variant name,
simply inversing the FromStr match would be better

Do you want to take this on? Basically switching left and right arm of this match

Ok(match chain {
"mainnet" => Chain::Mainnet,
"ropsten" => Chain::Ropsten,
"rinkeby" => Chain::Rinkeby,
"goerli" => Chain::Goerli,
"kovan" => Chain::Kovan,
"xdai" => Chain::XDai,
"polygon" => Chain::Polygon,
"polygon-mumbai" => Chain::PolygonMumbai,
"avalanche" => Chain::Avalanche,
"avalanche-fuji" => Chain::AvalancheFuji,
"sepolia" => Chain::Sepolia,
"moonbeam" => Chain::Moonbeam,
"moonbeam-dev" => Chain::MoonbeamDev,
"moonriver" => Chain::Moonriver,
"optimism" => Chain::Optimism,
"optimism-kovan" => Chain::OptimismKovan,
"fantom" => Chain::Fantom,
"fantom-testnet" => Chain::FantomTestnet,
"bsc" => Chain::BinanceSmartChain,
"bsc-testnet" => Chain::BinanceSmartChainTestnet,
"arbitrum" => Chain::Arbitrum,
"arbitrum-testnet" => Chain::ArbitrumTestnet,

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

Successfully merging a pull request may close this issue.

2 participants