Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync deps, start work on strict ts #278

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"ts-loader": "^9.2.6",
"ts-node": "^10.4.0",
"tsconfig-paths": "^3.12.0",
"typescript": "^5.5.3"
"typescript": "^5.5.4"
},
"resolutions": {
"node-fetch": "2.6.7"
Expand Down
2 changes: 1 addition & 1 deletion packages/common-cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@protobufs/google": "^0.0.10",
"@protobufs/ibc": "^0.1.0",
"@protobufs/tendermint": "^0.0.10",
"@subql/common": "^4.1.1",
"@subql/common": "^5.1.0",
"@subql/types-cosmos": "workspace:*",
"@subql/x-cosmology-telescope": "^1.4.14",
"fs-extra": "^11.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
messages: ['MsgSwapAmountInRoute'],
},
},
];
] as any;

Check warning on line 48 in packages/common-cosmos/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
expect(prepareProtobufRenderProps(mockChainTypes, PROJECT_PATH)).toStrictEqual([
{
messageNames: ['MsgSwapExactAmountIn'],
Expand Down Expand Up @@ -203,7 +203,7 @@
},
],
},
} as any;

Check warning on line 206 in packages/common-cosmos/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

expect(prepareSortedAssets([notCosmosDs], PROJECT_PATH)).toStrictEqual({});
});
Expand Down
64 changes: 35 additions & 29 deletions packages/common-cosmos/src/codegen/codegen-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
datasources
.filter((d) => !!d?.assets && isRuntimeCosmosDs(d))
.forEach((d) => {
if (!d.assets) return;
Object.entries(d.assets).map(([name, value]) => {
const filePath = path.join(projectPath, value.file);
if (!fs.existsSync(filePath)) {
Expand Down Expand Up @@ -168,7 +169,7 @@
);
})
);
} catch (e) {
} catch (e: any) {

Check warning on line 172 in packages/common-cosmos/src/codegen/codegen-controller.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
console.error(
`! Unable to generate from provided cosmwasm interface. ${e.message}\n` +
'Please check the path of your abi path in the project.yaml'
Expand All @@ -183,32 +184,34 @@
if (!chaintypes) {
return [];
}
return chaintypes.filter(Boolean).flatMap((chaintype) => {
return Object.entries(chaintype)
.map(([key, value]) => {
const filePath = path.join(projectPath, value.file);
if (!fs.existsSync(filePath)) {
throw new Error(`Error: chainType ${key}, file ${value.file} does not exist`);
}
if (!isProtoPath(value.file, projectPath)) {
console.error(
`Codegen will not apply for this file: ${value.file} Please ensure it is under the ./proto directory if you want to run codegen on it`
);
}
return chaintypes
.filter((v) => v !== undefined)
.flatMap((chaintype) => {
return Object.entries(chaintype)
.map(([key, value]) => {
const filePath = path.join(projectPath, value.file);
if (!fs.existsSync(filePath)) {
throw new Error(`Error: chainType ${key}, file ${value.file} does not exist`);
}
if (!isProtoPath(value.file, projectPath)) {
console.error(
`Codegen will not apply for this file: ${value.file} Please ensure it is under the ./proto directory if you want to run codegen on it`
);
}

// We only need to generate for RPC messages that are always prefixed with Msg
const messages = value.messages.filter((m: string) => m.indexOf('Msg') === 0);
if (!messages.length) return;
// We only need to generate for RPC messages that are always prefixed with Msg
const messages = value.messages.filter((m: string) => m.indexOf('Msg') === 0);
if (!messages.length) return;

return {
messageNames: messages,
namespace: pathToNamespace(value.file),
name: pathToName(value.file),
path: processProtoFilePath(value.file),
};
})
.filter(Boolean);
});
return {
messageNames: messages,
namespace: pathToNamespace(value.file),
name: pathToName(value.file),
path: processProtoFilePath(value.file),
};
})
.filter((v) => v !== undefined);
});
}

/**
Expand Down Expand Up @@ -244,7 +247,7 @@
renderTemplate: (templatePath: string, outputPath: string, templateData: Data) => Promise<void>,
upperFirst: (string?: string) => string
): Promise<void> {
let tmpPath: string;
let tmpPath = '';
try {
tmpPath = await tempProtoDir(projectPath);
const protobufRenderProps = prepareProtobufRenderProps(chaintypes, projectPath);
Expand All @@ -267,14 +270,16 @@
}
);
console.log('* Cosmos message wrappers generated !');
} catch (e: any) {

Check warning on line 273 in packages/common-cosmos/src/codegen/codegen-controller.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
const errorMessage = e.message.startsWith('Dependency')
? `Please add the missing protobuf file to ./proto directory`
: '';
console.log('ERRROR', e);
throw new Error(`Failed to generate from protobufs. ${e.message}, ${errorMessage}`);
} finally {
fs.rmSync(tmpPath, {recursive: true, force: true});
if (tmpPath !== '') {
fs.rmSync(tmpPath, {recursive: true, force: true});
}
}
}

Expand Down Expand Up @@ -302,9 +307,10 @@
await generateCosmwasm(datasources, projectPath, prepareDirPath, upperFirst, renderTemplate);
}

function getChaintypes(manifest: ProjectManifestV1_0_0[]): Map<string, CustomModule>[] {
function getChaintypes(manifest: ProjectManifestV1_0_0[]): CosmosChainTypeDataType[] {
return manifest
.filter((m) => validateCosmosManifest(m))
.map((m) => (m as CosmosProjectManifestV1_0_0).network.chaintypes)
.filter((value) => value && Object.keys(value).length !== 0);
.filter((value) => value !== undefined)
.filter((value) => Object.keys(value).length !== 0);
}
51 changes: 25 additions & 26 deletions packages/common-cosmos/src/project/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
CosmosMessageHandler,
CustomModule,
CosmosTxFilter,
SubqlCosmosProcessorOptions,
} from '@subql/types-cosmos';
import {plainToClass, Transform, Type} from 'class-transformer';
import {
Expand Down Expand Up @@ -56,7 +55,7 @@

export class MessageFilter extends TxFilter implements CosmosMessageFilter {
@IsString()
type: string;
type!: string;
@IsOptional()
@IsObject()
values?: {[key: string]: string};
Expand All @@ -68,7 +67,7 @@

export class EventFilter implements CosmosEventFilter {
@IsString()
type: string;
type!: string;
@IsOptional()
@Type(() => MessageFilter)
messageFilter?: CosmosMessageFilter;
Expand All @@ -79,26 +78,26 @@

export class BlockHandler implements CosmosBlockHandler {
@IsEnum(CosmosHandlerKind, {groups: [CosmosHandlerKind.Block]})
kind: CosmosHandlerKind.Block;
kind!: CosmosHandlerKind.Block;
@IsString()
handler: string;
handler!: string;
@IsOptional()
@Type(() => BlockFilter)
filter?: CosmosBlockFilter;
}

export class TransactionHandler implements CosmosTransactionHandler {
@IsEnum(CosmosHandlerKind, {groups: [CosmosHandlerKind.Transaction]})
kind: CosmosHandlerKind.Transaction;
kind!: CosmosHandlerKind.Transaction;
@IsString()
handler: string;
handler!: string;
}

export class MessageHandler implements CosmosMessageHandler {
@IsEnum(CosmosHandlerKind, {groups: [CosmosHandlerKind.Message]})
kind: CosmosHandlerKind.Message;
kind!: CosmosHandlerKind.Message;
@IsString()
handler: string;
handler!: string;
@IsOptional()
@ValidateNested()
@Type(() => MessageFilter)
Expand All @@ -111,16 +110,16 @@
@Type(() => EventFilter)
filter?: CosmosEventFilter;
@IsEnum(CosmosHandlerKind, {groups: [CosmosHandlerKind.Event]})
kind: CosmosHandlerKind.Event;
kind!: CosmosHandlerKind.Event;
@IsString()
handler: string;
handler!: string;
}

export class CustomHandler implements CosmosCustomHandler {
@IsString()
kind: string;
kind!: string;
@IsString()
handler: string;
handler!: string;
@IsObject()
@IsOptional()
filter?: Record<string, unknown>;
Expand All @@ -140,24 +139,24 @@
case CosmosHandlerKind.Block:
return plainToClass(BlockHandler, handler);
default:
throw new Error(`handler ${(handler as any).kind} not supported`);

Check warning on line 142 in packages/common-cosmos/src/project/models.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
}
});
})
@IsArray()
@ValidateNested()
handlers: CosmosHandler[];
handlers!: CosmosHandler[];
@IsString()
file: string;
file!: string;
}

export class CustomMapping implements CosmosMapping<CosmosCustomHandler> {
@IsArray()
@Type(() => CustomHandler)
@ValidateNested()
handlers: CosmosCustomHandler[];
handlers!: CosmosCustomHandler[];
@IsString()
file: string;
file!: string;
}

export class CosmosProcessorOptions implements CosmosProcessorOptions {
Expand All @@ -171,10 +170,10 @@
implements CosmosRuntimeDatasource<M>
{
@IsEnum(CosmosDatasourceKind, {groups: [CosmosDatasourceKind.Runtime]})
kind: CosmosDatasourceKind.Runtime;
kind!: CosmosDatasourceKind.Runtime;
@Type(() => RuntimeMapping)
@ValidateNested()
mapping: M;
mapping!: M;
@IsOptional()
@Validate(FileReferenceImp)
assets?: Map<string, FileReference>;
Expand All @@ -186,34 +185,34 @@

export class CosmosFileReferenceImpl implements FileReference {
@IsString()
file: string;
file!: string;
}

export class CosmosCustomModuleImpl implements CustomModule {
@IsString()
file: string;
file!: string;
@IsArray()
@Type(() => String)
messages: string[];
messages!: string[];
}

export class CosmosCustomDataSourceBase<
K extends string,
M extends CosmosMapping = CosmosMapping<CosmosCustomHandler>,
O = any

Check warning on line 202 in packages/common-cosmos/src/project/models.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
>
extends BaseDataSource
implements CosmosCustomDatasource<K, M, O>
{
@IsString()
kind: K;
kind!: K;
@Type(() => CustomMapping)
@ValidateNested()
mapping: M;
mapping!: M;
@Type(() => CosmosFileReferenceImpl)
@ValidateNested({each: true})
assets: Map<string, CustomDataSourceAsset>;
assets!: Map<string, CustomDataSourceAsset>;
@Type(() => ProcessorImpl)
@IsObject()
processor: Processor<O>;
processor!: Processor<O>;
}
2 changes: 1 addition & 1 deletion packages/common-cosmos/src/project/project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@
).toThrow('- property dataSources[0].assets has failed the following constraints: isFileReference');
});
it('Ensure correctness on Cosmos Manifest validate', () => {
const cosmosManifest = loadFromJsonOrYaml(path.join(projectsDir, './protoTest1', 'project.yaml')) as any;

Check warning on line 38 in packages/common-cosmos/src/project/project.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
const ethManifest = loadFromJsonOrYaml(path.join(projectsDir, 'project_1.0.0_bad_runner.yaml')) as any;

Check warning on line 39 in packages/common-cosmos/src/project/project.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
expect(validateCosmosManifest(cosmosManifest)).toBe(true);
expect(validateCosmosManifest(ethManifest)).toBe(false);
});
it('Validate incorrect chaintypes', () => {
const cosmosManifest = loadFromJsonOrYaml(
path.join(projectsDir, './protoTest1', 'bad-chaintypes-project.yaml')
) as any;

Check warning on line 46 in packages/common-cosmos/src/project/project.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
expect(() => parseCosmosProjectManifest(cosmosManifest)).toThrow(
'Failed to parse project. Please see below for more information'
);
});
it('Ensure chaintypes existence on manifest deployment', () => {
const cosmosManifest = loadFromJsonOrYaml(path.join(projectsDir, './protoTest1', 'project.yaml')) as any;

Check warning on line 52 in packages/common-cosmos/src/project/project.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
const manifest = parseCosmosProjectManifest(cosmosManifest);
expect(manifest.asImpl.network.chaintypes.size).toBeGreaterThan(0);
expect(manifest.asImpl.network.chaintypes?.size).toBeGreaterThan(0);
});
});
9 changes: 5 additions & 4 deletions packages/common-cosmos/src/project/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
CosmosRuntimeDatasource,
CustomDatasourceTemplate,
RuntimeDatasourceTemplate,
SecondLayerHandlerProcessorArray,
} from '@subql/types-cosmos';
import {ValidationArguments, ValidatorConstraint, ValidatorConstraintInterface} from 'class-validator';
import {gte} from 'semver';
Expand All @@ -30,25 +31,25 @@ export function isRuntimeCosmosDs(
type DefaultFilter = Record<string, unknown>;

export function isBlockHandlerProcessor<E>(
hp: SecondLayerHandlerProcessor<CosmosHandlerKind, DefaultFilter, unknown>
hp: SecondLayerHandlerProcessorArray<CosmosHandlerKind, DefaultFilter, unknown>
): hp is SecondLayerHandlerProcessor<CosmosHandlerKind.Block, DefaultFilter, E> {
return hp.baseHandlerKind === CosmosHandlerKind.Block;
}

export function isTransactionHandlerProcessor<E>(
hp: SecondLayerHandlerProcessor<CosmosHandlerKind, DefaultFilter, unknown>
hp: SecondLayerHandlerProcessorArray<CosmosHandlerKind, DefaultFilter, unknown>
): hp is SecondLayerHandlerProcessor<CosmosHandlerKind.Transaction, DefaultFilter, E> {
return hp.baseHandlerKind === CosmosHandlerKind.Transaction;
}

export function isMessageHandlerProcessor<E>(
hp: SecondLayerHandlerProcessor<CosmosHandlerKind, DefaultFilter, unknown>
hp: SecondLayerHandlerProcessorArray<CosmosHandlerKind, DefaultFilter, unknown>
): hp is SecondLayerHandlerProcessor<CosmosHandlerKind.Message, DefaultFilter, E> {
return hp.baseHandlerKind === CosmosHandlerKind.Message;
}

export function isEventHandlerProcessor<E>(
hp: SecondLayerHandlerProcessor<CosmosHandlerKind, DefaultFilter, unknown>
hp: SecondLayerHandlerProcessorArray<CosmosHandlerKind, DefaultFilter, unknown>
): hp is SecondLayerHandlerProcessor<CosmosHandlerKind.Event, DefaultFilter, E> {
return hp.baseHandlerKind === CosmosHandlerKind.Event;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class CosmosProjectManifestVersioned implements ICosmosProjectManifest {
return this._impl as ProjectManifestV1_0_0Impl;
}

toDeployment(): string | undefined {
toDeployment(): string {
return this._impl.deployment.toYaml();
}

Expand All @@ -63,11 +63,11 @@ export class CosmosProjectManifestVersioned implements ICosmosProjectManifest {
return this._impl.specVersion;
}

get description(): string {
get description(): string | undefined {
return this._impl.description;
}

get repository(): string {
get repository(): string | undefined {
return this._impl.repository;
}
}
Loading
Loading