Skip to content

Commit

Permalink
Enable noImplicitAny on node and fix any related errors (#2477)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname authored Jul 9, 2024
1 parent 233cb5d commit ad0a496
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('SchemaMigration integration tests', () => {
await apiService.init();
await projectService.init(1);

const dbResults = await sequelize.query(
const dbResults = await sequelize.query<string[]>(
`SELECT table_name FROM information_schema.tables WHERE table_schema= :schema;`,
{ type: QueryTypes.SELECT, replacements: { schema: schemaName } },
);
Expand Down
38 changes: 26 additions & 12 deletions packages/node/src/indexer/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import assert from 'assert';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { ApiPromise } from '@polkadot/api';
import { RpcMethodResult } from '@polkadot/api/types';
import {
ApiTypes,
DecoratedRpcSection,
RpcMethodResult,
} from '@polkadot/api/types';
import { RuntimeVersion, Header } from '@polkadot/types/interfaces';
import {
AnyFunction,
Expand All @@ -24,6 +28,7 @@ import {
MetadataMismatchError,
exitWithError,
} from '@subql/node-core';
import { SubstrateNetworkConfig } from '@subql/types';
import { SubstrateNodeConfig } from '../configure/NodeConfig';
import { SubqueryProject } from '../configure/SubqueryProject';
import { isOnlyEventHandlers } from '../utils/project';
Expand Down Expand Up @@ -65,8 +70,9 @@ async function dynamicImportHasher(
methodName: string,
): Promise<(data: Uint8Array) => Uint8Array> {
const module = await import('@polkadot/util-crypto');
if (module[methodName]) {
return module[methodName];
const method = module[methodName as keyof typeof module];
if (method) {
return method as (data: Uint8Array) => Uint8Array;
} else {
throw new Error(
`Hasher Method ${methodName} not found in @polkadot/util-crypto`,
Expand Down Expand Up @@ -155,13 +161,16 @@ export class ApiService

async init(): Promise<ApiService> {
overrideConsoleWarn();
let chainTypes, network;
let chainTypes: RegisteredTypes | undefined;
let network: SubstrateNetworkConfig;
try {
chainTypes = await updateChainTypesHasher(this.project.chainTypes);
network = this.project.network;

if (this.nodeConfig.primaryNetworkEndpoint) {
network.endpoint.push(this.nodeConfig.primaryNetworkEndpoint);
(network.endpoint as string[]).push(
this.nodeConfig.primaryNetworkEndpoint,
);
}
} catch (e) {
exitWithError(new Error(`Failed to init api`, { cause: e }), logger);
Expand Down Expand Up @@ -261,7 +270,7 @@ export class ApiService
return apiAt;
}

private redecorateRpcFunction<T extends 'promise' | 'rxjs'>(
private redecorateRpcFunction<T extends ApiTypes>(
original: RpcMethodResult<T, AnyFunction>,
): RpcMethodResult<T, AnyFunction> {
const methodName = this.getRPCFunctionName(original);
Expand Down Expand Up @@ -319,18 +328,23 @@ export class ApiService
return ret;
}

private patchApiRpc(api: ApiPromise, apiAt: ApiAt): void {
private patchApiRpc<T extends ApiTypes = 'promise'>(
api: ApiPromise,
apiAt: ApiAt,
): void {
apiAt.rpc = Object.entries(api.rpc).reduce(
(acc, [module, rpcMethods]) => {
acc[module] = Object.entries(rpcMethods).reduce(
acc[module as keyof ApiPromise['rpc']] = Object.entries(
rpcMethods,
).reduce(
(accInner, [name, rpcPromiseResult]) => {
accInner[name] = this.redecorateRpcFunction(
rpcPromiseResult as RpcMethodResult<any, AnyFunction>,
accInner[name] = this.redecorateRpcFunction<T>(
rpcPromiseResult as RpcMethodResult<T, AnyFunction>,
);
return accInner;
},
{},
);
{} as DecoratedRpcSection<T, any>,
) as any;
return acc;
},
{} as ApiPromise['rpc'],
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/indexer/apiPromise.connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ApiPromiseConnection
static async create(
endpoint: string,
fetchBlocksBatches: GetFetchFunc,
args: { chainTypes: RegisteredTypes },
args: { chainTypes?: RegisteredTypes },
): Promise<ApiPromiseConnection> {
let provider: ProviderInterface;
let throwOnConnect = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('Substrate Dictionary service', function () {
expect(spyDictionaryV1Create).toHaveBeenCalledTimes(1);
expect(
(dictionaryService as any)._dictionaries.every(
(d) => d instanceof SubstrateDictionaryV1,
(d: any) => d instanceof SubstrateDictionaryV1,
),
).toBeTruthy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function callFilterToQueryEntry(
(key) =>
({
field: key === 'method' ? 'call' : key,
value: filter[key],
value: filter[key as keyof SubstrateCallFilter],
}) as DictionaryQueryCondition,
)
.filter((c) => c.value !== undefined),
Expand Down
6 changes: 5 additions & 1 deletion packages/node/src/indexer/fetch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export class FetchService extends BaseFetchService<
);
}

protected async preLoopHook({ startHeight }): Promise<void> {
protected async preLoopHook({
startHeight,
}: {
startHeight: number;
}): Promise<void> {
this.runtimeService.init(this.getLatestFinalizedHeight.bind(this));

await this.runtimeService.syncDictionarySpecVersions(startHeight);
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/indexer/store.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ WHERE

tempDir = (projectService as any).project.root;

const result: any = await sequelize.query(
const result = await sequelize.query<{ enum_type: string }>(
`
SELECT n.nspname AS schema_name,
t.typname AS enum_type,
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/indexer/worker/worker.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import assert from 'assert';
import { Inject, Injectable } from '@nestjs/common';
import {
NodeConfig,
Expand Down Expand Up @@ -42,7 +41,7 @@ export class WorkerService extends BaseWorkerService<

protected async fetchChainBlock(
height: number,
{ specVersion },
{ specVersion }: { specVersion: number },
): Promise<IBlock<BlockContent | LightBlockContent>> {
const specChanged = await this.workerRuntimeService.specChanged(
height,
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/indexer/x-provider/cachedProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export function createCachedProvider(
const cachedMethodHandler = (
method: string,
params: unknown[],
target,
args,
target: any,
args: any[],
) => {
// If there are no parameters then we don't cache as we want the latest results
if (!params.length) {
Expand Down Expand Up @@ -53,7 +53,7 @@ export function createCachedProvider(
method,
params,
target.send.bind(target),
arguments,
arguments as unknown as any[],
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"sourceMap": true,
"tsBuildInfoFile": "dist/.tsbuildinfo",
"rootDir": "src",
"outDir": "./dist"
"outDir": "./dist",
"noImplicitAny": true
},
"references": [
{ "path": "../common" },
Expand Down

0 comments on commit ad0a496

Please sign in to comment.