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

feat(chain): add BSC and BSC testnet #831

Merged
merged 2 commits into from
Jan 27, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions ethers-core/src/types/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum Chain {
Moonriver = 1285,
Optimism = 10,
OptimismKovan = 69,
BinanceSmartChain = 56,
BinanceSmartChainTestnet = 97,
}

impl fmt::Display for Chain {
Expand Down Expand Up @@ -79,6 +81,8 @@ impl TryFrom<u64> for Chain {
1285 => Chain::Moonriver,
10 => Chain::Optimism,
69 => Chain::OptimismKovan,
56 => Chain::BinanceSmartChain,
97 => Chain::BinanceSmartChainTestnet,
_ => return Err(ParseChainError(chain.to_string())),
})
}
Expand Down Expand Up @@ -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())),
})
}
Expand Down
27 changes: 18 additions & 9 deletions ethers-etherscan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
};

Expand All @@ -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 => {
Expand Down