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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ const customAccountLink = etherscanLink.createCustomAccountLink(account, customN
const customExplorerLink = etherscanLink.createCustomExplorerLink(hash, customNetworkUrl)

// Generate custom or native block explorer link based on rcpPrefs
const blockExplorerLink = etherscanLink.getBlockExplorerUrlForTx(transaction, rcpPrefs)
const blockExplorerLink = etherscanLink.getBlockExplorerLink(transaction, rcpPrefs)

// Generate account link for custom or native network
const getAccountLink = etherscanLink.getAccountLink(address, chainId, rpcPrefs, networkId)

// Generate token tracker link for custom or native network
const tokenTrackerLink = etherscanLink.getTokenTrackerLink(tokenAddress, chainId, networkId, holderAddress, rpcPrefs)
```

## Running tests
Expand Down
18 changes: 16 additions & 2 deletions src/account-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { addPathToUrl } from './helpers';
import prefixForChain from './prefix-for-chain';
import prefixForNetwork from './prefix-for-network';

export function createAccountLink(address: string, network: string): string {
const prefix = prefixForNetwork(network);
interface RpcPrefsInterface {
blockExplorerUrl?: string;
}

export function createAccountLink(address: string, networkId: string): string {
const prefix = prefixForNetwork(networkId);
return prefix === null ? '' : `https://${prefix}etherscan.io/address/${address}`;
}

Expand All @@ -16,3 +20,13 @@ export function createCustomAccountLink(address: string, customNetworkUrl: strin
const parsedUrl = addPathToUrl(customNetworkUrl, 'address', address);
return parsedUrl;
}

export function getAccountLink(address: string, chainId: string, rpcPrefs: RpcPrefsInterface = {}, networkId = '') {
if (rpcPrefs.blockExplorerUrl) {
return createCustomAccountLink(address, rpcPrefs.blockExplorerUrl);
}
if (networkId) {
return createAccountLink(address, networkId);
}
return createAccountLinkForChain(address, chainId);
}
4 changes: 2 additions & 2 deletions src/explorer-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { addPathToUrl } from './helpers';
import prefixForChain from './prefix-for-chain';
import prefixForNetwork from './prefix-for-network';

// TODO improve type safety / discriminating unions (this may require a discriminant property)
interface TransactionInterface {
hash: string;
chainId: string;
metamaskNetworkId: string;
}

interface RpcPrefsInterface {
blockExplorerUrl?: string;
}
Expand All @@ -28,7 +28,7 @@ export function createExplorerLinkForChain(hash: string, chainId: string): strin
return prefix === null ? '' : `https://${prefix}etherscan.io/tx/${hash}`;
}

