From d32982ea8642927826ab87130d15d2130091f23c Mon Sep 17 00:00:00 2001
From: Marin Petrunic <marin.petrunic@gmail.com>
Date: Tue, 26 Sep 2023 11:26:58 +0200
Subject: [PATCH 1/2] fix: infer Timer and Timeout types depending on context
 instead of forcing nodejs types

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
---
 .../test/unit/web3_batch_request.test.ts      |  4 ++--
 .../src/utils/reject_if_block_timeout.ts      |  2 +-
 .../src/utils/wait_for_transaction_receipt.ts |  2 +-
 .../web3-utils/src/chunk_response_parser.ts   |  3 ++-
 packages/web3-utils/src/promise_helpers.ts    | 20 +++++++++++--------
 .../web3-utils/src/web3_deferred_promise.ts   |  3 ++-
 6 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/packages/web3-core/test/unit/web3_batch_request.test.ts b/packages/web3-core/test/unit/web3_batch_request.test.ts
index 7f5fc20ee24..cc861276a5f 100644
--- a/packages/web3-core/test/unit/web3_batch_request.test.ts
+++ b/packages/web3-core/test/unit/web3_batch_request.test.ts
@@ -16,7 +16,7 @@ along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 import { JsonRpcBatchRequest, JsonRpcBatchResponse, JsonRpcOptionalRequest } from 'web3-types';
-import { jsonRpc, Web3DeferredPromise } from 'web3-utils';
+import { jsonRpc, Web3DeferredPromise, Timeout } from 'web3-utils';
 import { OperationAbortError, OperationTimeoutError } from 'web3-errors';
 import { Web3BatchRequest } from '../../src/web3_batch_request';
 
