Skip to content

Commit

Permalink
update typescript version to 5 (#7272)
Browse files Browse the repository at this point in the history
* upgrade typescript to v5

* fix some issues detected by typescript v5

* fix lint issues after the typescript version upgrade
  • Loading branch information
Muhammad-Altabba authored Nov 25, 2024
1 parent 984cb7c commit 7a8df69
Show file tree
Hide file tree
Showing 68 changed files with 191 additions and 131 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tmp
lib/
dist/
tsconfig.tsbuildinfo
tsconfig.types.tsbuildinfo

# Unsupported lock files
package-lock.json
Expand All @@ -48,4 +49,4 @@ benchmark-data.txt

.eslintcache

.history
.history
1 change: 1 addition & 0 deletions fixtures/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const processAsync = async (
processFunc: (
resolver: (value: unknown) => void,
reject: (value: unknown) => void,
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
) => Promise<unknown> | unknown,
) =>
new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
"typedoc-plugin-markdown": "^3.17.0",
"typedoc-plugin-mdn-links": "^2.0.0",
"typedoc-plugin-merge-modules": "^5.1.0",
"typescript": "^4.7.4",
"typescript": "^5.5.4",
"utf-8-validate": "^5.0.9",
"web3-hardhat-plugin": "^1.0.0",
"webpack": "^5.73.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-account-abstraction/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"jest-extended": "^3.0.1",
"prettier": "^2.7.1",
"ts-jest": "^29.1.1",
"typescript": "^4.7.4"
"typescript": "^5.5.4"
},
"dependencies": {
"web3-core": "^4.7.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@
"jest-when": "^3.5.1",
"prettier": "^2.7.1",
"ts-jest": "^29.1.1",
"typescript": "^4.7.4"
"typescript": "^5.5.4"
}
}
1 change: 1 addition & 0 deletions packages/web3-core/src/web3_subscription_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export class Web3SubscriptionManager<
const subscription = new Klass(args ?? undefined, {
subscriptionManager: this as Web3SubscriptionManager<API, RegisteredSubs>,
returnFormat,
// eslint.disable-next-line @typescript-eslint/no-unsafe-any
} as any) as InstanceType<RegisteredSubs[T]>;

await this.addSubscription(subscription);
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/test/unit/web3_promi_event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('Web3PromiEvent', () => {
});

