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 version 43 #12340

Merged
merged 32 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1970754
multichain currency rate polling
bergeron Nov 12, 2024
e23a19c
remove unused places
bergeron Nov 12, 2024
9a2342c
lint
bergeron Nov 12, 2024
910395e
add unit test
bergeron Nov 12, 2024
1492b4c
update unit tests
bergeron Nov 12, 2024
ae15f45
update unit test
bergeron Nov 12, 2024
3b6c675
remove other network updates
bergeron Nov 12, 2024
2e9dff3
remove additional call
bergeron Nov 12, 2024
9a5935e
bump asset controller, and poll tokens across chains
bergeron Nov 13, 2024
14281c1
add platform argument to token detection controller
bergeron Nov 13, 2024
4a77428
Merge branch 'main' into brian/multichain-token-rates
bergeron Nov 14, 2024
4095e64
use feature flag
bergeron Nov 14, 2024
df6273f
use feature flag in token page refresh
bergeron Nov 15, 2024
eaeb2c7
fix unit test
bergeron Nov 15, 2024
662797c
Merge branch 'main' into brian/multichain-token-rates
bergeron Nov 15, 2024
1d75acc
Merge branch 'main' into brian/multichain-token-rates
bergeron Nov 19, 2024
ab2a484
upgrade assets controllers to 43
bergeron Nov 19, 2024
006ed8f
fix feature flag usage
bergeron Nov 19, 2024
e44fee4
...
bergeron Nov 19, 2024
21b6f62
Merge branch 'brian/multichain-token-rates' of github.com:MetaMask/me…
bergeron Nov 19, 2024
ec6497f
add unit test
bergeron Nov 19, 2024
1e65cb1
rm commented out code
bergeron Nov 19, 2024
b231245
fix and add unit tests
bergeron Nov 19, 2024
7fb639d
lint
bergeron Nov 19, 2024
00c5ec9
fix unit test
bergeron Nov 19, 2024
319642f
Merge branch 'main' into brian/multichain-token-rates
bergeron Nov 19, 2024
3cf9ffa
yarn deduplicate
bergeron Nov 19, 2024
41d64f0
Merge branch 'brian/multichain-token-rates' of github.com:MetaMask/me…
bergeron Nov 19, 2024
76159ba
formatting
bergeron Nov 20, 2024
f6f7646
Merge branch 'main' of github.com:MetaMask/metamask-mobile into brian…
bergeron Nov 20, 2024
edeafdf
Merge branch 'main' of github.com:MetaMask/metamask-mobile into brian…
bergeron Nov 20, 2024
09bc62b
Merge branch 'main' into brian/assets-controllers-43
bergeron Nov 20, 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
7 changes: 6 additions & 1 deletion app/components/UI/Tokens/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
import ButtonBase from '../../../component-library/components/Buttons/Button/foundation/ButtonBase';
import { selectNetworkName } from '../../../selectors/networkInfos';
import ButtonIcon from '../../../component-library/components/Buttons/ButtonIcon';
import { Hex } from '@metamask/utils';

