fix(ethers-etherscan): Broken Etherscan URL Construction (#1100)

* 👷 fix etherscan url stringification

* 🎨 fmt
This commit is contained in:
abigger87 2022-03-31 16:42:31 -07:00 committed by GitHub
parent b3679fe113
commit 33f5b7c5fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 5 deletions

View File

@ -144,22 +144,22 @@ impl Client {
/// Return the URL for the given block number /// Return the URL for the given block number
pub fn block_url(&self, block: u64) -> String { pub fn block_url(&self, block: u64) -> String {
format!("{}/block/{}", self.etherscan_url, block) format!("{}block/{}", self.etherscan_url, block)
} }
/// Return the URL for the given address /// Return the URL for the given address
pub fn address_url(&self, address: Address) -> String { pub fn address_url(&self, address: Address) -> String {
format!("{}/address/{}", self.etherscan_url, address) format!("{}address/{}", self.etherscan_url, address)
} }
/// Return the URL for the given transaction hash /// Return the URL for the given transaction hash
pub fn transaction_url(&self, tx_hash: impl AsRef<str>) -> String { pub fn transaction_url(&self, tx_hash: impl AsRef<str>) -> String {
format!("{}/tx/{}", self.etherscan_url, tx_hash.as_ref()) format!("{}tx/{}", self.etherscan_url, tx_hash.as_ref())
} }
/// Return the URL for the given token hash /// Return the URL for the given token hash
pub fn token_url(&self, token_hash: impl AsRef<str>) -> String { pub fn token_url(&self, token_hash: impl AsRef<str>) -> String {
format!("{}/token/{}", self.etherscan_url, token_hash.as_ref()) format!("{}token/{}", self.etherscan_url, token_hash.as_ref())
} }
/// Execute an API POST request with a form /// Execute an API POST request with a form
@ -231,7 +231,7 @@ mod tests {
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
use ethers_core::types::Chain; use ethers_core::types::{Address, Chain};
use crate::{Client, EtherscanError}; use crate::{Client, EtherscanError};
@ -243,6 +243,38 @@ mod tests {
assert_eq!(err.to_string(), "chain xdai not supported"); assert_eq!(err.to_string(), "chain xdai not supported");
} }
#[test]
fn stringifies_block_url() {
let etherscan = Client::new_from_env(Chain::Mainnet).unwrap();
let block: u64 = 1;
let block_url: String = etherscan.block_url(block);
assert_eq!(block_url, format!("https://etherscan.io/block/{}", block));
}
#[test]
fn stringifies_address_url() {
let etherscan = Client::new_from_env(Chain::Mainnet).unwrap();
let addr: Address = Address::zero();
let address_url: String = etherscan.address_url(addr);
assert_eq!(address_url, format!("https://etherscan.io/address/{}", addr));
}
#[test]
fn stringifies_transaction_url() {
let etherscan = Client::new_from_env(Chain::Mainnet).unwrap();
let tx_hash = "0x0";
let tx_url: String = etherscan.transaction_url(tx_hash);
assert_eq!(tx_url, format!("https://etherscan.io/tx/{}", tx_hash));
}
#[test]
fn stringifies_token_url() {
let etherscan = Client::new_from_env(Chain::Mainnet).unwrap();
let token_hash = "0x0";
let token_url: String = etherscan.token_url(token_hash);
assert_eq!(token_url, format!("https://etherscan.io/token/{}", token_hash));
}
#[test] #[test]
fn local_networks_not_supported() { fn local_networks_not_supported() {
let err = Client::new_from_env(Chain::Dev).unwrap_err(); let err = Client::new_from_env(Chain::Dev).unwrap_err();