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

feat(etherscan): additional chain apis #1343

Merged
merged 6 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
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
30 changes: 29 additions & 1 deletion ethers-core/src/types/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -128,6 +140,12 @@ impl TryFrom<u64> 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())),
})
}
Expand Down Expand Up @@ -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())),
})
}
Expand All @@ -192,7 +216,11 @@ impl Chain {
Chain::BinanceSmartChain |
Chain::BinanceSmartChainTestnet |
Chain::Arbitrum |
Chain::ArbitrumTestnet,
Chain::ArbitrumTestnet |
Chain::Rsk |
Chain::Oasis |
Chain::Emerald |
Chain::EmeraldTestnet,
)
}
}
Expand Down
37 changes: 33 additions & 4 deletions ethers-etherscan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?
}
Expand Down Expand Up @@ -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)),
};
Expand Down Expand Up @@ -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]
Expand Down