Skip to content
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/explorer-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}