diff --git a/README.md b/README.md index c4214d9..a0225e5 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ const customtTokenTrackerLink = etherscanLink.createCustomTokenTrackerLink(token const customAccountLink = etherscanLink.createCustomAccountLink(account, customNetworkUrl) const customExplorerLink = etherscanLink.createCustomExplorerLink(hash, customNetworkUrl) + +// Generate custom or native block explorer link based on rcpPrefs +const blockExplorerLink = etherscanLink.getBlockExplorerUrlForTx(transaction, rcpPrefs) ``` ## Running tests diff --git a/src/explorer-link.ts b/src/explorer-link.ts index 8217a4c..81ef7d6 100644 --- a/src/explorer-link.ts +++ b/src/explorer-link.ts @@ -2,6 +2,16 @@ import { addPathToUrl } from './helpers'; import prefixForChain from './prefix-for-chain'; import prefixForNetwork from './prefix-for-network'; +interface TransactionInterface { + hash: string; + chainId: string; + metamaskNetworkId: string; +} + +interface RpcPrefsInterface { + blockExplorerUrl?: string; +} + export function createCustomExplorerLink(hash: string, customNetworkUrl: string): string { const parsedUrl = addPathToUrl(customNetworkUrl, 'tx', hash); return parsedUrl; @@ -17,3 +27,13 @@ export function createExplorerLinkForChain(hash: string, chainId: string): strin const prefix = prefixForChain(chainId); return prefix === null ? '' : `https://${prefix}etherscan.io/tx/${hash}`; } + +export function getBlockExplorerUrlForTx(transaction: TransactionInterface, rpcPrefs: RpcPrefsInterface = {}) { + if (rpcPrefs.blockExplorerUrl) { + return createCustomExplorerLink(transaction.hash, rpcPrefs.blockExplorerUrl); + } + if (transaction.chainId) { + return createExplorerLinkForChain(transaction.hash, transaction.chainId); + } + return createExplorerLink(transaction.hash, transaction.metamaskNetworkId); +}