add support for polygon and avalanche (#606)

* add support for polygon and avalanche, include the block explorers

* add multicall addresses for polygon
This commit is contained in:
Ivan Porto Carrero 2021-11-22 01:02:28 -08:00 committed by GitHub
parent 1f9cd6b6c8
commit 1d65c9394f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 4 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target
.vscode
.vscode
/.envrc

View File

@ -27,6 +27,8 @@ pub static ADDRESS_BOOK: Lazy<HashMap<U256, Address>> = Lazy::new(|| {
(Chain::Goerli.into(), decode_address("77dca2c955b15e9de4dbbcf1246b4b85b651e50e")),
(Chain::Kovan.into(), decode_address("2cc8688c5f75e365aaeeb4ea8d6a480405a48d2a")),
(Chain::XDai.into(), decode_address("b5b692a88bdfc81ca69dcb1d924f59f0413a602a")),
(Chain::Polygon.into(), decode_address("11ce4B23bD875D7F5C6a31084f55fDe1e9A87507")),
(Chain::PolygonMumbai.into(), decode_address("08411ADd0b5AA8ee47563b146743C13b3556c9Cc")),
]
.into()
});

View File

@ -10,6 +10,10 @@ pub enum Chain {
Goerli,
Kovan,
XDai,
Polygon,
PolygonMumbai,
Avalanche,
AvalancheFuji,
}
impl fmt::Display for Chain {
@ -27,6 +31,10 @@ impl From<Chain> for u32 {
Chain::Goerli => 5,
Chain::Kovan => 42,
Chain::XDai => 100,
Chain::Polygon => 137,
Chain::PolygonMumbai => 80001,
Chain::Avalanche => 43114,
Chain::AvalancheFuji => 43113,
}
}
}

View File

@ -40,6 +40,21 @@ impl Client {
Url::parse(&format!("https://{}.etherscan.io", chain_name)),
)
}
Chain::Polygon => (
Url::parse("https://api.polygonscan.com/api"),
Url::parse("https://polygonscan.com"),
),
Chain::PolygonMumbai => (
Url::parse("https://api-testnet.polygonscan.com/api"),
Url::parse("https://mumbai.polygonscan.com"),
),
Chain::Avalanche => {
(Url::parse("https://api.snowtrace.io/api"), Url::parse("https://snowtrace.io"))
}
Chain::AvalancheFuji => (
Url::parse("https://api-testnet.snowtrace.io/api"),
Url::parse("https://testnet.snowtrace.io"),
),
chain => return Err(EtherscanError::ChainNotSupported(chain)),
};
@ -54,7 +69,15 @@ impl Client {
/// Create a new client with the correct endpoints based on the chain and API key
/// from ETHERSCAN_API_KEY environment variable
pub fn new_from_env(chain: Chain) -> Result<Self> {
Self::new(chain, std::env::var("ETHERSCAN_API_KEY")?)
let api_key = match chain {
Chain::Avalanche | Chain::AvalancheFuji => std::env::var("SNOWTRACE_API_KEY")?,
Chain::Polygon | Chain::PolygonMumbai => std::env::var("POLYGONSCAN_API_KEY")?,
Chain::Mainnet | Chain::Ropsten | Chain::Kovan | Chain::Rinkeby | Chain::Goerli => {
std::env::var("ETHERSCAN_API_KEY")?
}
Chain::XDai => String::default(),
};
Self::new(chain, api_key)
}
pub fn etherscan_api_url(&self) -> &Url {

View File

@ -1,5 +1,6 @@
# shellcheck shell=bash
# run all examples
for file in examples/*.rs; do
name=`echo $file | cut -f 1 -d '.'`
cargo r -p ethers --example `basename $name`
name="$(echo "$file" | cut -f 1 -d '.')"
cargo r -p ethers --example "$(basename "$name")"
done