// this will be imported from TokenRatesController when it is exported from there
// PR: https://github.com/MetaMask/core/pull/4622
Expand Down Expand Up @@ -180,7 +181,11 @@ const Tokens: React.FC<TokensI> = ({ tokens }) => {
TokenRatesController,
} = Engine.context;
const actions = [
TokenDetectionController.detectTokens(),
TokenDetectionController.detectTokens({
chainIds: isPortfolioViewEnabled
? Object.keys(networkConfigurationsByChainId) as Hex[]
: [chainId]
}),
AccountTrackerController.refresh(),
CurrencyRateController.updateExchangeRate(nativeCurrencies),
...(isPortfolioViewEnabled
Expand Down
4 changes: 4 additions & 0 deletions app/components/hooks/AssetPolling/AssetPollingProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React, { ReactNode } from 'react';
import useCurrencyRatePolling from './useCurrencyRatePolling';
import useTokenRatesPolling from './useTokenRatesPolling';
import useTokenDetectionPolling from './useTokenDetectionPolling';
import useTokenListPolling from './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}</>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { renderHookWithProvider } from '../../../util/test/renderWithProvider';
import Engine from '../../../core/Engine';
import useTokenDetectionPolling from './useTokenDetectionPolling';

jest.mock('../../../core/Engine', () => ({
context: {
TokenDetectionController: {
startPolling: jest.fn(),
stopPollingByPollingToken: jest.fn(),
},
},
}));

describe('useTokenDetectionPolling', () => {

beforeEach(() => {
jest.resetAllMocks();
});

const selectedAddress = '0x1234567890abcdef';
const selectedChainId = '0x1' as const;

const state = {
engine: {
backgroundState: {
AccountsController: {
internalAccounts: {
selectedAccount: '1',
accounts: {
'1': {
address: selectedAddress
}
},
},
},
PreferencesController: {
useTokenDetection: true,
},
NetworkController: {
selectedNetworkClientId: 'selectedNetworkClientId',
networkConfigurationsByChainId: {
[selectedChainId]: {
chainId: selectedChainId,
rpcEndpoints: [{
networkClientId: 'selectedNetworkClientId',
}]
},
'0x89': {},
},
},
},
},
};

it('Should poll by current chain ids/address, and stop polling on dismount', async () => {

const { unmount } = renderHookWithProvider(() => useTokenDetectionPolling(), {state});

const mockedTokenDetectionController = jest.mocked(Engine.context.TokenDetectionController);

expect(mockedTokenDetectionController.startPolling).toHaveBeenCalledTimes(1);
expect(
mockedTokenDetectionController.startPolling
).toHaveBeenCalledWith({chainIds: [selectedChainId], address: selectedAddress});

expect(mockedTokenDetectionController.stopPollingByPollingToken).toHaveBeenCalledTimes(0);
unmount();
expect(mockedTokenDetectionController.stopPollingByPollingToken).toHaveBeenCalledTimes(1);

});

it('Should not poll when token detection is disabled', async () => {

renderHookWithProvider(() => useTokenDetectionPolling({chainIds: ['0x1']}), {state:{
...state,
engine: {
...state.engine,
backgroundState: {
...state.engine.backgroundState,
PreferencesController: {
...state.engine.backgroundState.PreferencesController,
useTokenDetection: false,
},
},
},
}});

const mockedTokenDetectionController = jest.mocked(Engine.context.TokenDetectionController);
expect(mockedTokenDetectionController.startPolling).toHaveBeenCalledTimes(0);
expect(mockedTokenDetectionController.stopPollingByPollingToken).toHaveBeenCalledTimes(0);
});
});
37 changes: 37 additions & 0 deletions app/components/hooks/AssetPolling/useTokenDetectionPolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useSelector } from 'react-redux';
import usePolling from '../usePolling';
import Engine from '../../../core/Engine';
import { selectChainId, selectNetworkConfigurations } from '../../../selectors/networkController';
import { Hex } from '@metamask/utils';
import { isPortfolioViewEnabled } from '../../../util/networks';
import { selectSelectedInternalAccount } from '../../../selectors/accountsController';
import { selectUseTokenDetection } from '../../../selectors/preferencesController';

const useTokenDetectionPolling = ({ chainIds }: { chainIds?: Hex[] } = {}) => {

const networkConfigurations = useSelector(selectNetworkConfigurations);
const currentChainId = useSelector(selectChainId);
const selectedAccount = useSelector(selectSelectedInternalAccount);
const useTokenDetection = useSelector(selectUseTokenDetection);

const chainIdsToPoll = isPortfolioViewEnabled
? (chainIds ?? Object.keys(networkConfigurations))
: [currentChainId];

const { TokenDetectionController } = Engine.context;

usePolling({
startPolling:
TokenDetectionController.startPolling.bind(TokenDetectionController),
stopPollingByPollingToken:
TokenDetectionController.stopPollingByPollingToken.bind(TokenDetectionController),
input: useTokenDetection ? [{
chainIds: chainIdsToPoll as Hex[],
address: selectedAccount?.address as Hex
}] : []
});

return { };
};

export default useTokenDetectionPolling;
55 changes: 55 additions & 0 deletions app/components/hooks/AssetPolling/useTokenListPolling.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { renderHookWithProvider } from '../../../util/test/renderWithProvider';
import Engine from '../../../core/Engine';
import useTokenListPolling from './useTokenListPolling';

jest.mock('../../../core/Engine', () => ({
context: {
TokenListController: {
startPolling: jest.fn(),
stopPollingByPollingToken: jest.fn(),
},
},
}));

describe('useTokenListPolling', () => {

beforeEach(() => {
jest.resetAllMocks();
});

const selectedChainId = '0x1' as const;
const state = {
engine: {
backgroundState: {
NetworkController: {
selectedNetworkClientId: 'selectedNetworkClientId',
networkConfigurationsByChainId: {
[selectedChainId]: {
chainId: selectedChainId,
rpcEndpoints: [{
networkClientId: 'selectedNetworkClientId',
}]
},
'0x89': {},
},
},
},
},
};

it('Should poll by selected chain id, and stop polling on dismount', async () => {

const { unmount } = renderHookWithProvider(() => useTokenListPolling(), {state});

const mockedTokenListController = jest.mocked(Engine.context.TokenListController);

expect(mockedTokenListController.startPolling).toHaveBeenCalledTimes(1);
expect(
mockedTokenListController.startPolling
).toHaveBeenCalledWith({chainId: selectedChainId});

expect(mockedTokenListController.stopPollingByPollingToken).toHaveBeenCalledTimes(0);
unmount();
expect(mockedTokenListController.stopPollingByPollingToken).toHaveBeenCalledTimes(1);
});
});
39 changes: 39 additions & 0 deletions app/components/hooks/AssetPolling/useTokenListPolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useSelector } from 'react-redux';
import usePolling from '../usePolling';
import Engine from '../../../core/Engine';
import { selectChainId, selectNetworkConfigurations } from '../../../selectors/networkController';
import { Hex } from '@metamask/utils';
import { isPortfolioViewEnabled } from '../../../util/networks';
import { selectERC20TokensByChain, selectTokenList } from '../../../selectors/tokenListController';

