Etherscan links based on netVersion identifier (#4772)

* Use netVersion to determine external links

* Update additional isTest references
This commit is contained in:
Jaco Greeff
2017-03-06 08:54:59 +01:00
committed by Gav Wood
parent 944dcdc010
commit 0b24a3d7f6
41 changed files with 217 additions and 175 deletions

View File

@@ -32,16 +32,12 @@ const REGISTRY_V1_HASHES = [
'0x64c3ee34851517a9faecd995c102b339f03e564ad6772dc43a26f993238b20ec' // homestead
];
export const setIsTestnet = (isTestnet) => ({ type: 'set isTestnet', isTestnet });
export const setNetVersion = (netVersion) => ({ type: 'set netVersion', netVersion });
export const fetchIsTestnet = () => (dispatch) =>
api.net.version()
.then((netVersion) => {
dispatch(setIsTestnet([
'2', // morden
'3', // ropsten
'42' // kovan
].includes(netVersion)));
dispatch(setNetVersion(netVersion));
})
.catch((err) => {
console.error('could not check if testnet');

View File

@@ -22,8 +22,8 @@ import namesReducer from './Names/reducers.js';
import recordsReducer from './Records/reducers.js';
import reverseReducer from './Reverse/reducers.js';
const isTestnetReducer = (state = null, action) =>
action.type === 'set isTestnet' ? action.isTestnet : state;
const netVersionReducer = (state = null, action) =>
action.type === 'set netVersion' ? action.netVersion : state;
const contractReducer = (state = null, action) =>
action.type === 'set contract' ? action.contract : state;
@@ -35,7 +35,7 @@ const ownerReducer = (state = null, action) =>
action.type === 'set owner' ? action.owner : state;
const initialState = {
isTestnet: isTestnetReducer(undefined, { type: '' }),
netVersion: netVersionReducer(undefined, { type: '' }),
accounts: accountsReducer(undefined, { type: '' }),
contacts: contactsReducer(undefined, { type: '' }),
contract: contractReducer(undefined, { type: '' }),
@@ -49,7 +49,7 @@ const initialState = {
};
export default (state = initialState, action) => ({
isTestnet: isTestnetReducer(state.isTestnet, action),
netVersion: netVersionReducer(state.netVersion, action),
accounts: accountsReducer(state.accounts, action),
contacts: contactsReducer(state.contacts, action),
contract: contractReducer(state.contract, action),

View File

@@ -28,7 +28,7 @@ class Address extends Component {
static propTypes = {
address: PropTypes.string.isRequired,
account: nullableProptype(PropTypes.object.isRequired),
isTestnet: PropTypes.bool.isRequired,
netVersion: PropTypes.string.isRequired,
key: PropTypes.string,
shortenHash: PropTypes.bool
};
@@ -56,7 +56,7 @@ class Address extends Component {
}
renderCaption () {
const { address, account, isTestnet, shortenHash } = this.props;
const { address, account, netVersion, shortenHash } = this.props;
if (account) {
const { name } = account;
@@ -64,7 +64,7 @@ class Address extends Component {
return (
<a
className={ styles.link }
href={ etherscanUrl(address, isTestnet) }
href={ etherscanUrl(address, false, netVersion) }
target='_blank'
>
<abbr
@@ -103,14 +103,14 @@ function mapStateToProps (initState, initProps) {
});
return (state, props) => {
const { isTestnet } = state;
const { netVersion } = state;
const { address = '' } = props;
const account = allAccounts[address] || null;
return {
account,
isTestnet
netVersion
};
};
}

View File

@@ -26,7 +26,7 @@ const leading0x = /^0x/;
class Hash extends Component {
static propTypes = {
hash: PropTypes.string.isRequired,
isTestnet: PropTypes.bool.isRequired,
netVersion: PropTypes.string.isRequired,
linked: PropTypes.bool
}
@@ -35,7 +35,7 @@ class Hash extends Component {
}
render () {
const { hash, isTestnet, linked } = this.props;
const { hash, netVersion, linked } = this.props;
let shortened = hash.toLowerCase().replace(leading0x, '');
@@ -47,7 +47,7 @@ class Hash extends Component {
return (
<a
className={ styles.link }
href={ etherscanUrl(hash, isTestnet) }
href={ etherscanUrl(hash, false, netVersion) }
target='_blank'
>
<abbr title={ hash }>{ shortened }</abbr>
@@ -61,7 +61,7 @@ class Hash extends Component {
export default connect(
(state) => ({ // mapStateToProps
isTestnet: state.isTestnet
netVersion: state.netVersion
}),
null // mapDispatchToProps
)(Hash);

View File

@@ -14,13 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { url as externalUrl } from '~/3rdparty/etherscan/links';
const leading0x = /^0x/;
const etherscanUrl = (hash, isTestnet) => {
const etherscanUrl = (hash, isTestnet, netVersion) => {
hash = hash.toLowerCase().replace(leading0x, '');
const type = hash.length === 40 ? 'address' : 'tx';
return `https://${isTestnet ? 'testnet.' : ''}etherscan.io/${type}/0x${hash}`;
return `https://${externalUrl(isTestnet, netVersion)}/${type}/0x${hash}`;
};
export default etherscanUrl;

View File

@@ -16,6 +16,7 @@
import BigNumber from 'bignumber.js';
import { url as etherscanUrl } from '~/3rdparty/etherscan/links';
import * as abis from '~/contracts/abi';
import { api } from './parity';
@@ -28,7 +29,7 @@ const subscriptions = {};
let defaultSubscriptionId;
let nextSubscriptionId = 1000;
let isTest = false;
let netVersion = '0';
export function subscribeEvents (addresses, callback) {
const subscriptionId = nextSubscriptionId++;
@@ -117,15 +118,16 @@ export function attachInstances () {
return Promise
.all([
api.parity.registryAddress(),
api.parity.netChain()
api.parity.netChain(),
api.partiy.netVersion()
])
.then(([registryAddress, netChain]) => {
.then(([registryAddress, netChain, _netVersion]) => {
const registry = api.newContract(abis.registry, registryAddress).instance;
isTest = ['kovan', 'morden', 'ropsten', 'testnet'].includes(netChain);
netVersion = _netVersion;
console.log(`contract was found at registry=${registryAddress}`);
console.log(`running on ${netChain}, isTest=${isTest}`);
console.log(`running on ${netChain}, network ${netVersion}`);
return Promise
.all([
@@ -287,5 +289,5 @@ export function loadTokenBalance (tokenAddress, address) {
}
export function txLink (txHash) {
return `https://${isTest ? 'testnet.' : ''}etherscan.io/tx/${txHash}`;
return `https://${etherscanUrl(false, netVersion)}/tx/${txHash}`;
}