@@ -174,7 +174,7 @@ describe('Web3BatchRequest', () => {
 		});
 
 		it('should timeout if request not executed in a particular time', async () => {
-			let timerId!: NodeJS.Timeout;
+			let timerId!: Timeout;
 
 			jest.spyOn(requestManager, 'sendBatch').mockImplementation(async () => {
 				return new Promise(resolve => {
diff --git a/packages/web3-eth/src/utils/reject_if_block_timeout.ts b/packages/web3-eth/src/utils/reject_if_block_timeout.ts
index 3f7faa7e0f1..2019ce3380d 100644
--- a/packages/web3-eth/src/utils/reject_if_block_timeout.ts
+++ b/packages/web3-eth/src/utils/reject_if_block_timeout.ts
@@ -34,7 +34,7 @@ function resolveByPolling(
 	transactionHash?: Bytes,
 ): [Promise<never>, ResourceCleaner] {
 	const pollingInterval = web3Context.transactionPollingInterval;
-	const [intervalId, promiseToError]: [NodeJS.Timer, Promise<never>] =
+	const [intervalId, promiseToError] =
 		rejectIfConditionAtInterval(async () => {
 			let lastBlockNumber;
 			try {
diff --git a/packages/web3-eth/src/utils/wait_for_transaction_receipt.ts b/packages/web3-eth/src/utils/wait_for_transaction_receipt.ts
index 1809654bc1a..54ff06f588b 100644
--- a/packages/web3-eth/src/utils/wait_for_transaction_receipt.ts
+++ b/packages/web3-eth/src/utils/wait_for_transaction_receipt.ts
@@ -43,7 +43,7 @@ export async function waitForTransactionReceipt<ReturnFormat extends DataFormat>
 		}
 	}, pollingInterval);
 
-	const [timeoutId, rejectOnTimeout]: [NodeJS.Timer, Promise<never>] = rejectIfTimeout(
+	const [timeoutId, rejectOnTimeout] = rejectIfTimeout(
 		web3Context.transactionPollingTimeout,
 		new TransactionPollingTimeoutError({
 			numberOfSeconds: web3Context.transactionPollingTimeout / 1000,
diff --git a/packages/web3-utils/src/chunk_response_parser.ts b/packages/web3-utils/src/chunk_response_parser.ts
index f4949953e74..e6f00f8ca8f 100644
--- a/packages/web3-utils/src/chunk_response_parser.ts
+++ b/packages/web3-utils/src/chunk_response_parser.ts
@@ -17,10 +17,11 @@ along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
 import { JsonRpcResponse } from 'web3-types';
 import { InvalidResponseError } from 'web3-errors';
 import { EventEmitter } from 'events';
+import { Timeout } from './promise_helpers';
 
 export class ChunkResponseParser {
 	private lastChunk: string | undefined;
-	private lastChunkTimeout: NodeJS.Timeout | undefined;
+	private lastChunkTimeout: Timeout | undefined;
 	private _clearQueues: (() => void) | undefined;
 	private readonly eventEmitter: EventEmitter;
 	private readonly autoReconnect: boolean;
diff --git a/packages/web3-utils/src/promise_helpers.ts b/packages/web3-utils/src/promise_helpers.ts
index 43a3ade298c..c968ce247e9 100644
--- a/packages/web3-utils/src/promise_helpers.ts
+++ b/packages/web3-utils/src/promise_helpers.ts
@@ -17,6 +17,10 @@ along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
 
 import { isNullish } from 'web3-validator';
 
+export type Timer = ReturnType<typeof setInterval>;
+export type Timeout = ReturnType<typeof setTimeout>;
+
+
 /**
  * An alternative to the node function `isPromise` that exists in `util/types` because it is not available on the browser.
  * @param object - to check if it is a `Promise`
@@ -54,7 +58,7 @@ export async function waitWithTimeout<T>(
 	timeout: number,
 	error?: Error,
 ): Promise<T | undefined> {
-	let timeoutId: NodeJS.Timeout | undefined;
+	let timeoutId: Timeout | undefined;
 	const result = await Promise.race([
 		awaitable instanceof Promise ? awaitable : awaitable(),
 		new Promise<undefined | Error>((resolve, reject) => {
@@ -81,7 +85,7 @@ export async function pollTillDefined<T>(
 ): Promise<Exclude<T, undefined>> {
 	const awaitableRes = waitWithTimeout(func, interval);
 
-	let intervalId: NodeJS.Timer | undefined;
+	let intervalId: Timer | undefined;
 	const polledRes = new Promise<Exclude<T, undefined>>((resolve, reject) => {
 		intervalId = setInterval(() => {
 			(async () => {
@@ -122,14 +126,14 @@ export async function pollTillDefined<T>(
  * const [timerId, promise] = web3.utils.rejectIfTimeout(100, new Error('time out'));
  * ```
  */
-export function rejectIfTimeout(timeout: number, error: Error): [NodeJS.Timer, Promise<never>] {
-	let timeoutId: NodeJS.Timer | undefined;
+export function rejectIfTimeout(timeout: number, error: Error): [Timer, Promise<never>] {
+	let timeoutId: Timer | undefined;
 	const rejectOnTimeout = new Promise<never>((_, reject) => {
 		timeoutId = setTimeout(() => {
 			reject(error);
 		}, timeout);
 	});
-	return [timeoutId as unknown as NodeJS.Timer, rejectOnTimeout];
+	return [timeoutId!, rejectOnTimeout];
 }
 /**
  * Sets an interval that repeatedly executes the given cond function with the specified interval between each call.
@@ -141,8 +145,8 @@ export function rejectIfTimeout(timeout: number, error: Error): [NodeJS.Timer, P
 export function rejectIfConditionAtInterval<T>(
 	cond: AsyncFunction<T | undefined>,
 	interval: number,
-): [NodeJS.Timer, Promise<never>] {
-	let intervalId: NodeJS.Timer | undefined;
+): [Timer, Promise<never>] {
+	let intervalId: Timer | undefined;
 	const rejectIfCondition = new Promise<never>((_, reject) => {
 		intervalId = setInterval(() => {
 			(async () => {
@@ -154,5 +158,5 @@ export function rejectIfConditionAtInterval<T>(
 			})() as unknown;
 		}, interval);
 	});
-	return [intervalId as unknown as NodeJS.Timer, rejectIfCondition];
+	return [intervalId!, rejectIfCondition];
 }
diff --git a/packages/web3-utils/src/web3_deferred_promise.ts b/packages/web3-utils/src/web3_deferred_promise.ts
index f1a46c8ef03..1f6a4847d0d 100644
--- a/packages/web3-utils/src/web3_deferred_promise.ts
+++ b/packages/web3-utils/src/web3_deferred_promise.ts
@@ -17,6 +17,7 @@ along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
 
 import { OperationTimeoutError } from 'web3-errors';
 import { Web3DeferredPromiseInterface } from 'web3-types';
+import { Timeout } from './promise_helpers';
 
 /**
  * The class is a simple implementation of a deferred promise with optional timeout functionality,
@@ -32,7 +33,7 @@ export class Web3DeferredPromise<T> implements Promise<T>, Web3DeferredPromiseIn
 	private _resolve!: (value: T | PromiseLike<T>) => void;
 	private _reject!: (reason?: unknown) => void;
 	private _state: 'pending' | 'fulfilled' | 'rejected' = 'pending';
-	private _timeoutId?: NodeJS.Timeout;
+	private _timeoutId?: Timeout;
 	private readonly _timeoutInterval?: number;
 	private readonly _timeoutMessage: string;
 

From fc1bf0c535e7862c29b0987ab94bb49030d2529b Mon Sep 17 00:00:00 2001
From: Marin Petrunic <marin.petrunic@gmail.com>
Date: Tue, 26 Sep 2023 11:39:58 +0200
Subject: [PATCH 2/2] fix: lint

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
---
 packages/web3-utils/src/chunk_response_parser.ts | 2 +-
 packages/web3-utils/src/web3_deferred_promise.ts | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/packages/web3-utils/src/chunk_response_parser.ts b/packages/web3-utils/src/chunk_response_parser.ts
index e6f00f8ca8f..d9b37c7b6ee 100644
--- a/packages/web3-utils/src/chunk_response_parser.ts
+++ b/packages/web3-utils/src/chunk_response_parser.ts
@@ -17,7 +17,7 @@ along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
 import { JsonRpcResponse } from 'web3-types';
 import { InvalidResponseError } from 'web3-errors';
 import { EventEmitter } from 'events';
-import { Timeout } from './promise_helpers';
+import { Timeout } from './promise_helpers.js';
 
 export class ChunkResponseParser {
 	private lastChunk: string | undefined;
diff --git a/packages/web3-utils/src/web3_deferred_promise.ts b/packages/web3-utils/src/web3_deferred_promise.ts
index 1f6a4847d0d..01805b774be 100644
--- a/packages/web3-utils/src/web3_deferred_promise.ts
+++ b/packages/web3-utils/src/web3_deferred_promise.ts
@@ -17,7 +17,7 @@ along with web3.js.  If not, see <http://www.gnu.org/licenses/>.
 
 import { OperationTimeoutError } from 'web3-errors';
 import { Web3DeferredPromiseInterface } from 'web3-types';
-import { Timeout } from './promise_helpers';
+import { Timeout } from './promise_helpers.js';
 
 /**
  * The class is a simple implementation of a deferred promise with optional timeout functionality,