From cc59f2733eace3546a033682ea96ce8cc5db684e Mon Sep 17 00:00:00 2001 From: Austin Akers Date: Tue, 16 Feb 2021 04:28:10 -0800 Subject: [PATCH] Added Custom Network conditional (#29) * custom networks should return an empty url string instead of broken etherscan url * retuns null and removed parenthesis for ternary * updated account-link, token-tracker-link, and updated getPrefixForNetwork --- src/account-link.ts | 2 +- src/explorer-link.ts | 2 +- src/prefix-for-network.ts | 4 ++-- src/token-tracker-link.ts | 7 ++++--- test/index.test.js | 15 +++++++++++++++ 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/account-link.ts b/src/account-link.ts index 1f20eb0..1a5bae1 100644 --- a/src/account-link.ts +++ b/src/account-link.ts @@ -2,5 +2,5 @@ import prefixForNetwork from './prefix-for-network' export = function getAccountLink(address: string, network: string): string { const prefix = prefixForNetwork(network) - return `https://${prefix}etherscan.io/address/${address}` + return prefix !== null ? `https://${prefix}etherscan.io/address/${address}` : ''; } diff --git a/src/explorer-link.ts b/src/explorer-link.ts index 81a4002..c6f6589 100644 --- a/src/explorer-link.ts +++ b/src/explorer-link.ts @@ -2,5 +2,5 @@ import prefixForNetwork from './prefix-for-network' export = function getExplorerLink(hash: string, network: string): string { const prefix = prefixForNetwork(network) - return `https://${prefix}etherscan.io/tx/${hash}` + return prefix !== null ? `https://${prefix}etherscan.io/tx/${hash}`: ''; } diff --git a/src/prefix-for-network.ts b/src/prefix-for-network.ts index aaa5dfa..e612c52 100644 --- a/src/prefix-for-network.ts +++ b/src/prefix-for-network.ts @@ -1,4 +1,4 @@ -export = function getPrefixForNetwork(network: string): string { +export = function getPrefixForNetwork(network: string): string | null { const net = parseInt(network) let prefix; @@ -19,7 +19,7 @@ export = function getPrefixForNetwork(network: string): string { prefix = 'kovan.' break default: - prefix = '' + prefix = null } return prefix } diff --git a/src/token-tracker-link.ts b/src/token-tracker-link.ts index 17e63ab..b6cf390 100644 --- a/src/token-tracker-link.ts +++ b/src/token-tracker-link.ts @@ -6,7 +6,8 @@ export = function getTokenTrackerLink( holderAddress?: string, ): string { const prefix = prefixForNetwork(network) - return `https://${prefix}etherscan.io/token/${tokenAddress}${ - holderAddress ? `?a=${ holderAddress }` : '' - }` + return prefix !== null ? + `https://${prefix}etherscan.io/token/${tokenAddress}${ + holderAddress ? `?a=${ holderAddress }` : '' }` + : ''; } diff --git a/test/index.test.js b/test/index.test.js index 00f0de5..1926281 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -16,6 +16,11 @@ describe('account-link', function () { const result = createAccountLink('foo', '3') assert.strictEqual(result, 'https://ropsten.etherscan.io/address/foo', 'should handle ropsten') }) + + it('should have null as a prefix', function () { + const result = createAccountLink('foo', '3234') + assert.strictEqual(result, '', 'should return an empty string') + }) }) // `https://${prefix}etherscan.io/tx/${hash}` @@ -29,6 +34,11 @@ describe('explorer-link', function () { const result = createExplorerLink('foo', '3') assert.strictEqual(result, 'https://ropsten.etherscan.io/tx/foo', 'should handle ropsten') }) + + it('should have null as a prefix', function () { + const result = createExplorerLink('foo', '10') + assert.strictEqual(result, '', 'should return an empty string') + }) }) /** @@ -56,4 +66,9 @@ describe('token-tracker-link', function () { const result = createTokenTrackerLink('foo', '3', '0xabc') assert.strictEqual(result, 'https://ropsten.etherscan.io/token/foo?a=0xabc', 'should handle ropsten') }) + + it('should null has a prefix', function () { + const result = createTokenTrackerLink('foo', '3654', '0xabc') + assert.strictEqual(result, '', 'should return an empty string') + }) })