From d508dd103c5cf41ac3457f27b8e2b753a196c80d Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Mon, 26 Feb 2024 13:19:20 +0100 Subject: [PATCH 1/6] refactor(controller-utils): replace `any` with `BN` types for type safety --- packages/controller-utils/src/util.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/controller-utils/src/util.ts b/packages/controller-utils/src/util.ts index 2721befb3d5..fea7619386b 100644 --- a/packages/controller-utils/src/util.ts +++ b/packages/controller-utils/src/util.ts @@ -44,9 +44,7 @@ export function isSafeChainId(chainId: Hex): boolean { * @param inputBn - BN instance to convert to a hex string. * @returns A '0x'-prefixed hex string. */ -// TODO: Replace `any` with type -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function BNToHex(inputBn: any) { +export function BNToHex(inputBn: BN) { return add0x(inputBn.toString(16)); } @@ -59,9 +57,7 @@ export function BNToHex(inputBn: any) { * @returns Product of the multiplication. */ export function fractionBN( - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - targetBN: any, + targetBN: BN, numerator: number | string, denominator: number | string, ) { From 0bd1e9d8dd558c0f1fdbd025eb6c0c0349dcb090 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Mon, 26 Feb 2024 14:50:57 +0100 Subject: [PATCH 2/6] refactor(controller-utils): replace with in logOrRethrowError error param --- packages/controller-utils/src/util.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/controller-utils/src/util.ts b/packages/controller-utils/src/util.ts index fea7619386b..e5386fadd8f 100644 --- a/packages/controller-utils/src/util.ts +++ b/packages/controller-utils/src/util.ts @@ -535,25 +535,24 @@ export function isValidJson(value: unknown): value is Json { * @param error - Caught error that we should either rethrow or log to console * @param codesToCatch - array of error codes for errors we want to catch and log in a particular context */ -// TODO: Replace `any` with type -// eslint-disable-next-line @typescript-eslint/no-explicit-any -function logOrRethrowError(error: any, codesToCatch: number[] = []) { +function logOrRethrowError(error: unknown, codesToCatch: number[] = []) { if (!error) { return; } - const includesErrorCodeToCatch = codesToCatch.some((code) => - error.message?.includes(`Fetch failed with status '${code}'`), - ); + if (error instanceof Error) { + const includesErrorCodeToCatch = codesToCatch.some((code) => + error.message?.includes(`Fetch failed with status '${code}'`), + ); - if ( - error instanceof Error && - (includesErrorCodeToCatch || + if ( + includesErrorCodeToCatch || error.message?.includes('Failed to fetch') || - error === TIMEOUT_ERROR) - ) { - console.error(error); - } else { - throw error; + error === TIMEOUT_ERROR + ) { + console.error(error); + } else { + throw error; + } } } From d2e119f9f3d91dfbcfc5efab40dcb54e40ef4505 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Mon, 26 Feb 2024 15:02:31 +0100 Subject: [PATCH 3/6] refactor(controller-utils): replace `any` type with `string` in isNetworkType --- packages/controller-utils/src/types.test.ts | 2 ++ packages/controller-utils/src/types.ts | 6 ++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/controller-utils/src/types.test.ts b/packages/controller-utils/src/types.test.ts index ddac15c4999..8d1de7df7cb 100644 --- a/packages/controller-utils/src/types.test.ts +++ b/packages/controller-utils/src/types.test.ts @@ -2,7 +2,9 @@ import { isNetworkType, NetworkType } from './types'; describe('types', () => { it('isNetworkType', () => { + // @ts-expect-error We are intentionally passing bad input. expect(isNetworkType({})).toBe(false); + // @ts-expect-error We are intentionally passing bad input. expect(isNetworkType(1)).toBe(false); expect(isNetworkType('test')).toBe(false); expect(isNetworkType('mainnet')).toBe(true); diff --git a/packages/controller-utils/src/types.ts b/packages/controller-utils/src/types.ts index 248d7623b9e..1cfad3a3e13 100644 --- a/packages/controller-utils/src/types.ts +++ b/packages/controller-utils/src/types.ts @@ -28,10 +28,8 @@ export type NetworkType = (typeof NetworkType)[keyof typeof NetworkType]; * @param val - the value to check whether it is NetworkType or not. * @returns boolean indicating whether or not the argument is NetworkType. */ -// TODO: Replace `any` with type -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function isNetworkType(val: any): val is NetworkType { - return Object.values(NetworkType).includes(val); +export function isNetworkType(val: string): val is NetworkType { + return Object.values(NetworkType).includes(val as NetworkType); } /** From 8230c69c58bf6f41c437b536a96b3a24824dd007 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Mon, 4 Mar 2024 22:25:12 +0100 Subject: [PATCH 4/6] fix: remove useless bNToHex conversion --- .../src/AccountTrackerController.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/assets-controllers/src/AccountTrackerController.ts b/packages/assets-controllers/src/AccountTrackerController.ts index 5aa65496c03..6c8479dc2b7 100644 --- a/packages/assets-controllers/src/AccountTrackerController.ts +++ b/packages/assets-controllers/src/AccountTrackerController.ts @@ -1,9 +1,5 @@ import type { BaseConfig, BaseState } from '@metamask/base-controller'; -import { - BNToHex, - query, - safelyExecuteWithTimeout, -} from '@metamask/controller-utils'; +import { query, safelyExecuteWithTimeout } from '@metamask/controller-utils'; import EthQuery from '@metamask/eth-query'; import type { Provider } from '@metamask/eth-query'; import type { @@ -272,9 +268,12 @@ export class AccountTrackerController extends StaticIntervalPollingControllerV1< const accountsForChain = { ...accountsByChainId[chainId] }; for (const address of accountsToUpdate) { - accountsForChain[address] = { - balance: BNToHex(await this.getBalanceFromChain(address, ethQuery)), - }; + const balance = await this.getBalanceFromChain(address, ethQuery); + if (balance) { + accountsForChain[address] = { + balance, + }; + } } this.update({ From 04689efccda20dbd0757ed01812f8728a1bdab3a Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Tue, 5 Mar 2024 01:20:31 +0100 Subject: [PATCH 5/6] fix: throw unexpected error types --- packages/controller-utils/src/util.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/controller-utils/src/util.ts b/packages/controller-utils/src/util.ts index e5386fadd8f..84fe7425748 100644 --- a/packages/controller-utils/src/util.ts +++ b/packages/controller-utils/src/util.ts @@ -554,5 +554,8 @@ function logOrRethrowError(error: unknown, codesToCatch: number[] = []) { } else { throw error; } + } else { + // eslint-disable-next-line @typescript-eslint/no-throw-literal + throw error; } } From fef0b36d88e238908af5de99e9c796b2ae2a46b3 Mon Sep 17 00:00:00 2001 From: Salah-Eddine Saakoun Date: Tue, 5 Mar 2024 11:05:44 +0100 Subject: [PATCH 6/6] fix: remove useless optional chaining operator --- packages/controller-utils/src/util.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/controller-utils/src/util.ts b/packages/controller-utils/src/util.ts index 84fe7425748..d512142ce68 100644 --- a/packages/controller-utils/src/util.ts +++ b/packages/controller-utils/src/util.ts @@ -542,12 +542,12 @@ function logOrRethrowError(error: unknown, codesToCatch: number[] = []) { if (error instanceof Error) { const includesErrorCodeToCatch = codesToCatch.some((code) => - error.message?.includes(`Fetch failed with status '${code}'`), + error.message.includes(`Fetch failed with status '${code}'`), ); if ( includesErrorCodeToCatch || - error.message?.includes('Failed to fetch') || + error.message.includes('Failed to fetch') || error === TIMEOUT_ERROR ) { console.error(error);