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({ 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); } /** diff --git a/packages/controller-utils/src/util.ts b/packages/controller-utils/src/util.ts index 2721befb3d5..d512142ce68 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, ) { @@ -539,25 +535,27 @@ 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 || - error.message?.includes('Failed to fetch') || - error === TIMEOUT_ERROR) - ) { - console.error(error); + if ( + includesErrorCodeToCatch || + error.message.includes('Failed to fetch') || + error === TIMEOUT_ERROR + ) { + console.error(error); + } else { + throw error; + } } else { + // eslint-disable-next-line @typescript-eslint/no-throw-literal throw error; } }