Skip to content

Commit

Permalink
add EtherscanLink component
Browse files Browse the repository at this point in the history
  • Loading branch information
olekon committed Jan 7, 2020
1 parent 8abff4b commit 72836a2
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/components/common/EtherscanLink.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { PureComponent } from 'react';
import { getTxHashUrl, getAddressUrl, getRootUrl } from '../../scripts/etherscan';

/**
* Hyperlink to transaction, address, etc. on Etherscan website.
* Priority of parameters: tx > address. If multiple are set, the higher one takes precedence.
* For example if tx and address are both set, link points to tx page. If none is set, points to home page
*
* Props:
* networkId - network identifier, if undefined, points to mainnet site
* tx - hash of transaction, if set, points to transaction page
* address - Ethereum address, if set, points to address page.
*/
const EtherscanLink = ({ networkId = 1, txHash, address, children }) => {
const url = txHash ? getTxHashUrl(networkId, txHash) :
address ? getAddressUrl(networkId, address) : getRootUrl(networkId);

const text = txHash || txHash || address || url;

return (
<a href={url} target='_blank'>
{children || text}
</a>
);
};

export default EtherscanLink;
81 changes: 81 additions & 0 deletions src/stories/EtherscanLink.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React from 'react'
import { storiesOf } from '@storybook/react';
import { actionExtJson } from './storiesUtils.js';
import EtherscanLink from '../components/common/EtherscanLink.jsx';

storiesOf('EtherscanLink', module)
.add('tx hash', () => (
<div>
<div>
<p>Ropsten</p>
<EtherscanLink networkId='3' txHash='0xf99f80c443a7faaba79804233f18a47b03a9c6dfbaf24ea133fd91ed2feec12f' />
</div>
<br />
<div>
<p>Mainnet</p>
<EtherscanLink networkId='1' txHash='0x4203127198f6d321153fc2925ced7eef7f5476c7cec5ba8ac8a0d10e5b9c8b2f' />
</div>
<br />
<div>
<p>No network specified, default is Mainnet</p>
<EtherscanLink txHash='0x4203127198f6d321153fc2925ced7eef7f5476c7cec5ba8ac8a0d10e5b9c8b2f' />
</div>
<br />
<div>
<p>Custom text</p>
<EtherscanLink txHash='0x4203127198f6d321153fc2925ced7eef7f5476c7cec5ba8ac8a0d10e5b9c8b2f'>
Click here
</EtherscanLink>
</div>
</div>
))
.add('address', () => (
<div>
<div>
<p>Ropsten</p>
<EtherscanLink networkId='3' address='0xea674fdde714fd979de3edf0f56aa9716b898ec8' />
</div>
<br />
<div>
<p>Mainnet</p>
<EtherscanLink networkId='1' address='0xea674fdde714fd979de3edf0f56aa9716b898ec8' />
</div>
<br />
<div>
<p>No network specified, default is Mainnet</p>
<EtherscanLink address='0xea674fdde714fd979de3edf0f56aa9716b898ec8' />
</div>
<br />
<div>
<p>Custom text</p>
<EtherscanLink address='0xea674fdde714fd979de3edf0f56aa9716b898ec8'>
Click here
</EtherscanLink>
</div>
</div>
))
.add('none is set', () => (
<div>
<div>
If no tx or address is set, points to homepage
</div>
<br/>
<div>
<p>Ropsten</p>
<EtherscanLink networkId='3' />
</div>
<br />
<div>
<p>Mainnet</p>
<EtherscanLink networkId='1' />
</div>
<br />
<div>
<p>Custom text</p>
<EtherscanLink >
Click here
</EtherscanLink>
</div>
</div>
)
);

0 comments on commit 72836a2

Please sign in to comment.