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

Chore: upgrade assets controllers v33.0.0 #11755

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions app/components/UI/CollectibleContracts/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('CollectibleContracts', () => {
AccountsController: MOCK_ACCOUNTS_CONTROLLER_STATE,
NftController: {
allNfts: {
[MOCK_ADDRESS]: {
[MOCK_ADDRESS.toLowerCase()]: {
'0x1': [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NftController now gets the selected address from AccountsController:getAccount which is not a checksum address

{
address: '0x72b1FDb6443338A158DeC2FbF411B71aeB157A42',
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('CollectibleContracts', () => {
},
},
allNftContracts: {
[MOCK_ADDRESS]: {
[MOCK_ADDRESS.toLowerCase()]: {
'0x1': [
{
address: '0x72b1FDb6443338A158DeC2FbF411B71aeB157A42',
Expand Down
145 changes: 81 additions & 64 deletions app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
TokenListState,
TokenListStateChange,
TokenRatesController,
TokenRatesState,
TokenRatesControllerState,
TokensController,
TokensControllerState,
CodefiTokenPricesServiceV2,
Expand Down Expand Up @@ -345,7 +345,7 @@ export interface EngineState {
PreferencesController: PreferencesState;
PhishingController: PhishingControllerState;
TokenBalancesController: TokenBalancesControllerState;
TokenRatesController: TokenRatesState;
TokenRatesController: TokenRatesControllerState;
TransactionController: TransactionControllerState;
SmartTransactionsController: SmartTransactionsControllerState;
SwapsController: SwapsState;
Expand Down Expand Up @@ -515,7 +515,7 @@ class Engine {
showApprovalRequest: () => undefined,
typesExcludedFromRateLimiting: [
ApprovalType.Transaction,
ApprovalType.WatchAsset
ApprovalType.WatchAsset,
],
});

Expand Down Expand Up @@ -569,6 +569,34 @@ class Engine {
getNetworkClientById:
networkController.getNetworkClientById.bind(networkController),
});

const accountsControllerMessenger: AccountsControllerMessenger =
this.controllerMessenger.getRestricted({
name: 'AccountsController',
allowedEvents: [
'SnapController:stateChange',
'KeyringController:accountRemoved',
'KeyringController:stateChange',
],
allowedActions: [
'KeyringController:getAccounts',
'KeyringController:getKeyringsByType',
'KeyringController:getKeyringForAccount',
],
});

const defaultAccountsControllerState: AccountsControllerState = {
internalAccounts: {
accounts: {},
selectedAccount: '',
},
};

const accountsController = new AccountsController({
messenger: accountsControllerMessenger,
state: initialState.AccountsController ?? defaultAccountsControllerState,
});

const nftController = new NftController({
chainId: networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
Expand All @@ -580,10 +608,13 @@ class Engine {
allowedActions: [
`${approvalController.name}:addRequest`,
`${networkController.name}:getNetworkClientById`,
'AccountsController:getAccount',
'AccountsController:getSelectedAccount',
],
allowedEvents: [
'PreferencesController:stateChange',
'NetworkController:networkDidChange',
'AccountsController:selectedEvmAccountChange',
],
}),

Expand Down Expand Up @@ -619,32 +650,7 @@ class Engine {
}),
state: initialState.LoggingController,
});
const accountsControllerMessenger: AccountsControllerMessenger =
this.controllerMessenger.getRestricted({
name: 'AccountsController',
allowedEvents: [
'SnapController:stateChange',
'KeyringController:accountRemoved',
'KeyringController:stateChange',
],
allowedActions: [
'KeyringController:getAccounts',
'KeyringController:getKeyringsByType',
'KeyringController:getKeyringForAccount',
],
});

const defaultAccountsControllerState: AccountsControllerState = {
internalAccounts: {
accounts: {},
selectedAccount: '',
},
};

const accountsController = new AccountsController({
messenger: accountsControllerMessenger,
state: initialState.AccountsController ?? defaultAccountsControllerState,
});
const tokensController = new TokensController({
chainId: networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
Expand Down Expand Up @@ -936,21 +942,35 @@ class Engine {
),
});

const accountTrackerController = new AccountTrackerController({
onPreferencesStateChange,
getIdentities: () => preferencesController.state.identities,
getSelectedAddress: () => accountsController.getSelectedAccount().address,
getMultiAccountBalancesEnabled: () =>
preferencesController.state.isMultiAccountBalancesEnabled,
getCurrentChainId: () =>
toHexadecimal(
networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
).configuration.chainId,
),
getNetworkClientById:
networkController.getNetworkClientById.bind(networkController),
});
const accountTrackerController = new AccountTrackerController(
{
// @ts-expect-error TODO: Resolve mismatch between base-controller versions fix < ======.
messenger: this.controllerMessenger.getRestricted({
name: 'AccountTrackerController',
allowedActions: [
`AccountsController:getSelectedAccount`,
`AccountsController:listAccounts`,
],
allowedEvents: [
'AccountsController:selectedEvmAccountChange',
'AccountsController:selectedAccountChange',
],
}),
getMultiAccountBalancesEnabled: () =>
preferencesController.state.isMultiAccountBalancesEnabled,
getCurrentChainId: () =>
toHexadecimal(
networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
).configuration.chainId,
),
getNetworkClientById:
networkController.getNetworkClientById.bind(networkController),
},
undefined,
{ accounts: {} },
);

const permissionController = new PermissionController({
// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
messenger: this.controllerMessenger.getRestricted({
Expand Down Expand Up @@ -1480,6 +1500,7 @@ class Engine {
'NetworkController:getState',
'NetworkController:getNetworkClientById',
'PreferencesController:getState',
'AccountsController:getSelectedAccount',
],
}),
disabled: false,
Expand All @@ -1503,28 +1524,24 @@ class Engine {
interval: 180000,
}),
new TokenRatesController({
onTokensStateChange: (listener) =>
this.controllerMessenger.subscribe(
`${tokensController.name}:stateChange`,
listener,
),
onNetworkStateChange: (listener) =>
this.controllerMessenger.subscribe(
AppConstants.NETWORK_STATE_CHANGE_EVENT,
listener,
),
onPreferencesStateChange,
chainId: networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
).configuration.chainId,
ticker: networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
).configuration.ticker,
selectedAddress: preferencesController.state.selectedAddress,
// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
messenger: this.controllerMessenger.getRestricted({
name: 'TokenRatesController',
allowedActions: [
'TokensController:getState',
'NetworkController:getNetworkClientById',
'NetworkController:getState',
'PreferencesController:getState',
],
allowedEvents: [
'PreferencesController:stateChange',
'TokensController:stateChange',
'NetworkController:stateChange',
],
}),
tokenPricesService: codefiTokenApiV2,
interval: 30 * 60 * 1000,
getNetworkClientById:
networkController.getNetworkClientById.bind(networkController),
state: initialState.TokenRatesController || { marketData: {} },
}),
this.transactionController,
this.smartTransactionsController,
Expand Down Expand Up @@ -1990,7 +2007,7 @@ class Engine {
NftController.reset();

TokenBalancesController.reset();
TokenRatesController.update({ marketData: {} });
TokenRatesController.reset();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(TransactionController as any).update(() => ({
Expand Down
2 changes: 1 addition & 1 deletion app/core/EngineService/EngineService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class EngineService {
name: 'TokenBalancesController',
key: `${engine.context.TokenBalancesController.name}:stateChange`,
},
{ name: 'TokenRatesController' },
{ name: 'TokenRatesController', key: 'TokenRatesController:stateChange' },
{
name: 'TransactionController',
key: `${engine.context.TransactionController.name}:stateChange`,
Expand Down
11 changes: 7 additions & 4 deletions app/reducers/collectibles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
selectAllNftContracts,
selectAllNfts,
} from '../../selectors/nftController';
import { selectSelectedInternalAccountChecksummedAddress } from '../../selectors/accountsController';
import {
selectSelectedInternalAccountChecksummedAddress,
selectSelectedInternalAccountAddress,
} from '../../selectors/accountsController';
import { compareTokenIds } from '../../util/tokens';
import { createDeepEqualSelector } from '../../selectors/util';

Expand All @@ -14,22 +17,22 @@ export const isNftFetchingProgressSelector = (state) =>
state.collectibles.isNftFetchingProgress;

export const collectibleContractsSelector = createSelector(
selectSelectedInternalAccountChecksummedAddress,
selectSelectedInternalAccountAddress,
selectChainId,
selectAllNftContracts,
(address, chainId, allNftContracts) =>
allNftContracts[address]?.[chainId] || [],
);

export const collectiblesSelector = createDeepEqualSelector(
selectSelectedInternalAccountChecksummedAddress,
selectSelectedInternalAccountAddress,
selectChainId,
selectAllNfts,
(address, chainId, allNfts) => allNfts[address]?.[chainId] || [],
);

export const favoritesCollectiblesSelector = createSelector(
selectSelectedInternalAccountChecksummedAddress,
selectSelectedInternalAccountAddress,
selectChainId,
favoritesSelector,
(address, chainId, favorites) => favorites[address]?.[chainId] || [],
Expand Down
11 changes: 11 additions & 0 deletions app/selectors/accountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@ export const selectSelectedInternalAccountChecksummedAddress = createSelector(
return selectedAddress ? toChecksumHexAddress(selectedAddress) : undefined;
},
);

/**
* A memoized selector that returns the selected internal account address
*/
export const selectSelectedInternalAccountAddress = createSelector(
selectSelectedInternalAccount,
(account) => {
const selectedAddress = account?.address;
return selectedAddress || undefined;
},
);
4 changes: 2 additions & 2 deletions app/selectors/tokenRatesController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/prefer-default-export */
import { createSelector } from 'reselect';
import { TokenRatesState } from '@metamask/assets-controllers';
import { TokenRatesControllerState } from '@metamask/assets-controllers';
import { RootState } from '../reducers';
import { selectChainId } from './networkController';
import { Hex } from '@metamask/utils';
Expand All @@ -11,6 +11,6 @@ const selectTokenRatesControllerState = (state: RootState) =>
export const selectContractExchangeRates = createSelector(
selectChainId,
selectTokenRatesControllerState,
(chainId: Hex, tokenRatesControllerState: TokenRatesState) =>
(chainId: Hex, tokenRatesControllerState: TokenRatesControllerState) =>
tokenRatesControllerState.marketData[chainId],
);
4 changes: 2 additions & 2 deletions app/selectors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
NftControllerState,
TokenDetectionController,
TokenListState,
TokenRatesState,
TokenRatesControllerState,
TokensControllerState,
TokenBalancesControllerState,
} from '@metamask/assets-controllers';
Expand Down Expand Up @@ -39,7 +39,7 @@ export interface EngineState {
PhishingController: PhishingControllerState;
PPOMController: PPOMState;
TokenBalancesController: TokenBalancesControllerState;
TokenRatesController: TokenRatesState;
TokenRatesController: TokenRatesControllerState;
TransactionController: TransactionControllerState;
SwapsController: SwapsController;
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
Expand Down
4 changes: 2 additions & 2 deletions app/store/migrations/029.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
NftContract,
NftControllerState,
TokenListState,
TokenRatesState,
TokenRatesControllerState,
TokensControllerState,
} from '@metamask/assets-controllers';

Expand Down Expand Up @@ -530,7 +530,7 @@ export default async function migrate(stateAsync: unknown) {
const tokenRatesControllerState =
state?.engine?.backgroundState?.TokenRatesController;
const newTokenRatesControllerState = state?.engine?.backgroundState
?.TokenRatesController as TokenRatesState;
?.TokenRatesController as TokenRatesControllerState;

if (!isObject(tokenRatesControllerState)) {
captureException(
Expand Down
4 changes: 2 additions & 2 deletions app/store/migrations/031.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hasProperty, isObject } from '@metamask/utils';
import { captureException } from '@sentry/react-native';
import {
TokenListState,
TokenRatesState,
TokenRatesControllerState,
TokensControllerState,
} from '@metamask/assets-controllers';
import { toHex } from '@metamask/controller-utils';
Expand Down Expand Up @@ -105,7 +105,7 @@ export default async function migrate(stateAsync: unknown) {
const tokenRatesControllerState =
state?.engine?.backgroundState?.TokenRatesController;
const newTokenRatesControllerState = state?.engine?.backgroundState
?.TokenRatesController as TokenRatesState;
?.TokenRatesController as TokenRatesControllerState;

if (!isObject(tokenRatesControllerState)) {
captureException(
Expand Down
2 changes: 1 addition & 1 deletion bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1570,4 +1570,4 @@ trigger_map:
- tag: 'dev-e2e-*'
pipeline: pr_smoke_e2e_pipeline
- tag: 'v*.*.*-RC-*'
pipeline: release_e2e_pipeline
pipeline: release_e2e_pipeline
2 changes: 1 addition & 1 deletion ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ post_install do |installer|
:mac_catalyst_enabled => false
)
__apply_Xcode_12_5_M1_post_install_workaround(installer)
end
end
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1299,4 +1299,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: e0bcc4eb12d48746028cd4f4161a292fa9ddc627

COCOAPODS: 1.15.2
COCOAPODS: 1.15.2
Loading
Loading