const f = jest.fn();
p.finally(f);
await p.finally(f);
await p;
expect(f).toHaveBeenCalled();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"jest-extended": "^3.0.1",
"prettier": "^2.7.1",
"ts-jest": "^29.1.1",
"typescript": "^4.7.4"
"typescript": "^5.5.4"
}
}
1 change: 1 addition & 0 deletions packages/web3-errors/src/errors/contract_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class Eip838ExecutionError extends Web3ContractError {
super(error.message || 'Error');

this.name = ('name' in error && error.name) || this.constructor.name;
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
this.stack = ('stack' in error && error.stack) || undefined;
this.code = error.code;

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-abi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@
"jest-when": "^3.5.1",
"prettier": "^2.7.1",
"ts-jest": "^29.1.1",
"typescript": "^4.7.4"
"typescript": "^5.5.4"
}
}
2 changes: 1 addition & 1 deletion packages/web3-eth-abi/src/coders/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function inferParamsAbi(params: unknown[]): ReadonlyArray<AbiParameter> {
} as AbiParameter);
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
abi.push({ type: toHex(param as any, true) } as AbiParameter);
abi.push({ type: toHex(param as object, true) } as AbiParameter);
}
});
return abi;
Expand Down
1 change: 1 addition & 0 deletions packages/web3-eth-abi/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export const flattenTypes = (
* returns a string
*/
export const jsonInterfaceMethodToString = (json: AbiFragment): string => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (isAbiErrorFragment(json) || isAbiEventFragment(json) || isAbiFunctionFragment(json)) {
if (json.name?.includes('(')) {
return json.name;
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"jest-when": "^3.5.1",
"prettier": "^2.7.1",
"ts-jest": "^29.1.1",
"typescript": "^4.7.4",
"typescript": "^5.5.4",
"web3-providers-ipc": "^4.0.7"
},
"dependencies": {
Expand Down
13 changes: 7 additions & 6 deletions packages/web3-eth-accounts/src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export class Common extends EventEmitter {
* @returns Common
*/
public static fromGethGenesis(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
genesisJson: any,
{ chain, eips, genesisHash, hardfork, mergeForkIdPostMerge }: GethConfigOpts,
): Common {
Expand Down Expand Up @@ -811,9 +812,9 @@ export class Common extends EventEmitter {
*/
// eslint-disable-next-line @typescript-eslint/ban-types
public nextHardforkBlockOrTimestamp(_hardfork?: string | Hardfork): bigint | null {
const hardfork = _hardfork ?? this._hardfork;
const hardfork = (_hardfork as Hardfork) ?? this._hardfork;
const hfs = this.hardforks();
let hfIndex = hfs.findIndex(hf => hf.name === hardfork);
let hfIndex = hfs.findIndex(hf => (hf.name as Hardfork) === hardfork);
// If the current hardfork is merge, go one behind as merge hf is not part of these
// calcs even if the merge hf block is set
if (hardfork === Hardfork.Merge) {
Expand Down Expand Up @@ -842,7 +843,7 @@ export class Common extends EventEmitter {
: // eslint-disable-next-line no-null/no-null
null;
return (
hf.name !== Hardfork.Merge &&
(hf.name as Hardfork) !== Hardfork.Merge &&
// eslint-disable-next-line no-null/no-null
hfTimeOrBlock !== null &&
hfTimeOrBlock !== undefined &&
Expand Down Expand Up @@ -873,7 +874,7 @@ export class Common extends EventEmitter {
*/
// eslint-disable-next-line @typescript-eslint/ban-types
public nextHardforkBlock(_hardfork?: string | Hardfork): bigint | null {
const hardfork = _hardfork ?? this._hardfork;
const hardfork = (_hardfork as Hardfork) ?? this._hardfork;
let hfBlock = this.hardforkBlock(hardfork);
// If this is a merge hardfork with block not set, then we fallback to previous hardfork
// to find the nextHardforkBlock
Expand Down Expand Up @@ -905,7 +906,7 @@ export class Common extends EventEmitter {
);
// TypeScript can't seem to follow that the hfBlock is not null at this point
// eslint-disable-next-line no-null/no-null
return block > hfBlock! && acc === null ? block : acc;
return block > hfBlock && acc === null ? block : acc;
// eslint-disable-next-line no-null/no-null
}, null);
return nextHfBlock;
Expand Down Expand Up @@ -951,7 +952,7 @@ export class Common extends EventEmitter {
typeof blockOrTime === 'number' &&
blockOrTime !== 0 &&
blockOrTime !== prevBlockOrTime &&
name !== Hardfork.Merge
(name as Hardfork) !== Hardfork.Merge
) {
const hfBlockUint8Array = hexToBytes(blockOrTime.toString(16).padStart(16, '0'));
hfUint8Array = uint8ArrayConcat(hfUint8Array, hfBlockUint8Array);
Expand Down
11 changes: 7 additions & 4 deletions packages/web3-eth-accounts/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const intToHex = function (i: number) {
* before merge like in kiln genesis
* @returns genesis parameters in a `CommonOpts` compliant object
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseGethParams(json: any, mergeForkIdPostMerge = true) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const {
Expand All @@ -83,6 +84,7 @@ function parseGethParams(json: any, mergeForkIdPostMerge = true) {
baseFeePerGas,
}: {
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: any;
difficulty: string;
mixHash: string;
Expand Down Expand Up @@ -194,7 +196,7 @@ function parseGethParams(json: any, mergeForkIdPostMerge = true) {
}, {});
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const configHardforkNames = Object.keys(config).filter(
// eslint-disable-next-line no-null/no-null, @typescript-eslint/no-unsafe-member-access
// eslint-disable-next-line no-null/no-null, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/prefer-optional-chain
key => forkMapRev[key] !== undefined && config[key] !== undefined && config[key] !== null,
);

Expand Down Expand Up @@ -250,7 +252,7 @@ function parseGethParams(json: any, mergeForkIdPostMerge = true) {
// Merge hardfork has to be placed before first hardfork that is dependent on merge
const postMergeIndex = params.hardforks.findIndex(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(hf: any) => forkMap[hf.name]?.postMerge === true,
hf => forkMap[hf.name]?.postMerge === true,
);
if (postMergeIndex !== -1) {
params.hardforks.splice(postMergeIndex, 0, mergeConfig as unknown as ConfigHardfork);
Expand All @@ -272,6 +274,7 @@ function parseGethParams(json: any, mergeForkIdPostMerge = true) {
* @param name optional chain name
* @returns parsed params
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function parseGethGenesis(json: any, name?: string, mergeForkIdPostMerge?: boolean) {
try {
if (['config', 'difficulty', 'gasLimit', 'alloc'].some(field => !(field in json))) {
Expand All @@ -282,9 +285,9 @@ export function parseGethGenesis(json: any, name?: string, mergeForkIdPostMerge?
json.name = name;
}
return parseGethParams(json, mergeForkIdPostMerge);
} catch (e: any) {
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions
throw new Error(`Error parsing parameters file: ${e.message}`);
throw new Error(`Error parsing parameters file: ${(e as { message: string }).message}`);
}
}

Expand Down
14 changes: 8 additions & 6 deletions packages/web3-eth-accounts/src/tx/baseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export abstract class BaseTransaction<TransactionObject> {
// Main signature verification is done in `getSenderPublicKey()`
const publicKey = this.getSenderPublicKey();
return unpadUint8Array(publicKey).length !== 0;
} catch (e: any) {
} catch (e) {
return false;
}
}
Expand Down Expand Up @@ -510,6 +510,7 @@ export abstract class BaseTransaction<TransactionObject> {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected static _validateNotArray(values: { [key: string]: any }) {
const txDataKeys = [
'nonce',
Expand Down Expand Up @@ -556,19 +557,19 @@ export abstract class BaseTransaction<TransactionObject> {
let hash = '';
try {
hash = this.isSigned() ? bytesToHex(this.hash()) : 'not available (unsigned)';
} catch (e: any) {
} catch (e) {
hash = 'error';
}
let isSigned = '';
try {
isSigned = this.isSigned().toString();
} catch (e: any) {
} catch (e) {
hash = 'error';
}
let hf = '';
try {
hf = this.common.hardfork();
} catch (e: any) {
} catch (e) {
hf = 'error';
}

Expand Down Expand Up @@ -603,15 +604,16 @@ export abstract class BaseTransaction<TransactionObject> {
serialized: Uint8Array,
// @ts-expect-error unused variable
opts: TxOptions = {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-explicit-any
): any {}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public static fromTxData(
// @ts-expect-error unused variable
// eslint-disable-next-line @typescript-eslint/no-explicit-any
txData: any,
// @ts-expect-error unused variable
opts: TxOptions = {},
// eslint-disable-next-line @typescript-eslint/no-empty-function
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-explicit-any
): any {}
}
7 changes: 5 additions & 2 deletions packages/web3-eth-accounts/src/tx/eip1559Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ export class FeeMarketEIP1559Transaction extends BaseTransaction<FeeMarketEIP155
throw new Error('Invalid serialized tx input: must be array');
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return FeeMarketEIP1559Transaction.fromValuesArray(values as any, opts);
return FeeMarketEIP1559Transaction.fromValuesArray(
values as FeeMarketEIP1559ValuesArray,
opts,
);
}

/**
Expand Down Expand Up @@ -379,7 +382,7 @@ export class FeeMarketEIP1559Transaction extends BaseTransaction<FeeMarketEIP155
bigIntToUnpaddedUint8Array(r!),
bigIntToUnpaddedUint8Array(s!),
);
} catch (e: any) {
} catch (e) {
const msg = this._errorMsg('Invalid Signature');
throw new Error(msg);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/web3-eth-accounts/src/tx/eip2930Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ export class AccessListEIP2930Transaction extends BaseTransaction<AccessListEIP2
throw new Error('Invalid serialized tx input: must be array');
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return AccessListEIP2930Transaction.fromValuesArray(values as any, opts);
return AccessListEIP2930Transaction.fromValuesArray(
values as AccessListEIP2930ValuesArray,
opts,
);
}

/**
Expand Down Expand Up @@ -337,7 +340,7 @@ export class AccessListEIP2930Transaction extends BaseTransaction<AccessListEIP2
bigIntToUnpaddedUint8Array(r!),
bigIntToUnpaddedUint8Array(s!),
);
} catch (e: any) {
} catch (e) {
const msg = this._errorMsg('Invalid Signature');
throw new Error(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/src/tx/legacyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export class Transaction extends BaseTransaction<Transaction> {
? this.common.chainId()
: undefined,
);
} catch (e: any) {
} catch (e) {
const msg = this._errorMsg('Invalid Signature');
throw new Error(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/src/tx/transactionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const extraTxTypes: Map<Numbers, typeof BaseTransaction<unknown>> = new Map();
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class TransactionFactory {
// It is not possible to instantiate a TransactionFactory object.
// eslint-disable-next-line @typescript-eslint/no-empty-function, no-useless-constructor
// eslint-disable-next-line no-useless-constructor, @typescript-eslint/no-empty-function
private constructor() {}

public static typeToInt(txType: Numbers) {
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/src/tx/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface FeeMarketEIP1559TxData extends AccessListEIP2930TxData {
* The transaction's gas price, inherited from {@link Transaction}. This property is not used for EIP1559
* transactions and should always be undefined for this specific transaction type.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-redundant-type-constituents
gasPrice?: never | null;
/**
* The maximum inclusion fee per gas (this fee is given to the miner)
Expand Down
9 changes: 4 additions & 5 deletions packages/web3-eth-accounts/src/tx/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const verifyAccessList = (accessList: AccessListUint8Array) => {
const address = accessListItem[0];
const storageSlots = accessListItem[1];
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/consistent-type-assertions
if ((<any>accessListItem)[2] !== undefined) {
if ((<Array<unknown>>accessListItem)[2] !== undefined) {
throw new Error(
'Access list item cannot have 3 elements. It can only have an address, and an array of storage slots.',
);
Expand Down Expand Up @@ -115,13 +115,12 @@ export const getAccessListJSON = (
const accessListJSON: { address: HexString; storageKeys: HexString[] }[] = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let index = 0; index < accessList.length; index += 1) {
const item: any = accessList[index];
const item = accessList[index];
const JSONItem: { address: HexString; storageKeys: HexString[] } = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/consistent-type-assertions
address: bytesToHex(setLengthLeft(<Uint8Array>item[0], 20)),
address: bytesToHex(setLengthLeft(item[0], 20)),
storageKeys: [],
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/prefer-optional-chain
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
const storageSlots: Uint8Array[] = item && item[1];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let slot = 0; slot < storageSlots.length; slot += 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe('[Common]: Hardfork logic', () => {
c = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Shanghai });
// unschedule shanghai on it to test
c.hardforks()
.filter(hf => hf.name === Hardfork.Shanghai)
.filter(hf => (hf.name as Hardfork) === Hardfork.Shanghai)
// eslint-disable-next-line array-callback-return
.map(hf => {
// eslint-disable-next-line no-null/no-null, no-param-reassign
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe('[Common]: Merge/POS specific logic', () => {
it('should throw if encounters a double ttd hardfork specification', () => {
const c = new Common({ chain: Chain.Sepolia });
// Add the ttd to mergeForkIdTransition which occurs post merge in sepolia
c.hardforks().filter(hf => hf.name === 'mergeForkIdTransition')[0]!['ttd'] =
c.hardforks().filter(hf => hf.name === 'mergeForkIdTransition')[0]['ttd'] =
'17000000000000000';
expect(() => {
c.setHardforkByBlockNumber(1735371);
Expand Down
Loading

1 comment on commit 7a8df69

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7a8df69 Previous: 984cb7c Ratio
processingTx 22656 ops/sec (±7.04%) 22676 ops/sec (±7.62%) 1.00
processingContractDeploy 41609 ops/sec (±7.26%) 39814 ops/sec (±6.74%) 0.96
processingContractMethodSend 16513 ops/sec (±10.03%) 16632 ops/sec (±6.54%) 1.01
processingContractMethodCall 29019 ops/sec (±6.15%) 28383 ops/sec (±6.50%) 0.98
abiEncode 45433 ops/sec (±7.43%) 43764 ops/sec (±7.68%) 0.96
abiDecode 30564 ops/sec (±8.30%) 30524 ops/sec (±8.07%) 1.00
sign 1555 ops/sec (±3.00%) 1507 ops/sec (±3.41%) 0.97
verify 376 ops/sec (±0.59%) 369 ops/sec (±0.57%) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.