Skip to content

Commit

Permalink
Sync deps, start work on strict ts (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname authored Aug 12, 2024
1 parent 010f71c commit e77a555
Show file tree
Hide file tree
Showing 23 changed files with 509 additions and 302 deletions.
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 @@ describe('Codegen cosmos', () => {
messages: ['MsgSwapAmountInRoute'],
},
},
];
] as any;
expect(prepareProtobufRenderProps(mockChainTypes, PROJECT_PATH)).toStrictEqual([
{
messageNames: ['MsgSwapExactAmountIn'],
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 @@ export function prepareSortedAssets(
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 @@ export async function generateCosmwasm(
);
})
);
} catch (e) {
} catch (e: any) {
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 @@ export function prepareProtobufRenderProps(
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 @@ export async function generateProto(
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 Down Expand Up @@ -274,7 +277,9 @@ export async function generateProto(
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 @@ export async function projectCodegen(
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 @@ import {
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 TxFilter implements CosmosTxFilter {

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 MessageFilter extends TxFilter implements CosmosMessageFilter {

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

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 @@ export class EventHandler implements CosmosEventHandler {
@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 @@ -146,18 +145,18 @@ export class RuntimeMapping implements CosmosMapping {
})
@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 @@ export class CosmosRuntimeDataSourceBase<M extends CosmosMapping<CosmosRuntimeHa
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,15 +185,15 @@ export class CosmosRuntimeDataSourceBase<M extends CosmosMapping<CosmosRuntimeHa

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<
Expand All @@ -206,14 +205,14 @@ export class CosmosCustomDataSourceBase<
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 @@ -51,6 +51,6 @@ describe('project.yaml', () => {
it('Ensure chaintypes existence on manifest deployment', () => {
const cosmosManifest = loadFromJsonOrYaml(path.join(projectsDir, './protoTest1', 'project.yaml')) as any;
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

0 comments on commit e77a555

Please sign in to comment.