From 1d65c9394f6d519430586bbe437d4ba181dc8728 Mon Sep 17 00:00:00 2001 From: Ivan Porto Carrero Date: Mon, 22 Nov 2021 01:02:28 -0800 Subject: [PATCH] add support for polygon and avalanche (#606) * add support for polygon and avalanche, include the block explorers * add multicall addresses for polygon --- .gitignore | 3 ++- ethers-contract/src/multicall/mod.rs | 2 ++ ethers-core/src/types/chain.rs | 8 ++++++++ ethers-etherscan/src/lib.rs | 25 ++++++++++++++++++++++++- scripts/examples.sh | 5 +++-- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ccb51663..3668f4ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -.vscode \ No newline at end of file +.vscode +/.envrc diff --git a/ethers-contract/src/multicall/mod.rs b/ethers-contract/src/multicall/mod.rs index 84e9a1e7..86affacc 100644 --- a/ethers-contract/src/multicall/mod.rs +++ b/ethers-contract/src/multicall/mod.rs @@ -27,6 +27,8 @@ pub static ADDRESS_BOOK: Lazy> = 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() }); diff --git a/ethers-core/src/types/chain.rs b/ethers-core/src/types/chain.rs index 1b758de9..77353c20 100644 --- a/ethers-core/src/types/chain.rs +++ b/ethers-core/src/types/chain.rs @@ -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 for u32 { Chain::Goerli => 5, Chain::Kovan => 42, Chain::XDai => 100, + Chain::Polygon => 137, + Chain::PolygonMumbai => 80001, + Chain::Avalanche => 43114, + Chain::AvalancheFuji => 43113, } } } diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index a2e3b8fc..b7985573 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -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::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 { diff --git a/scripts/examples.sh b/scripts/examples.sh index 9639af55..8f2d3038 100755 --- a/scripts/examples.sh +++ b/scripts/examples.sh @@ -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