Skip to content

Commit

Permalink
Change filecoin explorer urls
Browse files Browse the repository at this point in the history
Resolves brave/brave-browser#23048

Use another explorer url for filecoin network to open transaction explorer tab.
Previous urls did not have web interface.
  • Loading branch information
cypt4 committed Jun 3, 2022
1 parent 017b1ae commit f133d5a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 13 deletions.
4 changes: 2 additions & 2 deletions components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const brave_wallet::mojom::NetworkInfo kKnownSolNetworks[] = {
const brave_wallet::mojom::NetworkInfo kKnownFilNetworks[] = {
{brave_wallet::mojom::kFilecoinMainnet,
"Filecoin Mainnet",
{"https://api.node.glif.io/rpc/v0"},
{"https://filscan.io/tipset/message-detail"},
{},
{"https://api.node.glif.io/rpc/v0"},
"FIL",
Expand All @@ -259,7 +259,7 @@ const brave_wallet::mojom::NetworkInfo kKnownFilNetworks[] = {
nullptr},
{brave_wallet::mojom::kFilecoinTestnet,
"Filecoin Testnet",
{"https://calibration.node.glif.io/rpc/v0"},
{"https://calibration.filscan.io/tipset/message-detail"},
{},
{"https://calibration.node.glif.io/rpc/v0"},
"FIL",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ TEST(BraveWalletUtilsUnitTest, GetChain) {
// Filecoin
mojom::NetworkInfo fil_mainnet(
brave_wallet::mojom::kFilecoinMainnet, "Filecoin Mainnet",
{"https://api.node.glif.io/rpc/v0"}, {},
{"https://filscan.io/tipset/message-detail"}, {},
{"https://api.node.glif.io/rpc/v0"}, "FIL", "Filecoin", 18,
brave_wallet::mojom::CoinType::FIL, nullptr);
EXPECT_FALSE(GetChain(&prefs, "0x123", mojom::CoinType::FIL));
Expand Down
54 changes: 53 additions & 1 deletion components/brave_wallet_ui/common/constants/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const mockNetwork: BraveWallet.NetworkInfo = {
chainId: '0x1',
chainName: 'Ethereum Main Net',
rpcUrls: ['https://mainnet.infura.io/v3/'],
blockExplorerUrls: ['https://etherscan.io/'],
blockExplorerUrls: ['https://etherscan.io'],
symbol: 'ETH',
symbolName: 'Ethereum',
decimals: 18,
Expand All @@ -67,6 +67,58 @@ export const mockNetwork: BraveWallet.NetworkInfo = {
data: undefined
}

export const mockFilecoinMainnetNetwork: BraveWallet.NetworkInfo = {
chainId: 'f',
chainName: 'Filecoin Mainnet',
rpcUrls: ['https://api.node.glif.io/rpc/v0'],
blockExplorerUrls: ['https://filscan.io/tipset/message-detail'],
symbol: 'FIL',
symbolName: 'Filecoin',
decimals: 18,
iconUrls: [],
coin: BraveWallet.CoinType.FIL,
data: undefined
}

export const mockFilecoinTestnetNetwork: BraveWallet.NetworkInfo = {
chainId: 't',
chainName: 'Filecoin Testnet',
rpcUrls: ['https://calibration.node.glif.io/rpc/v0'],
blockExplorerUrls: ['https://calibration.filscan.io/tipset/message-detail'],
symbol: 'FIL',
symbolName: 'Filecoin',
decimals: 18,
iconUrls: [],
coin: BraveWallet.CoinType.FIL,
data: undefined
}

export const mockSolanaMainnetNetwork: BraveWallet.NetworkInfo = {
chainId: '0x65',
chainName: 'Solana Mainnet Beta',
rpcUrls: ['https://mainnet-beta-solana.brave.com/rpc'],
blockExplorerUrls: ['https://explorer.solana.com'],
symbol: 'SOL',
symbolName: 'Solana',
decimals: 9,
iconUrls: [],
coin: BraveWallet.CoinType.SOL,
data: undefined
}

export const mockSolanaTestnetNetwork: BraveWallet.NetworkInfo = {
chainId: '0x66',
chainName: 'Solana Testnet',
rpcUrls: ['https://testnet-solana.brave.com/rpc'],
blockExplorerUrls: ['https://explorer.solana.com?cluster=testnet'],
symbol: 'SOL',
symbolName: 'Solana',
decimals: 9,
iconUrls: [],
coin: BraveWallet.CoinType.SOL,
data: undefined
}

export const mockERC20Token: BraveWallet.BlockchainToken = {
contractAddress: 'mockContractAddress',
name: 'Dog Coin',
Expand Down
46 changes: 46 additions & 0 deletions components/brave_wallet_ui/common/hooks/explorer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

import { buildExplorerUrl } from './explorer'
import {
mockNetwork,
mockFilecoinMainnetNetwork,
mockFilecoinTestnetNetwork,
mockSolanaMainnetNetwork,
mockSolanaTestnetNetwork
} from '../constants/mocks'

const etheriumTransactionId = '0xc0b00b52d0e4ea19d81f51730adc79a1850ce4c08c38e630b0c6668760d264c0'
const fileCointTransactionId = 'bafy2bzacec7altnqomkd6mu2kqrfgfontjfsg67xjv6zs3vn3wakumhj3bttg'
const solanaTransacitonId = 'wi14VnU8msHsmNq4N9j7YAWu7jdDPUxMKKM2VLcstSevKv4NfKbxNEUtMquSoMjtZUMd4YSASzmwvDamkWTfh9b'

it('etherium explorer url', () => {
const assertion = buildExplorerUrl(mockNetwork, 'tx', etheriumTransactionId, undefined)
expect(assertion).toEqual(`https://etherscan.io/tx/${etheriumTransactionId}`)
})

it('filecoin mainnet explorer url', () => {
const assertion = buildExplorerUrl(mockFilecoinMainnetNetwork, 'tx', fileCointTransactionId, undefined)
expect(assertion).toEqual(
`https://filscan.io/tipset/message-detail?cid=${fileCointTransactionId}`)
})

it('filecoin test explorer url', () => {
const assertion = buildExplorerUrl(mockFilecoinTestnetNetwork, 'tx', fileCointTransactionId, undefined)
expect(assertion).toEqual(
`https://calibration.filscan.io/tipset/message-detail?cid=${fileCointTransactionId}`)
})

it('solana test explorer url', () => {
const assertion = buildExplorerUrl(mockSolanaTestnetNetwork, 'tx', solanaTransacitonId, undefined)
expect(assertion).toEqual(
`https://explorer.solana.com/tx/${solanaTransacitonId}?cluster=testnet`)
})

it('solana mainnet explorer url', () => {
const assertion = buildExplorerUrl(mockSolanaMainnetNetwork, 'tx', solanaTransacitonId, undefined)
expect(assertion).toEqual(
`https://explorer.solana.com/tx/${solanaTransacitonId}`)
})
37 changes: 28 additions & 9 deletions components/brave_wallet_ui/common/hooks/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,41 @@ import { getLocale } from '../../../common/locale'
import { BraveWallet, BlockExplorerUrlTypes } from '../../constants/types'
import Amount from '../../utils/amount'

export function buildExplorerUrl (
network: BraveWallet.NetworkInfo, type: BlockExplorerUrlTypes,
value?: string, id?: string) {
const explorerURL = network.blockExplorerUrls[0]

if (type === 'contract') {
return `${explorerURL}/${value}?a=${new Amount(id ?? '').format()}`
}

const isFileCoinNet =
(network.chainId === BraveWallet.FILECOIN_TESTNET || network.chainId === BraveWallet.FILECOIN_MAINNET)

const isSolanaDevOrTestNet =
(network.chainId === BraveWallet.SOLANA_TESTNET || network.chainId === BraveWallet.SOLANA_DEVNET)

if (isFileCoinNet) {
return `${explorerURL}?cid=${value}`
} else if (isSolanaDevOrTestNet) {
const explorerIndex = explorerURL.lastIndexOf('?')
return `${explorerURL.substring(0, explorerIndex)}/${type}/${value}${explorerURL.substring(explorerIndex)}`
} else {
return `${explorerURL}/${type}/${value}`
}
}

export default function useExplorer (network: BraveWallet.NetworkInfo) {
return React.useCallback(
(type: BlockExplorerUrlTypes, value?: string, id?: string) => () => {
const explorerURL = network.blockExplorerUrls[0]
if (!explorerURL || !value) {
const explorerBaseURL = network.blockExplorerUrls[0]
if (!explorerBaseURL || !value) {
alert(getLocale('braveWalletTransactionExplorerMissing'))
return
}

const explorerIndex = explorerURL.lastIndexOf('?')

const url = type === 'contract'
? `${explorerURL}/${value}?a=${new Amount(id ?? '').format()}`
: (network.chainId === BraveWallet.SOLANA_TESTNET || network.chainId === BraveWallet.SOLANA_DEVNET)
? `${explorerURL.substring(0, explorerIndex)}/${type}/${value}${explorerURL.substring(explorerIndex)}`
: `${explorerURL}/${type}/${value}`
const url = buildExplorerUrl(network, type, value, id)

if (!chrome.tabs) {
window.open(url, '_blank')
Expand Down

0 comments on commit f133d5a

Please sign in to comment.