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 v44 #12344

Merged
merged 54 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 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
ef9596b
44 upgrade
bergeron Nov 20, 2024
76159ba
formatting
bergeron Nov 20, 2024
db8a847
Merge branch 'brian/assets-controllers-43' of github.com:MetaMask/met…
bergeron Nov 20, 2024
60c39e3
patch
bergeron Nov 20, 2024
2083e11
lint
bergeron Nov 20, 2024
cc0f586
fix unit test
bergeron Nov 20, 2024
3b60955
fix unit test
bergeron Nov 20, 2024
48c30d0
fix unit tests
bergeron Nov 20, 2024
96ebf57
fix unit test
bergeron Nov 20, 2024
75cefd8
remove console log
bergeron Nov 20, 2024
b50f1ca
lint
bergeron Nov 20, 2024
f1b57df
fix unit test
bergeron Nov 20, 2024
2e2b567
add unit test
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
eb7fb61
Merge branch 'brian/assets-controllers-43' into brian/asset-controlle…
bergeron Nov 21, 2024
b43238a
add unit test
bergeron Nov 21, 2024
c2015ee
Merge branch 'brian/asset-controller-44' of github.com:MetaMask/metam…
bergeron Nov 21, 2024
9a768ad
Merge branch 'main' of github.com:MetaMask/metamask-mobile into brian…
bergeron Nov 21, 2024
af49729
patch
bergeron Nov 21, 2024
6db4a2f
patch
bergeron Nov 21, 2024
5694cae
Merge branch 'main' into brian/asset-controller-44
salimtb Nov 25, 2024
794ad13
fix: fix after merge conflicts
salimtb Nov 25, 2024
89cab6c
Merge branch 'main' into brian/asset-controller-44
salimtb Nov 26, 2024
4fc915d
Merge branch 'main' into brian/asset-controller-44
salimtb Nov 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ const mockInitialState: DeepPartial<RootState> = {
},
},
TokenBalancesController: {
contractBalances: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': '0x5',
},
tokenBalances: { },
},
AccountsController: MOCK_ACCOUNTS_CONTROLLER_STATE,
},
Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/AccountInfoCard/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const mockInitialState: DeepPartial<RootState> = {
}),
},
TokenBalancesController: {
contractBalances: {},
tokenBalances: {},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { fireEvent } from '@testing-library/react-native';
import { BN } from 'ethereumjs-util';
import renderWithProvider from '../../../../../util/test/renderWithProvider';
import { backgroundState } from '../../../../../util/test/initial-root-state';
import AppConstants from '../../../../../../app/core/AppConstants';
Expand Down Expand Up @@ -76,11 +75,7 @@ const initialState = {
},
},
TokenBalancesController: {
contractBalances: {
'0x00': new BN(2),
'0x01': new BN(2),
'0x02': new BN(0),
},
tokenBalances: { },
salimtb marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
Expand Down
44 changes: 37 additions & 7 deletions app/components/UI/Tokens/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ jest.mock('../../../core/Engine', () => ({
},
}));

const selectedAddress = '0x123';

