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

feat: Upgrade assets controllers to 43 with multichain polling for token lists + detection #28447

Merged
merged 27 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
diff --git a/dist/assetsUtil.cjs b/dist/assetsUtil.cjs
index e90a1b6767bc8ac54b7a4d580035cf5db6861dca..a5e0f03d2541b4e3540431ef2e6e4b60fb7ae9fe 100644
index c2e83cf6caee19152aa164f1333cfef7b681e900..590b6de6e9d20ca402b82ac56b0929ab8c16c932 100644
--- a/dist/assetsUtil.cjs
+++ b/dist/assetsUtil.cjs
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
+function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }
exports.fetchTokenContractExchangeRates = exports.reduceInBatchesSerially = exports.divideIntoBatches = exports.ethersBigNumberToBN = exports.addUrlProtocolPrefix = exports.getFormattedIpfsUrl = exports.getIpfsCIDv1AndPath = exports.removeIpfsProtocolPrefix = exports.isTokenListSupportedForNetwork = exports.isTokenDetectionSupportedForNetwork = exports.SupportedTokenDetectionNetworks = exports.formatIconUrlWithProxy = exports.formatAggregatorNames = exports.hasNewCollectionFields = exports.compareNftMetadata = exports.TOKEN_PRICES_BATCH_SIZE = void 0;
exports.fetchTokenContractExchangeRates = exports.reduceInBatchesSerially = exports.divideIntoBatches = exports.ethersBigNumberToBN = exports.addUrlProtocolPrefix = exports.getFormattedIpfsUrl = exports.getIpfsCIDv1AndPath = exports.removeIpfsProtocolPrefix = exports.isTokenListSupportedForNetwork = exports.isTokenDetectionSupportedForNetwork = exports.SupportedStakedBalanceNetworks = exports.SupportedTokenDetectionNetworks = exports.formatIconUrlWithProxy = exports.formatAggregatorNames = exports.hasNewCollectionFields = exports.compareNftMetadata = exports.TOKEN_PRICES_BATCH_SIZE = void 0;
const controller_utils_1 = require("@metamask/controller-utils");
const utils_1 = require("@metamask/utils");
@@ -221,7 +222,7 @@ async function getIpfsCIDv1AndPath(ipfsUrl) {
@@ -233,7 +234,7 @@ async function getIpfsCIDv1AndPath(ipfsUrl) {
const index = url.indexOf('/');
const cid = index !== -1 ? url.substring(0, index) : url;
const path = index !== -1 ? url.substring(index) : undefined;
Expand Down
53 changes: 35 additions & 18 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2600,25 +2600,25 @@ export default class MetamaskController extends EventEmitter {
triggerNetworkrequests() {
this.accountTrackerController.start();
this.txController.startIncomingTransactionPolling();
this.tokenDetectionController.enable();
// this.tokenDetectionController.enable();

const preferencesControllerState = this.preferencesController.state;
// const preferencesControllerState = this.preferencesController.state;

if (this.#isTokenListPollingRequired(preferencesControllerState)) {
this.tokenListController.start();
}
// if (this.#isTokenListPollingRequired(preferencesControllerState)) {
// this.tokenListController.start();
// }
}

stopNetworkRequests() {
this.accountTrackerController.stop();
this.txController.stopIncomingTransactionPolling();
this.tokenDetectionController.disable();
// this.tokenDetectionController.disable();

const preferencesControllerState = this.preferencesController.state;
// const preferencesControllerState = this.preferencesController.state;

if (this.#isTokenListPollingRequired(preferencesControllerState)) {
this.tokenListController.stop();
}
// if (this.#isTokenListPollingRequired(preferencesControllerState)) {
// this.tokenListController.stop();
// }
}

resetStates(resetMethods) {
Expand Down Expand Up @@ -3238,6 +3238,7 @@ export default class MetamaskController extends EventEmitter {
currencyRateController,
tokenDetectionController,
ensController,
tokenListController,
gasFeeController,
metaMetricsController,
networkController,
Expand Down Expand Up @@ -4040,6 +4041,19 @@ export default class MetamaskController extends EventEmitter {
tokenRatesController,
),

tokenDetectionStartPolling: tokenDetectionController.startPolling.bind(
tokenDetectionController,
),
tokenDetectionStopPollingByPollingToken:
tokenDetectionController.stopPollingByPollingToken.bind(
tokenDetectionController,
),

tokenListStartPolling:
tokenListController.startPolling.bind(tokenListController),
tokenListStopPollingByPollingToken:
tokenListController.stopPollingByPollingToken.bind(tokenListController),

// GasFeeController
gasFeeStartPollingByNetworkClientId:
gasFeeController.startPollingByNetworkClientId.bind(gasFeeController),
Expand Down Expand Up @@ -4403,6 +4417,7 @@ export default class MetamaskController extends EventEmitter {
if (balance === '0x0') {
// This account has no balance, so check for tokens
await this.tokenDetectionController.detectTokens({
chainIds: [chainId],
selectedAddress: address,
});

Expand Down Expand Up @@ -6669,6 +6684,8 @@ export default class MetamaskController extends EventEmitter {
this.gasFeeController.stopAllPolling();
this.currencyRateController.stopAllPolling();
this.tokenRatesController.stopAllPolling();
this.tokenDetectionController.stopAllPolling();
this.tokenListController.stopAllPolling();
this.appStateController.clearPollingTokens();
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -7205,14 +7222,14 @@ export default class MetamaskController extends EventEmitter {

this.tokenListController.updatePreventPollingOnNetworkRestart(!newEnabled);
Copy link
Contributor Author

@bergeron bergeron Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never understood the purpose of preventPollingOnNetworkRestart, so I don't know whether it still makes sense. When it's set to true, all it seems to do is reset the state when switching chains. Keeping it around for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this same thing and had similar thoughts. Sounds good


if (newEnabled) {
log.debug('Started token list controller polling');
this.tokenListController.start();
} else {
log.debug('Stopped token list controller polling');
this.tokenListController.clearingTokenListData();
this.tokenListController.stop();
}
// if (newEnabled) {
// log.debug('Started token list controller polling');
// this.tokenListController.start();
// } else {
// log.debug('Stopped token list controller polling');
// this.tokenListController.clearingTokenListData();
// this.tokenListController.stop();
// }
}

#isTokenListPollingRequired(preferencesControllerState) {
Expand Down
109 changes: 0 additions & 109 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2301,115 +2301,6 @@ describe('MetaMaskController', () => {
});
});

describe('token list controller', () => {
it('stops polling if petnames, simulations, and token detection disabled', async () => {
expect(TokenListController.prototype.stop).not.toHaveBeenCalled();

expect(
TokenListController.prototype.clearingTokenListData,
).not.toHaveBeenCalled();

await simulatePreferencesChange({
useTransactionSimulations: false,
useTokenDetection: false,
preferences: {
petnamesEnabled: false,
},
});

expect(TokenListController.prototype.stop).toHaveBeenCalledTimes(1);

expect(
TokenListController.prototype.clearingTokenListData,
).toHaveBeenCalledTimes(1);
});

it.each([
[
'petnames',
{
preferences: { petnamesEnabled: false },
useTokenDetection: true,
useTransactionSimulations: true,
},
],
[
'simulations',
{
preferences: { petnamesEnabled: true },
useTokenDetection: true,
useTransactionSimulations: false,
},
],
[
'token detection',
{
preferences: { petnamesEnabled: true },
useTokenDetection: false,
useTransactionSimulations: true,
},
],
])(
'does not stop polling if only %s disabled',
async (_, preferences) => {
expect(TokenListController.prototype.stop).not.toHaveBeenCalled();

expect(
TokenListController.prototype.clearingTokenListData,
).not.toHaveBeenCalled();

await simulatePreferencesChange(preferences);

expect(TokenListController.prototype.stop).not.toHaveBeenCalled();

expect(
TokenListController.prototype.clearingTokenListData,
).not.toHaveBeenCalled();
},
);

it.each([
[
'petnames',
{
preferences: { petnamesEnabled: true },
useTokenDetection: false,
useTransactionSimulations: false,
},
],
[
'simulations',
{
preferences: { petnamesEnabled: false },
useTokenDetection: false,
useTransactionSimulations: true,
},
],
[
'token detection',
{
preferences: { petnamesEnabled: false },
useTokenDetection: true,
useTransactionSimulations: false,
},
],
])('starts polling if only %s enabled', async (_, preferences) => {
expect(TokenListController.prototype.start).not.toHaveBeenCalled();

await simulatePreferencesChange({
useTransactionSimulations: false,
useTokenDetection: false,
preferences: {
petnamesEnabled: false,
},
});

await simulatePreferencesChange(preferences);

expect(TokenListController.prototype.start).toHaveBeenCalledTimes(1);
});
});

describe('MultichainRatesController start/stop', () => {
const mockEvmAccount = createMockInternalAccount();
const mockNonEvmAccount = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
"@metamask/address-book-controller": "^6.0.0",
"@metamask/announcement-controller": "^7.0.0",
"@metamask/approval-controller": "^7.0.0",
"@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A42.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-42.0.0-57b3d695bb.patch",
"@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A43.1.1#~/.yarn/patches/@metamask-assets-controllers-npm-43.1.1-c223d56176.patch",
"@metamask/base-controller": "^7.0.0",
"@metamask/bitcoin-wallet-snap": "^0.8.2",
"@metamask/browser-passworder": "^4.3.0",
Expand Down
4 changes: 4 additions & 0 deletions ui/contexts/assetPolling.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React, { ReactNode } from 'react';
import useCurrencyRatePolling from '../hooks/useCurrencyRatePolling';
import useTokenRatesPolling from '../hooks/useTokenRatesPolling';
import useTokenDetectionPolling from '../hooks/useTokenDetectionPolling';
import useTokenListPolling from '../hooks/useTokenListPolling';

// This provider is a step towards making controller polling fully UI based.
// Eventually, individual UI components will call the use*Polling hooks to
// poll and return particular data. This polls globally in the meantime.
export const AssetPollingProvider = ({ children }: { children: ReactNode }) => {
useCurrencyRatePolling();
useTokenRatesPolling();
useTokenDetectionPolling();
useTokenListPolling();

return <>{children}</>;
};
27 changes: 27 additions & 0 deletions ui/hooks/useTokenDetectionPolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useSelector } from 'react-redux';
import {
getNetworkConfigurationsByChainId,
getUseTokenDetection,
} from '../selectors';
import {
tokenDetectionStartPolling,
tokenDetectionStopPollingByPollingToken,
} from '../store/actions';
import useMultiPolling from './useMultiPolling';

const useTokenDetectionPolling = () => {
const useTokenDetection = useSelector(getUseTokenDetection);
const networkConfigurations = useSelector(getNetworkConfigurationsByChainId);
const chainIds = Object.keys(networkConfigurations);

useMultiPolling({
startPolling: tokenDetectionStartPolling,
stopPollingByPollingToken: tokenDetectionStopPollingByPollingToken,
input: useTokenDetection ? [chainIds] : [],
});

return {
};
};

export default useTokenDetectionPolling;
33 changes: 33 additions & 0 deletions ui/hooks/useTokenListPolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useSelector } from 'react-redux';
import {
getNetworkConfigurationsByChainId,
getPetnamesEnabled,
getUseTokenDetection,
getUseTransactionSimulations,
} from '../selectors';
import {
tokenListStartPolling,
tokenListStopPollingByPollingToken,
} from '../store/actions';
import useMultiPolling from './useMultiPolling';

const useTokenListPolling = () => {
const networkConfigurations = useSelector(getNetworkConfigurationsByChainId);
const useTokenDetection = useSelector(getUseTokenDetection);
const useTransactionSimulations = useSelector(getUseTransactionSimulations);
const petnamesEnabled = useSelector(getPetnamesEnabled);

const enabled =
useTokenDetection || petnamesEnabled || useTransactionSimulations;

useMultiPolling({
startPolling: tokenListStartPolling,
stopPollingByPollingToken: tokenListStopPollingByPollingToken,
input: enabled ? Object.keys(networkConfigurations) : [],
});

return {
};
};

export default useTokenListPolling;
4 changes: 4 additions & 0 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,10 @@ export function getPetnamesEnabled(state) {
return petnamesEnabled;
}

export function getUseTransactionSimulations(state) {
return Boolean(state.metamask.useTransactionSimulations);
}

export function getRedesignedConfirmationsEnabled(state) {
const { redesignedConfirmationsEnabled } = getPreferences(state);
return redesignedConfirmationsEnabled;
Expand Down
Loading