Skip to content

Commit

Permalink
fix: update tokens controllers to use selectedAccountId instead of se…
Browse files Browse the repository at this point in the history
…lectedAddress (#4219)

## Explanation

This PR updates the `selectedAccount` to `selectedAccountId` in the
token controllers

## References

Fixes MetaMask/accounts-planning#381

## Changelog

### `@metamask/assets-controllers`

- **BREAKING**: `TokenBalancesController` update
`PreferencesConrtollerGetStateAction` to
`AccountsControllerGetSelectedAccountAction`
- **BREAKING**: `TokenDetectionController` change `selectedAddress` to
`selectedAccountId`
- **ADDED**: `TokenDetectionController` add `getAccountAction`
- **BREAKING**: `TokenRatesController` change `selectedAddress` to
`selectedAccountId`
- **BREAKING**: `onPreferencesStateChange` arg removed and
`getInternalAccount` and `onSelectedAccountChange` added in
`TokenRatesController`
- **ADDED**: `getAccountAction` added in`TokensController` 
- **BREAKING**: Changed `selectedAddress` to `selectedAccountId` and
`PreferencesControllerStateChangeEvent` to
`AccountsControllerSelectedEvmAccountChangeEvent` in the
`TokensController`
- **ADDED**: `getAccountAction` added in`TokensController` 

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
montelaidev authored Jun 20, 2024
1 parent d51609f commit ca683e8
Show file tree
Hide file tree
Showing 8 changed files with 1,138 additions and 633 deletions.
309 changes: 172 additions & 137 deletions packages/assets-controllers/src/TokenBalancesController.test.ts

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions packages/assets-controllers/src/TokenBalancesController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type AccountsControllerGetSelectedAccountAction } from '@metamask/accounts-controller';
import {
type RestrictedControllerMessenger,
type ControllerGetStateAction,
type ControllerStateChangeEvent,
BaseController,
} from '@metamask/base-controller';
import { safelyExecute, toHex } from '@metamask/controller-utils';
import type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';

import type { AssetsContractController } from './AssetsContractController';
import type { Token } from './TokenRatesController';
Expand Down Expand Up @@ -56,7 +56,7 @@ export type TokenBalancesControllerGetStateAction = ControllerGetStateAction<
export type TokenBalancesControllerActions =
TokenBalancesControllerGetStateAction;

export type AllowedActions = PreferencesControllerGetStateAction;
export type AllowedActions = AccountsControllerGetSelectedAccountAction;

export type TokenBalancesControllerStateChangeEvent =
ControllerStateChangeEvent<
Expand Down Expand Up @@ -201,16 +201,18 @@ export class TokenBalancesController extends BaseController<
if (this.#disabled) {
return;
}

const { selectedAddress } = this.messagingSystem.call(
'PreferencesController:getState',
const selectedInternalAccount = this.messagingSystem.call(
'AccountsController:getSelectedAccount',
);

const newContractBalances: ContractBalances = {};
for (const token of this.#tokens) {
const { address } = token;
try {
const balance = await this.#getERC20BalanceOf(address, selectedAddress);
const balance = await this.#getERC20BalanceOf(
address,
selectedInternalAccount.address,
);
newContractBalances[address] = toHex(balance);
token.hasBalanceError = false;
} catch (error) {
Expand Down
Loading

0 comments on commit ca683e8

Please sign in to comment.