Skip to content

Commit

Permalink
Added Custom Network conditional (#29)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Austin Akers authored Feb 16, 2021
1 parent 6a02ff0 commit cc59f27
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/account-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}` : '';
}
2 changes: 1 addition & 1 deletion src/explorer-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`: '';
}
4 changes: 2 additions & 2 deletions src/prefix-for-network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export = function getPrefixForNetwork(network: string): string {
export = function getPrefixForNetwork(network: string): string | null {
const net = parseInt(network)
let prefix;

Expand All @@ -19,7 +19,7 @@ export = function getPrefixForNetwork(network: string): string {
prefix = 'kovan.'
break
default:
prefix = ''
prefix = null
}
return prefix
}
7 changes: 4 additions & 3 deletions src/token-tracker-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }` : '' }`
: '';
}
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand All @@ -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')
})
})

/**
Expand Down Expand Up @@ -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')
})
})

0 comments on commit cc59f27

Please sign in to comment.