Skip to content

Commit

Permalink
fix: fix RatesController.setCryptoCurrencyList invalid internal field (
Browse files Browse the repository at this point in the history
…#4572)

## Explanation

We missed to rename that field in that PR:
#4242

## References

N/A

## Changelog

### `@metamask/assets-controllers`

- FIXED: Fix `RatesController.setCryptocurrencyList`
- It was not using the correct field when updating the internal state
(`fromCurrencies` -> `cryptocurrencies`)

## 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
ccharly committed Jul 30, 2024
1 parent 4398a99 commit 882be32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ describe('RatesController', () => {
const { fiatCurrency, rates, cryptocurrencies } = ratesController.state;
expect(ratesController).toBeDefined();
expect(fiatCurrency).toBe('usd');
expect(Object.keys(rates)).toStrictEqual(['btc']);
expect(cryptocurrencies).toStrictEqual(['btc']);
expect(Object.keys(rates)).toStrictEqual([Cryptocurrency.Btc]);
expect(cryptocurrencies).toStrictEqual([Cryptocurrency.Btc]);
});
});

Expand Down Expand Up @@ -315,7 +315,7 @@ describe('RatesController', () => {
describe('setCryptocurrencyList', () => {
it('updates the cryptocurrency list', async () => {
const fetchExchangeRateStub = jest.fn().mockResolvedValue({});
const mockCryptocurrencyList = [Cryptocurrency.Btc];
const mockCryptocurrencyList: Cryptocurrency[] = []; // Different from default list
const ratesController = setupRatesController({
interval: 150,
initialState: {},
Expand All @@ -326,7 +326,11 @@ describe('RatesController', () => {

const cryptocurrencyListPreUpdate =
ratesController.getCryptocurrencyList();
expect(cryptocurrencyListPreUpdate).toStrictEqual(['btc']);
expect(cryptocurrencyListPreUpdate).toStrictEqual([Cryptocurrency.Btc]);
// Just to make sure we're updating to something else than the default list
expect(cryptocurrencyListPreUpdate).not.toStrictEqual(
mockCryptocurrencyList,
);

await ratesController.setCryptocurrencyList(mockCryptocurrencyList);
const cryptocurrencyListPostUpdate =
Expand Down
49 changes: 29 additions & 20 deletions packages/assets-controllers/src/RatesController/RatesController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseController } from '@metamask/base-controller';
import { Mutex } from 'async-mutex';
import type { Draft } from 'immer';

import { fetchMultiExchangeRate as defaultFetchExchangeRate } from '../crypto-compare-service';
import type {
Expand Down Expand Up @@ -134,12 +135,14 @@ export class RatesController extends BaseController<
};
}

this.update(() => {
return {
...this.state,
rates: updatedRates,
};
});
this.update(
(state: Draft<RatesControllerState>): RatesControllerState => {
return {
...state,
rates: updatedRates,
};
},
);
});
}

Expand Down Expand Up @@ -182,16 +185,20 @@ export class RatesController extends BaseController<

/**
* Sets the list of supported cryptocurrencies.
* @param list - The list of supported cryptocurrencies.
* @param cryptocurrencies - The list of supported cryptocurrencies.
*/
async setCryptocurrencyList(list: Cryptocurrency[]): Promise<void> {
async setCryptocurrencyList(
cryptocurrencies: Cryptocurrency[],
): Promise<void> {
await this.#withLock(() => {
this.update(() => {
return {
...this.state,
fromCurrencies: list,
};
});
this.update(
(state: Draft<RatesControllerState>): RatesControllerState => {
return {
...state,
cryptocurrencies,
};
},
);
});
}

Expand All @@ -205,12 +212,14 @@ export class RatesController extends BaseController<
}

await this.#withLock(() => {
this.update(() => {
return {
...defaultState,
fiatCurrency,
};
});
this.update(
(state: Draft<RatesControllerState>): RatesControllerState => {
return {
...state,
fiatCurrency,
};
},
);
});
await this.#updateRates();
}
Expand Down

0 comments on commit 882be32

Please sign in to comment.