From 33f5b7c5fadd9061178b1944eea6c6b23b040c91 Mon Sep 17 00:00:00 2001 From: abigger87 Date: Thu, 31 Mar 2022 16:42:31 -0700 Subject: [PATCH] fix(ethers-etherscan): Broken Etherscan URL Construction (#1100) * :construction_worker: fix etherscan url stringification * :art: fmt --- ethers-etherscan/src/lib.rs | 42 ++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/ethers-etherscan/src/lib.rs b/ethers-etherscan/src/lib.rs index 2a0ad068..2255ac00 100644 --- a/ethers-etherscan/src/lib.rs +++ b/ethers-etherscan/src/lib.rs @@ -144,22 +144,22 @@ impl Client { /// Return the URL for the given block number 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 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 pub fn transaction_url(&self, tx_hash: impl AsRef) -> 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 pub fn token_url(&self, token_hash: impl AsRef) -> 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 @@ -231,7 +231,7 @@ mod tests { time::{Duration, SystemTime}, }; - use ethers_core::types::Chain; + use ethers_core::types::{Address, Chain}; use crate::{Client, EtherscanError}; @@ -243,6 +243,38 @@ mod tests { 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] fn local_networks_not_supported() { let err = Client::new_from_env(Chain::Dev).unwrap_err();