Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Etherscan links based on netVersion identifier (#4772)
Browse files Browse the repository at this point in the history
* Use netVersion to determine external links

* Update additional isTest references
  • Loading branch information
jacogr authored and gavofyork committed Mar 6, 2017
1 parent 944dcdc commit 0b24a3d
Show file tree
Hide file tree
Showing 41 changed files with 217 additions and 175 deletions.
22 changes: 11 additions & 11 deletions js/src/3rdparty/etherscan/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const PAGE_SIZE = 25;
import util from '../../api/util';
import { call } from './call';

function _call (method, params, test) {
return call('account', method, params, test);
function _call (method, params, test, netVersion) {
return call('account', method, params, test, netVersion);
}

function balance (address, test = false) {
function balance (address, test, netVersion) {
return _call('balance', {
address: address,
tag: 'latest'
}, test).then((balance) => {
}, test, netVersion).then((balance) => {
// same format as balancemulti below
return {
account: address,
Expand All @@ -38,21 +38,21 @@ function balance (address, test = false) {
});
}

function balances (addresses, test = false) {
function balances (addresses, test, netVersion) {
return _call('balancemulti', {
address: addresses.join(','),
tag: 'latest'
}, test);
}, test, netVersion);
}

function transactions (address, page, test = false) {
function transactions (address, page, test, netVersion) {
// page offset from 0
return _call('txlist', {
address: address,
offset: PAGE_SIZE,
page: (page || 0) + 1,
sort: 'desc'
}, test).then((transactions) => {
}, test, netVersion).then((transactions) => {
return transactions.map((tx) => {
return {
blockNumber: new BigNumber(tx.blockNumber || 0),
Expand All @@ -67,9 +67,9 @@ function transactions (address, page, test = false) {
}

const account = {
balance: balance,
balances: balances,
transactions: transactions
balance,
balances,
transactions
};

export { account };
24 changes: 21 additions & 3 deletions js/src/3rdparty/etherscan/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,32 @@ const options = {
}
};

export function call (module, action, _params, test) {
const host = test ? 'testnet.etherscan.io' : 'api.etherscan.io';
export function call (module, action, _params, test, netVersion) {
let prefix = 'api.';

switch (netVersion) {
case '2':
case '3':
prefix = 'testnet.';
break;

case '42':
prefix = 'kovan.';
break;

case '0':
default:
if (test) {
prefix = 'testnet.';
}
break;
}

const query = stringify(Object.assign({
module, action
}, _params || {}));

return fetch(`https://${host}/api?${query}`, options)
return fetch(`https://${prefix}etherscan.io/api?${query}`, options)
.then((response) => {
if (!response.ok) {
throw { code: response.status, message: response.statusText }; // eslint-disable-line
Expand Down
4 changes: 2 additions & 2 deletions js/src/3rdparty/etherscan/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { stringify } from 'qs';

import { url } from './links';

function mockget (requests, test) {
let scope = nock(url(test));
function mockget (requests, test, netVersion) {
let scope = nock(url(test, netVersion));

requests.forEach((request) => {
scope = scope
Expand Down
33 changes: 27 additions & 6 deletions js/src/3rdparty/etherscan/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,35 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

export const url = (isTestnet = false) => {
return `https://${isTestnet ? 'testnet.' : ''}etherscan.io`;
// NOTE: Keep 'isTestnet' for backwards library compatibility
export const url = (isTestnet = false, netVersion = '0') => {
let prefix = '';

switch (netVersion) {
case '2':
case '3':
prefix = 'testnet.';
break;

case '42':
prefix = 'kovan.';
break;

case '0':
default:
if (isTestnet) {
prefix = 'testnet.';
}
break;
}

return `https://${prefix}etherscan.io`;
};

export const txLink = (hash, isTestnet = false) => {
return `${url(isTestnet)}/tx/${hash}`;
export const txLink = (hash, isTestnet = false, netVersion = '0') => {
return `${url(isTestnet, netVersion)}/tx/${hash}`;
};

export const addressLink = (address, isTestnet = false) => {
return `${url(isTestnet)}/address/${address}`;
export const addressLink = (address, isTestnet = false, netVersion = '0') => {
return `${url(isTestnet, netVersion)}/address/${address}`;
};
8 changes: 2 additions & 6 deletions js/src/dapps/registry/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions js/src/dapps/registry/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: '' }),
Expand All @@ -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),
Expand Down
10 changes: 5 additions & 5 deletions js/src/dapps/registry/ui/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -56,15 +56,15 @@ class Address extends Component {
}

renderCaption () {
const { address, account, isTestnet, shortenHash } = this.props;
const { address, account, netVersion, shortenHash } = this.props;

if (account) {
const { name } = account;

return (
<a
className={ styles.link }
href={ etherscanUrl(address, isTestnet) }
href={ etherscanUrl(address, false, netVersion) }
target='_blank'
>
<abbr
Expand Down Expand Up @@ -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
};
};
}
Expand Down
8 changes: 4 additions & 4 deletions js/src/dapps/registry/ui/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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, '');

Expand All @@ -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>
Expand All @@ -61,7 +61,7 @@ class Hash extends Component {

export default connect(
(state) => ({ // mapStateToProps
isTestnet: state.isTestnet
netVersion: state.netVersion
}),
null // mapDispatchToProps
)(Hash);
6 changes: 4 additions & 2 deletions js/src/dapps/registry/util/etherscan-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
14 changes: 8 additions & 6 deletions js/src/dapps/tokendeploy/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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++;
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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}`;
}
1 change: 0 additions & 1 deletion js/src/modals/ExecuteContract/executeContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ class ExecuteContract extends Component {
contract: PropTypes.object.isRequired,
fromAddress: PropTypes.string,
gasLimit: PropTypes.object.isRequired,
isTest: PropTypes.bool,
onClose: PropTypes.func.isRequired,
onFromAddressChange: PropTypes.func.isRequired
}
Expand Down
1 change: 1 addition & 0 deletions js/src/redux/providers/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export default class Status {
defaultExtraData,
netChain,
netPort,
netVersion,
rpcSettings,
isTest,
enode
Expand Down
1 change: 1 addition & 0 deletions js/src/redux/providers/statusReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const initialState = {
max: new BigNumber(0)
},
netPort: new BigNumber(0),
netVersion: '0',
rpcSettings: {},
syncing: true,
isConnected: false,
Expand Down
12 changes: 7 additions & 5 deletions js/src/ui/TxHash/txHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class TxHash extends Component {

static propTypes = {
hash: PropTypes.string.isRequired,
isTest: PropTypes.bool,
maxConfirmations: PropTypes.number,
netVersion: PropTypes.string.isRequired,
summary: PropTypes.bool
}

Expand Down Expand Up @@ -116,10 +116,10 @@ class TxHash extends Component {
}

render () {
const { hash, isTest, summary } = this.props;
const { hash, netVersion, summary } = this.props;

const hashLink = (
<a href={ txLink(hash, isTest) } target='_blank'>
<a href={ txLink(hash, false, netVersion) } target='_blank'>
<ShortenedHash data={ hash } />
</a>
);
Expand Down Expand Up @@ -255,9 +255,11 @@ class TxHash extends Component {
}

function mapStateToProps (state) {
const { isTest } = state.nodeStatus;
const { netVersion } = state.nodeStatus;

return { isTest };
return {
netVersion
};
}

export default connect(
Expand Down
Loading

0 comments on commit 0b24a3d

Please sign in to comment.