From f4712fa47c110e03400872b8811487f082df7e81 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Fri, 3 Jun 2022 19:16:35 +0300 Subject: [PATCH] feat(etherscan): additional chain apis (#1343) * xdai * sokol * poa & rsk * oasis chains * fix test * test --- ethers-core/src/types/chain.rs | 30 ++++++++++++++++++++++++++- ethers-etherscan/src/lib.rs | 37 ++++++++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/ethers-core/src/types/chain.rs b/ethers-core/src/types/chain.rs index 1c8aa41d..acb8720e 100644 --- a/ethers-core/src/types/chain.rs +++ b/ethers-core/src/types/chain.rs @@ -44,6 +44,12 @@ pub enum Chain { BinanceSmartChain = 56, #[strum(serialize = "bsc-testnet")] BinanceSmartChainTestnet = 97, + Poa = 99, + Sokol = 77, + Rsk = 30, + Oasis = 26863, + Emerald = 42262, + EmeraldTestnet = 42261, } impl fmt::Display for Chain { @@ -74,6 +80,12 @@ impl fmt::Display for Chain { Chain::ArbitrumTestnet => "arbitrum-testnet", Chain::Cronos => "cronos", Chain::CronosTestnet => "cronos-testnet", + Chain::Poa => "poa", + Chain::Sokol => "sokol", + Chain::Rsk => "rsk", + Chain::Oasis => "oasis", + Chain::Emerald => "emerald", + Chain::EmeraldTestnet => "emerald-testnet", }; write!(formatter, "{}", chain) @@ -128,6 +140,12 @@ impl TryFrom for Chain { 421611 => Chain::ArbitrumTestnet, 25 => Chain::Cronos, 338 => Chain::CronosTestnet, + 99 => Chain::Poa, + 77 => Chain::Sokol, + 30 => Chain::Rsk, + 26863 => Chain::Oasis, + 42262 => Chain::Emerald, + 42261 => Chain::EmeraldTestnet, _ => return Err(ParseChainError(chain.to_string())), }) } @@ -173,6 +191,12 @@ impl FromStr for Chain { "arbitrum-testnet" => Chain::ArbitrumTestnet, "cronos" => Chain::Cronos, "cronos-testnet" => Chain::CronosTestnet, + "poa" => Chain::Poa, + "sokol" => Chain::Sokol, + "rsk" => Chain::Rsk, + "oasis" => Chain::Oasis, + "emerald" => Chain::Emerald, + "emerald-testnet" => Chain::EmeraldTestnet, _ => return Err(ParseChainError(chain.to_owned())), }) } @@ -192,7 +216,11 @@ impl Chain { Chain::BinanceSmartChain | Chain::BinanceSmartChainTestnet | Chain::Arbitrum | - Chain::ArbitrumTestnet, + Chain::ArbitrumTestnet | + Chain::Rsk | + Chain::Oasis | + Chain::Emerald | + Chain::EmeraldTestnet, ) } } diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index 36531a64..4f1af333 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -97,8 +97,14 @@ impl Client { Chain::Fantom | Chain::FantomTestnet => { std::env::var("FTMSCAN_API_KEY").or_else(|_| std::env::var("FANTOMSCAN_API_KEY"))? } - - Chain::XDai | Chain::Sepolia => String::default(), + Chain::XDai | + Chain::Sepolia | + Chain::Rsk | + Chain::Sokol | + Chain::Poa | + Chain::Oasis | + Chain::Emerald | + Chain::EmeraldTestnet => String::default(), Chain::Moonbeam | Chain::MoonbeamDev | Chain::Moonriver => { std::env::var("MOONSCAN_API_KEY")? } @@ -268,6 +274,29 @@ impl ClientBuilder { Chain::Moonriver => { urls("https://api-moonriver.moonscan.io/api", "https://moonriver.moonscan.io") } + // blockscout API is etherscan compatible + Chain::XDai => urls( + "https://blockscout.com/xdai/mainnet/api", + "https://blockscout.com/xdai/mainnet", + ), + Chain::Sokol => { + urls("https://blockscout.com/poa/sokol/api", "https://blockscout.com/poa/sokol") + } + Chain::Poa => { + urls("https://blockscout.com/poa/core/api", "https://blockscout.com/poa/core") + } + Chain::Rsk => { + urls("https://blockscout.com/rsk/mainnet/api", "https://blockscout.com/rsk/mainnet") + } + Chain::Oasis => urls("https://scan.oasischain.io/api", "https://scan.oasischain.io/"), + Chain::Emerald => urls( + "https://explorer.emerald.oasis.dev/api", + "https://explorer.emerald.oasis.dev/", + ), + Chain::EmeraldTestnet => urls( + "https://testnet.explorer.emerald.oasis.dev/api", + "https://testnet.explorer.emerald.oasis.dev/", + ), Chain::Dev => return Err(EtherscanError::LocalNetworksNotSupported), chain => return Err(EtherscanError::ChainNotSupported(chain)), }; @@ -447,10 +476,10 @@ mod tests { #[test] fn chain_not_supported() { - let err = Client::new_from_env(Chain::XDai).unwrap_err(); + let err = Client::new_from_env(Chain::Sepolia).unwrap_err(); assert!(matches!(err, EtherscanError::ChainNotSupported(_))); - assert_eq!(err.to_string(), "Chain xdai not supported"); + assert_eq!(err.to_string(), "Chain sepolia not supported"); } #[test]