From f434e0f57fb814de8e2744f63a2086fa26530639 Mon Sep 17 00:00:00 2001 From: Options Eskimo <78507171+optionseskimo@users.noreply.github.com> Date: Thu, 27 Jan 2022 14:50:11 +0000 Subject: [PATCH] feat(chain): add BSC and BSC testnet (#831) * feat(chain): add BSC and BSC testnet * docs(changelog): add note about new BSC known chains --- CHANGELOG.md | 2 ++ ethers-core/src/types/chain.rs | 6 ++++++ ethers-etherscan/src/lib.rs | 27 ++++++++++++++++++--------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52b185ff..cf71a070 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Unreleased +- Add BSC mainnet and testnet to the list of known chains + [831](https://github.com/gakonst/ethers-rs/pull/831) - Returns error on invalid type conversion instead of panicking [691](https://github.com/gakonst/ethers-rs/pull/691/files) - Change types mapping for solidity `bytes` to rust `ethers::core::Bytes` and diff --git a/ethers-core/src/types/chain.rs b/ethers-core/src/types/chain.rs index 6bcbdd56..49fc1f81 100644 --- a/ethers-core/src/types/chain.rs +++ b/ethers-core/src/types/chain.rs @@ -32,6 +32,8 @@ pub enum Chain { Moonriver = 1285, Optimism = 10, OptimismKovan = 69, + BinanceSmartChain = 56, + BinanceSmartChainTestnet = 97, } impl fmt::Display for Chain { @@ -79,6 +81,8 @@ impl TryFrom for Chain { 1285 => Chain::Moonriver, 10 => Chain::Optimism, 69 => Chain::OptimismKovan, + 56 => Chain::BinanceSmartChain, + 97 => Chain::BinanceSmartChainTestnet, _ => return Err(ParseChainError(chain.to_string())), }) } @@ -106,6 +110,8 @@ impl FromStr for Chain { "optimism-kovan" => Chain::OptimismKovan, "fantom" => Chain::Fantom, "fantom-testnet" => Chain::FantomTestnet, + "bsc" => Chain::BinanceSmartChain, + "bsc-testnet" => Chain::BinanceSmartChainTestnet, _ => return Err(ParseChainError(chain.to_owned())), }) } diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index 341b855b..68b8fa93 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -73,6 +73,13 @@ impl Client { Url::parse("https://api-testnet.ftmscan.com"), Url::parse("https://testnet.ftmscan.com"), ), + Chain::BinanceSmartChain => { + (Url::parse("https://api.bscscan.com/api"), Url::parse("https://bscscan.com")) + } + Chain::BinanceSmartChainTestnet => ( + Url::parse("https://api-testnet.bscscan.com/api"), + Url::parse("https://testnet.bscscan.com"), + ), chain => return Err(EtherscanError::ChainNotSupported(chain)), }; @@ -90,15 +97,17 @@ impl Client { 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 | - Chain::Optimism | - Chain::OptimismKovan | - Chain::Fantom | - Chain::FantomTestnet => std::env::var("ETHERSCAN_API_KEY")?, + Chain::Mainnet + | Chain::Ropsten + | Chain::Kovan + | Chain::Rinkeby + | Chain::Goerli + | Chain::Optimism + | Chain::OptimismKovan + | Chain::Fantom + | Chain::FantomTestnet + | Chain::BinanceSmartChain + | Chain::BinanceSmartChainTestnet => std::env::var("ETHERSCAN_API_KEY")?, Chain::XDai | Chain::Sepolia => String::default(), Chain::Moonbeam | Chain::MoonbeamDev | Chain::Moonriver => {