@cosmwasm/ts-codegen 1.13.0 → 1.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -126,6 +126,44 @@ Typescript types and interfaces are generated in separate files so they can be i
126
126
  | `types.aliasExecuteMsg` | generate a type alias based on the contract name |
127
127
  | `types.aliasEntryPoints` | generate type aliases for the entry points based on the contract name |
128
128
 
129
+ ### BaseClient
130
+ The `baseClient.ts` will be generated as dependency for most files. It includes the base client for interchainjs.
131
+
132
+ #### Gas Configuration
133
+
134
+ The generated client provides flexible gas fee configuration options to handle different blockchain networks and fee settings.
135
+
136
+ ##### Default Gas Settings
137
+
138
+ By default, the client uses a gas limit of `200000` for all transactions. You can customize this behavior through the `setDefaultGasAmount`.
139
+
140
+ ##### ChainConfig Options
141
+
142
+ The `ChainConfig` interface supports two approaches for gas configuration:
143
+
144
+ 1. Chain Registry Integration (Recommended)
145
+
146
+ When you provide chain information, the client automatically fetches gas prices from the chain registry:
147
+
148
+ ```typescript
149
+ import { useChain } from '@interchain-kit/react';
150
+
151
+ const { chain } = useChain('osmosistestnet');
152
+ const chainConfig: ChainConfig = { chain: chain };
153
+ ```
154
+
155
+ 2. Manual Gas Price Configuration
156
+ You can explicitly set gas prices for more control:
157
+
158
+ ```typescript
159
+ const chainConfig: ChainConfig = {
160
+ gasPrice: {
161
+ denom: 'uosmo',
162
+ amount: '0.025'
163
+ }
164
+ };
165
+ ```
166
+
129
167
  ### Client
130
168
 
131
169
  The `client` plugin will generate TS client classes for your contracts. This option generates a `QueryClient` for queries as well as a `Client` for queries and mutations.
