Skip to content

Commit

Permalink
TokenRatesController: Use browser compat eq check
Browse files Browse the repository at this point in the history
`isDeepStrictEqual` is a function from Node's `util` module. However, it
doesn't exist in a browser context, so using it crashes the extension.

This commit replaces this function with `fast-equals`'
[`strictDeepEqual`][1] function, which essentially does the same thing
as [`isDeepStrictEqual`][2] in a more robust way.

[1]: https://github.com/planttheidea/fast-equals/blob/7564fcf46a187d011261d4a6d60724cb2ec3a75d/src/equals.ts
[2]: https://github.com/sindresorhus/core-assert/blob/fa0ab246046a81deb4cac841fee8b89b4cb3b296/index.js#L165
  • Loading branch information
mcmire committed Dec 13, 2023
1 parent aea9bac commit 3d633a2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@types/uuid": "^8.3.0",
"async-mutex": "^0.2.6",
"ethereumjs-util": "^7.0.10",
"fast-equals": "^5.0.1",
"lodash": "^4.17.21",
"multiformats": "^9.5.2",
"single-call-balance-checker-abi": "^1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/assets-controllers/src/TokenRatesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import type { Hex } from '@metamask/utils';
import { isDeepStrictEqual } from 'util';
import { strictDeepEqual } from 'fast-equals';

import { reduceInBatchesSerially } from './assetsUtil';
import { fetchExchangeRate as fetchNativeCurrencyExchangeRate } from './crypto-compare';
Expand Down Expand Up @@ -253,7 +253,7 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
this.configure({ allTokens, allDetectedTokens });
const newTokenAddresses = this.#getTokenAddresses(this.config.chainId);
if (
!isDeepStrictEqual(previousTokenAddresses, newTokenAddresses) &&
!strictDeepEqual(previousTokenAddresses, newTokenAddresses) &&
this.#pollState === PollState.Active
) {
await this.updateExchangeRates();
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,7 @@ __metadata:
async-mutex: ^0.2.6
deepmerge: ^4.2.2
ethereumjs-util: ^7.0.10
fast-equals: ^5.0.1
jest: ^27.5.1
jest-environment-jsdom: ^27.5.1
lodash: ^4.17.21
Expand Down Expand Up @@ -6211,6 +6212,13 @@ __metadata:
languageName: node
linkType: hard

"fast-equals@npm:^5.0.1":
version: 5.0.1
resolution: "fast-equals@npm:5.0.1"
checksum: fbb3b6a74f3a0fa930afac151ff7d01639159b4fddd2678b5d50708e0ba38e9ec14602222d10dadb8398187342692c04fbef5a62b1cfcc7942fe03e754e064bc
languageName: node
linkType: hard

"fast-fifo@npm:^1.1.0, fast-fifo@npm:^1.2.0":
version: 1.3.2
resolution: "fast-fifo@npm:1.3.2"
Expand Down

0 comments on commit 3d633a2

Please sign in to comment.