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 9b55324
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 1 deletion.
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,105 @@ export class CosmosAuthAccount {
}
```

## Combined Clients Methods

Using combined option to expose instant RPC methods.

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.audit.v1beta1*",
"akash.audit.v1beta2*",
],
},
},
],
}
```

There'll be extra files generated in the root folder according to fileName in the setting.
The combined client will import proto files accroding to include.patterns.
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
}: {
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;
};
```

## Manually registering types

This example is with `osmosis` module in `osmojs`, but it is the same pattern for any module.
Expand Down
72 changes: 72 additions & 0 deletions __fixtures__/v-next/outputinstantrpc/akash-cosmos-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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 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 cosmosGroupV1TxAmino from "./cosmos/group/v1/tx.amino";
import * as cosmosNftV1beta1TxAmino from "./cosmos/nft/v1beta1/tx.amino";
export const akashCosmosAminoConverters = {
...akashAuditV1beta1AuditAmino.AminoConverter,
...akashAuditV1beta2AuditAmino.AminoConverter,
...cosmosGroupV1TxAmino.AminoConverter,
...cosmosNftV1beta1TxAmino.AminoConverter
};
export const akashCosmosProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...akashAuditV1beta1AuditRegistry.registry, ...akashAuditV1beta2AuditRegistry.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;
};
99 changes: 99 additions & 0 deletions packages/telescope/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,105 @@ export class CosmosAuthAccount {
}
```

## Combined Clients Methods

Using combined option to expose instant RPC methods.

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.audit.v1beta1*",
"akash.audit.v1beta2*",
],
},
},
],
}
```

There'll be extra files generated in the root folder according to fileName in the setting.
The combined client will import proto files accroding to include.patterns.
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
}: {
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;
};
```

## 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.audit.v1beta1*",
"akash.audit.v1beta2*",
],
},
},
],
},

reactQuery: {
Expand Down
Loading

0 comments on commit 9b55324

Please sign in to comment.