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

feat(chain): enum values + TryFrom<u64> #782

Merged
merged 3 commits into from
Jan 11, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 46 additions & 34 deletions ethers-core/src/types/chain.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
use serde::Deserialize;
use thiserror::Error;

use core::convert::TryFrom;
tarrencev marked this conversation as resolved.
Show resolved Hide resolved
use std::{fmt, str::FromStr};

use crate::types::U256;

#[derive(Debug, Clone, Error)]
#[error("Failed to parse chain: {0}")]
pub struct ParseChainError(String);
pub struct TryFromUnknownChain;

#[repr(u64)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Chain {
Mainnet,
Ropsten,
Rinkeby,
Goerli,
Kovan,
XDai,
Polygon,
PolygonMumbai,
Avalanche,
AvalancheFuji,
Sepolia,
Moonbeam,
MoonbeamDev,
Moonriver,
Optimism,
OptimismKovan,
Mainnet = 1,
Ropsten = 3,
Rinkeby = 4,
Goerli = 5,
Kovan = 42,
XDai = 100,
Polygon = 137,
PolygonMumbai = 80001,
Avalanche = 43114,
AvalancheFuji = 43113,
Sepolia = 11155111,
Moonbeam = 1287,
MoonbeamDev = 1281,
Moonriver = 1285,
Optimism = 10,
OptimismKovan = 69,
}

impl fmt::Display for Chain {
Expand All @@ -38,24 +41,7 @@ impl fmt::Display for Chain {

impl From<Chain> for u32 {
fn from(chain: Chain) -> Self {
match chain {
Chain::Mainnet => 1,
Chain::Ropsten => 3,
Chain::Rinkeby => 4,
Chain::Goerli => 5,
Chain::Kovan => 42,
Chain::XDai => 100,
Chain::Polygon => 137,
Chain::PolygonMumbai => 80001,
Chain::Avalanche => 43114,
Chain::AvalancheFuji => 43113,
Chain::Sepolia => 11155111,
Chain::Moonbeam => 1287,
Chain::MoonbeamDev => 1281,
Chain::Moonriver => 1285,
Chain::Optimism => 10,
Chain::OptimismKovan => 69,
}
chain as u32
}
}

Expand All @@ -71,6 +57,32 @@ impl From<Chain> for u64 {
}
}

impl TryFrom<u64> for Chain {
type Error = ();

fn try_from(chainid: u64) -> Result<Chain, Self::Error> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could use a crate like https://crates.io/crates/num_enum to avoid duplicating the mapping here

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it's fine as-is, if we see the maintenance of this becoming a problem we can consider

match chainid {
1 => Ok(Chain::Mainnet),
3 => Ok(Chain::Ropsten),
4 => Ok(Chain::Rinkeby),
5 => Ok(Chain::Goerli),
42 => Ok(Chain::Kovan),
100 => Ok(Chain::XDai),
137 => Ok(Chain::Polygon),
80001 => Ok(Chain::PolygonMumbai),
43114 => Ok(Chain::Avalanche),
43113 => Ok(Chain::AvalancheFuji),
11155111 => Ok(Chain::Sepolia),
1287 => Ok(Chain::Moonbeam),
1281 => Ok(Chain::MoonbeamDev),
1285 => Ok(Chain::Moonriver),
10 => Ok(Chain::Optimism),
69 => Ok(Chain::OptimismKovan),
_ => Err(()),
}
}
}

impl FromStr for Chain {
type Err = ParseChainError;
fn from_str(chain: &str) -> Result<Self, Self::Err> {
Expand Down