Skip to content

Commit

Permalink
Added generation for combined clients in root directory, added docs a…
Browse files Browse the repository at this point in the history
…ccordingly
  • Loading branch information
NorOldBurden committed Nov 25, 2024
1 parent ad9f1c5 commit 1bbb08e
Show file tree
Hide file tree
Showing 13 changed files with 489 additions and 1 deletion.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ 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`

See [RPC Clients](#rpc-clients) for more info.

Expand Down Expand Up @@ -1060,6 +1063,69 @@ export class CosmosAuthAccount {
}
```

## Combined Clients Methods

Using combined option to generate multiple module clients

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*",
],
},
},
],
}
```

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:
```ts
export const cosmosIbcAminoConverters = {
...cosmosGovV1TxAmino.AminoConverter,
...cosmosGovV1beta1TxAmino.AminoConverter,
...ibcCoreChannelV1TxAmino.AminoConverter
};
export const cosmosIbcProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...cosmosGovV1TxRegistry.registry, ...cosmosGovV1beta1TxRegistry.registry, ...ibcCoreChannelV1TxRegistry.registry];
export const getCosmosIbcSigningClientOptions = ({
defaultTypes = defaultRegistryTypes
}: {
...
};
export const getCosmosIbcSigningClient = async ({
rpcEndpoint,
signer,
defaultTypes = defaultRegistryTypes
}: {
rpcEndpoint: string | HttpEndpoint;
signer: OfflineSigner;
defaultTypes?: ReadonlyArray<[string, GeneratedType]>;
}) => {
...
};
```
## Manually registering types
This example is with `osmosis` module in `osmojs`, but it is the same pattern for any module.
Expand Down
84 changes: 84 additions & 0 deletions __fixtures__/v-next/outputinstantrpc/akash-cosmos-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 akashProviderV1beta2ProviderRegistry from "./akash/provider/v1beta2/provider.registry";
import * as cosmosGroupV1TxRegistry from "./cosmos/group/v1/tx.registry";
import * as cosmosNftV1beta1TxRegistry from "./cosmos/nft/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 akashProviderV1beta2ProviderAmino from "./akash/provider/v1beta2/provider.amino";
import * as cosmosGroupV1TxAmino from "./cosmos/group/v1/tx.amino";
import * as cosmosNftV1beta1TxAmino from "./cosmos/nft/v1beta1/tx.amino";
export const akashCosmosAminoConverters = {
...akashAuditV1beta1AuditAmino.AminoConverter,
...akashAuditV1beta2AuditAmino.AminoConverter,
...akashCertV1beta2CertAmino.AminoConverter,
...akashDeploymentV1beta2ServiceAmino.AminoConverter,
...akashMarketV1beta2ServiceAmino.AminoConverter,
...akashProviderV1beta2ProviderAmino.AminoConverter,
...cosmosGroupV1TxAmino.AminoConverter,
...cosmosNftV1beta1TxAmino.AminoConverter
};
export const akashCosmosProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...akashAuditV1beta1AuditRegistry.registry, ...akashAuditV1beta2AuditRegistry.registry, ...akashCertV1beta2CertRegistry.registry, ...akashDeploymentV1beta2ServiceRegistry.registry, ...akashMarketV1beta2ServiceRegistry.registry, ...akashProviderV1beta2ProviderRegistry.registry, ...cosmosGroupV1TxRegistry.registry, ...cosmosNftV1beta1TxRegistry.registry];
export const getAkashCosmosSigningClientOptions = ({
defaultTypes = defaultRegistryTypes
}: {
defaultTypes?: ReadonlyArray<[string, GeneratedType]>;
} = {}): {
registry: Registry;
aminoTypes: AminoTypes;
} => {
const registry = new Registry([...defaultTypes, ...akashCosmosProtoRegistry]);
const aminoTypes = new AminoTypes({
...akashCosmosAminoConverters
});
return {
registry,
aminoTypes
};
};
export const getAkashCosmosSigningClient = async ({
rpcEndpoint,
signer,
defaultTypes = defaultRegistryTypes
}: {
rpcEndpoint: string | HttpEndpoint;
signer: OfflineSigner;
defaultTypes?: ReadonlyArray<[string, GeneratedType]>;
}) => {
const {
registry,
aminoTypes
} = getAkashCosmosSigningClientOptions({
defaultTypes
});
const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, {
registry: (registry as any),
aminoTypes
});
return client;
};
export const getAkashCosmosSigningTxRpc = async ({
rpcEndpoint,
signer
}: SigningClientParams) => {
let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc);
const signingClient = await getAkashCosmosSigningClient({
rpcEndpoint,
signer
});
txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => {
return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise<DeliverTxResponse>);
};
return txRpc;
};
69 changes: 69 additions & 0 deletions __fixtures__/v-next/outputinstantrpc/cosmos-ibc-client.ts
Original file line number Diff line number Diff line change
@@ -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 cosmosIbcAminoConverters = {
...cosmosGovV1TxAmino.AminoConverter,
...cosmosGovV1beta1TxAmino.AminoConverter,
...ibcCoreChannelV1TxAmino.AminoConverter
};
export const cosmosIbcProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...cosmosGovV1TxRegistry.registry, ...cosmosGovV1beta1TxRegistry.registry, ...ibcCoreChannelV1TxRegistry.registry];
export const getCosmosIbcSigningClientOptions = ({
defaultTypes = defaultRegistryTypes
}: {
defaultTypes?: ReadonlyArray<[string, GeneratedType]>;
} = {}): {
registry: Registry;
aminoTypes: AminoTypes;
} => {
const registry = new Registry([...defaultTypes, ...cosmosIbcProtoRegistry]);
const aminoTypes = new AminoTypes({
...cosmosIbcAminoConverters
});
return {
registry,
aminoTypes
};
};
export const getCosmosIbcSigningClient = async ({
rpcEndpoint,
signer,
defaultTypes = defaultRegistryTypes
}: {
rpcEndpoint: string | HttpEndpoint;
signer: OfflineSigner;
defaultTypes?: ReadonlyArray<[string, GeneratedType]>;
}) => {
const {
registry,
aminoTypes
} = getCosmosIbcSigningClientOptions({
defaultTypes
});
const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, {
registry: (registry as any),
aminoTypes
});
return client;
};
export const getCosmosIbcSigningTxRpc = async ({
rpcEndpoint,
signer
}: SigningClientParams) => {
let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc);
const signingClient = await getCosmosIbcSigningClient({
rpcEndpoint,
signer
});
txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => {
return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise<DeliverTxResponse>);
};
return txRpc;
};
66 changes: 66 additions & 0 deletions packages/telescope/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ 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`

See [RPC Clients](#rpc-clients) for more info.

Expand Down Expand Up @@ -1060,6 +1063,69 @@ export class CosmosAuthAccount {
}
```

## Combined Clients Methods

Using combined option to generate multiple module clients

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*",
],
},
},
],
}
```

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:
```ts
export const cosmosIbcAminoConverters = {
...cosmosGovV1TxAmino.AminoConverter,
...cosmosGovV1beta1TxAmino.AminoConverter,
...ibcCoreChannelV1TxAmino.AminoConverter
};
export const cosmosIbcProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...cosmosGovV1TxRegistry.registry, ...cosmosGovV1beta1TxRegistry.registry, ...ibcCoreChannelV1TxRegistry.registry];
export const getCosmosIbcSigningClientOptions = ({
defaultTypes = defaultRegistryTypes
}: {
...
};
export const getCosmosIbcSigningClient = async ({
rpcEndpoint,
signer,
defaultTypes = defaultRegistryTypes
}: {
rpcEndpoint: string | HttpEndpoint;
signer: OfflineSigner;
defaultTypes?: ReadonlyArray<[string, GeneratedType]>;
}) => {
...
};
```
## Manually registering types
This example is with `osmosis` module in `osmojs`, but it is the same pattern for any module.
Expand Down
25 changes: 25 additions & 0 deletions packages/telescope/__tests__/telescope-instant-rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,31 @@ 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: {
Expand Down
13 changes: 13 additions & 0 deletions packages/telescope/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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 createBundle } from './generators/create-bundle';
import { plugin as createIndex } from './generators/create-index';
import { plugin as createHelpers } from './generators/create-helpers';
Expand Down Expand Up @@ -146,6 +147,8 @@ export class TelescopeBuilder {

customizeUtils(this);

const allConverters = [];
const allRegistries = [];
// [x] get bundle of all packages
const bundles = bundlePackages(this.store).map((bundle) => {
// store bundleFile in filesToInclude
Expand All @@ -171,10 +174,20 @@ export class TelescopeBuilder {

// [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)

}
return bundler;
});

if (this.options.rpcClients.combinedClient && this.options.rpcClients.combinedClient.length !== 0) {
createCombinedStargateClients(this, allRegistries, allConverters)
}

// post run plugins
bundles.forEach((bundler) => {
createLCDClientsScoped(this, bundler);
Expand Down
Loading

0 comments on commit 1bbb08e

Please sign in to comment.