From feccd598257cbc7255049333cc5b670eca556b6c Mon Sep 17 00:00:00 2001 From: JQQQ Date: Mon, 24 Jun 2024 16:53:14 +1200 Subject: [PATCH] cosmos modular --- packages/common-cosmos/CHANGELOG.md | 3 ++ packages/common-cosmos/package.json | 2 +- .../src/codegen/codegen-controller.ts | 33 +++++++++++++++++-- packages/common-cosmos/src/index.ts | 11 +++++++ packages/common-cosmos/src/project/index.ts | 5 +++ packages/types/CHANGELOG.md | 3 ++ packages/types/package.json | 2 +- packages/types/src/index.ts | 1 + packages/types/src/modular.ts | 17 ++++++++++ yarn.lock | 31 +++++++++++++++-- 10 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 packages/types/src/modular.ts diff --git a/packages/common-cosmos/CHANGELOG.md b/packages/common-cosmos/CHANGELOG.md index fe2dde412..8908e28b3 100644 --- a/packages/common-cosmos/CHANGELOG.md +++ b/packages/common-cosmos/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Add alias follow type of `CosmosNetworkModule`. Also add method `networkCliCodegen` include steps of cosmos codegen. (#267) + ## [4.4.0] - 2024-06-21 ### Changed - Add default value in model class to follow ES2022 rule (#264) diff --git a/packages/common-cosmos/package.json b/packages/common-cosmos/package.json index 3b7fd83eb..5735e6988 100644 --- a/packages/common-cosmos/package.json +++ b/packages/common-cosmos/package.json @@ -22,7 +22,7 @@ "@protobufs/google": "^0.0.10", "@protobufs/ibc": "^0.1.0", "@protobufs/tendermint": "^0.0.10", - "@subql/common": "^3.8.0", + "@subql/common": "^3.9.1-0", "@subql/types-cosmos": "workspace:*", "@subql/x-cosmology-telescope": "^1.4.14", "fs-extra": "^11.1.1", diff --git a/packages/common-cosmos/src/codegen/codegen-controller.ts b/packages/common-cosmos/src/codegen/codegen-controller.ts index 2c34127de..a28c06657 100644 --- a/packages/common-cosmos/src/codegen/codegen-controller.ts +++ b/packages/common-cosmos/src/codegen/codegen-controller.ts @@ -5,7 +5,13 @@ import fs from 'fs'; import path from 'path'; import cosmwasmCodegen from '@cosmwasm/ts-codegen'; import {makeTempDir} from '@subql/common'; -import {CosmosChaintypes, CustomModule, CosmosRuntimeDatasource} from '@subql/types-cosmos'; +import {ProjectManifestV1_0_0} from '@subql/types-core/dist/project/versioned/v1_0_0/types'; +import { + CosmosChaintypes, + CustomModule, + CosmosRuntimeDatasource, + CosmosProjectManifestV1_0_0, +} from '@subql/types-cosmos'; import telescope from '@subql/x-cosmology-telescope'; import {Data} from 'ejs'; import {copySync} from 'fs-extra'; @@ -13,7 +19,7 @@ import {upperFirst} from 'lodash'; import {IDLObject} from 'wasm-ast-types'; import {isRuntimeCosmosDs} from '../project'; import {COSMWASM_OPTS, TELESCOPE_OPTS} from './constants'; -import {loadCosmwasmAbis, tmpProtoDir} from './util'; +import {loadCosmwasmAbis, tmpProtoDir, validateCosmosManifest} from './util'; const TYPE_ROOT_DIR = 'src/types'; @@ -273,3 +279,26 @@ export async function generateProto( fs.rmSync(tmpPath, {recursive: true, force: true}); } } + +export async function networkCliCodegen( + manifest: ProjectManifestV1_0_0[], + projectPath: string, + prepareDirPath: (path: string, recreate: boolean) => Promise, + renderTemplate: (templatePath: string, outputPath: string, templateData: Data) => Promise, + upperFirst: (string?: string) => string, + datasources: CosmosRuntimeDatasource[] +): Promise { + const chainTypes = getChaintypes(manifest); + if (chainTypes.length) { + await generateProto(chainTypes, projectPath, prepareDirPath, renderTemplate, upperFirst, tempProtoDir); + } + await generateCosmwasm(datasources, projectPath, prepareDirPath, upperFirst, renderTemplate); +} + +//Deprecated export in future version +export function getChaintypes(manifest: ProjectManifestV1_0_0[]): Map[] { + return manifest + .filter((m) => validateCosmosManifest(m)) + .map((m) => (m as CosmosProjectManifestV1_0_0).network.chaintypes) + .filter((value) => value && Object.keys(value).length !== 0); +} diff --git a/packages/common-cosmos/src/index.ts b/packages/common-cosmos/src/index.ts index 0b20c0933..1059e1aaf 100644 --- a/packages/common-cosmos/src/index.ts +++ b/packages/common-cosmos/src/index.ts @@ -3,3 +3,14 @@ export * from './codegen'; export * from './project'; + +import {CosmosNetworkModule} from '@subql/types-cosmos'; +import * as c from './codegen'; +import * as p from './project'; + +// This provides a compiled time check to ensure that the correct exports are provided +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _ = { + ...p, + ...c, +} satisfies CosmosNetworkModule; diff --git a/packages/common-cosmos/src/project/index.ts b/packages/common-cosmos/src/project/index.ts index 2eaa386f1..ae5c5db1a 100644 --- a/packages/common-cosmos/src/project/index.ts +++ b/packages/common-cosmos/src/project/index.ts @@ -6,3 +6,8 @@ export * from './models'; export * from './types'; export * from './utils'; export * from './versioned'; + +import {parseCosmosProjectManifest} from './load'; +export {parseCosmosProjectManifest as parseProjectManifest}; +import {isRuntimeCosmosDs, isCustomCosmosDs} from './utils'; +export {isRuntimeCosmosDs as isRuntimeDs, isCustomCosmosDs as isCustomDs}; diff --git a/packages/types/CHANGELOG.md b/packages/types/CHANGELOG.md index 3046c889f..63e38a4a8 100644 --- a/packages/types/CHANGELOG.md +++ b/packages/types/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Add type `CosmosNetworkModule` to support cosmos module (#267) + ## [3.4.0] - 2024-05-02 ### Changed - Update `@subql/types-core` and use types from there (#254) diff --git a/packages/types/package.json b/packages/types/package.json index 0b73d17e9..5084cf57e 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -19,6 +19,6 @@ "@cosmjs/cosmwasm-stargate": "^0.32.3", "@cosmjs/proto-signing": "^0.32.3", "@cosmjs/stargate": "^0.32.3", - "@subql/types-core": "^0.7.0" + "@subql/types-core": "^0.8.1-1" } } diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 8d163673f..0666c12c5 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -3,3 +3,4 @@ export * from './interfaces'; export * from './project'; +export * from './modular'; diff --git a/packages/types/src/modular.ts b/packages/types/src/modular.ts new file mode 100644 index 000000000..956f87f75 --- /dev/null +++ b/packages/types/src/modular.ts @@ -0,0 +1,17 @@ +// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors +// SPDX-License-Identifier: GPL-3.0 + +import {INetworkCommonModule, ProjectManifestV1_0_0} from '@subql/types-core'; +import {Data} from 'ejs'; +import {CosmosRuntimeDatasource} from './project'; + +export interface CosmosNetworkModule extends INetworkCommonModule { + networkCliCodegen( + manifest: ProjectManifestV1_0_0[], + projectPath: string, + prepareDirPath: (path: string, recreate: boolean) => Promise, + renderTemplate: (templatePath: string, outputPath: string, templateData: Data) => Promise, + upperFirst: (string?: string) => string, + datasources: CosmosRuntimeDatasource[] + ): Promise; +} diff --git a/yarn.lock b/yarn.lock index cdaa2f224..1e6882b86 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6268,7 +6268,7 @@ __metadata: "@protobufs/google": ^0.0.10 "@protobufs/ibc": ^0.1.0 "@protobufs/tendermint": ^0.0.10 - "@subql/common": ^3.8.0 + "@subql/common": ^3.9.1-0 "@subql/types-cosmos": "workspace:*" "@subql/x-cosmology-telescope": ^1.4.14 "@types/bn.js": 4.11.6 @@ -6309,6 +6309,24 @@ __metadata: languageName: node linkType: hard +"@subql/common@npm:^3.9.1-0": + version: 3.9.1-0 + resolution: "@subql/common@npm:3.9.1-0" + dependencies: + "@subql/types-core": 0.8.1-1 + axios: ^0.28.0 + class-transformer: ^0.5.1 + class-validator: ^0.14.0 + fs-extra: ^10.1.0 + ipfs-http-client: 56 + js-yaml: ^4.1.0 + reflect-metadata: ^0.1.13 + semver: ^7.5.2 + update-notifier: 5.1.0 + checksum: db23560ee596b590c0a497708c55a8f142af085ad61a42b636624f883b8557e30b0f59cfb8b40870899651df02548ccae6a99778cb303c38a66ad31a496cbd9e + languageName: node + linkType: hard + "@subql/node-core@npm:^10.6.0": version: 10.6.0 resolution: "@subql/node-core@npm:10.6.0" @@ -6400,6 +6418,15 @@ __metadata: languageName: node linkType: hard +"@subql/types-core@npm:0.8.1-1, @subql/types-core@npm:^0.8.1-1": + version: 0.8.1-1 + resolution: "@subql/types-core@npm:0.8.1-1" + dependencies: + package-json-type: ^1.0.3 + checksum: 403ddeb1f3902a3256b67523a297cbadc5b964e07f2c94631554f76c7b14d1780445db6164b01f87113e5e21e4b497ae7f1fb1578d7e508405efc535c92ef1a3 + languageName: node + linkType: hard + "@subql/types-cosmos@workspace:*, @subql/types-cosmos@workspace:packages/types": version: 0.0.0-use.local resolution: "@subql/types-cosmos@workspace:packages/types" @@ -6407,7 +6434,7 @@ __metadata: "@cosmjs/cosmwasm-stargate": ^0.32.3 "@cosmjs/proto-signing": ^0.32.3 "@cosmjs/stargate": ^0.32.3 - "@subql/types-core": ^0.7.0 + "@subql/types-core": ^0.8.1-1 languageName: unknown linkType: soft