const useTokenListPolling = ({ chainIds }: { chainIds?: Hex[] } = {}) => {

// Selectors to determine polling input
const networkConfigurations = useSelector(selectNetworkConfigurations);
const currentChainId = useSelector(selectChainId);

// Selectors returning state updated by the polling
const tokenList = useSelector(selectTokenList);
const tokenListByChain = useSelector(selectERC20TokensByChain);

const chainIdsToPoll = isPortfolioViewEnabled
? (chainIds ?? Object.keys(networkConfigurations))
: [currentChainId];

const { TokenListController } = Engine.context;

usePolling({
startPolling:
TokenListController.startPolling.bind(TokenListController),
stopPollingByPollingToken:
TokenListController.stopPollingByPollingToken.bind(TokenListController),
input: chainIdsToPoll.map((chainId) => ({ chainId: chainId as Hex }))
});

return {
tokenList,
tokenListByChain,
};
};

export default useTokenListPolling;
5 changes: 1 addition & 4 deletions app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,7 @@ export class Engine {
),
platform: 'mobile',
useAccountsAPI: true,
disabled: false
}),

new NftDetectionController({
Expand Down Expand Up @@ -1908,13 +1909,9 @@ export class Engine {

startPolling() {
const {
TokenDetectionController,
TokenListController,
TransactionController,
} = this.context;

TokenListController.start();
TokenDetectionController.start();
// leaving the reference of TransactionController here, rather than importing it from utils to avoid circular dependency
TransactionController.startIncomingTransactionPolling();
}
Expand Down
2 changes: 1 addition & 1 deletion app/core/NotificationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class NotificationManager {
pollPromises.push(
...[
TokenBalancesController.poll(),
TokenDetectionController.start(),
TokenDetectionController.detectTokens({ chainIds: [transactionMeta.chainId] }),
],
);
break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"@metamask/accounts-controller": "^18.2.1",
"@metamask/address-book-controller": "^6.0.1",
"@metamask/approval-controller": "^7.1.0",
"@metamask/assets-controllers": "^42.0.0",
"@metamask/assets-controllers": "^43.1.1",
"@metamask/base-controller": "^7.0.1",
"@metamask/composable-controller": "^3.0.0",
"@metamask/controller-utils": "^11.3.0",
Expand Down
Loading
Loading