feat: add anvil hardhat chain id (#1356)

* feat: add anvil hardhat chain id

* chore: cover anvil hh
This commit is contained in:
Matthias Seitz 2022-06-08 12:29:00 +02:00 committed by GitHub
parent d6d76c6024
commit 0abc3ca39a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 20 deletions

View File

@ -1,11 +1,12 @@
use serde::Deserialize;
use thiserror::Error;
use core::convert::TryFrom;
use std::{convert::TryInto, default, fmt, str::FromStr};
use crate::types::U256; use crate::types::U256;
use serde::Deserialize;
use std::{
convert::{TryFrom, TryInto},
fmt,
str::FromStr,
};
use strum::EnumVariantNames; use strum::EnumVariantNames;
use thiserror::Error;
#[derive(Debug, Clone, Error)] #[derive(Debug, Clone, Error)]
#[error("Failed to parse chain: {0}")] #[error("Failed to parse chain: {0}")]
@ -26,6 +27,7 @@ pub enum Chain {
Polygon = 137, Polygon = 137,
Fantom = 250, Fantom = 250,
Dev = 1337, Dev = 1337,
AnvilHardhat = 31337,
FantomTestnet = 4002, FantomTestnet = 4002,
PolygonMumbai = 80001, PolygonMumbai = 80001,
Avalanche = 43114, Avalanche = 43114,
@ -86,6 +88,7 @@ impl fmt::Display for Chain {
Chain::Oasis => "oasis", Chain::Oasis => "oasis",
Chain::Emerald => "emerald", Chain::Emerald => "emerald",
Chain::EmeraldTestnet => "emerald-testnet", Chain::EmeraldTestnet => "emerald-testnet",
Chain::AnvilHardhat => "anvil-hardhat",
}; };
write!(formatter, "{}", chain) write!(formatter, "{}", chain)
@ -123,6 +126,7 @@ impl TryFrom<u64> for Chain {
100 => Chain::XDai, 100 => Chain::XDai,
137 => Chain::Polygon, 137 => Chain::Polygon,
1337 => Chain::Dev, 1337 => Chain::Dev,
31337 => Chain::Dev,
250 => Chain::Fantom, 250 => Chain::Fantom,
4002 => Chain::FantomTestnet, 4002 => Chain::FantomTestnet,
80001 => Chain::PolygonMumbai, 80001 => Chain::PolygonMumbai,
@ -185,6 +189,7 @@ impl FromStr for Chain {
"fantom" => Chain::Fantom, "fantom" => Chain::Fantom,
"fantom-testnet" => Chain::FantomTestnet, "fantom-testnet" => Chain::FantomTestnet,
"dev" => Chain::Dev, "dev" => Chain::Dev,
"anvil" | "hardhat" | "anvil-hardhat" => Chain::AnvilHardhat,
"bsc" => Chain::BinanceSmartChain, "bsc" => Chain::BinanceSmartChain,
"bsc-testnet" => Chain::BinanceSmartChainTestnet, "bsc-testnet" => Chain::BinanceSmartChainTestnet,
"arbitrum" => Chain::Arbitrum, "arbitrum" => Chain::Arbitrum,
@ -225,7 +230,7 @@ impl Chain {
} }
} }
impl default::Default for Chain { impl Default for Chain {
fn default() -> Self { fn default() -> Self {
Chain::Mainnet Chain::Mainnet
} }

View File

@ -1,23 +1,20 @@
//! Bindings for [etherscan.io web api](https://docs.etherscan.io/) //! Bindings for [etherscan.io web api](https://docs.etherscan.io/)
use contract::ContractMetadata;
use errors::EtherscanError;
use ethers_core::{
abi::{Abi, Address},
types::{Chain, H256},
};
use reqwest::{header, IntoUrl, Url};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{ use std::{
borrow::Cow, borrow::Cow,
io::Write, io::Write,
path::PathBuf, path::PathBuf,
time::{Duration, SystemTime, UNIX_EPOCH}, time::{Duration, SystemTime, UNIX_EPOCH},
}; };
use contract::ContractMetadata;
use reqwest::{header, IntoUrl, Url};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tracing::trace; use tracing::trace;
use errors::EtherscanError;
use ethers_core::{
abi::{Abi, Address},
types::{Chain, H256},
};
pub mod account; pub mod account;
pub mod contract; pub mod contract;
pub mod errors; pub mod errors;
@ -108,7 +105,9 @@ impl Client {
Chain::Moonbeam | Chain::MoonbeamDev | Chain::Moonriver => { Chain::Moonbeam | Chain::MoonbeamDev | Chain::Moonriver => {
std::env::var("MOONSCAN_API_KEY")? std::env::var("MOONSCAN_API_KEY")?
} }
Chain::Dev => return Err(EtherscanError::LocalNetworksNotSupported), Chain::AnvilHardhat | Chain::Dev => {
return Err(EtherscanError::LocalNetworksNotSupported)
}
}; };
Self::new(chain, api_key) Self::new(chain, api_key)
} }
@ -297,7 +296,9 @@ impl ClientBuilder {
"https://testnet.explorer.emerald.oasis.dev/api", "https://testnet.explorer.emerald.oasis.dev/api",
"https://testnet.explorer.emerald.oasis.dev/", "https://testnet.explorer.emerald.oasis.dev/",
), ),
Chain::Dev => return Err(EtherscanError::LocalNetworksNotSupported), Chain::AnvilHardhat | Chain::Dev => {
return Err(EtherscanError::LocalNetworksNotSupported)
}
chain => return Err(EtherscanError::ChainNotSupported(chain)), chain => return Err(EtherscanError::ChainNotSupported(chain)),
}; };
self.with_api_url(etherscan_api_url?)?.with_url(etherscan_url?) self.with_api_url(etherscan_api_url?)?.with_url(etherscan_url?)