From c44c022630ec6e4d9cd6d8176ea1242c5701fd8a Mon Sep 17 00:00:00 2001 From: Gefei Hou Date: Tue, 10 Dec 2024 11:20:49 +0800 Subject: [PATCH] Reorganization of client style option with all, sdk-module, custom --- README.md | 67 ++++--- .../akash-sdk-module-client.ts | 81 +++++++++ .../outputinstantrpc/all-module-client.ts | 168 ++++++++++++++++++ .../cosmos-sdk-module-client.ts | 94 ++++++++++ .../v-next/outputinstantrpc/custom-client.ts | 69 +++++++ __fixtures__/v-next/outputinstantrpc/index.ts | 11 +- .../osmosis-sdk-module-client.ts | 81 +++++++++ packages/telescope/README.md | 67 ++++--- .../__tests__/telescope-instant-rpc.test.ts | 50 +++--- packages/telescope/src/builder.ts | 44 +++-- .../generators/create-all-stargate-clients.ts | 121 +++++++++++++ ...s.ts => create-custom-stargate-clients.ts} | 4 +- .../telescope/src/generators/create-index.ts | 1 - .../create-sdk-module-stargate-clients.ts | 120 +++++++++++++ .../src/generators/create-stargate-clients.ts | 1 - .../create-all-stargate-clients.d.ts | 3 + .../create-custom-stargate-clients.d.ts | 3 + .../create-scoped-stargate-clients.d.ts | 3 + .../create-sdk-module-stargate-clients.d.ts | 3 + .../create-unified-stargate-clients.d.ts | 3 + packages/types/src/telescope.ts | 36 ++-- packages/types/types/telescope.d.ts | 19 +- 22 files changed, 913 insertions(+), 136 deletions(-) create mode 100644 __fixtures__/v-next/outputinstantrpc/akash-sdk-module-client.ts create mode 100644 __fixtures__/v-next/outputinstantrpc/all-module-client.ts create mode 100644 __fixtures__/v-next/outputinstantrpc/cosmos-sdk-module-client.ts create mode 100644 __fixtures__/v-next/outputinstantrpc/custom-client.ts create mode 100644 __fixtures__/v-next/outputinstantrpc/osmosis-sdk-module-client.ts create mode 100644 packages/telescope/src/generators/create-all-stargate-clients.ts rename packages/telescope/src/generators/{create-combined-stargate-clients.ts => create-custom-stargate-clients.ts} (97%) create mode 100644 packages/telescope/src/generators/create-sdk-module-stargate-clients.ts create mode 100644 packages/telescope/types/generators/create-all-stargate-clients.d.ts create mode 100644 packages/telescope/types/generators/create-custom-stargate-clients.d.ts create mode 100644 packages/telescope/types/generators/create-scoped-stargate-clients.d.ts create mode 100644 packages/telescope/types/generators/create-sdk-module-stargate-clients.d.ts create mode 100644 packages/telescope/types/generators/create-unified-stargate-clients.d.ts diff --git a/README.md b/README.md index 9e9efbdd8d..3b0927f632 100644 --- a/README.md +++ b/README.md @@ -443,9 +443,11 @@ See [LCD Clients](#lcd-clients) for more info. | `rpcClients.enabledServices` | which services to enable | [`Msg`,`Query`,`Service`] | | `rpcClients.instantOps` | will generate instant rpc operations in the file `service-ops.ts` under root folder, which contains customized classes having selected rpc methods | `undefined` | | `rpcClients.serviceImplement` | assign implement type of rpc methods, `Query` or `Tx`, by setting patterns under service types. | `undefined` | - `rpcClients.combinedClient.name` | assign the client name like `{name}AminoConverters`, `get{name}SigningClient` etc | `undefined` -| `rpcClients.combinedClient.fileName` | assign the file name of generated client in root directory | `undefined` -| `rpcClients.combinedClient.include.patterns` | determine which proto files will be imported for the current client such as `cosmos.gov.v1beta1.**` | `undefined` + `rpcClients.clientStyle.useUpdatedClientStyle` | The default value is `false`, which sets the generated client to use the legacy style. Setting it to `true` applies the updated style and activates the remaining options in clientStyle. | `false` +| `rpcClients.clientStyle.type` | A string array containing possible values: `all-client`, `sdk-module-client`, and `custom-client`. The value `all-client` generates an all-module-client file. The value `sdk-module-client` generates a client for the module specified by the `sdkModuleClientOption`. The value `custom-client` generates a customized client as specified by `customClientOption` | `undefined` +`rpcClients.clientStyle.customClientOption.name` | assign the client name like `{name}AminoConverters`, `get{name}SigningClient` etc | `undefined` +| `rpcClients.clientStyle.customClientOption.fileName` | assign the file name of generated client in root directory | `undefined` +| `rpcClients.clientStyle.customClientOption.include.patterns` | determine which proto files will be imported for the current client such as `cosmos.gov.v1beta1.**` | `undefined` See [RPC Clients](#rpc-clients) for more info. @@ -1063,44 +1065,41 @@ export class CosmosAuthAccount { } ``` -## Combined Clients Methods +## Client Style Methods -Using combined option to generate multiple module clients +Use client style to define the client file generated according to the config For example, for this config: ```js -{ - combinedClient: [ - { - name: "CosmosIbc", - fileName: "cosmos-ibc-client.ts", - include: { - patterns: [ - "cosmos.gov.v1beta1*", - "cosmos.gov.v1*", - "ibc.core.channel.*", - ], - }, - }, - { - name: "AkashCosmos", - fileName: "akash-cosmos-client.ts", - include: { - patterns: [ - "cosmos.group.v1*", - "cosmos.nft.v1beta1*", - "akash.**.v1beta2", - "akash.audit.v1beta1*", - ], +clientStyle: { + useUpdatedClientStyle: true, + type: ['all-client', 'sdk-module-client', 'custom-client'], + customClientOption: [ + { + name: "custom", + fileName: "custom-client.ts", + include: { + patterns: [ + "cosmos.gov.v1beta1*", + "cosmos.gov.v1*", + "ibc.core.channel.*", + ], + }, }, - }, - ], -} + ], + sdkModuleClientOption: [ + 'akash', + 'osmosis', + 'cosmos', + ], + }, ``` -There'll be client files (`cosmos-ibc-client.ts`, `akash-cosmos-client.ts`) generated in the root folder according to fileName in the setting. -The combined client will import proto files accroding to include.patterns. (The protos can come from different modules) -For example the cosmos-ibc-client.ts will be like: +There'll be client files (`all-module-client.ts`, `akash-sdk-module-client.ts`, `osmosis-sdk-module-client.ts`, `cosmos-sdk-module-client.ts`, `custom-client.ts`) generated in the root directory according to the setting.
+The `all-module-client.ts` file consolidates all proto imports into one file and exports them as a single client.
+All sdk module client files will be identical to the legacy `client.ts` files generated in each module directory, except they will be located in the root directory.
+The custom client imports proto files based on `include.patterns`, allowing protos to originate from different modules.
+For example the custom-client.ts will be like: ```ts export const cosmosIbcAminoConverters = { ...cosmosGovV1TxAmino.AminoConverter, diff --git a/__fixtures__/v-next/outputinstantrpc/akash-sdk-module-client.ts b/__fixtures__/v-next/outputinstantrpc/akash-sdk-module-client.ts new file mode 100644 index 0000000000..503b57a67f --- /dev/null +++ b/__fixtures__/v-next/outputinstantrpc/akash-sdk-module-client.ts @@ -0,0 +1,81 @@ +import { GeneratedType, Registry, OfflineSigner } from "@cosmjs/proto-signing"; +import { defaultRegistryTypes, AminoTypes, SigningStargateClient } from "@cosmjs/stargate"; +import { HttpEndpoint } from "@cosmjs/tendermint-rpc"; +import { createRpcClient } from "./extern"; +import { DeliverTxResponse, EncodeObject, StdFee, TxRpc, SigningClientParams } from "./types"; +import * as akashAuditV1beta1AuditRegistry from "./akash/audit/v1beta1/audit.registry"; +import * as akashAuditV1beta2AuditRegistry from "./akash/audit/v1beta2/audit.registry"; +import * as akashCertV1beta2CertRegistry from "./akash/cert/v1beta2/cert.registry"; +import * as akashDeploymentV1beta2ServiceRegistry from "./akash/deployment/v1beta2/service.registry"; +import * as akashMarketV1beta2ServiceRegistry from "./akash/market/v1beta2/service.registry"; +import * as akashProviderV1beta1ProviderRegistry from "./akash/provider/v1beta1/provider.registry"; +import * as akashProviderV1beta2ProviderRegistry from "./akash/provider/v1beta2/provider.registry"; +import * as akashAuditV1beta1AuditAmino from "./akash/audit/v1beta1/audit.amino"; +import * as akashAuditV1beta2AuditAmino from "./akash/audit/v1beta2/audit.amino"; +import * as akashCertV1beta2CertAmino from "./akash/cert/v1beta2/cert.amino"; +import * as akashDeploymentV1beta2ServiceAmino from "./akash/deployment/v1beta2/service.amino"; +import * as akashMarketV1beta2ServiceAmino from "./akash/market/v1beta2/service.amino"; +import * as akashProviderV1beta1ProviderAmino from "./akash/provider/v1beta1/provider.amino"; +import * as akashProviderV1beta2ProviderAmino from "./akash/provider/v1beta2/provider.amino"; +export const akashAminoConverters = { + ...akashAuditV1beta1AuditAmino.AminoConverter, + ...akashAuditV1beta2AuditAmino.AminoConverter, + ...akashCertV1beta2CertAmino.AminoConverter, + ...akashDeploymentV1beta2ServiceAmino.AminoConverter, + ...akashMarketV1beta2ServiceAmino.AminoConverter, + ...akashProviderV1beta1ProviderAmino.AminoConverter, + ...akashProviderV1beta2ProviderAmino.AminoConverter +}; +export const akashProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...akashAuditV1beta1AuditRegistry.registry, ...akashAuditV1beta2AuditRegistry.registry, ...akashCertV1beta2CertRegistry.registry, ...akashDeploymentV1beta2ServiceRegistry.registry, ...akashMarketV1beta2ServiceRegistry.registry, ...akashProviderV1beta1ProviderRegistry.registry, ...akashProviderV1beta2ProviderRegistry.registry]; +export const getSigningAkashClientOptions = ({ + defaultTypes = defaultRegistryTypes +}: { + defaultTypes?: ReadonlyArray<[string, GeneratedType]>; +} = {}): { + registry: Registry; + aminoTypes: AminoTypes; +} => { + const registry = new Registry([...defaultTypes, ...akashProtoRegistry]); + const aminoTypes = new AminoTypes({ + ...akashAminoConverters + }); + return { + registry, + aminoTypes + }; +}; +export const getSigningAkashClient = async ({ + rpcEndpoint, + signer, + defaultTypes = defaultRegistryTypes +}: { + rpcEndpoint: string | HttpEndpoint; + signer: OfflineSigner; + defaultTypes?: ReadonlyArray<[string, GeneratedType]>; +}) => { + const { + registry, + aminoTypes + } = getSigningAkashClientOptions({ + defaultTypes + }); + const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, { + registry: (registry as any), + aminoTypes + }); + return client; +}; +export const getSigningAkashTxRpc = async ({ + rpcEndpoint, + signer +}: SigningClientParams) => { + let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc); + const signingClient = await getSigningAkashClient({ + rpcEndpoint, + signer + }); + txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => { + return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise); + }; + return txRpc; +}; \ No newline at end of file diff --git a/__fixtures__/v-next/outputinstantrpc/all-module-client.ts b/__fixtures__/v-next/outputinstantrpc/all-module-client.ts new file mode 100644 index 0000000000..f30b417031 --- /dev/null +++ b/__fixtures__/v-next/outputinstantrpc/all-module-client.ts @@ -0,0 +1,168 @@ +import { GeneratedType, Registry, OfflineSigner } from "@cosmjs/proto-signing"; +import { defaultRegistryTypes, AminoTypes, SigningStargateClient } from "@cosmjs/stargate"; +import { HttpEndpoint } from "@cosmjs/tendermint-rpc"; +import { createRpcClient } from "./extern"; +import { DeliverTxResponse, EncodeObject, StdFee, TxRpc, SigningClientParams } from "./types"; +import * as akashAuditV1beta1AuditRegistry from "./akash/audit/v1beta1/audit.registry"; +import * as akashAuditV1beta2AuditRegistry from "./akash/audit/v1beta2/audit.registry"; +import * as akashCertV1beta2CertRegistry from "./akash/cert/v1beta2/cert.registry"; +import * as akashDeploymentV1beta2ServiceRegistry from "./akash/deployment/v1beta2/service.registry"; +import * as akashMarketV1beta2ServiceRegistry from "./akash/market/v1beta2/service.registry"; +import * as akashProviderV1beta1ProviderRegistry from "./akash/provider/v1beta1/provider.registry"; +import * as akashProviderV1beta2ProviderRegistry from "./akash/provider/v1beta2/provider.registry"; +import * as cosmosAuthzV1beta1TxRegistry from "./cosmos/authz/v1beta1/tx.registry"; +import * as cosmosBankV1beta1TxRegistry from "./cosmos/bank/v1beta1/tx.registry"; +import * as cosmosCrisisV1beta1TxRegistry from "./cosmos/crisis/v1beta1/tx.registry"; +import * as cosmosDistributionV1beta1TxRegistry from "./cosmos/distribution/v1beta1/tx.registry"; +import * as cosmosEvidenceV1beta1TxRegistry from "./cosmos/evidence/v1beta1/tx.registry"; +import * as cosmosFeegrantV1beta1TxRegistry from "./cosmos/feegrant/v1beta1/tx.registry"; +import * as cosmosGovV1TxRegistry from "./cosmos/gov/v1/tx.registry"; +import * as cosmosGovV1beta1TxRegistry from "./cosmos/gov/v1beta1/tx.registry"; +import * as cosmosGroupV1TxRegistry from "./cosmos/group/v1/tx.registry"; +import * as cosmosNftV1beta1TxRegistry from "./cosmos/nft/v1beta1/tx.registry"; +import * as cosmosSlashingV1beta1TxRegistry from "./cosmos/slashing/v1beta1/tx.registry"; +import * as cosmosStakingV1beta1TxRegistry from "./cosmos/staking/v1beta1/tx.registry"; +import * as cosmosUpgradeV1beta1TxRegistry from "./cosmos/upgrade/v1beta1/tx.registry"; +import * as cosmosVestingV1beta1TxRegistry from "./cosmos/vesting/v1beta1/tx.registry"; +import * as cosmwasmWasmV1TxRegistry from "./cosmwasm/wasm/v1/tx.registry"; +import * as evmosErc20V1TxRegistry from "./evmos/erc20/v1/tx.registry"; +import * as evmosFeesV1TxRegistry from "./evmos/fees/v1/tx.registry"; +import * as evmosVestingV1TxRegistry from "./evmos/vesting/v1/tx.registry"; +import * as ibcApplicationsTransferV1TxRegistry from "./ibc/applications/transfer/v1/tx.registry"; +import * as ibcCoreChannelV1TxRegistry from "./ibc/core/channel/v1/tx.registry"; +import * as ibcCoreClientV1TxRegistry from "./ibc/core/client/v1/tx.registry"; +import * as ibcCoreConnectionV1TxRegistry from "./ibc/core/connection/v1/tx.registry"; +import * as osmosisGammPoolmodelsBalancerTxTxRegistry from "./osmosis/gamm/pool-models/balancer/tx/tx.registry"; +import * as osmosisGammPoolmodelsStableswapTxRegistry from "./osmosis/gamm/pool-models/stableswap/tx.registry"; +import * as osmosisGammV1beta1TxRegistry from "./osmosis/gamm/v1beta1/tx.registry"; +import * as osmosisIncentivesTxRegistry from "./osmosis/incentives/tx.registry"; +import * as osmosisLockupTxRegistry from "./osmosis/lockup/tx.registry"; +import * as osmosisSuperfluidTxRegistry from "./osmosis/superfluid/tx.registry"; +import * as osmosisTokenfactoryV1beta1TxRegistry from "./osmosis/tokenfactory/v1beta1/tx.registry"; +import * as akashAuditV1beta1AuditAmino from "./akash/audit/v1beta1/audit.amino"; +import * as akashAuditV1beta2AuditAmino from "./akash/audit/v1beta2/audit.amino"; +import * as akashCertV1beta2CertAmino from "./akash/cert/v1beta2/cert.amino"; +import * as akashDeploymentV1beta2ServiceAmino from "./akash/deployment/v1beta2/service.amino"; +import * as akashMarketV1beta2ServiceAmino from "./akash/market/v1beta2/service.amino"; +import * as akashProviderV1beta1ProviderAmino from "./akash/provider/v1beta1/provider.amino"; +import * as akashProviderV1beta2ProviderAmino from "./akash/provider/v1beta2/provider.amino"; +import * as cosmosAuthzV1beta1TxAmino from "./cosmos/authz/v1beta1/tx.amino"; +import * as cosmosBankV1beta1TxAmino from "./cosmos/bank/v1beta1/tx.amino"; +import * as cosmosCrisisV1beta1TxAmino from "./cosmos/crisis/v1beta1/tx.amino"; +import * as cosmosDistributionV1beta1TxAmino from "./cosmos/distribution/v1beta1/tx.amino"; +import * as cosmosEvidenceV1beta1TxAmino from "./cosmos/evidence/v1beta1/tx.amino"; +import * as cosmosFeegrantV1beta1TxAmino from "./cosmos/feegrant/v1beta1/tx.amino"; +import * as cosmosGovV1TxAmino from "./cosmos/gov/v1/tx.amino"; +import * as cosmosGovV1beta1TxAmino from "./cosmos/gov/v1beta1/tx.amino"; +import * as cosmosGroupV1TxAmino from "./cosmos/group/v1/tx.amino"; +import * as cosmosNftV1beta1TxAmino from "./cosmos/nft/v1beta1/tx.amino"; +import * as cosmosSlashingV1beta1TxAmino from "./cosmos/slashing/v1beta1/tx.amino"; +import * as cosmosStakingV1beta1TxAmino from "./cosmos/staking/v1beta1/tx.amino"; +import * as cosmosUpgradeV1beta1TxAmino from "./cosmos/upgrade/v1beta1/tx.amino"; +import * as cosmosVestingV1beta1TxAmino from "./cosmos/vesting/v1beta1/tx.amino"; +import * as cosmwasmWasmV1TxAmino from "./cosmwasm/wasm/v1/tx.amino"; +import * as evmosErc20V1TxAmino from "./evmos/erc20/v1/tx.amino"; +import * as evmosFeesV1TxAmino from "./evmos/fees/v1/tx.amino"; +import * as evmosVestingV1TxAmino from "./evmos/vesting/v1/tx.amino"; +import * as ibcApplicationsTransferV1TxAmino from "./ibc/applications/transfer/v1/tx.amino"; +import * as ibcCoreChannelV1TxAmino from "./ibc/core/channel/v1/tx.amino"; +import * as ibcCoreClientV1TxAmino from "./ibc/core/client/v1/tx.amino"; +import * as ibcCoreConnectionV1TxAmino from "./ibc/core/connection/v1/tx.amino"; +import * as osmosisGammPoolmodelsBalancerTxTxAmino from "./osmosis/gamm/pool-models/balancer/tx/tx.amino"; +import * as osmosisGammPoolmodelsStableswapTxAmino from "./osmosis/gamm/pool-models/stableswap/tx.amino"; +import * as osmosisGammV1beta1TxAmino from "./osmosis/gamm/v1beta1/tx.amino"; +import * as osmosisIncentivesTxAmino from "./osmosis/incentives/tx.amino"; +import * as osmosisLockupTxAmino from "./osmosis/lockup/tx.amino"; +import * as osmosisSuperfluidTxAmino from "./osmosis/superfluid/tx.amino"; +import * as osmosisTokenfactoryV1beta1TxAmino from "./osmosis/tokenfactory/v1beta1/tx.amino"; +export const allAminoConverters = { + ...akashAuditV1beta1AuditAmino.AminoConverter, + ...akashAuditV1beta2AuditAmino.AminoConverter, + ...akashCertV1beta2CertAmino.AminoConverter, + ...akashDeploymentV1beta2ServiceAmino.AminoConverter, + ...akashMarketV1beta2ServiceAmino.AminoConverter, + ...akashProviderV1beta1ProviderAmino.AminoConverter, + ...akashProviderV1beta2ProviderAmino.AminoConverter, + ...cosmosAuthzV1beta1TxAmino.AminoConverter, + ...cosmosBankV1beta1TxAmino.AminoConverter, + ...cosmosCrisisV1beta1TxAmino.AminoConverter, + ...cosmosDistributionV1beta1TxAmino.AminoConverter, + ...cosmosEvidenceV1beta1TxAmino.AminoConverter, + ...cosmosFeegrantV1beta1TxAmino.AminoConverter, + ...cosmosGovV1TxAmino.AminoConverter, + ...cosmosGovV1beta1TxAmino.AminoConverter, + ...cosmosGroupV1TxAmino.AminoConverter, + ...cosmosNftV1beta1TxAmino.AminoConverter, + ...cosmosSlashingV1beta1TxAmino.AminoConverter, + ...cosmosStakingV1beta1TxAmino.AminoConverter, + ...cosmosUpgradeV1beta1TxAmino.AminoConverter, + ...cosmosVestingV1beta1TxAmino.AminoConverter, + ...cosmwasmWasmV1TxAmino.AminoConverter, + ...evmosErc20V1TxAmino.AminoConverter, + ...evmosFeesV1TxAmino.AminoConverter, + ...evmosVestingV1TxAmino.AminoConverter, + ...ibcApplicationsTransferV1TxAmino.AminoConverter, + ...ibcCoreChannelV1TxAmino.AminoConverter, + ...ibcCoreClientV1TxAmino.AminoConverter, + ...ibcCoreConnectionV1TxAmino.AminoConverter, + ...osmosisGammPoolmodelsBalancerTxTxAmino.AminoConverter, + ...osmosisGammPoolmodelsStableswapTxAmino.AminoConverter, + ...osmosisGammV1beta1TxAmino.AminoConverter, + ...osmosisIncentivesTxAmino.AminoConverter, + ...osmosisLockupTxAmino.AminoConverter, + ...osmosisSuperfluidTxAmino.AminoConverter, + ...osmosisTokenfactoryV1beta1TxAmino.AminoConverter +}; +export const allProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...akashAuditV1beta1AuditRegistry.registry, ...akashAuditV1beta2AuditRegistry.registry, ...akashCertV1beta2CertRegistry.registry, ...akashDeploymentV1beta2ServiceRegistry.registry, ...akashMarketV1beta2ServiceRegistry.registry, ...akashProviderV1beta1ProviderRegistry.registry, ...akashProviderV1beta2ProviderRegistry.registry, ...cosmosAuthzV1beta1TxRegistry.registry, ...cosmosBankV1beta1TxRegistry.registry, ...cosmosCrisisV1beta1TxRegistry.registry, ...cosmosDistributionV1beta1TxRegistry.registry, ...cosmosEvidenceV1beta1TxRegistry.registry, ...cosmosFeegrantV1beta1TxRegistry.registry, ...cosmosGovV1TxRegistry.registry, ...cosmosGovV1beta1TxRegistry.registry, ...cosmosGroupV1TxRegistry.registry, ...cosmosNftV1beta1TxRegistry.registry, ...cosmosSlashingV1beta1TxRegistry.registry, ...cosmosStakingV1beta1TxRegistry.registry, ...cosmosUpgradeV1beta1TxRegistry.registry, ...cosmosVestingV1beta1TxRegistry.registry, ...cosmwasmWasmV1TxRegistry.registry, ...evmosErc20V1TxRegistry.registry, ...evmosFeesV1TxRegistry.registry, ...evmosVestingV1TxRegistry.registry, ...ibcApplicationsTransferV1TxRegistry.registry, ...ibcCoreChannelV1TxRegistry.registry, ...ibcCoreClientV1TxRegistry.registry, ...ibcCoreConnectionV1TxRegistry.registry, ...osmosisGammPoolmodelsBalancerTxTxRegistry.registry, ...osmosisGammPoolmodelsStableswapTxRegistry.registry, ...osmosisGammV1beta1TxRegistry.registry, ...osmosisIncentivesTxRegistry.registry, ...osmosisLockupTxRegistry.registry, ...osmosisSuperfluidTxRegistry.registry, ...osmosisTokenfactoryV1beta1TxRegistry.registry]; +export const getAllSigningClientOptions = ({ + defaultTypes = defaultRegistryTypes +}: { + defaultTypes?: ReadonlyArray<[string, GeneratedType]>; +} = {}): { + registry: Registry; + aminoTypes: AminoTypes; +} => { + const registry = new Registry([...defaultTypes, ...allProtoRegistry]); + const aminoTypes = new AminoTypes({ + ...allAminoConverters + }); + return { + registry, + aminoTypes + }; +}; +export const getAllSigningClient = async ({ + rpcEndpoint, + signer, + defaultTypes = defaultRegistryTypes +}: { + rpcEndpoint: string | HttpEndpoint; + signer: OfflineSigner; + defaultTypes?: ReadonlyArray<[string, GeneratedType]>; +}) => { + const { + registry, + aminoTypes + } = getAllSigningClientOptions({ + defaultTypes + }); + const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, { + registry: (registry as any), + aminoTypes + }); + return client; +}; +export const getAllSigningTxRpc = async ({ + rpcEndpoint, + signer +}: SigningClientParams) => { + let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc); + const signingClient = await getAllSigningClient({ + rpcEndpoint, + signer + }); + txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => { + return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise); + }; + return txRpc; +}; \ No newline at end of file diff --git a/__fixtures__/v-next/outputinstantrpc/cosmos-sdk-module-client.ts b/__fixtures__/v-next/outputinstantrpc/cosmos-sdk-module-client.ts new file mode 100644 index 0000000000..3711879440 --- /dev/null +++ b/__fixtures__/v-next/outputinstantrpc/cosmos-sdk-module-client.ts @@ -0,0 +1,94 @@ +import { GeneratedType, Registry, OfflineSigner } from "@cosmjs/proto-signing"; +import { AminoTypes, SigningStargateClient } from "@cosmjs/stargate"; +import { HttpEndpoint } from "@cosmjs/tendermint-rpc"; +import { createRpcClient } from "./extern"; +import { DeliverTxResponse, EncodeObject, StdFee, TxRpc, SigningClientParams } from "./types"; +import * as cosmosAuthzV1beta1TxRegistry from "./cosmos/authz/v1beta1/tx.registry"; +import * as cosmosBankV1beta1TxRegistry from "./cosmos/bank/v1beta1/tx.registry"; +import * as cosmosCrisisV1beta1TxRegistry from "./cosmos/crisis/v1beta1/tx.registry"; +import * as cosmosDistributionV1beta1TxRegistry from "./cosmos/distribution/v1beta1/tx.registry"; +import * as cosmosEvidenceV1beta1TxRegistry from "./cosmos/evidence/v1beta1/tx.registry"; +import * as cosmosFeegrantV1beta1TxRegistry from "./cosmos/feegrant/v1beta1/tx.registry"; +import * as cosmosGovV1TxRegistry from "./cosmos/gov/v1/tx.registry"; +import * as cosmosGovV1beta1TxRegistry from "./cosmos/gov/v1beta1/tx.registry"; +import * as cosmosGroupV1TxRegistry from "./cosmos/group/v1/tx.registry"; +import * as cosmosNftV1beta1TxRegistry from "./cosmos/nft/v1beta1/tx.registry"; +import * as cosmosSlashingV1beta1TxRegistry from "./cosmos/slashing/v1beta1/tx.registry"; +import * as cosmosStakingV1beta1TxRegistry from "./cosmos/staking/v1beta1/tx.registry"; +import * as cosmosUpgradeV1beta1TxRegistry from "./cosmos/upgrade/v1beta1/tx.registry"; +import * as cosmosVestingV1beta1TxRegistry from "./cosmos/vesting/v1beta1/tx.registry"; +import * as cosmosAuthzV1beta1TxAmino from "./cosmos/authz/v1beta1/tx.amino"; +import * as cosmosBankV1beta1TxAmino from "./cosmos/bank/v1beta1/tx.amino"; +import * as cosmosCrisisV1beta1TxAmino from "./cosmos/crisis/v1beta1/tx.amino"; +import * as cosmosDistributionV1beta1TxAmino from "./cosmos/distribution/v1beta1/tx.amino"; +import * as cosmosEvidenceV1beta1TxAmino from "./cosmos/evidence/v1beta1/tx.amino"; +import * as cosmosFeegrantV1beta1TxAmino from "./cosmos/feegrant/v1beta1/tx.amino"; +import * as cosmosGovV1TxAmino from "./cosmos/gov/v1/tx.amino"; +import * as cosmosGovV1beta1TxAmino from "./cosmos/gov/v1beta1/tx.amino"; +import * as cosmosGroupV1TxAmino from "./cosmos/group/v1/tx.amino"; +import * as cosmosNftV1beta1TxAmino from "./cosmos/nft/v1beta1/tx.amino"; +import * as cosmosSlashingV1beta1TxAmino from "./cosmos/slashing/v1beta1/tx.amino"; +import * as cosmosStakingV1beta1TxAmino from "./cosmos/staking/v1beta1/tx.amino"; +import * as cosmosUpgradeV1beta1TxAmino from "./cosmos/upgrade/v1beta1/tx.amino"; +import * as cosmosVestingV1beta1TxAmino from "./cosmos/vesting/v1beta1/tx.amino"; +export const cosmosAminoConverters = { + ...cosmosAuthzV1beta1TxAmino.AminoConverter, + ...cosmosBankV1beta1TxAmino.AminoConverter, + ...cosmosCrisisV1beta1TxAmino.AminoConverter, + ...cosmosDistributionV1beta1TxAmino.AminoConverter, + ...cosmosEvidenceV1beta1TxAmino.AminoConverter, + ...cosmosFeegrantV1beta1TxAmino.AminoConverter, + ...cosmosGovV1TxAmino.AminoConverter, + ...cosmosGovV1beta1TxAmino.AminoConverter, + ...cosmosGroupV1TxAmino.AminoConverter, + ...cosmosNftV1beta1TxAmino.AminoConverter, + ...cosmosSlashingV1beta1TxAmino.AminoConverter, + ...cosmosStakingV1beta1TxAmino.AminoConverter, + ...cosmosUpgradeV1beta1TxAmino.AminoConverter, + ...cosmosVestingV1beta1TxAmino.AminoConverter +}; +export const cosmosProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...cosmosAuthzV1beta1TxRegistry.registry, ...cosmosBankV1beta1TxRegistry.registry, ...cosmosCrisisV1beta1TxRegistry.registry, ...cosmosDistributionV1beta1TxRegistry.registry, ...cosmosEvidenceV1beta1TxRegistry.registry, ...cosmosFeegrantV1beta1TxRegistry.registry, ...cosmosGovV1TxRegistry.registry, ...cosmosGovV1beta1TxRegistry.registry, ...cosmosGroupV1TxRegistry.registry, ...cosmosNftV1beta1TxRegistry.registry, ...cosmosSlashingV1beta1TxRegistry.registry, ...cosmosStakingV1beta1TxRegistry.registry, ...cosmosUpgradeV1beta1TxRegistry.registry, ...cosmosVestingV1beta1TxRegistry.registry]; +export const getSigningCosmosClientOptions = (): { + registry: Registry; + aminoTypes: AminoTypes; +} => { + const registry = new Registry([...cosmosProtoRegistry]); + const aminoTypes = new AminoTypes({ + ...cosmosAminoConverters + }); + return { + registry, + aminoTypes + }; +}; +export const getSigningCosmosClient = async ({ + rpcEndpoint, + signer +}: { + rpcEndpoint: string | HttpEndpoint; + signer: OfflineSigner; +}) => { + const { + registry, + aminoTypes + } = getSigningCosmosClientOptions(); + const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, { + registry: (registry as any), + aminoTypes + }); + return client; +}; +export const getSigningCosmosTxRpc = async ({ + rpcEndpoint, + signer +}: SigningClientParams) => { + let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc); + const signingClient = await getSigningCosmosClient({ + rpcEndpoint, + signer + }); + txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => { + return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise); + }; + return txRpc; +}; \ No newline at end of file diff --git a/__fixtures__/v-next/outputinstantrpc/custom-client.ts b/__fixtures__/v-next/outputinstantrpc/custom-client.ts new file mode 100644 index 0000000000..825cd91f8a --- /dev/null +++ b/__fixtures__/v-next/outputinstantrpc/custom-client.ts @@ -0,0 +1,69 @@ +import { GeneratedType, Registry, OfflineSigner } from "@cosmjs/proto-signing"; +import { defaultRegistryTypes, AminoTypes, SigningStargateClient } from "@cosmjs/stargate"; +import { HttpEndpoint } from "@cosmjs/tendermint-rpc"; +import { createRpcClient } from "./extern"; +import { DeliverTxResponse, EncodeObject, StdFee, TxRpc, SigningClientParams } from "./types"; +import * as cosmosGovV1TxRegistry from "./cosmos/gov/v1/tx.registry"; +import * as cosmosGovV1beta1TxRegistry from "./cosmos/gov/v1beta1/tx.registry"; +import * as ibcCoreChannelV1TxRegistry from "./ibc/core/channel/v1/tx.registry"; +import * as cosmosGovV1TxAmino from "./cosmos/gov/v1/tx.amino"; +import * as cosmosGovV1beta1TxAmino from "./cosmos/gov/v1beta1/tx.amino"; +import * as ibcCoreChannelV1TxAmino from "./ibc/core/channel/v1/tx.amino"; +export const customAminoConverters = { + ...cosmosGovV1TxAmino.AminoConverter, + ...cosmosGovV1beta1TxAmino.AminoConverter, + ...ibcCoreChannelV1TxAmino.AminoConverter +}; +export const customProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...cosmosGovV1TxRegistry.registry, ...cosmosGovV1beta1TxRegistry.registry, ...ibcCoreChannelV1TxRegistry.registry]; +export const getCustomSigningClientOptions = ({ + defaultTypes = defaultRegistryTypes +}: { + defaultTypes?: ReadonlyArray<[string, GeneratedType]>; +} = {}): { + registry: Registry; + aminoTypes: AminoTypes; +} => { + const registry = new Registry([...defaultTypes, ...customProtoRegistry]); + const aminoTypes = new AminoTypes({ + ...customAminoConverters + }); + return { + registry, + aminoTypes + }; +}; +export const getCustomSigningClient = async ({ + rpcEndpoint, + signer, + defaultTypes = defaultRegistryTypes +}: { + rpcEndpoint: string | HttpEndpoint; + signer: OfflineSigner; + defaultTypes?: ReadonlyArray<[string, GeneratedType]>; +}) => { + const { + registry, + aminoTypes + } = getCustomSigningClientOptions({ + defaultTypes + }); + const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, { + registry: (registry as any), + aminoTypes + }); + return client; +}; +export const getCustomSigningTxRpc = async ({ + rpcEndpoint, + signer +}: SigningClientParams) => { + let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc); + const signingClient = await getCustomSigningClient({ + rpcEndpoint, + signer + }); + txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => { + return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise); + }; + return txRpc; +}; \ No newline at end of file diff --git a/__fixtures__/v-next/outputinstantrpc/index.ts b/__fixtures__/v-next/outputinstantrpc/index.ts index 1e773172b3..9cc88aafe9 100644 --- a/__fixtures__/v-next/outputinstantrpc/index.ts +++ b/__fixtures__/v-next/outputinstantrpc/index.ts @@ -4,22 +4,21 @@ * and run the transpile command or npm scripts command that is used to regenerate this bundle. */ +export * from "./custom-client"; +export * from "./all-module-client"; export * from "./akash/bundle"; -export * from "./akash/client"; +export * from "./akash-sdk-module-client"; export * from "./ics23/bundle"; export * from "./cosmos_proto/bundle"; export * from "./cosmos/bundle"; -export * from "./cosmos/client"; +export * from "./cosmos-sdk-module-client"; export * from "./cosmwasm/bundle"; -export * from "./cosmwasm/client"; export * from "./evmos/bundle"; -export * from "./evmos/client"; export * from "./gogoproto/bundle"; export * from "./google/bundle"; export * from "./ibc/bundle"; -export * from "./ibc/client"; export * from "./osmosis/bundle"; -export * from "./osmosis/client"; +export * from "./osmosis-sdk-module-client"; export * from "./tendermint/bundle"; export * from "./service-ops"; export * from "./hooks"; diff --git a/__fixtures__/v-next/outputinstantrpc/osmosis-sdk-module-client.ts b/__fixtures__/v-next/outputinstantrpc/osmosis-sdk-module-client.ts new file mode 100644 index 0000000000..7ed3e3f0cd --- /dev/null +++ b/__fixtures__/v-next/outputinstantrpc/osmosis-sdk-module-client.ts @@ -0,0 +1,81 @@ +import { GeneratedType, Registry, OfflineSigner } from "@cosmjs/proto-signing"; +import { defaultRegistryTypes, AminoTypes, SigningStargateClient } from "@cosmjs/stargate"; +import { HttpEndpoint } from "@cosmjs/tendermint-rpc"; +import { createRpcClient } from "./extern"; +import { DeliverTxResponse, EncodeObject, StdFee, TxRpc, SigningClientParams } from "./types"; +import * as osmosisGammPoolmodelsBalancerTxTxRegistry from "./osmosis/gamm/pool-models/balancer/tx/tx.registry"; +import * as osmosisGammPoolmodelsStableswapTxRegistry from "./osmosis/gamm/pool-models/stableswap/tx.registry"; +import * as osmosisGammV1beta1TxRegistry from "./osmosis/gamm/v1beta1/tx.registry"; +import * as osmosisIncentivesTxRegistry from "./osmosis/incentives/tx.registry"; +import * as osmosisLockupTxRegistry from "./osmosis/lockup/tx.registry"; +import * as osmosisSuperfluidTxRegistry from "./osmosis/superfluid/tx.registry"; +import * as osmosisTokenfactoryV1beta1TxRegistry from "./osmosis/tokenfactory/v1beta1/tx.registry"; +import * as osmosisGammPoolmodelsBalancerTxTxAmino from "./osmosis/gamm/pool-models/balancer/tx/tx.amino"; +import * as osmosisGammPoolmodelsStableswapTxAmino from "./osmosis/gamm/pool-models/stableswap/tx.amino"; +import * as osmosisGammV1beta1TxAmino from "./osmosis/gamm/v1beta1/tx.amino"; +import * as osmosisIncentivesTxAmino from "./osmosis/incentives/tx.amino"; +import * as osmosisLockupTxAmino from "./osmosis/lockup/tx.amino"; +import * as osmosisSuperfluidTxAmino from "./osmosis/superfluid/tx.amino"; +import * as osmosisTokenfactoryV1beta1TxAmino from "./osmosis/tokenfactory/v1beta1/tx.amino"; +export const osmosisAminoConverters = { + ...osmosisGammPoolmodelsBalancerTxTxAmino.AminoConverter, + ...osmosisGammPoolmodelsStableswapTxAmino.AminoConverter, + ...osmosisGammV1beta1TxAmino.AminoConverter, + ...osmosisIncentivesTxAmino.AminoConverter, + ...osmosisLockupTxAmino.AminoConverter, + ...osmosisSuperfluidTxAmino.AminoConverter, + ...osmosisTokenfactoryV1beta1TxAmino.AminoConverter +}; +export const osmosisProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...osmosisGammPoolmodelsBalancerTxTxRegistry.registry, ...osmosisGammPoolmodelsStableswapTxRegistry.registry, ...osmosisGammV1beta1TxRegistry.registry, ...osmosisIncentivesTxRegistry.registry, ...osmosisLockupTxRegistry.registry, ...osmosisSuperfluidTxRegistry.registry, ...osmosisTokenfactoryV1beta1TxRegistry.registry]; +export const getSigningOsmosisClientOptions = ({ + defaultTypes = defaultRegistryTypes +}: { + defaultTypes?: ReadonlyArray<[string, GeneratedType]>; +} = {}): { + registry: Registry; + aminoTypes: AminoTypes; +} => { + const registry = new Registry([...defaultTypes, ...osmosisProtoRegistry]); + const aminoTypes = new AminoTypes({ + ...osmosisAminoConverters + }); + return { + registry, + aminoTypes + }; +}; +export const getSigningOsmosisClient = async ({ + rpcEndpoint, + signer, + defaultTypes = defaultRegistryTypes +}: { + rpcEndpoint: string | HttpEndpoint; + signer: OfflineSigner; + defaultTypes?: ReadonlyArray<[string, GeneratedType]>; +}) => { + const { + registry, + aminoTypes + } = getSigningOsmosisClientOptions({ + defaultTypes + }); + const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, { + registry: (registry as any), + aminoTypes + }); + return client; +}; +export const getSigningOsmosisTxRpc = async ({ + rpcEndpoint, + signer +}: SigningClientParams) => { + let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc); + const signingClient = await getSigningOsmosisClient({ + rpcEndpoint, + signer + }); + txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => { + return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise); + }; + return txRpc; +}; \ No newline at end of file diff --git a/packages/telescope/README.md b/packages/telescope/README.md index 9e9efbdd8d..3b0927f632 100644 --- a/packages/telescope/README.md +++ b/packages/telescope/README.md @@ -443,9 +443,11 @@ See [LCD Clients](#lcd-clients) for more info. | `rpcClients.enabledServices` | which services to enable | [`Msg`,`Query`,`Service`] | | `rpcClients.instantOps` | will generate instant rpc operations in the file `service-ops.ts` under root folder, which contains customized classes having selected rpc methods | `undefined` | | `rpcClients.serviceImplement` | assign implement type of rpc methods, `Query` or `Tx`, by setting patterns under service types. | `undefined` | - `rpcClients.combinedClient.name` | assign the client name like `{name}AminoConverters`, `get{name}SigningClient` etc | `undefined` -| `rpcClients.combinedClient.fileName` | assign the file name of generated client in root directory | `undefined` -| `rpcClients.combinedClient.include.patterns` | determine which proto files will be imported for the current client such as `cosmos.gov.v1beta1.**` | `undefined` + `rpcClients.clientStyle.useUpdatedClientStyle` | The default value is `false`, which sets the generated client to use the legacy style. Setting it to `true` applies the updated style and activates the remaining options in clientStyle. | `false` +| `rpcClients.clientStyle.type` | A string array containing possible values: `all-client`, `sdk-module-client`, and `custom-client`. The value `all-client` generates an all-module-client file. The value `sdk-module-client` generates a client for the module specified by the `sdkModuleClientOption`. The value `custom-client` generates a customized client as specified by `customClientOption` | `undefined` +`rpcClients.clientStyle.customClientOption.name` | assign the client name like `{name}AminoConverters`, `get{name}SigningClient` etc | `undefined` +| `rpcClients.clientStyle.customClientOption.fileName` | assign the file name of generated client in root directory | `undefined` +| `rpcClients.clientStyle.customClientOption.include.patterns` | determine which proto files will be imported for the current client such as `cosmos.gov.v1beta1.**` | `undefined` See [RPC Clients](#rpc-clients) for more info. @@ -1063,44 +1065,41 @@ export class CosmosAuthAccount { } ``` -## Combined Clients Methods +## Client Style Methods -Using combined option to generate multiple module clients +Use client style to define the client file generated according to the config For example, for this config: ```js -{ - combinedClient: [ - { - name: "CosmosIbc", - fileName: "cosmos-ibc-client.ts", - include: { - patterns: [ - "cosmos.gov.v1beta1*", - "cosmos.gov.v1*", - "ibc.core.channel.*", - ], - }, - }, - { - name: "AkashCosmos", - fileName: "akash-cosmos-client.ts", - include: { - patterns: [ - "cosmos.group.v1*", - "cosmos.nft.v1beta1*", - "akash.**.v1beta2", - "akash.audit.v1beta1*", - ], +clientStyle: { + useUpdatedClientStyle: true, + type: ['all-client', 'sdk-module-client', 'custom-client'], + customClientOption: [ + { + name: "custom", + fileName: "custom-client.ts", + include: { + patterns: [ + "cosmos.gov.v1beta1*", + "cosmos.gov.v1*", + "ibc.core.channel.*", + ], + }, }, - }, - ], -} + ], + sdkModuleClientOption: [ + 'akash', + 'osmosis', + 'cosmos', + ], + }, ``` -There'll be client files (`cosmos-ibc-client.ts`, `akash-cosmos-client.ts`) generated in the root folder according to fileName in the setting. -The combined client will import proto files accroding to include.patterns. (The protos can come from different modules) -For example the cosmos-ibc-client.ts will be like: +There'll be client files (`all-module-client.ts`, `akash-sdk-module-client.ts`, `osmosis-sdk-module-client.ts`, `cosmos-sdk-module-client.ts`, `custom-client.ts`) generated in the root directory according to the setting.
+The `all-module-client.ts` file consolidates all proto imports into one file and exports them as a single client.
+All sdk module client files will be identical to the legacy `client.ts` files generated in each module directory, except they will be located in the root directory.
+The custom client imports proto files based on `include.patterns`, allowing protos to originate from different modules.
+For example the custom-client.ts will be like: ```ts export const cosmosIbcAminoConverters = { ...cosmosGovV1TxAmino.AminoConverter, diff --git a/packages/telescope/__tests__/telescope-instant-rpc.test.ts b/packages/telescope/__tests__/telescope-instant-rpc.test.ts index 1d3eb19c9e..fe3f4a3d3e 100644 --- a/packages/telescope/__tests__/telescope-instant-rpc.test.ts +++ b/packages/telescope/__tests__/telescope-instant-rpc.test.ts @@ -127,6 +127,28 @@ const options: TelescopeOptions = { rpcClients: { enabled: true, + clientStyle: { + useUpdatedClientStyle: true, + type: ['all-client', 'sdk-module-client', 'custom-client'], + customClientOption: [ + { + name: "custom", + fileName: "custom-client.ts", + include: { + patterns: [ + "cosmos.gov.v1beta1*", + "cosmos.gov.v1*", + "ibc.core.channel.*", + ], + }, + }, + ], + sdkModuleClientOption: [ + 'akash', + 'osmosis', + 'cosmos', + ], + }, extensions: false, camelCase: true, scopedIsExclusive: false, @@ -213,31 +235,6 @@ const options: TelescopeOptions = { }, }, ], - combinedClient: [ - { - name: "CosmosIbc", - fileName: "cosmos-ibc-client.ts", - include: { - patterns: [ - "cosmos.gov.v1beta1*", - "cosmos.gov.v1*", - "ibc.core.channel.*", - ], - }, - }, - { - name: "AkashCosmos", - fileName: "akash-cosmos-client.ts", - include: { - patterns: [ - "cosmos.group.v1*", - "cosmos.nft.v1beta1*", - "akash.**.v1beta2", - "akash.audit.v1beta1*", - ], - }, - }, - ], }, reactQuery: { @@ -321,6 +318,9 @@ const options: TelescopeOptions = { }, rpcClients: { inline: true, + clientStyle: { + useUpdatedClientStyle: true + }, }, }, }, diff --git a/packages/telescope/src/builder.ts b/packages/telescope/src/builder.ts index 61ae063512..d5c676f2a3 100644 --- a/packages/telescope/src/builder.ts +++ b/packages/telescope/src/builder.ts @@ -23,7 +23,9 @@ import { plugin as createMsgFuncs } from './generators/create-msg-funcs'; import { plugin as createReactQueryBundle } from './generators/create-react-query-bundle'; import { plugin as createMobxBundle } from './generators/create-mobx-bundle'; import { plugin as createStargateClients } from './generators/create-stargate-clients'; -import { plugin as createCombinedStargateClients } from './generators/create-combined-stargate-clients'; +import { plugin as createCustomStargateClients } from './generators/create-custom-stargate-clients'; +import { plugin as createAllStargateClients } from './generators/create-all-stargate-clients'; +import { plugin as createSdkModuleStargateClients } from './generators/create-sdk-module-stargate-clients'; import { plugin as createBundle } from './generators/create-bundle'; import { plugin as createIndex } from './generators/create-index'; import { plugin as createHelpers } from './generators/create-helpers'; @@ -74,7 +76,6 @@ export class TelescopeBuilder { readonly rpcMsgClients: BundlerFile[] = []; readonly registries: BundlerFile[] = []; readonly stateManagers: Record = {}; - constructor({ protoDirs, outPath, @@ -84,11 +85,13 @@ export class TelescopeBuilder { const fixedDirs = protoDirs.map((directory) => { return toPosixPath(directory); }); + this.protoDirs = fixedDirs; this.outPath = resolve(toPosixPath(outPath)); this.options = sanitizeOptions(options); this.store = store ?? new ProtoStore(fixedDirs, this.options); this.store.traverseAll(); + } context(ref) { @@ -127,6 +130,7 @@ export class TelescopeBuilder { [].push.apply(this.converters, files); } + async build() { // check warnings if ( @@ -154,6 +158,7 @@ export class TelescopeBuilder { // store bundleFile in filesToInclude const bundler = new Bundler(this, bundle); + // [x] write out all TS files for package createTypes(this, bundler); @@ -172,20 +177,35 @@ export class TelescopeBuilder { createRPCMsgClients(this, bundler); createPiniaStore(this, bundler); - // [x] write out one client for each base package, referencing the last two steps - createStargateClients(this, bundler); - if (bundler.registries) { - allRegistries.push(...bundler.registries) - } - if (bundler.converters) { - allConverters.push(...bundler.converters) - + if (this.options.rpcClients.clientStyle.useUpdatedClientStyle) { + // generate sdk-module client file + if (this.options.rpcClients.clientStyle.type.includes('sdk-module-client') && this.options.rpcClients.clientStyle.sdkModuleClientOption.includes(bundler.bundle.base)) { + createSdkModuleStargateClients(this, bundler) + } + + // store registry and converter for all/custom client generation + if (bundler.registries) { + allRegistries.push(...bundler.registries) + } + if (bundler.converters) { + allConverters.push(...bundler.converters) + } + } else { + // [x] write out one client for each base package, referencing the last two steps + createStargateClients(this, bundler); } return bundler; }); - if (this.options.rpcClients.combinedClient && this.options.rpcClients.combinedClient.length !== 0) { - createCombinedStargateClients(this, allRegistries, allConverters) + if (this.options.rpcClients.clientStyle.useUpdatedClientStyle) { + // generate cutsom client file + if (this.options.rpcClients.clientStyle.type.includes('custom-client') && this.options.rpcClients.clientStyle.customClientOption && this.options.rpcClients.clientStyle.customClientOption.length !== 0) { + createCustomStargateClients(this, allRegistries, allConverters) + } + // generate all module client file + if (this.options.rpcClients.clientStyle.type.includes('all-client')) { + createAllStargateClients(this, allRegistries, allConverters) + } } // post run plugins diff --git a/packages/telescope/src/generators/create-all-stargate-clients.ts b/packages/telescope/src/generators/create-all-stargate-clients.ts new file mode 100644 index 0000000000..98462b5b5a --- /dev/null +++ b/packages/telescope/src/generators/create-all-stargate-clients.ts @@ -0,0 +1,121 @@ +import { Bundler } from '../bundler'; +import { TelescopeBuilder } from '../builder'; +import { join, dirname, relative } from 'path'; +import { + importNamespace, + GenericParseContext, + createStargateClient, + createStargateClientOptions, + createStargateClientProtoRegistry, + createStargateClientAminoRegistry, + createGetTxRpc +} from '@cosmology/ast'; +import { ProtoRef } from '@cosmology/types'; +import { camel, pascal } from 'case'; +import { duplicateImportPathsWithExt, variableSlug, toPosixPath } from '@cosmology/utils'; +import { buildAllImportsFromGenericContext } from '../imports'; +import { writeAstToFile } from '../utils/files'; +import { BundlerFile } from '../types'; + +export const plugin = ( + builder: TelescopeBuilder, + allRegistries: BundlerFile[], + allConverters: BundlerFile[] +) => { + + if (!allRegistries || !allRegistries.length) { + return; + } + + const registryImports = []; + const converterImports = []; + + const clientFile = 'all-module-client.ts' + builder.files.push(clientFile); + const ctxRef: ProtoRef = { + absolute: '/', + filename: '/', + proto: { + imports: [], + package: null, + root: {}, + } + }; + const ctx = new GenericParseContext(ctxRef, null, builder.options); + + const registryVariables = []; + const converterVariables = []; + + allRegistries.forEach(registry => { + let rel = relative(dirname(clientFile), registry.localname); + if (!rel.startsWith('.')) rel = `./${rel}`; + rel = toPosixPath(rel) + const variable = variableSlug(registry.localname); + registryVariables.push(variable); + registryImports.push(importNamespace(variable, rel)); + }); + + allConverters.forEach(converter => { + let rel = relative(dirname(clientFile), converter.localname); + if (!rel.startsWith('.')) rel = `./${rel}`; + rel = toPosixPath(rel) + const variable = variableSlug(converter.localname); + converterVariables.push(variable); + converterImports.push(importNamespace(variable, rel)); + }); + const name = 'get' + pascal('AllSigningClient'); + const txRpcName = 'get' + pascal('AllSigningTxRpc'); + const prefix = camel('all'); + const aminos = createStargateClientAminoRegistry({ + context: ctx, + aminos: converterVariables, + aminoConverters: prefix + 'AminoConverters' + }); + const protos = createStargateClientProtoRegistry({ + context: ctx, + registries: registryVariables, + protoTypeRegistry: prefix + 'ProtoRegistry' + }); + const clientOptions = createStargateClientOptions({ + context: ctx, + name: name + 'Options', + protoTypeRegistry: prefix + 'ProtoRegistry', + aminoConverters: prefix + 'AminoConverters' + }); + const clientBody = createStargateClient({ + context: ctx, + name, + options: name + 'Options', + }); + + let getTxRpc; + + if (ctx.pluginValue("stargateClients.addGetTxRpc")) { + getTxRpc = createGetTxRpc(ctx, txRpcName, name); + } + + const imports = buildAllImportsFromGenericContext(ctx, clientFile); + + let importDecls = [...imports, ...registryImports, ...converterImports]; + + importDecls = duplicateImportPathsWithExt(importDecls, builder.options.restoreImportExtension); + + let cProg = importDecls + .concat(aminos) + .concat(protos) + .concat(clientOptions) + .concat(clientBody); + + // replace all backslash path for windows + for (let i = 0; i < cProg.length; i++) { + if (cProg[i].source?.value) { + cProg[i].source.value = toPosixPath(cProg[i].source?.value) + } + } + + if (getTxRpc) { + cProg = cProg.concat(getTxRpc); + } + const clientOutFile = join(builder.outPath, clientFile); + writeAstToFile(builder.outPath, builder.options, cProg, clientOutFile); +} diff --git a/packages/telescope/src/generators/create-combined-stargate-clients.ts b/packages/telescope/src/generators/create-custom-stargate-clients.ts similarity index 97% rename from packages/telescope/src/generators/create-combined-stargate-clients.ts rename to packages/telescope/src/generators/create-custom-stargate-clients.ts index 22d1c5e062..ed63b2b4f2 100644 --- a/packages/telescope/src/generators/create-combined-stargate-clients.ts +++ b/packages/telescope/src/generators/create-custom-stargate-clients.ts @@ -16,14 +16,13 @@ import { duplicateImportPathsWithExt, variableSlug, toPosixPath, isPackageInclud import { buildAllImportsFromGenericContext } from '../imports'; import { writeAstToFile } from '../utils/files'; import { BundlerFile } from '../types'; -import minimatch from "minimatch"; export const plugin = ( builder: TelescopeBuilder, allRegistries: BundlerFile[], allConverters: BundlerFile[] ) => { - builder.options.rpcClients.combinedClient.map((currentClient) => { + builder.options.rpcClients.clientStyle.customClientOption.map((currentClient) => { if (!allRegistries || !allRegistries.length) { return; @@ -33,6 +32,7 @@ export const plugin = ( const converterImports = []; const clientFile = currentClient.fileName + builder.files.push(clientFile); const ctxRef: ProtoRef = { absolute: '/', filename: '/', diff --git a/packages/telescope/src/generators/create-index.ts b/packages/telescope/src/generators/create-index.ts index 5487bee0da..303593ebf2 100644 --- a/packages/telescope/src/generators/create-index.ts +++ b/packages/telescope/src/generators/create-index.ts @@ -13,7 +13,6 @@ const version = process.env.NODE_ENV === 'test' ? 'latest' : pkg.version; export const plugin = ( builder: TelescopeBuilder ) => { - if (!builder.options.bundle.enabled) { return; } diff --git a/packages/telescope/src/generators/create-sdk-module-stargate-clients.ts b/packages/telescope/src/generators/create-sdk-module-stargate-clients.ts new file mode 100644 index 0000000000..3267fb9414 --- /dev/null +++ b/packages/telescope/src/generators/create-sdk-module-stargate-clients.ts @@ -0,0 +1,120 @@ +import { Bundler } from '../bundler'; +import { TelescopeBuilder } from '../builder'; +import { join, dirname, relative } from 'path'; +import { + importNamespace, + GenericParseContext, + createStargateClient, + createStargateClientOptions, + createStargateClientProtoRegistry, + createStargateClientAminoRegistry, + createGetTxRpc +} from '@cosmology/ast'; +import { ProtoRef } from '@cosmology/types'; +import { camel, pascal } from 'case'; +import { duplicateImportPathsWithExt, variableSlug, toPosixPath } from '@cosmology/utils'; +import { buildAllImportsFromGenericContext } from '../imports'; + +export const plugin = ( + builder: TelescopeBuilder, + bundler: Bundler +) => { + + if (!bundler.registries || !bundler.registries.length) { + return; + } + + const registryImports = []; + const converterImports = []; + const clientFile = bundler.bundle.base.concat('-sdk-module-client.ts'); + bundler.files.push(clientFile); + + const ctxRef: ProtoRef = { + absolute: '/', + filename: '/', + proto: { + imports: [], + package: bundler.bundle.base, // for package options + root: {}, + } + }; + const ctx = new GenericParseContext(ctxRef, null, builder.options); + + const registryVariables = []; + const converterVariables = []; + + bundler.registries.forEach(registry => { + let rel = relative(dirname(clientFile), registry.localname); + if (!rel.startsWith('.')) rel = `./${rel}`; + rel = toPosixPath(rel) + const variable = variableSlug(registry.localname); + registryVariables.push(variable); + registryImports.push(importNamespace(variable, rel)); + }); + + bundler.converters.forEach(converter => { + let rel = relative(dirname(clientFile), converter.localname); + if (!rel.startsWith('.')) rel = `./${rel}`; + rel = toPosixPath(rel) + const variable = variableSlug(converter.localname); + converterVariables.push(variable); + converterImports.push(importNamespace(variable, rel)); + }); + + const name = 'getSigning' + pascal(bundler.bundle.base + 'Client'); + const txRpcName = 'getSigning' + pascal(bundler.bundle.base + 'TxRpc'); + const prefix = camel(bundler.bundle.base); + const aminos = createStargateClientAminoRegistry({ + context: ctx, + aminos: converterVariables, + aminoConverters: prefix + 'AminoConverters' + }); + const protos = createStargateClientProtoRegistry({ + context: ctx, + registries: registryVariables, + protoTypeRegistry: prefix + 'ProtoRegistry' + }); + const clientOptions = createStargateClientOptions({ + context: ctx, + name: name + 'Options', + protoTypeRegistry: prefix + 'ProtoRegistry', + aminoConverters: prefix + 'AminoConverters' + }); + const clientBody = createStargateClient({ + context: ctx, + name, + options: name + 'Options', + }); + + let getTxRpc; + + if(ctx.pluginValue("stargateClients.addGetTxRpc")){ + getTxRpc = createGetTxRpc(ctx, txRpcName, name); + } + + const imports = buildAllImportsFromGenericContext(ctx, clientFile); + + let importDecls = [...imports, ...registryImports, ...converterImports]; + + importDecls = duplicateImportPathsWithExt(importDecls, builder.options.restoreImportExtension); + + let cProg = importDecls + .concat(aminos) + .concat(protos) + .concat(clientOptions) + .concat(clientBody); + + // replace all backslash path for windows + for (let i = 0; i < cProg.length; i++) { + if(cProg[i].source?.value){ + cProg[i].source.value = toPosixPath(cProg[i].source?.value) + } + } + + if (getTxRpc) { + cProg = cProg.concat(getTxRpc); + } + const clientOutFile = join(builder.outPath, clientFile); + bundler.writeAst(cProg, clientOutFile); + +}; diff --git a/packages/telescope/src/generators/create-stargate-clients.ts b/packages/telescope/src/generators/create-stargate-clients.ts index 807f44d2b1..33ec6bc2a6 100644 --- a/packages/telescope/src/generators/create-stargate-clients.ts +++ b/packages/telescope/src/generators/create-stargate-clients.ts @@ -26,7 +26,6 @@ export const plugin = ( const registryImports = []; const converterImports = []; - const clientFile = join(`${bundler.bundle.base}`, 'client.ts'); bundler.files.push(clientFile); diff --git a/packages/telescope/types/generators/create-all-stargate-clients.d.ts b/packages/telescope/types/generators/create-all-stargate-clients.d.ts new file mode 100644 index 0000000000..3ec94a9eb6 --- /dev/null +++ b/packages/telescope/types/generators/create-all-stargate-clients.d.ts @@ -0,0 +1,3 @@ +import { TelescopeBuilder } from '../builder'; +import { BundlerFile } from '../types'; +export declare const plugin: (builder: TelescopeBuilder, allRegistries: BundlerFile[], allConverters: BundlerFile[]) => void; diff --git a/packages/telescope/types/generators/create-custom-stargate-clients.d.ts b/packages/telescope/types/generators/create-custom-stargate-clients.d.ts new file mode 100644 index 0000000000..3ec94a9eb6 --- /dev/null +++ b/packages/telescope/types/generators/create-custom-stargate-clients.d.ts @@ -0,0 +1,3 @@ +import { TelescopeBuilder } from '../builder'; +import { BundlerFile } from '../types'; +export declare const plugin: (builder: TelescopeBuilder, allRegistries: BundlerFile[], allConverters: BundlerFile[]) => void; diff --git a/packages/telescope/types/generators/create-scoped-stargate-clients.d.ts b/packages/telescope/types/generators/create-scoped-stargate-clients.d.ts new file mode 100644 index 0000000000..3ec94a9eb6 --- /dev/null +++ b/packages/telescope/types/generators/create-scoped-stargate-clients.d.ts @@ -0,0 +1,3 @@ +import { TelescopeBuilder } from '../builder'; +import { BundlerFile } from '../types'; +export declare const plugin: (builder: TelescopeBuilder, allRegistries: BundlerFile[], allConverters: BundlerFile[]) => void; diff --git a/packages/telescope/types/generators/create-sdk-module-stargate-clients.d.ts b/packages/telescope/types/generators/create-sdk-module-stargate-clients.d.ts new file mode 100644 index 0000000000..c5d6971870 --- /dev/null +++ b/packages/telescope/types/generators/create-sdk-module-stargate-clients.d.ts @@ -0,0 +1,3 @@ +import { Bundler } from '../bundler'; +import { TelescopeBuilder } from '../builder'; +export declare const plugin: (builder: TelescopeBuilder, bundler: Bundler) => void; diff --git a/packages/telescope/types/generators/create-unified-stargate-clients.d.ts b/packages/telescope/types/generators/create-unified-stargate-clients.d.ts new file mode 100644 index 0000000000..3ec94a9eb6 --- /dev/null +++ b/packages/telescope/types/generators/create-unified-stargate-clients.d.ts @@ -0,0 +1,3 @@ +import { TelescopeBuilder } from '../builder'; +import { BundlerFile } from '../types'; +export declare const plugin: (builder: TelescopeBuilder, allRegistries: BundlerFile[], allConverters: BundlerFile[]) => void; diff --git a/packages/types/src/telescope.ts b/packages/types/src/telescope.ts index 3fadc45eb7..5547c787f4 100644 --- a/packages/types/src/telescope.ts +++ b/packages/types/src/telescope.ts @@ -219,6 +219,18 @@ export interface TelescopeOpts { }; rpcClients?: { type?: "tendermint" | "grpc-web" | "grpc-gateway"; + clientStyle?: { + useUpdatedClientStyle?: boolean, + type?: ("all-client" | "sdk-module-client" | "custom-client")[], + customClientOption?: { + name: string; + fileName: string; + include?: { + patterns?: string[]; + }; + }[], + sdkModuleClientOption?: string[], + } enabled: boolean; inline?: boolean; extensions?: boolean; @@ -227,13 +239,13 @@ export interface TelescopeOpts { bundle?: boolean; serviceImplement?: { [ - key: - | "Msg" - | "Query" - | "Service" - | "ReflectionService" - | "ABCIApplication" - | string + key: + | "Msg" + | "Query" + | "Service" + | "ReflectionService" + | "ABCIApplication" + | string ]: { include?: { patterns?: string[]; @@ -278,13 +290,6 @@ export interface TelescopeOpts { }; }[]; useConnectComet?: boolean; - combinedClient?: { - name: string; - fileName: string; - include?: { - patterns?: string[]; - }; - }[]; }; helperFuncCreators?: { enabled: boolean; @@ -480,6 +485,9 @@ export const defaultTelescopeOptions: TelescopeOptions = { }, rpcClients: { type: "tendermint", + clientStyle: { + useUpdatedClientStyle: false, + }, enabled: true, extensions: true, inline: false, diff --git a/packages/types/types/telescope.d.ts b/packages/types/types/telescope.d.ts index c47f9ac396..7be61f43c4 100644 --- a/packages/types/types/telescope.d.ts +++ b/packages/types/types/telescope.d.ts @@ -182,6 +182,18 @@ export interface TelescopeOpts { }; rpcClients?: { type?: "tendermint" | "grpc-web" | "grpc-gateway"; + clientStyle?: { + useUpdatedClientStyle?: boolean; + type?: ("all-client" | "sdk-module-client" | "custom-client")[]; + customClientOption?: { + name: string; + fileName: string; + include?: { + patterns?: string[]; + }; + }[]; + sdkModuleClientOption?: string[]; + }; enabled: boolean; inline?: boolean; extensions?: boolean; @@ -225,13 +237,6 @@ export interface TelescopeOpts { }; }[]; useConnectComet?: boolean; - combinedClient?: { - name: string; - fileName: string; - include?: { - patterns?: string[]; - }; - }[]; }; helperFuncCreators?: { enabled: boolean;