Skip to content

Commit

Permalink
chore: refactor out CHAIN_SPEC_URL constant
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Mar 19, 2024
1 parent 8893286 commit dd35f12
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
23 changes: 16 additions & 7 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ import {
} from '../../shared/constants/swaps';
import {
CHAIN_IDS,
CHAIN_SPEC_URL,
NETWORK_TYPES,
TEST_NETWORK_TICKER_MAP,
NetworkStatus,
Expand Down Expand Up @@ -201,7 +202,10 @@ import {
} from '../../shared/constants/metametrics';
import { LOG_EVENT } from '../../shared/constants/logs';

import { getStorageItem, setStorageItem } from '../../shared/lib/storage-helpers';
import {
getStorageItem,
setStorageItem,
} from '../../shared/lib/storage-helpers';
import {
getTokenIdParam,
fetchTokenBalance,
Expand Down Expand Up @@ -5410,15 +5414,20 @@ export default class MetamaskController extends EventEmitter {
}

/**
* The chain list is fetched live at runtime, falling back to a cache.
* This preseeds the cache at startup with a static list provided at build.
*/
* The chain list is fetched live at runtime, falling back to a cache.
* This preseeds the cache at startup with a static list provided at build.
*/
async initializeChainlist() {
const cacheKey = 'cachedFetch:https://chainid.network/chains.json';
if (await getStorageItem(cacheKey)) {
const cacheKey = `cachedFetch:${CHAIN_SPEC_URL}`;
const { cachedResponse } = (await getStorageItem(cacheKey)) || {};
if (cachedResponse) {
return;
}
await setStorage(cacheKey, rawChainData());
await setStorageItem(cacheKey, {
cachedResponse: rawChainData(),
// Cached value is immediately invalidated
cachedTime: 0,
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions shared/constants/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const NETWORK_NAMES = {
HOMESTEAD: 'homestead',
};

export const CHAIN_SPEC_URL = 'https://chainid.network/chains.json';
/**
* An object containing all of the chain ids for networks both built in and
* those that we have added custom code to support our feature set.
Expand Down
7 changes: 5 additions & 2 deletions ui/hooks/useIsOriginalNativeTokenSymbol.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import fetchWithCache from '../../shared/lib/fetch-with-cache';
import { CHAIN_ID_TO_CURRENCY_SYMBOL_MAP } from '../../shared/constants/network';
import {
CHAIN_ID_TO_CURRENCY_SYMBOL_MAP,
CHAIN_SPEC_URL,
} from '../../shared/constants/network';
import { DAY } from '../../shared/constants/time';
import { useSafeChainsListValidationSelector } from '../selectors';

Expand All @@ -25,7 +28,7 @@ export function useIsOriginalNativeTokenSymbol(chainId, ticker, type) {
}

const safeChainsList = await fetchWithCache({
url: 'https://chainid.network/chains.json',
url: CHAIN_SPEC_URL,
allowStale: true,
cacheOptions: { cacheRefreshTime: DAY },
functionName: 'getSafeChainsList',
Expand Down
3 changes: 2 additions & 1 deletion ui/pages/confirmations/confirmation/confirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import log from 'loglevel';
///: BEGIN:ONLY_INCLUDE_IF(snaps)
import { ApprovalType } from '@metamask/controller-utils';
///: END:ONLY_INCLUDE_IF
import { CHAIN_SPEC_URL } from '../../../../shared/constants/network';
import fetchWithCache from '../../../../shared/lib/fetch-with-cache';
import Box from '../../../components/ui/box';
import MetaMaskTemplateRenderer from '../../../components/app/metamask-template-renderer';
Expand Down Expand Up @@ -373,7 +374,7 @@ export default function ConfirmationPage({
try {
if (useSafeChainsListValidation) {
const response = await fetchWithCache({
url: 'https://chainid.network/chains.json',
url: CHAIN_SPEC_URL,
allowStale: true,
cacheOptions: { cacheRefreshTime: DAY },
functionName: 'getSafeChainsList',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {
BUILT_IN_NETWORKS,
CHAIN_IDS,
CHAIN_SPEC_URL,
FEATURED_RPCS,
infuraProjectId,
} from '../../../../../shared/constants/network';
Expand Down Expand Up @@ -133,7 +134,7 @@ const NetworksForm = ({
async function fetchChainList() {
try {
const chainList = await fetchWithCache({
url: 'https://chainid.network/chains.json',
url: CHAIN_SPEC_URL,
functionName: 'getSafeChainsList',
allowStale: true,
});
Expand Down

0 comments on commit dd35f12

Please sign in to comment.