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

fix: Prevent State Changes for ignoreTokens on Non-Current Network #5014

Merged
merged 9 commits into from
Dec 17, 2024
55 changes: 53 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,57 @@ describe('TokensController', () => {
);
});

it('should add tokens to allIgnoredTokens state only if we are not using current network', async () => {
Copy link
Contributor

@mcmire mcmire Dec 6, 2024

Choose a reason for hiding this comment

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

Is this test name as accurate as it could be?

  • It says that tokens should only be added to allIgnoredTokens state, but we are never checking that state. We are just checking that tokens and ignoredTokens are not affected, which is a slightly different thing to check.
  • Who are "we" in this context?

Perhaps a better name would be:

Suggested change
it('should add tokens to allIgnoredTokens state only if we are not using current network', async () => {
it('should not update detectedTokens, tokens, and ignoredTokens state given a network that is different from the globally selected network', async () => {

Copy link
Contributor

Choose a reason for hiding this comment

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

On top of this, do we have a test which ensures that detectedTokens, tokens, and ignoredTokens do get updated when passed the globally selected network? I'm not quite seeing that.

Copy link
Contributor Author

@salimtb salimtb Dec 9, 2024

Choose a reason for hiding this comment

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

suggestions implemented with two tests scenarios

  • should not update detectedTokens, tokens, and ignoredTokens state given a network that is different from the globally selected network
  • should update tokens, and ignoredTokens and detectedTokens state for the globally selected network

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for detected tokens, it's a bit complex to test them, so I've put some assertions to make sure they're not affected, but I'm open to suggestions.

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 not retain ignored tokens from a different network', async () => {
const selectedAddress = '0x0001';
const selectedAccount = createMockInternalAccount({
Expand Down Expand Up @@ -879,7 +930,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
Loading