@@ -173,7 +211,7 @@ Generate [recoil](https://recoiljs.org/) bindings for your contracts with the `r
173
211
 
174
212
  ### Message Composer
175
213
 
176
- Generate pure message objects with the proper `utf8` encoding and `typeUrl` configured that you can broadcast yourself via `cosmjs` with the `message-composer` command.
214
+ Generate pure message objects with the proper `utf8` encoding and `typeUrl` configured that you can broadcast yourself via `interchainjs` with the `message-composer` command.
177
215
 
178
216
  [see example output code](https://github.com/hyperweb-io/ts-codegen/blob/main/__output__/sg721/Sg721.message-composer.ts)
179
217
 
@@ -0,0 +1,184 @@
1
+ export const baseClient = `
2
+ import { StdFee, Coin } from '@interchainjs/types';
3
+ import { DirectSigner } from '@interchainjs/cosmos';
4
+ import { getSmartContractState } from 'interchainjs/cosmwasm/wasm/v1/query.rpc.func';
5
+ import { executeContract } from 'interchainjs/cosmwasm/wasm/v1/tx.rpc.func';
6
+ import { QuerySmartContractStateRequest, QuerySmartContractStateResponse } from 'interchainjs/cosmwasm/wasm/v1/query';
7
+ import { MsgExecuteContract } from 'interchainjs/cosmwasm/wasm/v1/tx';
8
+ import { Chain } from '@chain-registry/v2-types';
9
+
10
+ // Encoding utility functions
11
+ const fromUint8Array = <T>(uint8Array: Uint8Array): T => {
12
+ const text = new TextDecoder().decode(uint8Array);
13
+ return JSON.parse(text);
14
+ };
15
+
16
+ const toUint8Array = (obj: any): Uint8Array => {
17
+ const text = JSON.stringify(obj);
18
+ return new TextEncoder().encode(text);
19
+ };
20
+
21
+ // Chain registry configuration
22
+ // The amount under gasPrice represents gas price per unit
23
+ export interface ChainConfig {
24
+ chain?: Chain;
25
+ gasPrice?: {
26
+ denom: string;
27
+ amount: string;
28
+ };
29
+ }
30
+
31
+ // Gas fee calculation utilities
32
+ export const calculateGasFromChain = (chain: Chain, gasAmount: string): StdFee => {
33
+ try {
34
+ const feeTokens = chain.fees?.feeTokens;
35
+
36
+ if (feeTokens && feeTokens.length > 0) {
37
+ const primaryToken = feeTokens[0];
38
+ // v2 chain-registry uses camelCase: averageGasPrice, lowGasPrice, fixedMinGasPrice
39
+ const gasPrice = primaryToken.averageGasPrice || primaryToken.lowGasPrice || primaryToken.fixedMinGasPrice || 0.025;
40
+ const gasAmountNum = parseInt(gasAmount);
41
+ const feeAmount = Math.ceil(gasAmountNum * gasPrice).toString();
42
+
43
+ return {
44
+ amount: [{
45
+ denom: primaryToken.denom,
46
+ amount: feeAmount
47
+ }],
48
+ gas: gasAmount
49
+ };
50
+ }
51
+ } catch (error) {
52
+ console.warn('Failed to calculate gas from chain registry:', error);
53
+ }
54
+
55
+ // Fallback to default
56
+ return { amount: [], gas: gasAmount };
57
+ };
58
+
59
+ // Default gas amount - users can easily change this
60
+ export let DEFAULT_GAS_AMOUNT = '200000';
61
+
62
+ // Allow users to set their preferred default gas amount
63
+ export const setDefaultGasAmount = (gasAmount: string): void => {
64
+ DEFAULT_GAS_AMOUNT = gasAmount;
65
+ };
66
+
67
+ // Get current default gas amount
68
+ export const getDefaultGasAmount = (): string => DEFAULT_GAS_AMOUNT;
69
+
70
+ export const getAutoGasFee = (chainConfig?: ChainConfig): StdFee => {
71
+ const gasAmount = DEFAULT_GAS_AMOUNT;
72
+
73
+ if (chainConfig?.chain) {
74
+ return calculateGasFromChain(chainConfig.chain, gasAmount);
75
+ }
76
+
77
+ if (chainConfig?.gasPrice) {
78
+ const gasAmountNum = parseInt(gasAmount);
79
+ const gasPriceNum = parseFloat(chainConfig.gasPrice.amount);
80
+ const feeAmount = Math.ceil(gasAmountNum * gasPriceNum).toString();
81
+
82
+ return {
83
+ amount: [{
84
+ denom: chainConfig.gasPrice.denom,
85
+ amount: feeAmount
86
+ }],
87
+ gas: gasAmount
88
+ };
89
+ }
90
+
91
+ // Fallback: no fee tokens, just gas amount
92
+ return { amount: [], gas: gasAmount };
93
+ };
94
+
95
+ // InterchainJS interfaces for CosmWasm clients
96
+ export interface ICosmWasmClient {
97
+ queryContractSmart(contractAddr: string, query: any): Promise<any>;
98
+ }
99
+
100
+ export interface ISigningCosmWasmClient {
101
+ execute(
102
+ sender: string,
103
+ contractAddress: string,
104
+ msg: any,
105
+ fee?: number | StdFee | "auto",
106
+ memo?: string,
107
+ funds?: Coin[],
108
+ chainConfig?: ChainConfig
109
+ ): Promise<any>;
110
+ }
111
+
112
+ export interface ISigningClient {
113
+ signAndBroadcast(
114
+ signerAddress: string,
115
+ messages: any[],
116
+ fee: number | StdFee | "auto",
117
+ memo?: string
118
+ ): Promise<any>;
119
+ }
120
+
121
+ // Helper functions to create InterchainJS clients
122
+ export function getCosmWasmClient(rpcEndpoint: string): ICosmWasmClient {
123
+ return {
124
+ queryContractSmart: async (contractAddr: string, query: any) => {
125
+ // Create the request object
126
+ const request: QuerySmartContractStateRequest = {
127
+ address: contractAddr,
128
+ queryData: toUint8Array(query)
129
+ };
130
+
131
+ // Execute the query using InterchainJS
132
+ const response: QuerySmartContractStateResponse = await getSmartContractState(rpcEndpoint, request);
133
+
134
+ // Parse and return the result
135
+ return fromUint8Array(response.data);
136
+ },
137
+ };
138
+ }
139
+
140
+ export function getSigningCosmWasmClient(signingClient: DirectSigner): ISigningCosmWasmClient {
141
+ return {
142
+ execute: async (
143
+ sender: string,
144
+ contractAddress: string,
145
+ msg: any,
146
+ fee?: number | StdFee | "auto",
147
+ memo?: string,
148
+ funds?: Coin[],
149
+ chainConfig?: ChainConfig
150
+ ) => {
151
+ // Handle fee conversion
152
+ let finalFee: StdFee;
153
+ if (typeof fee === 'number') {
154
+ finalFee = { amount: [], gas: fee.toString() };
155
+ } else if (fee === 'auto') {
156
+ finalFee = getAutoGasFee(chainConfig);
157
+ } else if (fee) {
158
+ finalFee = fee;
159
+ } else {
160
+ finalFee = getAutoGasFee(chainConfig);
161
+ }
162
+
163
+ // Create the message object
164
+ const message: MsgExecuteContract = {
165
+ sender,
166
+ contract: contractAddress,
167
+ msg: toUint8Array(msg),
168
+ funds: funds || []
169
+ };
170
+
171
+ // Execute the transaction using InterchainJS
172
+ const result = await executeContract(
173
+ signingClient as any,
174
+ sender,
175
+ message,
176
+ finalFee,
177
+ memo || ''
178
+ );
179
+
180
+ return result;
181
+ },
182
+ };
183
+ }
184
+ `;
@@ -1,13 +1,15 @@
1
1
  export const contractContextBase = `
2
2
  import {
3
- CosmWasmClient,
4
- SigningCosmWasmClient,
5
- } from '@cosmjs/cosmwasm-stargate';
3
+ ICosmWasmClient,
4
+ ISigningCosmWasmClient,
5
+ getCosmWasmClient,
6
+ getSigningCosmWasmClient,
7
+ } from './baseClient';
6
8
 
7
9
  export interface IContractConstructor {
8
10
  address: string | undefined;
9
- cosmWasmClient: CosmWasmClient | undefined;
10
- signingCosmWasmClient: SigningCosmWasmClient | undefined;
11
+ cosmWasmClient: ICosmWasmClient | undefined;
12
+ signingCosmWasmClient: ISigningCosmWasmClient | undefined;
11
13
  }
12
14
 
13
15
  export const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';
@@ -49,15 +51,15 @@ export class ContractBase<
49
51
  > {
50
52
 
51
53
  address: string | undefined;
52
- cosmWasmClient: CosmWasmClient | undefined;
53
- signingCosmWasmClient: SigningCosmWasmClient | undefined;
54
+ cosmWasmClient: ICosmWasmClient | undefined;
55
+ signingCosmWasmClient: ISigningCosmWasmClient | undefined;
54
56
  TSign?: new (
55
- client: SigningCosmWasmClient,
57
+ client: ISigningCosmWasmClient,
56
58
  sender: string,
57
59
  contractAddress: string
58
60
  ) => TSign;
59
61
  TQuery?: new (
60
- client: CosmWasmClient,
62
+ client: ICosmWasmClient,
61
63
  contractAddress: string
62
64
  ) => TQuery;
63
65
  TMsgComposer?: new (
@@ -67,15 +69,15 @@ export class ContractBase<
67
69
 
68
70
  constructor(
69
71
  address: string | undefined,
70
- cosmWasmClient: CosmWasmClient | undefined,
71
- signingCosmWasmClient: SigningCosmWasmClient | undefined,
72
+ cosmWasmClient: ICosmWasmClient | undefined,
73
+ signingCosmWasmClient: ISigningCosmWasmClient | undefined,
72
74
  TSign?: new (
73
- client: SigningCosmWasmClient,
75
+ client: ISigningCosmWasmClient,
74
76
  sender: string,
75
77
  contractAddress: string
76
78
  ) => TSign,
77
79
  TQuery?: new (
78
- client: CosmWasmClient,
80
+ client: ICosmWasmClient,
79
81
  contractAddress: string
80
82
  ) => TQuery,
81
83
  TMsgComposer?: new (
@@ -1,13 +1,15 @@
1
1
  export const contractContextBaseShortHandCtor = `
2
2
  import {
3
- CosmWasmClient,
4
- SigningCosmWasmClient,
5
- } from '@cosmjs/cosmwasm-stargate';
3
+ ICosmWasmClient,
4
+ ISigningCosmWasmClient,
5
+ getCosmWasmClient,
6
+ getSigningCosmWasmClient,
7
+ } from './baseClient';
6
8
 
7
9
  export interface IContractConstructor {
8
10
  address: string | undefined;
9
- cosmWasmClient: CosmWasmClient | undefined;
10
- signingCosmWasmClient: SigningCosmWasmClient | undefined;
11
+ cosmWasmClient: ICosmWasmClient | undefined;
12
+ signingCosmWasmClient: ISigningCosmWasmClient | undefined;
11
13
  }
12
14
 
13
15
  export const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';
@@ -49,15 +51,15 @@ export class ContractBase<
49
51
  > {
50
52
  constructor(
51
53
  protected address: string | undefined,
52
- protected cosmWasmClient: CosmWasmClient | undefined,
53
- protected signingCosmWasmClient: SigningCosmWasmClient | undefined,
54
+ protected cosmWasmClient: ICosmWasmClient | undefined,
55
+ protected signingCosmWasmClient: ISigningCosmWasmClient | undefined,
54
56
  private TSign?: new (
55
- client: SigningCosmWasmClient,
57
+ client: ISigningCosmWasmClient,
56
58
  sender: string,
57
59
  contractAddress: string
58
60
  ) => TSign,
59
61
  private TQuery?: new (
60
- client: CosmWasmClient,
62
+ client: ICosmWasmClient,
61
63
  contractAddress: string
62
64
  ) => TQuery,
63
65
  private TMsgComposer?: new (
@@ -1,16 +1,16 @@
1
1
  export const contractsContextTSX = `
2
2
  import React, { useEffect, useMemo, useRef, useState, useContext } from 'react';
3
3
  import {
4
- CosmWasmClient,
5
- SigningCosmWasmClient,
6
- } from '@cosmjs/cosmwasm-stargate';
4
+ ICosmWasmClient,
5
+ ISigningCosmWasmClient,
6
+ } from './baseClient';
7
7
 
8
8
  import { IContractsContext, getProviders } from './contractContextProviders';
9
9
 
10
10
  export interface ContractsConfig {
11
11
  address: string | undefined;
12
- getCosmWasmClient: () => Promise<CosmWasmClient>;
13
- getSigningCosmWasmClient: () => Promise<SigningCosmWasmClient>;
12
+ getCosmWasmClient: () => Promise<ICosmWasmClient>;
13
+ getSigningCosmWasmClient: () => Promise<ISigningCosmWasmClient>;
14
14
  }
15
15
 
16
16
  const ContractsContext = React.createContext<IContractsContext | null>(null);
@@ -22,9 +22,9 @@ export const ContractsProvider = ({
22
22
  children: React.ReactNode;
23
23
  contractsConfig: ContractsConfig;
24
24
  }) => {
25
- const [cosmWasmClient, setCosmWasmClient] = useState<CosmWasmClient>();
25
+ const [cosmWasmClient, setCosmWasmClient] = useState<ICosmWasmClient>();
26
26
  const [signingCosmWasmClient, setSigningCosmWasmClient] =
27
- useState<SigningCosmWasmClient>();
27
+ useState<ISigningCosmWasmClient>();
28
28
 
29
29
  const { address, getCosmWasmClient, getSigningCosmWasmClient } =
30
30
  contractsConfig;
@@ -1,6 +1,6 @@
1
1
  import { sync as mkdirp } from 'mkdirp';
2
2
  import { basename, dirname, extname, join } from 'path';
3
- import { contractContextBase, contractContextBaseShortHandCtor, contractsContextTSX, } from '../helpers';
3
+ import { contractContextBase, contractContextBaseShortHandCtor, contractsContextTSX, baseClient, } from '../helpers';
4
4
  import { writeContentToFile } from '../utils/files';
5
5
  import { header } from '../utils/header';
6
6
  const write = (outPath, file, content, varname) => {
@@ -17,6 +17,8 @@ const write = (outPath, file, content, varname) => {
17
17
  };
18
18
  export const createHelpers = (input, builderContext) => {
19
19
  const files = [];
20
+ // Always generate baseClient.ts since InterchainJS interfaces are needed by all clients
21
+ files.push(write(input.outPath, 'baseClient.ts', baseClient));
20
22
  if (input.options?.useContractsHook?.enabled &&
21
23
  Object.keys(builderContext.providers)?.length) {
22
24
  const useShorthandCtor = input.options?.useShorthandCtor;
@@ -1,3 +1,4 @@
1
1
  export * from './contractContextBase';
2
2
  export * from './contractContextBaseShortHandCtor';
3
+ export * from './baseClient';
3
4
  export * from './contractsContextTSX';
@@ -9,8 +9,10 @@ export class ContractsProviderBundlePlugin extends BuilderPluginBase {
9
9
  this.lifecycle = 'after';
10
10
  this.defaultContractName = 'contractContextProviders';
11
11
  this.utils = {
12
- CosmWasmClient: '@cosmjs/cosmwasm-stargate',
13
- SigningCosmWasmClient: '@cosmjs/cosmwasm-stargate',
12
+ ICosmWasmClient: '__baseClient__',
13
+ ISigningCosmWasmClient: '__baseClient__',
14
+ getCosmWasmClient: '__baseClient__',
15
+ getSigningCosmWasmClient: '__baseClient__',
14
16
  IQueryClientProvider: '__contractContextBase__',
15
17
  ISigningClientProvider: '__contractContextBase__',
16
18
  IMessageComposerProvider: '__contractContextBase__',
@@ -29,8 +31,10 @@ export class ContractsProviderBundlePlugin extends BuilderPluginBase {
29
31
  }
30
32
  const localname = 'contractContextProviders.ts';
31
33
  const body = [];
32
- context.addUtil('CosmWasmClient');
33
- context.addUtil('SigningCosmWasmClient');
34
+ context.addUtil('ICosmWasmClient');
35
+ context.addUtil('ISigningCosmWasmClient');
36
+ context.addUtil('getCosmWasmClient');
37
+ context.addUtil('getSigningCosmWasmClient');
34
38
  context.addUtil('IQueryClientProvider');
35
39
  context.addUtil('ISigningClientProvider');
36
40
  context.addUtil('IMessageComposerProvider');
@@ -0,0 +1 @@
1
+ export declare const baseClient = "\nimport { StdFee, Coin } from '@interchainjs/types';\nimport { DirectSigner } from '@interchainjs/cosmos';\nimport { getSmartContractState } from 'interchainjs/cosmwasm/wasm/v1/query.rpc.func';\nimport { executeContract } from 'interchainjs/cosmwasm/wasm/v1/tx.rpc.func';\nimport { QuerySmartContractStateRequest, QuerySmartContractStateResponse } from 'interchainjs/cosmwasm/wasm/v1/query';\nimport { MsgExecuteContract } from 'interchainjs/cosmwasm/wasm/v1/tx';\nimport { Chain } from '@chain-registry/v2-types';\n\n// Encoding utility functions\nconst fromUint8Array = <T>(uint8Array: Uint8Array): T => {\n const text = new TextDecoder().decode(uint8Array);\n return JSON.parse(text);\n};\n\nconst toUint8Array = (obj: any): Uint8Array => {\n const text = JSON.stringify(obj);\n return new TextEncoder().encode(text);\n};\n\n// Chain registry configuration\n// The amount under gasPrice represents gas price per unit\nexport interface ChainConfig {\n chain?: Chain;\n gasPrice?: {\n denom: string;\n amount: string;\n };\n}\n\n// Gas fee calculation utilities\nexport const calculateGasFromChain = (chain: Chain, gasAmount: string): StdFee => {\n try {\n const feeTokens = chain.fees?.feeTokens;\n \n if (feeTokens && feeTokens.length > 0) {\n const primaryToken = feeTokens[0];\n // v2 chain-registry uses camelCase: averageGasPrice, lowGasPrice, fixedMinGasPrice\n const gasPrice = primaryToken.averageGasPrice || primaryToken.lowGasPrice || primaryToken.fixedMinGasPrice || 0.025;\n const gasAmountNum = parseInt(gasAmount);\n const feeAmount = Math.ceil(gasAmountNum * gasPrice).toString();\n \n return {\n amount: [{\n denom: primaryToken.denom,\n amount: feeAmount\n }],\n gas: gasAmount\n };\n }\n } catch (error) {\n console.warn('Failed to calculate gas from chain registry:', error);\n }\n \n // Fallback to default\n return { amount: [], gas: gasAmount };\n};\n\n// Default gas amount - users can easily change this\nexport let DEFAULT_GAS_AMOUNT = '200000';\n\n// Allow users to set their preferred default gas amount\nexport const setDefaultGasAmount = (gasAmount: string): void => {\n DEFAULT_GAS_AMOUNT = gasAmount;\n};\n\n// Get current default gas amount\nexport const getDefaultGasAmount = (): string => DEFAULT_GAS_AMOUNT;\n\nexport const getAutoGasFee = (chainConfig?: ChainConfig): StdFee => {\n const gasAmount = DEFAULT_GAS_AMOUNT;\n \n if (chainConfig?.chain) {\n return calculateGasFromChain(chainConfig.chain, gasAmount);\n }\n \n if (chainConfig?.gasPrice) {\n const gasAmountNum = parseInt(gasAmount);\n const gasPriceNum = parseFloat(chainConfig.gasPrice.amount);\n const feeAmount = Math.ceil(gasAmountNum * gasPriceNum).toString();\n \n return {\n amount: [{\n denom: chainConfig.gasPrice.denom,\n amount: feeAmount\n }],\n gas: gasAmount\n };\n }\n \n // Fallback: no fee tokens, just gas amount\n return { amount: [], gas: gasAmount };\n};\n\n// InterchainJS interfaces for CosmWasm clients\nexport interface ICosmWasmClient {\n queryContractSmart(contractAddr: string, query: any): Promise<any>;\n}\n\nexport interface ISigningCosmWasmClient {\n execute(\n sender: string, \n contractAddress: string, \n msg: any, \n fee?: number | StdFee | \"auto\", \n memo?: string, \n funds?: Coin[], \n chainConfig?: ChainConfig\n ): Promise<any>;\n}\n\nexport interface ISigningClient {\n signAndBroadcast(\n signerAddress: string,\n messages: any[],\n fee: number | StdFee | \"auto\",\n memo?: string\n ): Promise<any>;\n}\n\n// Helper functions to create InterchainJS clients\nexport function getCosmWasmClient(rpcEndpoint: string): ICosmWasmClient {\n return {\n queryContractSmart: async (contractAddr: string, query: any) => {\n // Create the request object\n const request: QuerySmartContractStateRequest = {\n address: contractAddr,\n queryData: toUint8Array(query)\n };\n \n // Execute the query using InterchainJS\n const response: QuerySmartContractStateResponse = await getSmartContractState(rpcEndpoint, request);\n \n // Parse and return the result\n return fromUint8Array(response.data);\n },\n };\n}\n\nexport function getSigningCosmWasmClient(signingClient: DirectSigner): ISigningCosmWasmClient {\n return {\n execute: async (\n sender: string, \n contractAddress: string, \n msg: any, \n fee?: number | StdFee | \"auto\", \n memo?: string, \n funds?: Coin[], \n chainConfig?: ChainConfig\n ) => {\n // Handle fee conversion\n let finalFee: StdFee;\n if (typeof fee === 'number') {\n finalFee = { amount: [], gas: fee.toString() };\n } else if (fee === 'auto') {\n finalFee = getAutoGasFee(chainConfig);\n } else if (fee) {\n finalFee = fee;\n } else {\n finalFee = getAutoGasFee(chainConfig);\n }\n\n // Create the message object\n const message: MsgExecuteContract = {\n sender,\n contract: contractAddress,\n msg: toUint8Array(msg),\n funds: funds || []\n };\n \n // Execute the transaction using InterchainJS\n const result = await executeContract(\n signingClient as any,\n sender,\n message,\n finalFee,\n memo || ''\n );\n \n return result;\n },\n };\n}\n";
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.baseClient = void 0;
4
+ exports.baseClient = `
5
+ import { StdFee, Coin } from '@interchainjs/types';
6
+ import { DirectSigner } from '@interchainjs/cosmos';
7
+ import { getSmartContractState } from 'interchainjs/cosmwasm/wasm/v1/query.rpc.func';
8
+ import { executeContract } from 'interchainjs/cosmwasm/wasm/v1/tx.rpc.func';
9
+ import { QuerySmartContractStateRequest, QuerySmartContractStateResponse } from 'interchainjs/cosmwasm/wasm/v1/query';
10
+ import { MsgExecuteContract } from 'interchainjs/cosmwasm/wasm/v1/tx';
11
+ import { Chain } from '@chain-registry/v2-types';
12
+
13
+ // Encoding utility functions
14
+ const fromUint8Array = <T>(uint8Array: Uint8Array): T => {
15
+ const text = new TextDecoder().decode(uint8Array);
16
+ return JSON.parse(text);
17
+ };
18
+
19
+ const toUint8Array = (obj: any): Uint8Array => {
20
+ const text = JSON.stringify(obj);
21
+ return new TextEncoder().encode(text);
22
+ };
23
+
24
+ // Chain registry configuration
25
+ // The amount under gasPrice represents gas price per unit
26
+ export interface ChainConfig {
27
+ chain?: Chain;
28
+ gasPrice?: {
29
+ denom: string;
30
+ amount: string;
31
+ };
32
+ }
33
+
34
+ // Gas fee calculation utilities
35
+ export const calculateGasFromChain = (chain: Chain, gasAmount: string): StdFee => {
36
+ try {
37
+ const feeTokens = chain.fees?.feeTokens;
38
+
39
+ if (feeTokens && feeTokens.length > 0) {
40
+ const primaryToken = feeTokens[0];
41
+ // v2 chain-registry uses camelCase: averageGasPrice, lowGasPrice, fixedMinGasPrice
42
+ const gasPrice = primaryToken.averageGasPrice || primaryToken.lowGasPrice || primaryToken.fixedMinGasPrice || 0.025;
43
+ const gasAmountNum = parseInt(gasAmount);
44
+ const feeAmount = Math.ceil(gasAmountNum * gasPrice).toString();
45
+
46
+ return {
47
+ amount: [{
48
+ denom: primaryToken.denom,
49
+ amount: feeAmount
50
+ }],
51
+ gas: gasAmount
52
+ };
53
+ }
54
+ } catch (error) {
55
+ console.warn('Failed to calculate gas from chain registry:', error);
56
+ }
57
+
58
+ // Fallback to default
59
+ return { amount: [], gas: gasAmount };
60
+ };
61
+
62
+ // Default gas amount - users can easily change this
63
+ export let DEFAULT_GAS_AMOUNT = '200000';
64
+
65
+ // Allow users to set their preferred default gas amount
66
+ export const setDefaultGasAmount = (gasAmount: string): void => {
67
+ DEFAULT_GAS_AMOUNT = gasAmount;
68
+ };
69
+
70
+ // Get current default gas amount
71
+ export const getDefaultGasAmount = (): string => DEFAULT_GAS_AMOUNT;
72
+
73
+ export const getAutoGasFee = (chainConfig?: ChainConfig): StdFee => {
74
+ const gasAmount = DEFAULT_GAS_AMOUNT;
75
+
76
+ if (chainConfig?.chain) {
77
+ return calculateGasFromChain(chainConfig.chain, gasAmount);
78
+ }
79
+
80
+ if (chainConfig?.gasPrice) {
81
+ const gasAmountNum = parseInt(gasAmount);
82
+ const gasPriceNum = parseFloat(chainConfig.gasPrice.amount);
83
+ const feeAmount = Math.ceil(gasAmountNum * gasPriceNum).toString();
84
+
85
+ return {
86
+ amount: [{
87
+ denom: chainConfig.gasPrice.denom,
88
+ amount: feeAmount
89
+ }],
90
+ gas: gasAmount
91
+ };
92
+ }
93
+
94
+ // Fallback: no fee tokens, just gas amount
95
+ return { amount: [], gas: gasAmount };
96
+ };
97
+
98
+ // InterchainJS interfaces for CosmWasm clients
99
+ export interface ICosmWasmClient {
100
+ queryContractSmart(contractAddr: string, query: any): Promise<any>;
101
+ }
102
+
103
+ export interface ISigningCosmWasmClient {
104
+ execute(
105
+ sender: string,
106
+ contractAddress: string,
107
+ msg: any,
108
+ fee?: number | StdFee | "auto",
109
+ memo?: string,
110
+ funds?: Coin[],
111
+ chainConfig?: ChainConfig
112
+ ): Promise<any>;
113
+ }
114
+
115
+ export interface ISigningClient {
116
+ signAndBroadcast(
117
+ signerAddress: string,
118
+ messages: any[],
119
+ fee: number | StdFee | "auto",
120
+ memo?: string
121
+ ): Promise<any>;
122
+ }
123
+
124
+ // Helper functions to create InterchainJS clients
125
+ export function getCosmWasmClient(rpcEndpoint: string): ICosmWasmClient {
126
+ return {
127
+ queryContractSmart: async (contractAddr: string, query: any) => {
128
+ // Create the request object
129
+ const request: QuerySmartContractStateRequest = {
130
+ address: contractAddr,
131
+ queryData: toUint8Array(query)
132
+ };
133
+
134
+ // Execute the query using InterchainJS
135
+ const response: QuerySmartContractStateResponse = await getSmartContractState(rpcEndpoint, request);
136
+
137
+ // Parse and return the result
138
+ return fromUint8Array(response.data);
139
+ },
140
+ };
141
+ }
142
+
143
+ export function getSigningCosmWasmClient(signingClient: DirectSigner): ISigningCosmWasmClient {
144
+ return {
145
+ execute: async (
146
+ sender: string,
147
+ contractAddress: string,
148
+ msg: any,
149
+ fee?: number | StdFee | "auto",
150
+ memo?: string,
151
+ funds?: Coin[],
152
+ chainConfig?: ChainConfig
153
+ ) => {
154
+ // Handle fee conversion
155
+ let finalFee: StdFee;
156
+ if (typeof fee === 'number') {
157
+ finalFee = { amount: [], gas: fee.toString() };
158
+ } else if (fee === 'auto') {
159
+ finalFee = getAutoGasFee(chainConfig);
160
+ } else if (fee) {
161
+ finalFee = fee;
162
+ } else {
163
+ finalFee = getAutoGasFee(chainConfig);
164
+ }
165
+
166
+ // Create the message object
167
+ const message: MsgExecuteContract = {
168
+ sender,
169
+ contract: contractAddress,
170
+ msg: toUint8Array(msg),
171
+ funds: funds || []
172
+ };
173
+
174
+ // Execute the transaction using InterchainJS
175
+ const result = await executeContract(
176
+ signingClient as any,
177
+ sender,
178
+ message,
179
+ finalFee,
180
+ memo || ''
181
+ );
182
+
183
+ return result;
184
+ },
185
+ };
186
+ }
187
+ `;
@@ -1 +1 @@
1
- export declare const contractContextBase = "\nimport {\n CosmWasmClient,\n SigningCosmWasmClient,\n} from '@cosmjs/cosmwasm-stargate';\n\nexport interface IContractConstructor {\n address: string | undefined;\n cosmWasmClient: CosmWasmClient | undefined;\n signingCosmWasmClient: SigningCosmWasmClient | undefined;\n}\n\nexport const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';\n\nexport const NO_COSMWASW_CLIENT_ERROR_MESSAGE = 'cosmWasmClient not connected';\n\nexport const NO_ADDRESS_ERROR_MESSAGE = \"address doesn't exist\";\n\nexport const NO_SIGNING_CLIENT_ERROR_MESSAGE =\n 'Signing client is not generated. Please check ts-codegen config';\n\nexport const NO_QUERY_CLIENT_ERROR_MESSAGE =\n 'Query client is not generated. Please check ts-codegen config';\n\nexport const NO_MESSAGE_COMPOSER_ERROR_MESSAGE =\n 'Message composer client is not generated. Please check ts-codegen config';\n\n/**\n * a placeholder for non-generated classes\n */\nexport interface IEmptyClient {}\n\nexport interface ISigningClientProvider<T> {\n getSigningClient(contractAddr: string): T;\n}\n\nexport interface IQueryClientProvider<T> {\n getQueryClient(contractAddr: string): T;\n}\n\nexport interface IMessageComposerProvider<T> {\n getMessageComposer(contractAddr: string): T;\n}\n\nexport class ContractBase<\n TSign = IEmptyClient,\n TQuery = IEmptyClient,\n TMsgComposer = IEmptyClient\n> {\n\n address: string | undefined;\n cosmWasmClient: CosmWasmClient | undefined;\n signingCosmWasmClient: SigningCosmWasmClient | undefined;\n TSign?: new (\n client: SigningCosmWasmClient,\n sender: string,\n contractAddress: string\n ) => TSign;\n TQuery?: new (\n client: CosmWasmClient,\n contractAddress: string\n ) => TQuery;\n TMsgComposer?: new (\n sender: string,\n contractAddress: string\n ) => TMsgComposer;\n\n constructor(\n address: string | undefined,\n cosmWasmClient: CosmWasmClient | undefined,\n signingCosmWasmClient: SigningCosmWasmClient | undefined,\n TSign?: new (\n client: SigningCosmWasmClient,\n sender: string,\n contractAddress: string\n ) => TSign,\n TQuery?: new (\n client: CosmWasmClient,\n contractAddress: string\n ) => TQuery,\n TMsgComposer?: new (\n sender: string,\n contractAddress: string\n ) => TMsgComposer\n ) {\n this.address = address;\n this.cosmWasmClient = cosmWasmClient;\n this.signingCosmWasmClient = signingCosmWasmClient;\n this.TSign = TSign;\n this.TQuery = TQuery;\n this.TMsgComposer = TMsgComposer;\n }\n\n public getSigningClient(contractAddr: string): TSign {\n if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);\n if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);\n if (!this.TSign) throw new Error(NO_SIGNING_CLIENT_ERROR_MESSAGE);\n return new this.TSign(\n this.signingCosmWasmClient,\n this.address,\n contractAddr\n );\n }\n\n public getQueryClient(contractAddr: string): TQuery {\n if (!this.cosmWasmClient) throw new Error(NO_COSMWASW_CLIENT_ERROR_MESSAGE);\n if (!this.TQuery) throw new Error(NO_QUERY_CLIENT_ERROR_MESSAGE);\n return new this.TQuery(this.cosmWasmClient, contractAddr);\n }\n\n public getMessageComposer(contractAddr: string): TMsgComposer {\n if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);\n if (!this.TMsgComposer) throw new Error(NO_MESSAGE_COMPOSER_ERROR_MESSAGE);\n return new this.TMsgComposer(this.address, contractAddr);\n }\n}\n";
1
+ export declare const contractContextBase = "\nimport {\n ICosmWasmClient,\n ISigningCosmWasmClient,\n getCosmWasmClient,\n getSigningCosmWasmClient,\n} from './baseClient';\n\nexport interface IContractConstructor {\n address: string | undefined;\n cosmWasmClient: ICosmWasmClient | undefined;\n signingCosmWasmClient: ISigningCosmWasmClient | undefined;\n}\n\nexport const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';\n\nexport const NO_COSMWASW_CLIENT_ERROR_MESSAGE = 'cosmWasmClient not connected';\n\nexport const NO_ADDRESS_ERROR_MESSAGE = \"address doesn't exist\";\n\nexport const NO_SIGNING_CLIENT_ERROR_MESSAGE =\n 'Signing client is not generated. Please check ts-codegen config';\n\nexport const NO_QUERY_CLIENT_ERROR_MESSAGE =\n 'Query client is not generated. Please check ts-codegen config';\n\nexport const NO_MESSAGE_COMPOSER_ERROR_MESSAGE =\n 'Message composer client is not generated. Please check ts-codegen config';\n\n/**\n * a placeholder for non-generated classes\n */\nexport interface IEmptyClient {}\n\nexport interface ISigningClientProvider<T> {\n getSigningClient(contractAddr: string): T;\n}\n\nexport interface IQueryClientProvider<T> {\n getQueryClient(contractAddr: string): T;\n}\n\nexport interface IMessageComposerProvider<T> {\n getMessageComposer(contractAddr: string): T;\n}\n\nexport class ContractBase<\n TSign = IEmptyClient,\n TQuery = IEmptyClient,\n TMsgComposer = IEmptyClient\n> {\n\n address: string | undefined;\n cosmWasmClient: ICosmWasmClient | undefined;\n signingCosmWasmClient: ISigningCosmWasmClient | undefined;\n TSign?: new (\n client: ISigningCosmWasmClient,\n sender: string,\n contractAddress: string\n ) => TSign;\n TQuery?: new (\n client: ICosmWasmClient,\n contractAddress: string\n ) => TQuery;\n TMsgComposer?: new (\n sender: string,\n contractAddress: string\n ) => TMsgComposer;\n\n constructor(\n address: string | undefined,\n cosmWasmClient: ICosmWasmClient | undefined,\n signingCosmWasmClient: ISigningCosmWasmClient | undefined,\n TSign?: new (\n client: ISigningCosmWasmClient,\n sender: string,\n contractAddress: string\n ) => TSign,\n TQuery?: new (\n client: ICosmWasmClient,\n contractAddress: string\n ) => TQuery,\n TMsgComposer?: new (\n sender: string,\n contractAddress: string\n ) => TMsgComposer\n ) {\n this.address = address;\n this.cosmWasmClient = cosmWasmClient;\n this.signingCosmWasmClient = signingCosmWasmClient;\n this.TSign = TSign;\n this.TQuery = TQuery;\n this.TMsgComposer = TMsgComposer;\n }\n\n public getSigningClient(contractAddr: string): TSign {\n if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);\n if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);\n if (!this.TSign) throw new Error(NO_SIGNING_CLIENT_ERROR_MESSAGE);\n return new this.TSign(\n this.signingCosmWasmClient,\n this.address,\n contractAddr\n );\n }\n\n public getQueryClient(contractAddr: string): TQuery {\n if (!this.cosmWasmClient) throw new Error(NO_COSMWASW_CLIENT_ERROR_MESSAGE);\n if (!this.TQuery) throw new Error(NO_QUERY_CLIENT_ERROR_MESSAGE);\n return new this.TQuery(this.cosmWasmClient, contractAddr);\n }\n\n public getMessageComposer(contractAddr: string): TMsgComposer {\n if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);\n if (!this.TMsgComposer) throw new Error(NO_MESSAGE_COMPOSER_ERROR_MESSAGE);\n return new this.TMsgComposer(this.address, contractAddr);\n }\n}\n";
@@ -3,14 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.contractContextBase = void 0;
4
4
  exports.contractContextBase = `
5
5
  import {
6
- CosmWasmClient,
7
- SigningCosmWasmClient,
8
- } from '@cosmjs/cosmwasm-stargate';
6
+ ICosmWasmClient,
7
+ ISigningCosmWasmClient,
8
+ getCosmWasmClient,
9
+ getSigningCosmWasmClient,
10
+ } from './baseClient';
9
11
 
10
12
  export interface IContractConstructor {
11
13
  address: string | undefined;
12
- cosmWasmClient: CosmWasmClient | undefined;
13
- signingCosmWasmClient: SigningCosmWasmClient | undefined;
14
+ cosmWasmClient: ICosmWasmClient | undefined;
15
+ signingCosmWasmClient: ISigningCosmWasmClient | undefined;
14
16
  }
15
17
 
16
18
  export const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';
@@ -52,15 +54,15 @@ export class ContractBase<
52
54
  > {
53
55
 
54
56
  address: string | undefined;
55
- cosmWasmClient: CosmWasmClient | undefined;
56
- signingCosmWasmClient: SigningCosmWasmClient | undefined;
57
+ cosmWasmClient: ICosmWasmClient | undefined;
58
+ signingCosmWasmClient: ISigningCosmWasmClient | undefined;
57
59
  TSign?: new (
58
- client: SigningCosmWasmClient,
60
+ client: ISigningCosmWasmClient,
59
61
  sender: string,
60
62
  contractAddress: string
61
63
  ) => TSign;
62
64
  TQuery?: new (
63
- client: CosmWasmClient,
65
+ client: ICosmWasmClient,
64
66
  contractAddress: string
65
67
  ) => TQuery;
66
68
  TMsgComposer?: new (
@@ -70,15 +72,15 @@ export class ContractBase<
70
72
 
71
73
  constructor(
72
74
  address: string | undefined,
73
- cosmWasmClient: CosmWasmClient | undefined,
74
- signingCosmWasmClient: SigningCosmWasmClient | undefined,
75
+ cosmWasmClient: ICosmWasmClient | undefined,
76
+ signingCosmWasmClient: ISigningCosmWasmClient | undefined,
75
77
  TSign?: new (
76
- client: SigningCosmWasmClient,
78
+ client: ISigningCosmWasmClient,
77
79
  sender: string,
78
80
  contractAddress: string
79
81
  ) => TSign,
80
82
  TQuery?: new (
81
- client: CosmWasmClient,
83
+ client: ICosmWasmClient,
82
84
  contractAddress: string
83
85
  ) => TQuery,
84
86
  TMsgComposer?: new (
@@ -1 +1 @@
1
- export declare const contractContextBaseShortHandCtor = "\nimport {\n CosmWasmClient,\n SigningCosmWasmClient,\n} from '@cosmjs/cosmwasm-stargate';\n\nexport interface IContractConstructor {\n address: string | undefined;\n cosmWasmClient: CosmWasmClient | undefined;\n signingCosmWasmClient: SigningCosmWasmClient | undefined;\n}\n\nexport const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';\n\nexport const NO_COSMWASW_CLIENT_ERROR_MESSAGE = 'cosmWasmClient not connected';\n\nexport const NO_ADDRESS_ERROR_MESSAGE = \"address doesn't exist\";\n\nexport const NO_SIGNING_CLIENT_ERROR_MESSAGE =\n 'Signing client is not generated. Please check ts-codegen config';\n\nexport const NO_QUERY_CLIENT_ERROR_MESSAGE =\n 'Query client is not generated. Please check ts-codegen config';\n\nexport const NO_MESSAGE_COMPOSER_ERROR_MESSAGE =\n 'Message composer client is not generated. Please check ts-codegen config';\n\n/**\n * a placeholder for non-generated classes\n */\nexport interface IEmptyClient {}\n\nexport interface ISigningClientProvider<T> {\n getSigningClient(contractAddr: string): T;\n}\n\nexport interface IQueryClientProvider<T> {\n getQueryClient(contractAddr: string): T;\n}\n\nexport interface IMessageComposerProvider<T> {\n getMessageComposer(contractAddr: string): T;\n}\n\nexport class ContractBase<\n TSign = IEmptyClient,\n TQuery = IEmptyClient,\n TMsgComposer = IEmptyClient\n> {\n constructor(\n protected address: string | undefined,\n protected cosmWasmClient: CosmWasmClient | undefined,\n protected signingCosmWasmClient: SigningCosmWasmClient | undefined,\n private TSign?: new (\n client: SigningCosmWasmClient,\n sender: string,\n contractAddress: string\n ) => TSign,\n private TQuery?: new (\n client: CosmWasmClient,\n contractAddress: string\n ) => TQuery,\n private TMsgComposer?: new (\n sender: string,\n contractAddress: string\n ) => TMsgComposer\n ) {}\n\n public getSigningClient(contractAddr: string): TSign {\n if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);\n if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);\n if (!this.TSign) throw new Error(NO_SIGNING_CLIENT_ERROR_MESSAGE);\n return new this.TSign(\n this.signingCosmWasmClient,\n this.address,\n contractAddr\n );\n }\n\n public getQueryClient(contractAddr: string): TQuery {\n if (!this.cosmWasmClient) throw new Error(NO_COSMWASW_CLIENT_ERROR_MESSAGE);\n if (!this.TQuery) throw new Error(NO_QUERY_CLIENT_ERROR_MESSAGE);\n return new this.TQuery(this.cosmWasmClient, contractAddr);\n }\n\n public getMessageComposer(contractAddr: string): TMsgComposer {\n if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);\n if (!this.TMsgComposer) throw new Error(NO_MESSAGE_COMPOSER_ERROR_MESSAGE);\n return new this.TMsgComposer(this.address, contractAddr);\n }\n}\n";
1
+ export declare const contractContextBaseShortHandCtor = "\nimport {\n ICosmWasmClient,\n ISigningCosmWasmClient,\n getCosmWasmClient,\n getSigningCosmWasmClient,\n} from './baseClient';\n\nexport interface IContractConstructor {\n address: string | undefined;\n cosmWasmClient: ICosmWasmClient | undefined;\n signingCosmWasmClient: ISigningCosmWasmClient | undefined;\n}\n\nexport const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';\n\nexport const NO_COSMWASW_CLIENT_ERROR_MESSAGE = 'cosmWasmClient not connected';\n\nexport const NO_ADDRESS_ERROR_MESSAGE = \"address doesn't exist\";\n\nexport const NO_SIGNING_CLIENT_ERROR_MESSAGE =\n 'Signing client is not generated. Please check ts-codegen config';\n\nexport const NO_QUERY_CLIENT_ERROR_MESSAGE =\n 'Query client is not generated. Please check ts-codegen config';\n\nexport const NO_MESSAGE_COMPOSER_ERROR_MESSAGE =\n 'Message composer client is not generated. Please check ts-codegen config';\n\n/**\n * a placeholder for non-generated classes\n */\nexport interface IEmptyClient {}\n\nexport interface ISigningClientProvider<T> {\n getSigningClient(contractAddr: string): T;\n}\n\nexport interface IQueryClientProvider<T> {\n getQueryClient(contractAddr: string): T;\n}\n\nexport interface IMessageComposerProvider<T> {\n getMessageComposer(contractAddr: string): T;\n}\n\nexport class ContractBase<\n TSign = IEmptyClient,\n TQuery = IEmptyClient,\n TMsgComposer = IEmptyClient\n> {\n constructor(\n protected address: string | undefined,\n protected cosmWasmClient: ICosmWasmClient | undefined,\n protected signingCosmWasmClient: ISigningCosmWasmClient | undefined,\n private TSign?: new (\n client: ISigningCosmWasmClient,\n sender: string,\n contractAddress: string\n ) => TSign,\n private TQuery?: new (\n client: ICosmWasmClient,\n contractAddress: string\n ) => TQuery,\n private TMsgComposer?: new (\n sender: string,\n contractAddress: string\n ) => TMsgComposer\n ) {}\n\n public getSigningClient(contractAddr: string): TSign {\n if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);\n if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);\n if (!this.TSign) throw new Error(NO_SIGNING_CLIENT_ERROR_MESSAGE);\n return new this.TSign(\n this.signingCosmWasmClient,\n this.address,\n contractAddr\n );\n }\n\n public getQueryClient(contractAddr: string): TQuery {\n if (!this.cosmWasmClient) throw new Error(NO_COSMWASW_CLIENT_ERROR_MESSAGE);\n if (!this.TQuery) throw new Error(NO_QUERY_CLIENT_ERROR_MESSAGE);\n return new this.TQuery(this.cosmWasmClient, contractAddr);\n }\n\n public getMessageComposer(contractAddr: string): TMsgComposer {\n if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);\n if (!this.TMsgComposer) throw new Error(NO_MESSAGE_COMPOSER_ERROR_MESSAGE);\n return new this.TMsgComposer(this.address, contractAddr);\n }\n}\n";
@@ -3,14 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.contractContextBaseShortHandCtor = void 0;
4
4
  exports.contractContextBaseShortHandCtor = `
5
5
  import {
6
- CosmWasmClient,
7
- SigningCosmWasmClient,
8
- } from '@cosmjs/cosmwasm-stargate';
6
+ ICosmWasmClient,
7
+ ISigningCosmWasmClient,
8
+ getCosmWasmClient,
9
+ getSigningCosmWasmClient,
10
+ } from './baseClient';
9
11
 
10
12
  export interface IContractConstructor {
11
13
  address: string | undefined;
12
- cosmWasmClient: CosmWasmClient | undefined;
13
- signingCosmWasmClient: SigningCosmWasmClient | undefined;
14
+ cosmWasmClient: ICosmWasmClient | undefined;
15
+ signingCosmWasmClient: ISigningCosmWasmClient | undefined;
14
16
  }
15
17
 
16
18
  export const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';
@@ -52,15 +54,15 @@ export class ContractBase<
52
54
  > {
53
55
  constructor(
54
56
  protected address: string | undefined,
55
- protected cosmWasmClient: CosmWasmClient | undefined,
56
- protected signingCosmWasmClient: SigningCosmWasmClient | undefined,
57
+ protected cosmWasmClient: ICosmWasmClient | undefined,
58
+ protected signingCosmWasmClient: ISigningCosmWasmClient | undefined,
57
59
  private TSign?: new (
58
- client: SigningCosmWasmClient,
60
+ client: ISigningCosmWasmClient,
59
61
  sender: string,
60
62
  contractAddress: string
61
63
  ) => TSign,
62
64
  private TQuery?: new (
63
- client: CosmWasmClient,
65
+ client: ICosmWasmClient,
64
66
  contractAddress: string
65
67
  ) => TQuery,
66
68
  private TMsgComposer?: new (
@@ -1 +1 @@
1
- export declare const contractsContextTSX = "\nimport React, { useEffect, useMemo, useRef, useState, useContext } from 'react';\nimport {\n CosmWasmClient,\n SigningCosmWasmClient,\n} from '@cosmjs/cosmwasm-stargate';\n\nimport { IContractsContext, getProviders } from './contractContextProviders';\n\nexport interface ContractsConfig {\n address: string | undefined;\n getCosmWasmClient: () => Promise<CosmWasmClient>;\n getSigningCosmWasmClient: () => Promise<SigningCosmWasmClient>;\n}\n\nconst ContractsContext = React.createContext<IContractsContext | null>(null);\n\nexport const ContractsProvider = ({\n children,\n contractsConfig,\n}: {\n children: React.ReactNode;\n contractsConfig: ContractsConfig;\n}) => {\n const [cosmWasmClient, setCosmWasmClient] = useState<CosmWasmClient>();\n const [signingCosmWasmClient, setSigningCosmWasmClient] =\n useState<SigningCosmWasmClient>();\n\n const { address, getCosmWasmClient, getSigningCosmWasmClient } =\n contractsConfig;\n\n const prevAddressRef = useRef<string | undefined>(address);\n\n const contracts: IContractsContext = useMemo(() => {\n return getProviders(address, cosmWasmClient, signingCosmWasmClient);\n }, [address, cosmWasmClient, signingCosmWasmClient]);\n\n useEffect(() => {\n const connectSigningCwClient = async () => {\n if (address && prevAddressRef.current !== address) {\n const signingCosmWasmClient = await getSigningCosmWasmClient();\n setSigningCosmWasmClient(signingCosmWasmClient);\n } else if (!address) {\n setSigningCosmWasmClient(undefined);\n }\n prevAddressRef.current = address;\n };\n connectSigningCwClient();\n }, [address, getSigningCosmWasmClient]);\n\n useEffect(() => {\n const connectCosmWasmClient = async () => {\n const cosmWasmClient = await getCosmWasmClient();\n setCosmWasmClient(cosmWasmClient);\n };\n connectCosmWasmClient();\n }, [getCosmWasmClient]);\n\n return (\n <ContractsContext.Provider value={contracts}>\n {children}\n </ContractsContext.Provider>\n );\n};\n\nexport const useContracts = () => {\n const contracts: IContractsContext = useContext(ContractsContext);\n if (contracts === null) {\n throw new Error('useContracts must be used within a ContractsProvider');\n }\n return contracts;\n};\n";
1
+ export declare const contractsContextTSX = "\nimport React, { useEffect, useMemo, useRef, useState, useContext } from 'react';\nimport {\n ICosmWasmClient,\n ISigningCosmWasmClient,\n} from './baseClient';\n\nimport { IContractsContext, getProviders } from './contractContextProviders';\n\nexport interface ContractsConfig {\n address: string | undefined;\n getCosmWasmClient: () => Promise<ICosmWasmClient>;\n getSigningCosmWasmClient: () => Promise<ISigningCosmWasmClient>;\n}\n\nconst ContractsContext = React.createContext<IContractsContext | null>(null);\n\nexport const ContractsProvider = ({\n children,\n contractsConfig,\n}: {\n children: React.ReactNode;\n contractsConfig: ContractsConfig;\n}) => {\n const [cosmWasmClient, setCosmWasmClient] = useState<ICosmWasmClient>();\n const [signingCosmWasmClient, setSigningCosmWasmClient] =\n useState<ISigningCosmWasmClient>();\n\n const { address, getCosmWasmClient, getSigningCosmWasmClient } =\n contractsConfig;\n\n const prevAddressRef = useRef<string | undefined>(address);\n\n const contracts: IContractsContext = useMemo(() => {\n return getProviders(address, cosmWasmClient, signingCosmWasmClient);\n }, [address, cosmWasmClient, signingCosmWasmClient]);\n\n useEffect(() => {\n const connectSigningCwClient = async () => {\n if (address && prevAddressRef.current !== address) {\n const signingCosmWasmClient = await getSigningCosmWasmClient();\n setSigningCosmWasmClient(signingCosmWasmClient);\n } else if (!address) {\n setSigningCosmWasmClient(undefined);\n }\n prevAddressRef.current = address;\n };\n connectSigningCwClient();\n }, [address, getSigningCosmWasmClient]);\n\n useEffect(() => {\n const connectCosmWasmClient = async () => {\n const cosmWasmClient = await getCosmWasmClient();\n setCosmWasmClient(cosmWasmClient);\n };\n connectCosmWasmClient();\n }, [getCosmWasmClient]);\n\n return (\n <ContractsContext.Provider value={contracts}>\n {children}\n </ContractsContext.Provider>\n );\n};\n\nexport const useContracts = () => {\n const contracts: IContractsContext = useContext(ContractsContext);\n if (contracts === null) {\n throw new Error('useContracts must be used within a ContractsProvider');\n }\n return contracts;\n};\n";
@@ -4,16 +4,16 @@ exports.contractsContextTSX = void 0;
4
4
  exports.contractsContextTSX = `
5
5
  import React, { useEffect, useMemo, useRef, useState, useContext } from 'react';
6
6
  import {
7
- CosmWasmClient,
8
- SigningCosmWasmClient,
9
- } from '@cosmjs/cosmwasm-stargate';
7
+ ICosmWasmClient,
8
+ ISigningCosmWasmClient,
9
+ } from './baseClient';
10
10
 
11
11
  import { IContractsContext, getProviders } from './contractContextProviders';
12
12
 
13
13
  export interface ContractsConfig {
14
14
  address: string | undefined;
15
- getCosmWasmClient: () => Promise<CosmWasmClient>;
16
- getSigningCosmWasmClient: () => Promise<SigningCosmWasmClient>;
15
+ getCosmWasmClient: () => Promise<ICosmWasmClient>;
16
+ getSigningCosmWasmClient: () => Promise<ISigningCosmWasmClient>;
17
17
  }
18
18
 
19
19
  const ContractsContext = React.createContext<IContractsContext | null>(null);
@@ -25,9 +25,9 @@ export const ContractsProvider = ({
25
25
  children: React.ReactNode;
26
26
  contractsConfig: ContractsConfig;
27
27
  }) => {
28
- const [cosmWasmClient, setCosmWasmClient] = useState<CosmWasmClient>();
28
+ const [cosmWasmClient, setCosmWasmClient] = useState<ICosmWasmClient>();
29
29
  const [signingCosmWasmClient, setSigningCosmWasmClient] =
30
- useState<SigningCosmWasmClient>();
30
+ useState<ISigningCosmWasmClient>();
31
31
 
32
32
  const { address, getCosmWasmClient, getSigningCosmWasmClient } =
33
33
  contractsConfig;
@@ -20,6 +20,8 @@ const write = (outPath, file, content, varname) => {
20
20
  };
21
21
  const createHelpers = (input, builderContext) => {
22
22
  const files = [];
23
+ // Always generate baseClient.ts since InterchainJS interfaces are needed by all clients
24
+ files.push(write(input.outPath, 'baseClient.ts', helpers_1.baseClient));
23
25
  if (input.options?.useContractsHook?.enabled &&
24
26
  Object.keys(builderContext.providers)?.length) {
25
27
  const useShorthandCtor = input.options?.useShorthandCtor;
@@ -1,3 +1,4 @@
1
1
  export * from './contractContextBase';
2
2
  export * from './contractContextBaseShortHandCtor';
3
+ export * from './baseClient';
3
4
  export * from './contractsContextTSX';
package/helpers/index.js CHANGED
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./contractContextBase"), exports);
18
18
  __exportStar(require("./contractContextBaseShortHandCtor"), exports);
19
+ __exportStar(require("./baseClient"), exports);
19
20
  __exportStar(require("./contractsContextTSX"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmwasm/ts-codegen",
3
- "version": "1.13.0",
3
+ "version": "1.13.1",
4
4
  "description": "@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/hyperweb-io/ts-codegen",
@@ -36,15 +36,18 @@
36
36
  "bugs": {
37
37
  "url": "https://github.com/hyperweb-io/ts-codegen/issues"
38
38
  },
39
- "devDependencies": {
40
- "@cosmjs/cosmwasm-stargate": "0.32.3"
41
- },
42
39
  "dependencies": {
43
40
  "@babel/generator": "7.24.4",
44
41
  "@babel/traverse": "7.24.1",
45
42
  "@babel/types": "7.24.0",
46
- "@cosmwasm/ts-codegen-ast": "^1.9.0",
47
- "@cosmwasm/ts-codegen-types": "^1.4.0",
43
+ "@chain-registry/types": "^0.17.1",
44
+ "@chain-registry/utils": "^1.51.71",
45
+ "@chain-registry/v2": "^1.71.229",
46
+ "@cosmwasm/ts-codegen-ast": "^1.9.1",
47
+ "@cosmwasm/ts-codegen-types": "^1.4.1",
48
+ "@interchainjs/amino": "1.17.1",
49
+ "@interchainjs/cosmos": "1.17.1",
50
+ "@interchainjs/types": "1.17.1",
48
51
  "@pyramation/json-schema-to-typescript": " 11.0.4",
49
52
  "@types/rimraf": "3.0.2",
50
53
  "@types/shelljs": "0.8.15",
@@ -54,6 +57,8 @@
54
57
  "fuzzy": "0.1.3",
55
58
  "glob": "^10",
56
59
  "inquirerer": "0.1.3",
60
+ "interchainjs": "1.17.1",
61
+ "minimatch": "^10.0.3",
57
62
  "minimist": "1.2.6",
58
63
  "mkdirp": "1.0.4",
59
64
  "nested-obj": "0.0.1",
@@ -67,5 +72,5 @@
67
72
  "smart contracts",
68
73
  "codegen"
69
74
  ],
70
- "gitHead": "bbc6af3d780a810a8bdb8d905ef5413f1ab08d4b"
75
+ "gitHead": "472007b2c11c211f16e18d4653350bf7a8e16901"
71
76
  }
@@ -45,8 +45,10 @@ class ContractsProviderBundlePlugin extends plugin_base_1.BuilderPluginBase {
45
45
  this.lifecycle = 'after';
46
46
  this.defaultContractName = 'contractContextProviders';
47
47
  this.utils = {
48
- CosmWasmClient: '@cosmjs/cosmwasm-stargate',
49
- SigningCosmWasmClient: '@cosmjs/cosmwasm-stargate',
48
+ ICosmWasmClient: '__baseClient__',
49
+ ISigningCosmWasmClient: '__baseClient__',
50
+ getCosmWasmClient: '__baseClient__',
51
+ getSigningCosmWasmClient: '__baseClient__',
50
52
  IQueryClientProvider: '__contractContextBase__',
51
53
  ISigningClientProvider: '__contractContextBase__',
52
54
  IMessageComposerProvider: '__contractContextBase__',
@@ -65,8 +67,10 @@ class ContractsProviderBundlePlugin extends plugin_base_1.BuilderPluginBase {
65
67
  }
66
68
  const localname = 'contractContextProviders.ts';
67
69
  const body = [];
68
- context.addUtil('CosmWasmClient');
69
- context.addUtil('SigningCosmWasmClient');
70
+ context.addUtil('ICosmWasmClient');
71
+ context.addUtil('ISigningCosmWasmClient');
72
+ context.addUtil('getCosmWasmClient');
73
+ context.addUtil('getSigningCosmWasmClient');
70
74
  context.addUtil('IQueryClientProvider');
71
75
  context.addUtil('ISigningClientProvider');
72
76
  context.addUtil('IMessageComposerProvider');