feat(ethers-core/Chain): make to_string and from_str inverse functions (#903)

This commit is contained in:
Meet Mangukiya 2022-02-12 21:11:18 +05:30 committed by GitHub
parent d4eb78730a
commit 1f0eb725a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 2 deletions

View File

@ -4,6 +4,7 @@
### Unreleased ### 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) - Add `eth_syncing` [848](https://github.com/gakonst/ethers-rs/pull/848)
- Fix overflow and possible divide-by-zero in `estimate_priority_fee` - Fix overflow and possible divide-by-zero in `estimate_priority_fee`
- Add BSC mainnet and testnet to the list of known chains - Add BSC mainnet and testnet to the list of known chains

View File

@ -40,7 +40,32 @@ pub enum Chain {
impl fmt::Display for Chain { impl fmt::Display for Chain {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 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)
} }
} }

View File

@ -232,7 +232,7 @@ mod tests {
let err = Client::new_from_env(Chain::XDai).unwrap_err(); let err = Client::new_from_env(Chain::XDai).unwrap_err();
assert!(matches!(err, EtherscanError::ChainNotSupported(_))); 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) { pub async fn run_at_least_duration(duration: Duration, block: impl Future) {