From 1f0eb725a7658101b13ac8f96f93704095949100 Mon Sep 17 00:00:00 2001 From: Meet Mangukiya Date: Sat, 12 Feb 2022 21:11:18 +0530 Subject: [PATCH] feat(ethers-core/Chain): make to_string and from_str inverse functions (#903) --- CHANGELOG.md | 1 + ethers-core/src/types/chain.rs | 27 ++++++++++++++++++++++++++- ethers-etherscan/src/lib.rs | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7379e9b2..be90c928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Unreleased +- `Chain::to_string` will return the same chain name as `Chain::from_str` - Add `eth_syncing` [848](https://github.com/gakonst/ethers-rs/pull/848) - Fix overflow and possible divide-by-zero in `estimate_priority_fee` - Add BSC mainnet and testnet to the list of known chains diff --git a/ethers-core/src/types/chain.rs b/ethers-core/src/types/chain.rs index c1626fe8..ebf5855c 100644 --- a/ethers-core/src/types/chain.rs +++ b/ethers-core/src/types/chain.rs @@ -40,7 +40,32 @@ pub enum Chain { impl fmt::Display for Chain { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "{:?}", self) + let chain = match self { + Chain::Mainnet => "mainnet", + Chain::Ropsten => "ropsten", + Chain::Rinkeby => "rinkeby", + Chain::Goerli => "goerli", + Chain::Kovan => "kovan", + Chain::XDai => "xdai", + Chain::Polygon => "polygon", + Chain::PolygonMumbai => "polygon-mumbai", + Chain::Avalanche => "avalanche", + Chain::AvalancheFuji => "avalanche-fuji", + Chain::Sepolia => "sepolia", + Chain::Moonbeam => "moonbeam", + Chain::MoonbeamDev => "moonbeam-dev", + Chain::Moonriver => "moonriver", + Chain::Optimism => "optimism", + Chain::OptimismKovan => "optimism-kovan", + Chain::Fantom => "fantom", + Chain::FantomTestnet => "fantom-testnet", + Chain::BinanceSmartChain => "bsc", + Chain::BinanceSmartChainTestnet => "bsc-testnet", + Chain::Arbitrum => "arbitrum", + Chain::ArbitrumTestnet => "arbitrum-testnet", + }; + + write!(formatter, "{}", chain) } } diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index a1a261d5..d9a2f10b 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -232,7 +232,7 @@ mod tests { let err = Client::new_from_env(Chain::XDai).unwrap_err(); assert!(matches!(err, EtherscanError::ChainNotSupported(_))); - assert_eq!(err.to_string(), "chain XDai not supported"); + assert_eq!(err.to_string(), "chain xdai not supported"); } pub async fn run_at_least_duration(duration: Duration, block: impl Future) {