Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New utility method #14

Merged
merged 9 commits into from
Dec 12, 2023
Merged
Changes from all 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
144 changes: 131 additions & 13 deletions src/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,15 @@ impl NamedChain {
pub const fn average_blocktime_hint(self) -> Option<Duration> {
use NamedChain as C;

let ms = match self {
Some(Duration::from_millis(match self {
C::Mainnet => 12_000,

C::Arbitrum
| C::ArbitrumTestnet
| C::ArbitrumGoerli
| C::ArbitrumSepolia
| C::ArbitrumNova => 1_300,

C::Optimism
| C::OptimismGoerli
| C::OptimismSepolia
Expand All @@ -289,22 +291,37 @@ impl NamedChain {
| C::Zora
| C::ZoraGoerli
| C::ZoraSepolia => 2_000,

C::Polygon | C::PolygonMumbai => 2_100,

C::Moonbeam | C::Moonriver => 12_500,

C::BinanceSmartChain | C::BinanceSmartChainTestnet => 3_000,

C::Avalanche | C::AvalancheFuji => 2_000,

C::Fantom | C::FantomTestnet => 1_200,

C::Cronos | C::CronosTestnet | C::Canto | C::CantoTestnet => 5_700,

C::Evmos | C::EvmosTestnet => 1_900,

C::Aurora | C::AuroraTestnet => 1_100,

C::Oasis => 5_500,

C::Emerald => 6_000,

C::Dev | C::AnvilHardhat => 200,

C::Celo | C::CeloAlfajores | C::CeloBaklava => 5_000,

C::FilecoinCalibrationTestnet | C::FilecoinMainnet => 30_000,

C::Scroll | C::ScrollSepolia | C::ScrollAlphaTestnet => 3_000,

C::Gnosis | C::Chiado => 5_000,
// Explicitly exhaustive. See NB above.

C::Morden
| C::Ropsten
| C::Rinkeby
Expand All @@ -329,9 +346,7 @@ impl NamedChain {
| C::LineaTestnet
| C::Mantle
| C::MantleTestnet => return None,
};

Some(Duration::from_millis(ms))
}))
}

/// Returns whether the chain implements EIP-1559 (with the type 2 EIP-2718 transaction type).
Expand All @@ -348,7 +363,7 @@ impl NamedChain {
use NamedChain as C;

match self {
// Known legacy chains / non EIP-1559 compliant
// Known legacy chains / non EIP-1559 compliant.
C::OptimismKovan
| C::Fantom
| C::FantomTestnet
Expand All @@ -373,7 +388,7 @@ impl NamedChain {
| C::ScrollSepolia
| C::Metis => true,

// Known EIP-1559 chains
// Known EIP-1559 chains.
C::Mainnet
| C::Goerli
| C::Sepolia
Expand Down Expand Up @@ -401,7 +416,7 @@ impl NamedChain {
| C::ZoraGoerli
| C::ZoraSepolia => false,

// Unknown / not applicable, default to false for backwards compatibility
// Unknown / not applicable, default to false for backwards compatibility.
C::Dev
| C::AnvilHardhat
| C::Morden
Expand Down Expand Up @@ -454,6 +469,110 @@ impl NamedChain {
self.supports_shanghai()
}

/// Returns whether the chain is a testnet.
pub const fn is_testnet(self) -> bool {
use NamedChain as C;

match self {
// Ethereum testnets.
C::Goerli
| C::Holesky
| C::Kovan
| C::Sepolia
| C::Morden
| C::Ropsten
| C::Rinkeby => true,

// Other testnets.
C::ArbitrumGoerli
| C::ArbitrumSepolia
| C::ArbitrumTestnet
| C::AuroraTestnet
| C::AvalancheFuji
| C::BaseGoerli
| C::BinanceSmartChainTestnet
| C::CantoTestnet
| C::CronosTestnet
| C::CeloAlfajores
| C::CeloBaklava
| C::EmeraldTestnet
| C::EvmosTestnet
| C::FantomTestnet
| C::FilecoinCalibrationTestnet
| C::LineaTestnet
| C::MantleTestnet
| C::MoonbeamDev
| C::OptimismGoerli
| C::OptimismKovan
| C::OptimismSepolia
| C::PolygonMumbai
| C::PolygonZkEvmTestnet
| C::ScrollAlphaTestnet
| C::ScrollSepolia
| C::ZkSyncTestnet
| C::ZoraGoerli
| C::ZoraSepolia => true,

// Dev chains.
C::Dev | C::AnvilHardhat => true,

// Mainnets.
C::Mainnet
| C::Optimism
| C::Arbitrum
| C::ArbitrumNova
| C::Cronos
| C::Rsk
| C::BinanceSmartChain
| C::Poa
| C::Sokol
| C::Scroll
| C::Metis
| C::Gnosis
| C::Polygon
| C::PolygonZkEvm
| C::Fantom
| C::Moonbeam
| C::Moonriver
| C::Moonbase
| C::Evmos
| C::Chiado
| C::Oasis
| C::Emerald
| C::FilecoinMainnet
| C::Avalanche
| C::Celo
| C::Aurora
| C::Canto
| C::Boba
| C::Base
| C::Linea
| C::ZkSync
| C::Mantle
| C::Zora => false,
}
}

/// Returns the symbol of the chain's native currency.
pub const fn native_currency_symbol(self) -> Option<&'static str> {
use NamedChain as C;

Some(match self {
C::Mainnet
| C::Goerli
| C::Holesky
| C::Kovan
| C::Sepolia
| C::Morden
| C::Ropsten
| C::Rinkeby => "ETH",

C::BinanceSmartChain | C::BinanceSmartChainTestnet => "BNB",

_ => return None,
})
}

/// Returns the chain's blockchain explorer and its API (Etherscan and Etherscan-like) URLs.
///
/// Returns `(API_URL, BASE_URL)`.
Expand All @@ -476,7 +595,7 @@ impl NamedChain {
pub const fn etherscan_urls(self) -> Option<(&'static str, &'static str)> {
use NamedChain as C;

let urls = match self {
Some(match self {
C::Mainnet => ("https://api.etherscan.io/api", "https://etherscan.io"),
C::Ropsten => ("https://api-ropsten.etherscan.io/api", "https://ropsten.etherscan.io"),
C::Kovan => ("https://api-kovan.etherscan.io/api", "https://kovan.etherscan.io"),
Expand Down Expand Up @@ -626,7 +745,6 @@ impl NamedChain {
C::Boba => ("https://api.bobascan.com/api", "https://bobascan.com"),

C::Base => ("https://api.basescan.org/api", "https://basescan.org"),

C::BaseGoerli => ("https://api-goerli.basescan.org/api", "https://goerli.basescan.org"),

C::ZkSync => {
Expand All @@ -636,10 +754,12 @@ impl NamedChain {
"https://zksync2-testnet-explorer.zksync.dev/",
"https://goerli.explorer.zksync.io/",
),

C::Linea => ("https://api.lineascan.build/api", "https://lineascan.build/"),
C::LineaTestnet => {
("https://explorer.goerli.linea.build/api", "https://explorer.goerli.linea.build/")
}

C::Mantle => ("https://explorer.mantle.xyz/api", "https://explorer.mantle.xyz"),
C::MantleTestnet => {
("https://explorer.testnet.mantle.xyz/api", "https://explorer.testnet.mantle.xyz")
Expand All @@ -656,9 +776,7 @@ impl NamedChain {
C::AnvilHardhat | C::Dev | C::Morden | C::MoonbeamDev | C::FilecoinMainnet => {
return None;
}
};

Some(urls)
})
}

/// Returns the chain's blockchain explorer's API key environment variable's default name.
Expand Down