Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-multichain-balances
Browse files Browse the repository at this point in the history
  • Loading branch information
gantunesr authored Dec 18, 2024
2 parents 80f4288 + 2094380 commit 920699e
Show file tree
Hide file tree
Showing 26 changed files with 1,426 additions and 1,027 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/core-monorepo",
"version": "271.0.0",
"version": "272.0.0",
"private": true,
"description": "Monorepo for packages shared between MetaMask clients",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/approval-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@metamask/base-controller": "^7.0.2",
"@metamask/rpc-errors": "^7.0.1",
"@metamask/utils": "^10.0.0",
"nanoid": "^3.1.31"
"nanoid": "^3.3.8"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
Expand Down
104 changes: 102 additions & 2 deletions packages/assets-controllers/src/TokensController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ describe('TokensController', () => {
decimals: 6,
});
controller.ignoreTokens(['0x03'], InfuraNetworkType.goerli);
expect(controller.state.ignoredTokens).toStrictEqual(['0x03']);
expect(controller.state.ignoredTokens).toStrictEqual([]);

// Validate the overall ignored tokens state
expect(controller.state.allIgnoredTokens).toStrictEqual({
Expand All @@ -845,6 +845,106 @@ describe('TokensController', () => {
);
});

it('should not update detectedTokens, tokens, and ignoredTokens state given a network that is different from the globally selected network', async () => {
const selectedAddress = '0x0001';
const selectedAccount = createMockInternalAccount({
address: selectedAddress,
});

await withController(
{
mocks: {
getSelectedAccount: selectedAccount,
getAccount: selectedAccount,
},
},
async ({ controller, triggerSelectedAccountChange, changeNetwork }) => {
// Select the first account
triggerSelectedAccountChange(selectedAccount);

// Add tokens to sepolia
changeNetwork({ selectedNetworkClientId: InfuraNetworkType.sepolia });
await controller.addToken({
address: '0x01',
symbol: 'Token1',
decimals: 18,
});
expect(controller.state.tokens).toHaveLength(1);
expect(controller.state.ignoredTokens).toHaveLength(0);

// switch to goerli
changeNetwork({ selectedNetworkClientId: InfuraNetworkType.goerli });

// Add tokens to goerli
await controller.addToken({
address: '0x02',
symbol: 'Token2',
decimals: 8,
});

expect(controller.state.tokens).toHaveLength(1);
expect(controller.state.ignoredTokens).toHaveLength(0);

// ignore token on sepolia
controller.ignoreTokens(['0x01'], InfuraNetworkType.sepolia);

// as we are not on sepolia, tokens, ignoredTokens, and detectedTokens should not be affected
expect(controller.state.tokens).toHaveLength(1);
expect(controller.state.ignoredTokens).toHaveLength(0);
expect(controller.state.detectedTokens).toHaveLength(0);
},
);
});

it('should update tokens, and ignoredTokens and detectedTokens state for the globally selected network', async () => {
const selectedAddress = '0x0001';
const selectedAccount = createMockInternalAccount({
address: selectedAddress,
});

await withController(
{
mocks: {
getSelectedAccount: selectedAccount,
getAccount: selectedAccount,
},
},
async ({ controller, triggerSelectedAccountChange, changeNetwork }) => {
// Select the first account
triggerSelectedAccountChange(selectedAccount);

// Set globally selected network to sepolia
changeNetwork({ selectedNetworkClientId: InfuraNetworkType.sepolia });

// Add a token to sepolia
await controller.addToken({
address: '0x01',
symbol: 'Token1',
decimals: 18,
});
// Add a detected token to sepolia
await controller.addDetectedTokens([
{
address: '0x03',
symbol: 'Token3',
decimals: 18,
},
]);

expect(controller.state.tokens).toHaveLength(1);
expect(controller.state.ignoredTokens).toHaveLength(0);

// Ignore the token on sepolia
controller.ignoreTokens(['0x01'], InfuraNetworkType.sepolia);

// Ensure the tokens and ignoredTokens are updated for sepolia (globally selected network)
expect(controller.state.tokens).toHaveLength(0);
expect(controller.state.ignoredTokens).toHaveLength(1);
expect(controller.state.detectedTokens).toHaveLength(1);
},
);
});

it('should not retain ignored tokens from a different network', async () => {
const selectedAddress = '0x0001';
const selectedAccount = createMockInternalAccount({
Expand Down Expand Up @@ -879,7 +979,7 @@ describe('TokensController', () => {
// Ignore the token on Sepolia
controller.ignoreTokens(['0x01'], InfuraNetworkType.sepolia);
expect(controller.state.tokens).toHaveLength(0);
expect(controller.state.ignoredTokens).toStrictEqual(['0x01']);
expect(controller.state.ignoredTokens).toStrictEqual([]);

// Attempt to ignore a token that was added on Goerli
await controller.addToken({
Expand Down
10 changes: 6 additions & 4 deletions packages/assets-controllers/src/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ export class TokensController extends BaseController<
tokenAddressesToIgnore: string[],
networkClientId?: NetworkClientId,
) {
let interactingChainId;
let interactingChainId = this.#chainId;
if (networkClientId) {
interactingChainId = this.messagingSystem.call(
'NetworkController:getNetworkClientById',
Expand Down Expand Up @@ -624,12 +624,14 @@ export class TokensController extends BaseController<
});

this.update((state) => {
state.ignoredTokens = newIgnoredTokens;
state.tokens = newTokens;
state.detectedTokens = newDetectedTokens;
state.allIgnoredTokens = newAllIgnoredTokens;
state.allDetectedTokens = newAllDetectedTokens;
state.allTokens = newAllTokens;
if (interactingChainId === this.#chainId) {
state.detectedTokens = newDetectedTokens;
state.tokens = newTokens;
state.ignoredTokens = newIgnoredTokens;
}
});
}

Expand Down
9 changes: 9 additions & 0 deletions packages/base-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Rename `ControllerMessenger` to `Messenger` ([#5050](https://github.com/MetaMask/core/pull/5050))
- `ControllerMessenger` has been renamed to `Messenger`
- `RestrictedControllerMessengerConstraint` has been renamed to `RestrictedMessengerConstraint`
- `RestrictedControllerMessenger` has been renamed to `RestrictedMessenger`
- The `RestrictedMessenger` constructor parameter `controllerMessenger` has been renamed to `messenger`, though the old name is still accepted
- The old names remain exported as deprecated aliases of the new names, so this is not a breaking change.

## [7.0.2]

### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/base-controller/src/BaseControllerV1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
countControllerStateMetadata,
getCountMessenger,
} from './BaseControllerV2.test';
import { ControllerMessenger } from './ControllerMessenger';
import { ControllerMessenger } from './Messenger';

const STATE = { name: 'foo' };
const CONFIG = { disabled: true };
Expand Down
4 changes: 2 additions & 2 deletions packages/base-controller/src/BaseControllerV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
getPersistentState,
isBaseController,
} from './BaseControllerV2';
import { ControllerMessenger } from './ControllerMessenger';
import type { RestrictedControllerMessenger } from './RestrictedControllerMessenger';
import { ControllerMessenger } from './Messenger';
import type { RestrictedControllerMessenger } from './RestrictedMessenger';

export const countControllerName = 'CountController';

Expand Down
4 changes: 2 additions & 2 deletions packages/base-controller/src/BaseControllerV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import type {
BaseControllerV1Instance,
StateConstraint as StateConstraintV1,
} from './BaseControllerV1';
import type { ActionConstraint, EventConstraint } from './ControllerMessenger';
import type { ActionConstraint, EventConstraint } from './Messenger';
import type {
RestrictedControllerMessenger,
RestrictedControllerMessengerConstraint,
} from './RestrictedControllerMessenger';
} from './RestrictedMessenger';

enablePatches();

Expand Down
Loading

0 comments on commit 920699e

Please sign in to comment.