Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: map the supported block explorers #25908

Merged
merged 17 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
7df68db
fix: :bug: map the supported block explorers
matteoscurati Jul 17, 2024
53cfbcd
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 17, 2024
20c95ba
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 18, 2024
af53459
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 19, 2024
7c6e667
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 19, 2024
11ffbf3
refactor: :recycle: use I18n insert values
matteoscurati Jul 19, 2024
279857a
refactor: :recycle: change object name
matteoscurati Jul 19, 2024
00014e4
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 19, 2024
f24b45f
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 22, 2024
7cf463c
feat: :bug: don't use a name from a different block explorer
matteoscurati Jul 22, 2024
866491c
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 22, 2024
648054b
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 22, 2024
d1f3ffa
docs: :memo: improve TSDOC
matteoscurati Jul 22, 2024
4b30108
refactor: :recycle: add type guard for SUPPORTED_NOTIFICATION_BLOCK_E…
matteoscurati Jul 22, 2024
cb801a0
refactor: :label: update local message
matteoscurati Jul 22, 2024
ff2f9a6
fix: :label: fix locales
matteoscurati Jul 22, 2024
c89757b
Merge branch 'develop' into fix/metamask-notifications-block-explorers
matteoscurati Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const NotificationDetailBlockExplorerButton = ({
const t = useI18nContext();

const chainIdHex = decimalToHex(chainId);
const { nativeBlockExplorerUrl } = getNetworkDetailsByChainId(
const { blockExplorerConfig } = getNetworkDetailsByChainId(
`0x${chainIdHex}` as keyof typeof CHAIN_IDS,
);

Expand All @@ -36,7 +36,21 @@ export const NotificationDetailBlockExplorerButton = ({
}, [defaultNetworks]);

const blockExplorerUrl =
defaultNetwork?.rpcPrefs?.blockExplorerUrl ?? nativeBlockExplorerUrl;
defaultNetwork?.rpcPrefs?.blockExplorerUrl ?? blockExplorerConfig?.url;

const getBlockExplorerButtonText = () => {
if (defaultNetwork?.rpcPrefs?.blockExplorerUrl) {
return t('notificationItemCheckBlockExplorer');
}
if (blockExplorerConfig?.name) {
return t('notificationTransactionSuccessView', [
blockExplorerConfig.name,
]);
}
return t('notificationItemCheckBlockExplorer');
};

const blockExplorerButtonText = getBlockExplorerButtonText();

if (!blockExplorerUrl) {
return null;
Expand All @@ -46,7 +60,7 @@ export const NotificationDetailBlockExplorerButton = ({
<NotificationDetailButton
notification={notification}
variant={ButtonVariant.Secondary}
text={t('notificationItemCheckBlockExplorer') || ''}
text={blockExplorerButtonText}
href={`${blockExplorerUrl}/tx/${txHash}`}
id={id}
isExternal={true}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { CHAIN_IDS } from '../../../../shared/constants/network';

/**
* Configuration for a block explorer.
*
* @property {string} url - The URL of the block explorer.
* @property {string} name - The name of the block explorer.
*/
export type BlockExplorerConfig = {
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
url: string;
name: string;
};

/**
* Map of all supported block explorers for notifications.
*/
export const SUPPORTED_NOTIFICATION_BLOCK_EXPLORERS = {
// ETHEREUM
[CHAIN_IDS.MAINNET]: {
url: 'https://etherscan.io',
name: 'Etherscan',
},
// OPTIMISM
[CHAIN_IDS.OPTIMISM]: {
url: 'https://optimistic.etherscan.io',
name: 'Optimistic Etherscan',
},
// BSC
[CHAIN_IDS.BSC]: {
url: 'https://bscscan.com',
name: 'BscScan',
},
// POLYGON
[CHAIN_IDS.POLYGON]: {
url: 'https://polygonscan.com',
name: 'PolygonScan',
},
// ARBITRUM
[CHAIN_IDS.ARBITRUM]: {
url: 'https://arbiscan.io',
name: 'Arbiscan',
},
// AVALANCHE
[CHAIN_IDS.AVALANCHE]: {
url: 'https://snowtrace.io',
name: 'Snowtrace',
},
// LINEA
[CHAIN_IDS.LINEA_MAINNET]: {
url: 'https://lineascan.build',
name: 'LineaScan',
},
} as const;
30 changes: 18 additions & 12 deletions ui/helpers/utils/notification.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
CHAIN_IDS,
CHAIN_ID_TO_CURRENCY_SYMBOL_MAP,
CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP,
ETHERSCAN_SUPPORTED_NETWORKS,
NETWORK_TO_NAME_MAP,
FEATURED_RPCS,
MAINNET_RPC_URL,
Expand All @@ -16,17 +15,30 @@ import {
LINEA_MAINNET_RPC_URL,
LOCALHOST_RPC_URL,
} from '../../../shared/constants/network';
import { SUPPORTED_NOTIFICATION_BLOCK_EXPLORERS } from '../constants/metamask-notifications/metamask-notifications';
import { calcTokenAmount } from '../../../shared/lib/transactions-controller-utils';
import type {
OnChainRawNotification,
OnChainRawNotificationsWithNetworkFields,
} from '../../../app/scripts/controllers/metamask-notifications/types/on-chain-notification/on-chain-notification';
import type { BlockExplorerConfig } from '../constants/metamask-notifications/metamask-notifications';
import {
hexWEIToDecGWEI,
hexWEIToDecETH,
decimalToHex,
} from '../../../shared/modules/conversion.utils';

/**
* Type guard to ensure a key is present in an object.
*
* @param object - The object to check.
* @param key - The key to check for.
* @returns True if the key is present, false otherwise.
*/
function isKey<T extends object>(object: T, key: PropertyKey): key is keyof T {
return key in object;
}

/**
* Checks if 2 date objects are on the same day
*
Expand Down Expand Up @@ -301,7 +313,7 @@ export function getNetworkDetailsByChainId(chainId?: keyof typeof CHAIN_IDS): {
nativeCurrencySymbol: string;
nativeCurrencyLogo: string;
nativeCurrencyAddress: string;
nativeBlockExplorerUrl?: string;
blockExplorerConfig?: BlockExplorerConfig;
} {
const fullNativeCurrencyName =
NETWORK_TO_NAME_MAP[chainId as keyof typeof NETWORK_TO_NAME_MAP] ?? '';
Expand All @@ -315,22 +327,16 @@ export function getNetworkDetailsByChainId(chainId?: keyof typeof CHAIN_IDS): {
chainId as keyof typeof CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP
];
const nativeCurrencyAddress = '0x0000000000000000000000000000000000000000';

const blockExplorerConfig =
ETHERSCAN_SUPPORTED_NETWORKS[
chainId as keyof typeof ETHERSCAN_SUPPORTED_NETWORKS
];
let nativeBlockExplorerUrl;
if (blockExplorerConfig) {
nativeBlockExplorerUrl = `https://www.${blockExplorerConfig.domain}`;
}

chainId && isKey(SUPPORTED_NOTIFICATION_BLOCK_EXPLORERS, chainId)
? SUPPORTED_NOTIFICATION_BLOCK_EXPLORERS[chainId]
: undefined;
return {
nativeCurrencyName,
nativeCurrencySymbol,
nativeCurrencyLogo,
nativeCurrencyAddress,
nativeBlockExplorerUrl,
blockExplorerConfig,
};
}

Expand Down