const initialState = {
engine: {
backgroundState: {
...backgroundState,
AccountsController: {
internalAccounts: {
selectedAccount: '1',
accounts: {
'1': {
address: selectedAddress,
},
},
},
},
TokensController: {
tokens: [
{
Expand Down Expand Up @@ -109,11 +121,15 @@ const initialState = {
},
},
TokenBalancesController: {
contractBalances: {
'0x00': new BN(2),
'0x01': new BN(2),
'0x02': new BN(0),
},
tokenBalances:{
[selectedAddress]: {
'0x1': {
'0x00': new BN(2),
'0x01': new BN(2),
'0x02': new BN(0),
}
}
}
},
},
},
Expand Down Expand Up @@ -263,9 +279,23 @@ describe('Tokens', () => {
},
},
},
AccountsController: {
internalAccounts: {
selectedAccount: '1',
accounts: {
'1': {
address: selectedAddress,
},
},
},
},
TokenBalancesController: {
contractBalances: {
'0x02': new BN(1),
tokenBalances: {
[selectedAddress]: {
'0x1': {
'0x02': new BN(1),
},
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion app/components/Views/DetectedTokens/components/Token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const Token = ({ token, selected, toggleSelected }: Props) => {
(tokenExchangeRates as Record<Hex, MarketDataDetails>)?.[address as Hex] ??
null;
const tokenBalance = renderFromTokenMinimalUnit(
tokenBalances[address],
tokenBalances[address as Hex],
decimals,
);
const tokenBalanceWithSymbol = `${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const mockInitialState: DeepPartial<RootState> = {
},
},
TokenBalancesController: {
contractBalances: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': '0x5',
},
tokenBalances: { },
salimtb marked this conversation as resolved.
Show resolved Hide resolved
},
PreferencesController: {
selectedAddress: '0xe64dD0AB5ad7e8C5F2bf6Ce75C34e187af8b920A',
Expand Down
2 changes: 1 addition & 1 deletion app/components/Views/confirmations/Send/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const initialState: DeepPartial<RootState> = {
addressBook: {},
},
TokenBalancesController: {
contractBalances: {},
tokenBalances: {},
},
TokenListController: {
tokenList: { '0x1': {} },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ const initialState: DeepPartial<RootState> = {
},
},
TokenBalancesController: {
contractBalances: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': '0x5',
},
tokenBalances: { },
salimtb marked this conversation as resolved.
Show resolved Hide resolved
},
CurrencyRateController: {
currentCurrency: 'usd',
Expand Down
27 changes: 27 additions & 0 deletions app/components/hooks/AssetPolling/AssetPollingProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { render } from '@testing-library/react-native';

import { AssetPollingProvider } from './AssetPollingProvider';

jest.mock('./useCurrencyRatePolling', () => jest.fn());
jest.mock('./useTokenRatesPolling', () => jest.fn());
jest.mock('./useTokenDetectionPolling', () => jest.fn());
jest.mock('./useTokenListPolling', () => jest.fn());
jest.mock('./useTokenBalancesPolling', () => jest.fn());

describe('AssetPollingProvider', () => {
it('should call all polling hooks', () => {

Check warning on line 14 in app/components/hooks/AssetPolling/AssetPollingProvider.test.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint)

Trailing spaces not allowed
render(
<AssetPollingProvider>
<div></div>
</AssetPollingProvider>
);

expect(jest.requireMock('./useCurrencyRatePolling')).toHaveBeenCalled();
expect(jest.requireMock('./useTokenRatesPolling')).toHaveBeenCalled();
expect(jest.requireMock('./useTokenDetectionPolling')).toHaveBeenCalled();
expect(jest.requireMock('./useTokenListPolling')).toHaveBeenCalled();
expect(jest.requireMock('./useTokenBalancesPolling')).toHaveBeenCalled();
});
});
2 changes: 2 additions & 0 deletions app/components/hooks/AssetPolling/AssetPollingProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import useCurrencyRatePolling from './useCurrencyRatePolling';
import useTokenRatesPolling from './useTokenRatesPolling';
import useTokenDetectionPolling from './useTokenDetectionPolling';
import useTokenListPolling from './useTokenListPolling';
import useTokenBalancesPolling from './useTokenBalancesPolling';

// This provider is a step towards making controller polling fully UI based.
// Eventually, individual UI components will call the use*Polling hooks to
Expand All @@ -12,6 +13,7 @@ export const AssetPollingProvider = ({ children }: { children: ReactNode }) => {
useTokenRatesPolling();
useTokenDetectionPolling();
useTokenListPolling();
useTokenBalancesPolling();

return <>{children}</>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { renderHookWithProvider } from '../../../util/test/renderWithProvider';
import Engine from '../../../core/Engine';
import useTokenBalancesPolling from './useTokenBalancesPolling';

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

describe('useTokenBalancesPolling', () => {

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

const selectedChainId = '0x1' as const;
const state = {
engine: {
backgroundState: {
TokenBalancesController: {
tokenBalances: {},
},
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(() => useTokenBalancesPolling(), {state});

const mockedTokenBalancesController = jest.mocked(Engine.context.TokenBalancesController);

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

expect(mockedTokenBalancesController.stopPollingByPollingToken).toHaveBeenCalledTimes(0);
unmount();
expect(mockedTokenBalancesController.stopPollingByPollingToken).toHaveBeenCalledTimes(1);
});
});
37 changes: 37 additions & 0 deletions app/components/hooks/AssetPolling/useTokenBalancesPolling.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 { selectAllTokenBalances } from '../../../selectors/tokenBalancesController';

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

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

// Selectors returning state updated by the polling
const tokenBalances = useSelector(selectAllTokenBalances);

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

const { TokenBalancesController } = Engine.context;

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

return {
tokenBalances
};
};

export default useTokenBalancesPolling;
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ const mockInitialState = {
},
},
TokenBalancesController: {
contractBalances: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': '0x5',
tokenBalances: {
[MOCK_ADDRESS_1]: {
'0x1': {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': '0x5',
},
},
},
},
PreferencesController: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,31 @@ import { BN } from 'ethereumjs-util';
import { cloneDeep } from 'lodash';
import { backgroundState } from '../../../util/test/initial-root-state';

const accountAddress = '0x123';
const chainId = '0x1';

// initial state for the test store
const mockInitialState = {
engine: {
backgroundState: {
...backgroundState,
AccountsController: {
internalAccounts: {
selectedAccount: '1',
accounts: {
'1': {
address: accountAddress,
},
},
},
},
TokenBalancesController: {
contractBalances: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': new BN(0x2a),
tokenBalances: {
[accountAddress]: {
[chainId]: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': new BN(0x2a),
}
}
},
},
},
Expand All @@ -35,10 +52,20 @@ const testBalancesReducer = (state: any, action: any) => {
...state.engine.backgroundState,
TokenBalancesController: {
...state.engine.backgroundState.TokenBalancesController,
contractBalances: {
tokenBalances: {
...state.engine.backgroundState.TokenBalancesController
.contractBalances,
.tokenBalances,
...action.value,
[accountAddress]: {
...state.engine.backgroundState.TokenBalancesController
.tokenBalances[accountAddress],
...action.value[accountAddress],
[chainId]: {
...state.engine.backgroundState.TokenBalancesController
.tokenBalances[accountAddress][chainId],
...action.value[accountAddress][chainId],
}
},
},
},
},
Expand Down Expand Up @@ -104,7 +131,11 @@ describe('useTokenBalancesController()', () => {
testStore.dispatch({
type: 'add-balances',
value: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b96': new BN(0x539),
[accountAddress]: {
[chainId]: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b96': new BN(0x539),
}
}
},
});
});
Expand All @@ -122,7 +153,11 @@ describe('useTokenBalancesController()', () => {
testStore.dispatch({
type: 'add-balances',
value: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': new BN(0x2a),
[accountAddress]: {
[chainId]: {
'0x326836cc6cd09B5aa59B81A7F72F25FcC0136b95': new BN(0x2a),
}
}
},
});
});
Expand Down
Loading
Loading