export function getBlockExplorerUrlForTx(transaction: TransactionInterface, rpcPrefs: RpcPrefsInterface = {}) {
export function getBlockExplorerLink(transaction: TransactionInterface, rpcPrefs: RpcPrefsInterface = {}) {
if (rpcPrefs.blockExplorerUrl) {
return createCustomExplorerLink(transaction.hash, rpcPrefs.blockExplorerUrl);
}
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import { createAccountLink, createAccountLinkForChain, createCustomAccountLink } from './account-link';
import { createCustomExplorerLink, createExplorerLink, createExplorerLinkForChain } from './explorer-link';
import { createTokenTrackerLink, createCustomTokenTrackerLink, createTokenTrackerLinkForChain } from './token-tracker-link';
import { createAccountLink, createAccountLinkForChain, createCustomAccountLink, getAccountLink } from './account-link';
import { createCustomExplorerLink, createExplorerLink, createExplorerLinkForChain, getBlockExplorerLink } from './explorer-link';
import { createTokenTrackerLink, createCustomTokenTrackerLink, createTokenTrackerLinkForChain, getTokenTrackerLink } from './token-tracker-link';

export = {
createExplorerLink,
Expand All @@ -13,4 +13,7 @@ export = {
createTokenTrackerLink,
createCustomTokenTrackerLink,
createTokenTrackerLinkForChain,
getBlockExplorerLink,
getAccountLink,
getTokenTrackerLink,
};
4 changes: 2 additions & 2 deletions src/prefix-for-network.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export = function getPrefixForNetwork(network: string): string | null {
export = function getPrefixForNetwork(networkId: string): string | null {
// eslint-disable-next-line radix
const net = parseInt(network);
const net = parseInt(networkId);
let prefix;

switch (net) {
Expand Down
18 changes: 16 additions & 2 deletions src/token-tracker-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { addPathToUrl } from './helpers';
import prefixForChain from './prefix-for-chain';
import prefixForNetwork from './prefix-for-network';

interface RpcPrefsInterface {
blockExplorerUrl?: string;
}

export function createTokenTrackerLink(
tokenAddress: string,
network: string,
networkId: string,
holderAddress?: string,
): string {
const prefix = prefixForNetwork(network);
const prefix = prefixForNetwork(networkId);
return prefix === null ? '' :
`https://${prefix}etherscan.io/token/${tokenAddress}${
holderAddress ? `?a=${holderAddress}` : ''}`;
Expand All @@ -31,3 +35,13 @@ export function createTokenTrackerLinkForChain(
`https://${prefix}etherscan.io/token/${tokenAddress}${
holderAddress ? `?a=${holderAddress}` : ''}`;
}

export function getTokenTrackerLink(tokenAddress: string, chainId: string, networkId: string, holderAddress?: string, rpcPrefs: RpcPrefsInterface = {}) {
if (rpcPrefs.blockExplorerUrl) {
return createCustomTokenTrackerLink(tokenAddress, rpcPrefs.blockExplorerUrl);
}
if (networkId) {
return createTokenTrackerLink(tokenAddress, networkId, holderAddress);
}
return createTokenTrackerLinkForChain(tokenAddress, chainId, holderAddress);
}
210 changes: 210 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const {
createExplorerLinkForChain,
createAccountLinkForChain,
createTokenTrackerLinkForChain,
getBlockExplorerLink,
getAccountLink,
getTokenTrackerLink,
} = require('../dist');

// `https://${prefix}etherscan.io/address/${address}`
Expand Down Expand Up @@ -51,6 +54,50 @@ describe('account-link', function () {
const result = createCustomAccountLink('foo', 'https://data-seed-prebsc-1-s1.binance.org:8545');
assert.strictEqual(result, 'https://data-seed-prebsc-1-s1.binance.org:8545/address/foo', 'should return binance testnet address url');
});

describe('getAccountLink', function () {
it('should return the correct account-link url for an account based on chainId, networkId and rpcPref args', function () {
const getAccountLinkTests = [
{
expected: 'https://etherscan.io/address/0xabcd',
chainId: '0x1',
address: '0xabcd',
},
{
expected: 'https://etherscan.io/address/0xabcd',
networkId: '1',
address: '0xabcd',
},
{
expected: 'https://ropsten.etherscan.io/address/0xdef0',
chainId: '0x3',
address: '0xdef0',
rpcPrefs: {},
},
{
// test handling of `blockExplorerUrl` for a custom RPC
expected: 'https://block.explorer/address/0xabcd',
chainId: '0x21',
address: '0xabcd',
rpcPrefs: {
blockExplorerUrl: 'https://block.explorer',
},
},
{
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
expected: 'https://another.block.explorer/address/0xdef0',
chainId: '0x1f',
address: '0xdef0',
rpcPrefs: {
blockExplorerUrl: 'https://another.block.explorer/',
},
},
];
getAccountLinkTests.forEach(({ expected, address, chainId, rpcPrefs, networkId }) => {
assert.strictEqual(getAccountLink(address, chainId, rpcPrefs, networkId), expected);
});
});
});
});

// `https://${prefix}etherscan.io/tx/${hash}`
Expand Down Expand Up @@ -158,5 +205,168 @@ describe('token-tracker-link', function () {
const result = createCustomTokenTrackerLink('foo', 'https://data-seed-prebsc-1-s1.binance.org:8545/');
assert.strictEqual(result, 'https://data-seed-prebsc-1-s1.binance.org:8545/token/foo', 'should return binance testnet token url');
});

describe('getTokenTrackerLink', function () {
it('should return the correct token tracker url based on chainId, networkId and rpcPref args', function () {

const getTokenTrackerTests = [
{
expected: 'https://etherscan.io/token/0xabcd',
networkId: '1',
tokenAddress: '0xabcd',
},
{
expected: 'https://ropsten.etherscan.io/token/0xdef0',
networkId: '3',
tokenAddress: '0xdef0',
},
{
// test handling of `blockExplorerUrl` for a custom RPC
expected: 'https://block.explorer/token/0xar31',
tokenAddress: '0xar31',
rpcPrefs: {
blockExplorerUrl: 'https://block.explorer',
},
},
{
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
expected: 'https://another.block.explorer/token/0xdef0',
tokenAddress: '0xdef0',
rpcPrefs: {
blockExplorerUrl: 'https://another.block.explorer/',
},
},
{
expected: 'https://etherscan.io/token/0xabcd',
chainId: '0x1',
tokenAddress: '0xabcd',
},
{
expected: 'https://ropsten.etherscan.io/token/0xdef0',
chainId: '0x3',
tokenAddress: '0xdef0',
rpcPrefs: {},
},
{
// test handling of `blockExplorerUrl` for a custom RPC
expected: 'https://block.explorer/token/0xabcd',
chainId: '0x1f',
tokenAddress: '0xabcd',
rpcPrefs: {
blockExplorerUrl: 'https://block.explorer',
},
},
{
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
expected: 'https://another.block.explorer/token/0xdef0',
chainId: '0x21',
tokenAddress: '0xdef0',
rpcPrefs: {
blockExplorerUrl: 'https://another.block.explorer/',
},
},
];

getTokenTrackerTests.forEach((test) => {
assert.strictEqual(
getTokenTrackerLink(test.tokenAddress, test.chainId, test.networkId, test.holderAddress, test.rpcPrefs),
test.expected
);
});
});
});
});

/*
* Test getBlockExplorerLink,
* Which applies correct explorer-link generator based on args
*/
describe('getBlockExplorerLink', function () {
it('should return the correct block explorer url for an account based on chainId, networkId and rpcPref args', function () {

const getBlockExplorerLinkTests = [
{
expected: 'https://etherscan.io/tx/0xabcd',
transaction: {
metamaskNetworkId: '1',
hash: '0xabcd',
},
},
{
expected: 'https://ropsten.etherscan.io/tx/0xdef0',
transaction: {
metamaskNetworkId: '3',
hash: '0xdef0',
},
rpcPrefs: {},
},
{
// test handling of `blockExplorerUrl` for a custom RPC
expected: 'https://block.explorer/tx/0xabcd',
transaction: {
metamaskNetworkId: '31',
hash: '0xabcd',
},
rpcPrefs: {
blockExplorerUrl: 'https://block.explorer',
},
},
{
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
expected: 'https://another.block.explorer/tx/0xdef0',
transaction: {
metamaskNetworkId: '33',
hash: '0xdef0',
},
rpcPrefs: {
blockExplorerUrl: 'https://another.block.explorer/',
},
},
{
expected: 'https://etherscan.io/tx/0xabcd',
transaction: {
chainId: '0x1',
hash: '0xabcd',
},
},
{
expected: 'https://ropsten.etherscan.io/tx/0xdef0',
transaction: {
chainId: '0x3',
hash: '0xdef0',
},
rpcPrefs: {},
},
{
// test handling of `blockExplorerUrl` for a custom RPC
expected: 'https://block.explorer/tx/0xabcd',
transaction: {
chainId: '0x1f',
hash: '0xabcd',
},
rpcPrefs: {
blockExplorerUrl: 'https://block.explorer',
},
},
{
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
expected: 'https://another.block.explorer/tx/0xdef0',
transaction: {
chainId: '0x21',
hash: '0xdef0',
},
rpcPrefs: {
blockExplorerUrl: 'https://another.block.explorer/',
},
},
];

getBlockExplorerLinkTests.forEach((test) => {
assert.strictEqual(
getBlockExplorerLink(test.transaction, test.rpcPrefs),
test.expected
);
});
});
});
});