prefer chainId when building block explorer urls (#10587)
parent
92680cf56f
commit
38fe75b7d9
@ -0,0 +1,96 @@ |
||||
import assert from 'assert'; |
||||
import { |
||||
MAINNET_CHAIN_ID, |
||||
MAINNET_NETWORK_ID, |
||||
ROPSTEN_CHAIN_ID, |
||||
ROPSTEN_NETWORK_ID, |
||||
} from '../../constants/network'; |
||||
import { getBlockExplorerUrlForTx } from '../transaction.utils'; |
||||
|
||||
const tests = [ |
||||
{ |
||||
expected: 'https://etherscan.io/tx/0xabcd', |
||||
transaction: { |
||||
metamaskNetworkId: MAINNET_NETWORK_ID, |
||||
hash: '0xabcd', |
||||
}, |
||||
}, |
||||
{ |
||||
expected: 'https://ropsten.etherscan.io/tx/0xdef0', |
||||
transaction: { |
||||
metamaskNetworkId: ROPSTEN_NETWORK_ID, |
||||
hash: '0xdef0', |
||||
}, |
||||
rpcPrefs: {}, |
||||
}, |
||||
{ |
||||
// test handling of `blockExplorerUrl` for a custom RPC
|
||||
expected: 'https://block.explorer/tx/0xabcd', |
||||
transaction: { |
||||
metamaskNetworkId: '31', |
||||
hash: '0xabcd', |
||||
}, |
||||
rpcPrefs: { |
||||
blockExplorerUrl: 'https://block.explorer', |
||||
}, |
||||
}, |
||||
{ |
||||
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
|
||||
expected: 'https://another.block.explorer/tx/0xdef0', |
||||
transaction: { |
||||
networkId: '33', |
||||
hash: '0xdef0', |
||||
}, |
||||
rpcPrefs: { |
||||
blockExplorerUrl: 'https://another.block.explorer/', |
||||
}, |
||||
}, |
||||
{ |
||||
expected: 'https://etherscan.io/tx/0xabcd', |
||||
transaction: { |
||||
chainId: MAINNET_CHAIN_ID, |
||||
hash: '0xabcd', |
||||
}, |
||||
}, |
||||
{ |
||||
expected: 'https://ropsten.etherscan.io/tx/0xdef0', |
||||
transaction: { |
||||
chainId: ROPSTEN_CHAIN_ID, |
||||
hash: '0xdef0', |
||||
}, |
||||
rpcPrefs: {}, |
||||
}, |
||||
{ |
||||
// test handling of `blockExplorerUrl` for a custom RPC
|
||||
expected: 'https://block.explorer/tx/0xabcd', |
||||
transaction: { |
||||
chainId: '0x1f', |
||||
hash: '0xabcd', |
||||
}, |
||||
rpcPrefs: { |
||||
blockExplorerUrl: 'https://block.explorer', |
||||
}, |
||||
}, |
||||
{ |
||||
// test handling of trailing `/` in `blockExplorerUrl` for a custom RPC
|
||||
expected: 'https://another.block.explorer/tx/0xdef0', |
||||
transaction: { |
||||
chainId: '0x21', |
||||
hash: '0xdef0', |
||||
}, |
||||
rpcPrefs: { |
||||
blockExplorerUrl: 'https://another.block.explorer/', |
||||
}, |
||||
}, |
||||
]; |
||||
|
||||
describe('getBlockExplorerUrlForTx', function () { |
||||
tests.forEach((test) => { |
||||
it(`should return '${test.expected}' for transaction with hash: '${test.transaction.hash}'`, function () { |
||||
assert.strictEqual( |
||||
getBlockExplorerUrlForTx(test.transaction, test.rpcPrefs), |
||||
test.expected, |
||||
); |
||||
}); |
||||
}); |
||||
}); |
@ -1,6 +1,37 @@ |
||||
import { |
||||
createExplorerLink, |
||||
createExplorerLinkForChain, |
||||
} from '@metamask/etherscan-link'; |
||||
|
||||
export function transactionMatchesNetwork(transaction, chainId, networkId) { |
||||
if (typeof transaction.chainId !== 'undefined') { |
||||
return transaction.chainId === chainId; |
||||
} |
||||
return transaction.metamaskNetworkId === networkId; |
||||
} |
||||
|
||||
/** |
||||
* build the etherscan link for a transaction by either chainId, if available |
||||
* or metamaskNetworkId as a fallback. If rpcPrefs is provided will build the |
||||
* url for the provided blockExplorerUrl. |
||||
* |
||||
* @param {Object} transaction - a transaction object from state |
||||
* @param {string} [transaction.metamaskNetworkId] - network id tx occurred on |
||||
* @param {string} [transaction.chainId] - chain id tx occurred on |
||||
* @param {string} [transaction.hash] - hash of the transaction |
||||
* @param {Object} [rpcPrefs] - the rpc preferences for the current RPC network |
||||
* @param {string} [rpcPrefs.blockExplorerUrl] - the block explorer url for RPC |
||||
* networks |
||||
* @returns {string} |
||||
*/ |
||||
export function getBlockExplorerUrlForTx(transaction, rpcPrefs = {}) { |
||||
if (rpcPrefs.blockExplorerUrl) { |
||||
return `${rpcPrefs.blockExplorerUrl.replace(/\/+$/u, '')}/tx/${ |
||||
transaction.hash |
||||
}`;
|
||||
} |
||||
if (transaction.chainId) { |
||||
return createExplorerLinkForChain(transaction.hash, transaction.chainId); |
||||
} |
||||
return createExplorerLink(transaction.hash, transaction.metamaskNetworkId); |
||||
} |
||||
|
Loading…
Reference in new issue