@cosmwasm/ts-codegen 0.33.0 → 0.34.0

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.
Files changed (31) hide show
  1. package/main/builder/builder.js +17 -12
  2. package/main/generators/create-helpers.js +17 -3
  3. package/main/helpers/contractContextBase.js +1 -1
  4. package/main/helpers/contractContextBaseShortHandCtor.js +8 -0
  5. package/main/helpers/contractsContextTSX.js +1 -1
  6. package/main/helpers/index.js +13 -0
  7. package/main/plugins/provider-bundle.js +1 -0
  8. package/main/plugins/provider.js +1 -0
  9. package/module/builder/builder.js +13 -8
  10. package/module/generators/create-helpers.js +17 -5
  11. package/module/helpers/contractContextBase.js +32 -7
  12. package/module/helpers/contractContextBaseShortHandCtor.js +92 -0
  13. package/module/helpers/contractsContextTSX.js +1 -1
  14. package/module/helpers/index.js +1 -0
  15. package/module/plugins/provider-bundle.js +1 -0
  16. package/module/plugins/provider.js +1 -0
  17. package/package.json +3 -3
  18. package/src/builder/builder.ts +20 -8
  19. package/src/generators/create-helpers.ts +51 -8
  20. package/src/helpers/contractContextBase.ts +32 -7
  21. package/src/helpers/contractContextBaseShortHandCtor.ts +92 -0
  22. package/src/helpers/contractsContextTSX.ts +1 -1
  23. package/src/helpers/index.ts +1 -0
  24. package/src/plugins/provider-bundle.ts +1 -0
  25. package/src/plugins/provider.ts +1 -0
  26. package/types/src/builder/builder.d.ts +6 -3
  27. package/types/src/generators/create-helpers.d.ts +2 -2
  28. package/types/src/helpers/contractContextBase.d.ts +1 -1
  29. package/types/src/helpers/contractContextBaseShortHandCtor.d.ts +1 -0
  30. package/types/src/helpers/contractsContextTSX.d.ts +1 -1
  31. package/types/src/helpers/index.d.ts +1 -0
@@ -303,38 +303,43 @@ var TSBuilder = /*#__PURE__*/function () {
303
303
  key: "after",
304
304
  value: function () {
305
305
  var _after = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
306
- var contractsProviderBundlePlugin, files;
306
+ var contractsProviderBundlePlugin, files, helpers;
307
307
  return _regenerator["default"].wrap(function _callee4$(_context4) {
308
308
  while (1) {
309
309
  switch (_context4.prev = _context4.next) {
310
310
  case 0:
311
- if (this.options.bundle.enabled) {
312
- this.bundle();
313
- } //create useContracts bundle file
314
-
315
-
311
+ //create useContracts bundle file
316
312
  contractsProviderBundlePlugin = new _providerBundle.ContractsProviderBundlePlugin(this.options);
317
- contractsProviderBundlePlugin.setBuilder(this);
318
- _context4.next = 5;
319
- return contractsProviderBundlePlugin.render("", {
313
+ contractsProviderBundlePlugin.setBuilder(this); //contractContextProviders.ts
314
+
315
+ _context4.next = 4;
316
+ return contractsProviderBundlePlugin.render("contractContextProviders", {
320
317
  schemas: []
321
318
  }, this.outPath);
322
319
 
323
- case 5:
320
+ case 4:
324
321
  files = _context4.sent;
325
322
 
326
323
  if (files && files.length) {
327
324
  [].push.apply(this.files, files);
328
325
  }
329
326
 
330
- (0, _createHelpers.createHelpers)({
327
+ helpers = (0, _createHelpers.createHelpers)({
331
328
  outPath: this.outPath,
332
329
  contracts: this.contracts,
333
330
  options: this.options,
334
331
  plugins: this.plugins
335
332
  }, this.builderContext);
336
333
 
337
- case 8:
334
+ if (helpers && helpers.length) {
335
+ [].push.apply(this.files, helpers);
336
+ }
337
+
338
+ if (this.options.bundle.enabled) {
339
+ this.bundle();
340
+ }
341
+
342
+ case 9:
338
343
  case "end":
339
344
  return _context4.stop();
340
345
  }
@@ -20,19 +20,33 @@ var _helpers = require("../helpers");
20
20
  var version = process.env.NODE_ENV === "test" ? "latest" : _package["default"].version;
21
21
  var header = "/**\n* This file and any referenced files were automatically generated by ".concat(_package["default"].name, "@").concat(version, "\n* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain\n* and run the transpile command or yarn proto command to regenerate this bundle.\n*/\n\n");
22
22
 
23
- var write = function write(outPath, file, content) {
23
+ var write = function write(outPath, file, content, varname) {
24
24
  var outFile = (0, _path.join)(outPath, file);
25
25
  (0, _mkdirp.sync)((0, _path.dirname)(outFile));
26
26
  (0, _files.writeContentToFile)(outPath, header + content, outFile);
27
+ return {
28
+ type: "plugin",
29
+ pluginType: "helper",
30
+ contract: varname !== null && varname !== void 0 ? varname : (0, _path.basename)(file, (0, _path.extname)(file)),
31
+ localname: file,
32
+ filename: outFile
33
+ };
27
34
  };
28
35
 
29
36
  var createHelpers = function createHelpers(input, builderContext) {
30
37
  var _input$options, _input$options$useCon, _Object$keys;
31
38
 
39
+ var files = [];
40
+
32
41
  if ((_input$options = input.options) !== null && _input$options !== void 0 && (_input$options$useCon = _input$options.useContractsHooks) !== null && _input$options$useCon !== void 0 && _input$options$useCon.enabled && (_Object$keys = Object.keys(builderContext.providers)) !== null && _Object$keys !== void 0 && _Object$keys.length) {
33
- write(input.outPath, "contractContextBase.ts", _helpers.contractContextBase);
34
- write(input.outPath, "contracts-context.tsx", _helpers.contractsContextTSX);
42
+ var _input$options2;
43
+
44
+ var useShorthandCtor = (_input$options2 = input.options) === null || _input$options2 === void 0 ? void 0 : _input$options2.useShorthandCtor;
45
+ files.push(write(input.outPath, "contractContextBase.ts", useShorthandCtor ? _helpers.contractContextBaseShortHandCtor : _helpers.contractContextBase));
46
+ files.push(write(input.outPath, "contracts-context.tsx", _helpers.contractsContextTSX, "contractsContext"));
35
47
  }
48
+
49
+ return files;
36
50
  };
37
51
 
38
52
  exports.createHelpers = createHelpers;
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.contractContextBase = void 0;
7
- var 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 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";
7
+ var 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";
8
8
  exports.contractContextBase = contractContextBase;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.contractContextBaseShortHandCtor = void 0;
7
+ var 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";
8
+ exports.contractContextBaseShortHandCtor = contractContextBaseShortHandCtor;
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.contractsContextTSX = void 0;
7
- var 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\ninterface 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 = useContext(ContractsContext);\n if (contracts === null) {\n throw new Error('useContracts must be used within a ContractsProvider');\n }\n return contracts;\n};\n";
7
+ var 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\ninterface 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";
8
8
  exports.contractsContextTSX = contractsContextTSX;
@@ -17,6 +17,19 @@ Object.keys(_contractContextBase).forEach(function (key) {
17
17
  });
18
18
  });
19
19
 
20
+ var _contractContextBaseShortHandCtor = require("./contractContextBaseShortHandCtor");
21
+
22
+ Object.keys(_contractContextBaseShortHandCtor).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (key in exports && exports[key] === _contractContextBaseShortHandCtor[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function get() {
28
+ return _contractContextBaseShortHandCtor[key];
29
+ }
30
+ });
31
+ });
32
+
20
33
  var _contractsContextTSX = require("./contractsContextTSX");
21
34
 
22
35
  Object.keys(_contractsContextTSX).forEach(function (key) {
@@ -121,6 +121,7 @@ var ContractsProviderBundlePlugin = /*#__PURE__*/function (_BuilderPluginBase) {
121
121
  body.push(w.createGettingProviders(providerInfos));
122
122
  return _context.abrupt("return", [{
123
123
  type: "plugin",
124
+ pluginType: "contractContextProviders",
124
125
  localname: localname,
125
126
  body: body
126
127
  }]);
@@ -145,6 +145,7 @@ var ContractsContextProviderPlugin = /*#__PURE__*/function (_BuilderPluginBase)
145
145
  body.push(w.createProvider(name, providerInfo));
146
146
  return _context.abrupt("return", [{
147
147
  type: "plugin",
148
+ pluginType: "provider",
148
149
  localname: localname,
149
150
  body: body
150
151
  }]);
@@ -112,14 +112,11 @@ export class TSBuilder {
112
112
  }
113
113
 
114
114
  async after() {
115
- if (this.options.bundle.enabled) {
116
- this.bundle();
117
- } //create useContracts bundle file
118
-
119
-
115
+ //create useContracts bundle file
120
116
  const contractsProviderBundlePlugin = new ContractsProviderBundlePlugin(this.options);
121
- contractsProviderBundlePlugin.setBuilder(this);
122
- let files = await contractsProviderBundlePlugin.render("", {
117
+ contractsProviderBundlePlugin.setBuilder(this); //contractContextProviders.ts
118
+
119
+ const files = await contractsProviderBundlePlugin.render("contractContextProviders", {
123
120
  schemas: []
124
121
  }, this.outPath);
125
122
 
@@ -127,12 +124,20 @@ export class TSBuilder {
127
124
  [].push.apply(this.files, files);
128
125
  }
129
126
 
130
- createHelpers({
127
+ const helpers = createHelpers({
131
128
  outPath: this.outPath,
132
129
  contracts: this.contracts,
133
130
  options: this.options,
134
131
  plugins: this.plugins
135
132
  }, this.builderContext);
133
+
134
+ if (helpers && helpers.length) {
135
+ [].push.apply(this.files, helpers);
136
+ }
137
+
138
+ if (this.options.bundle.enabled) {
139
+ this.bundle();
140
+ }
136
141
  }
137
142
 
138
143
  async bundle() {
@@ -1,8 +1,8 @@
1
- import { join, dirname } from "path";
1
+ import { join, dirname, basename, extname } from "path";
2
2
  import { sync as mkdirp } from "mkdirp";
3
3
  import pkg from "../../package.json";
4
4
  import { writeContentToFile } from "../utils/files";
5
- import { contractContextBase, contractsContextTSX } from "../helpers";
5
+ import { contractContextBase, contractContextBaseShortHandCtor, contractsContextTSX } from "../helpers";
6
6
  const version = process.env.NODE_ENV === "test" ? "latest" : pkg.version;
7
7
  const header = `/**
8
8
  * This file and any referenced files were automatically generated by ${pkg.name}@${version}
@@ -11,15 +11,27 @@ const header = `/**
11
11
  */
12
12
  \n`;
13
13
 
14
- const write = (outPath, file, content) => {
14
+ const write = (outPath, file, content, varname) => {
15
15
  const outFile = join(outPath, file);
16
16
  mkdirp(dirname(outFile));
17
17
  writeContentToFile(outPath, header + content, outFile);
18
+ return {
19
+ type: "plugin",
20
+ pluginType: "helper",
21
+ contract: varname ?? basename(file, extname(file)),
22
+ localname: file,
23
+ filename: outFile
24
+ };
18
25
  };
19
26
 
20
27
  export const createHelpers = (input, builderContext) => {
28
+ const files = [];
29
+
21
30
  if (input.options?.useContractsHooks?.enabled && Object.keys(builderContext.providers)?.length) {
22
- write(input.outPath, "contractContextBase.ts", contractContextBase);
23
- write(input.outPath, "contracts-context.tsx", contractsContextTSX);
31
+ const useShorthandCtor = input.options?.useShorthandCtor;
32
+ files.push(write(input.outPath, "contractContextBase.ts", useShorthandCtor ? contractContextBaseShortHandCtor : contractContextBase));
33
+ files.push(write(input.outPath, "contracts-context.tsx", contractsContextTSX, "contractsContext"));
24
34
  }
35
+
36
+ return files;
25
37
  };
@@ -47,24 +47,49 @@ export class ContractBase<
47
47
  TQuery = IEmptyClient,
48
48
  TMsgComposer = IEmptyClient
49
49
  > {
50
+
51
+ address: string | undefined;
52
+ cosmWasmClient: CosmWasmClient | undefined;
53
+ signingCosmWasmClient: SigningCosmWasmClient | undefined;
54
+ TSign?: new (
55
+ client: SigningCosmWasmClient,
56
+ sender: string,
57
+ contractAddress: string
58
+ ) => TSign;
59
+ TQuery?: new (
60
+ client: CosmWasmClient,
61
+ contractAddress: string
62
+ ) => TQuery;
63
+ TMsgComposer?: new (
64
+ sender: string,
65
+ contractAddress: string
66
+ ) => TMsgComposer;
67
+
50
68
  constructor(
51
- protected address: string | undefined,
52
- protected cosmWasmClient: CosmWasmClient | undefined,
53
- protected signingCosmWasmClient: SigningCosmWasmClient | undefined,
54
- private TSign?: new (
69
+ address: string | undefined,
70
+ cosmWasmClient: CosmWasmClient | undefined,
71
+ signingCosmWasmClient: SigningCosmWasmClient | undefined,
72
+ TSign?: new (
55
73
  client: SigningCosmWasmClient,
56
74
  sender: string,
57
75
  contractAddress: string
58
76
  ) => TSign,
59
- private TQuery?: new (
77
+ TQuery?: new (
60
78
  client: CosmWasmClient,
61
79
  contractAddress: string
62
80
  ) => TQuery,
63
- private TMsgComposer?: new (
81
+ TMsgComposer?: new (
64
82
  sender: string,
65
83
  contractAddress: string
66
84
  ) => TMsgComposer
67
- ) {}
85
+ ) {
86
+ this.address = address;
87
+ this.cosmWasmClient = cosmWasmClient;
88
+ this.signingCosmWasmClient = signingCosmWasmClient;
89
+ this.TSign = TSign;
90
+ this.TQuery = TQuery;
91
+ this.TMsgComposer = TMsgComposer;
92
+ }
68
93
 
69
94
  public getSigningClient(contractAddr: string): TSign {
70
95
  if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);
@@ -0,0 +1,92 @@
1
+ export const contractContextBaseShortHandCtor = `
2
+ import {
3
+ CosmWasmClient,
4
+ SigningCosmWasmClient,
5
+ } from '@cosmjs/cosmwasm-stargate';
6
+
7
+ export interface IContractConstructor {
8
+ address: string | undefined;
9
+ cosmWasmClient: CosmWasmClient | undefined;
10
+ signingCosmWasmClient: SigningCosmWasmClient | undefined;
11
+ }
12
+
13
+ export const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';
14
+
15
+ export const NO_COSMWASW_CLIENT_ERROR_MESSAGE = 'cosmWasmClient not connected';
16
+
17
+ export const NO_ADDRESS_ERROR_MESSAGE = "address doesn't exist";
18
+
19
+ export const NO_SIGNING_CLIENT_ERROR_MESSAGE =
20
+ 'Signing client is not generated. Please check ts-codegen config';
21
+
22
+ export const NO_QUERY_CLIENT_ERROR_MESSAGE =
23
+ 'Query client is not generated. Please check ts-codegen config';
24
+
25
+ export const NO_MESSAGE_COMPOSER_ERROR_MESSAGE =
26
+ 'Message composer client is not generated. Please check ts-codegen config';
27
+
28
+ /**
29
+ * a placeholder for non-generated classes
30
+ */
31
+ export interface IEmptyClient {}
32
+
33
+ export interface ISigningClientProvider<T> {
34
+ getSigningClient(contractAddr: string): T;
35
+ }
36
+
37
+ export interface IQueryClientProvider<T> {
38
+ getQueryClient(contractAddr: string): T;
39
+ }
40
+
41
+ export interface IMessageComposerProvider<T> {
42
+ getMessageComposer(contractAddr: string): T;
43
+ }
44
+
45
+ export class ContractBase<
46
+ TSign = IEmptyClient,
47
+ TQuery = IEmptyClient,
48
+ TMsgComposer = IEmptyClient
49
+ > {
50
+ constructor(
51
+ protected address: string | undefined,
52
+ protected cosmWasmClient: CosmWasmClient | undefined,
53
+ protected signingCosmWasmClient: SigningCosmWasmClient | undefined,
54
+ private TSign?: new (
55
+ client: SigningCosmWasmClient,
56
+ sender: string,
57
+ contractAddress: string
58
+ ) => TSign,
59
+ private TQuery?: new (
60
+ client: CosmWasmClient,
61
+ contractAddress: string
62
+ ) => TQuery,
63
+ private TMsgComposer?: new (
64
+ sender: string,
65
+ contractAddress: string
66
+ ) => TMsgComposer
67
+ ) {}
68
+
69
+ public getSigningClient(contractAddr: string): TSign {
70
+ if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);
71
+ if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);
72
+ if (!this.TSign) throw new Error(NO_SIGNING_CLIENT_ERROR_MESSAGE);
73
+ return new this.TSign(
74
+ this.signingCosmWasmClient,
75
+ this.address,
76
+ contractAddr
77
+ );
78
+ }
79
+
80
+ public getQueryClient(contractAddr: string): TQuery {
81
+ if (!this.cosmWasmClient) throw new Error(NO_COSMWASW_CLIENT_ERROR_MESSAGE);
82
+ if (!this.TQuery) throw new Error(NO_QUERY_CLIENT_ERROR_MESSAGE);
83
+ return new this.TQuery(this.cosmWasmClient, contractAddr);
84
+ }
85
+
86
+ public getMessageComposer(contractAddr: string): TMsgComposer {
87
+ if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);
88
+ if (!this.TMsgComposer) throw new Error(NO_MESSAGE_COMPOSER_ERROR_MESSAGE);
89
+ return new this.TMsgComposer(this.address, contractAddr);
90
+ }
91
+ }
92
+ `;
@@ -64,7 +64,7 @@ export const ContractsProvider = ({
64
64
  };
65
65
 
66
66
  export const useContracts = () => {
67
- const contracts = useContext(ContractsContext);
67
+ const contracts: IContractsContext = useContext(ContractsContext);
68
68
  if (contracts === null) {
69
69
  throw new Error('useContracts must be used within a ContractsProvider');
70
70
  }
@@ -1,2 +1,3 @@
1
1
  export * from './contractContextBase';
2
+ export * from './contractContextBaseShortHandCtor';
2
3
  export * from './contractsContextTSX';
@@ -57,6 +57,7 @@ export class ContractsProviderBundlePlugin extends BuilderPluginBase {
57
57
  body.push(w.createGettingProviders(providerInfos));
58
58
  return [{
59
59
  type: "plugin",
60
+ pluginType: "contractContextProviders",
60
61
  localname,
61
62
  body
62
63
  }];
@@ -73,6 +73,7 @@ export class ContractsContextProviderPlugin extends BuilderPluginBase {
73
73
  body.push(w.createProvider(name, providerInfo));
74
74
  return [{
75
75
  type: "plugin",
76
+ pluginType: "provider",
76
77
  localname,
77
78
  body
78
79
  }];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cosmwasm/ts-codegen",
3
- "version": "0.33.0",
3
+ "version": "0.34.0",
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/cosmwasm/ts-codegen",
@@ -96,7 +96,7 @@
96
96
  "parse-package-name": "1.0.0",
97
97
  "rimraf": "3.0.2",
98
98
  "shelljs": "0.8.5",
99
- "wasm-ast-types": "^0.24.0"
99
+ "wasm-ast-types": "^0.25.0"
100
100
  },
101
- "gitHead": "9d4f9eca0ee378e0f53c5b95343842d76d446534"
101
+ "gitHead": "676f2d7bf1f7317e75660f8c19a71619064857d2"
102
102
  }
@@ -49,11 +49,15 @@ export interface BundleOptions {
49
49
 
50
50
  export interface UseContractsOptions {
51
51
  enabled?: boolean;
52
- filename?: string;
53
52
  };
54
53
 
55
54
  export type TSBuilderOptions = {
56
55
  bundle?: BundleOptions;
56
+ /**
57
+ * Enable using shorthand constructor.
58
+ * Default: false
59
+ */
60
+ useShorthandCtor?: boolean;
57
61
  useContractsHooks?: UseContractsOptions;
58
62
  } & RenderOptions;
59
63
 
@@ -63,7 +67,9 @@ export interface BuilderFile {
63
67
  type: BuilderFileType;
64
68
  pluginType?: string;
65
69
  contract: string;
70
+ //filename only: Factory.client.ts
66
71
  localname: string;
72
+ //full path: contracts/Factory.client.ts
67
73
  filename: string;
68
74
  };
69
75
 
@@ -157,35 +163,41 @@ export class TSBuilder {
157
163
  }
158
164
 
159
165
  private async after() {
160
- if (this.options.bundle.enabled) {
161
- this.bundle();
162
- }
163
166
 
164
167
  //create useContracts bundle file
165
168
  const contractsProviderBundlePlugin = new ContractsProviderBundlePlugin(this.options);
166
169
  contractsProviderBundlePlugin.setBuilder(this);
167
170
 
168
- let files = await contractsProviderBundlePlugin.render(
169
- "",
171
+ //contractContextProviders.ts
172
+ const files = await contractsProviderBundlePlugin.render(
173
+ "contractContextProviders",
170
174
  {
171
175
  schemas: [],
172
176
  },
173
177
  this.outPath
174
178
  );
179
+
175
180
  if (files && files.length) {
176
181
  [].push.apply(this.files, files);
177
182
  }
178
183
 
179
- createHelpers({
184
+ const helpers = createHelpers({
180
185
  outPath: this.outPath,
181
186
  contracts: this.contracts,
182
187
  options: this.options,
183
188
  plugins: this.plugins,
184
189
  }, this.builderContext);
190
+
191
+ if (helpers && helpers.length) {
192
+ [].push.apply(this.files, helpers);
193
+ }
194
+
195
+ if (this.options.bundle.enabled) {
196
+ this.bundle();
197
+ }
185
198
  }
186
199
 
187
200
  async bundle() {
188
-
189
201
  const allFiles = this.files;
190
202
 
191
203
  const bundleFile = this.options.bundle.bundleFile;
@@ -1,9 +1,13 @@
1
- import { join, dirname } from "path";
1
+ import { join, dirname, basename, extname } from "path";
2
2
  import { sync as mkdirp } from "mkdirp";
3
3
  import pkg from "../../package.json";
4
4
  import { writeContentToFile } from "../utils/files";
5
- import { TSBuilderInput } from "../builder";
6
- import { contractContextBase, contractsContextTSX } from "../helpers";
5
+ import { BuilderFile, TSBuilderInput } from "../builder";
6
+ import {
7
+ contractContextBase,
8
+ contractContextBaseShortHandCtor,
9
+ contractsContextTSX,
10
+ } from "../helpers";
7
11
  import { BuilderContext } from "wasm-ast-types";
8
12
 
9
13
  const version = process.env.NODE_ENV === "test" ? "latest" : pkg.version;
@@ -14,15 +18,54 @@ const header = `/**
14
18
  */
15
19
  \n`;
16
20
 
17
- const write = (outPath: string, file: string, content: string) => {
21
+ const write = (
22
+ outPath: string,
23
+ file: string,
24
+ content: string,
25
+ varname?: string
26
+ ): BuilderFile => {
18
27
  const outFile = join(outPath, file);
19
28
  mkdirp(dirname(outFile));
20
29
  writeContentToFile(outPath, header + content, outFile);
30
+
31
+ return {
32
+ type: "plugin",
33
+ pluginType: "helper",
34
+ contract: varname ?? basename(file, extname(file)),
35
+ localname: file,
36
+ filename: outFile,
37
+ };
21
38
  };
22
39
 
23
- export const createHelpers = (input: TSBuilderInput, builderContext: BuilderContext) => {
24
- if (input.options?.useContractsHooks?.enabled && Object.keys(builderContext.providers)?.length) {
25
- write(input.outPath, "contractContextBase.ts", contractContextBase);
26
- write(input.outPath, "contracts-context.tsx", contractsContextTSX);
40
+ export const createHelpers = (
41
+ input: TSBuilderInput,
42
+ builderContext: BuilderContext
43
+ ): BuilderFile[] => {
44
+ const files: BuilderFile[] = [];
45
+
46
+ if (
47
+ input.options?.useContractsHooks?.enabled &&
48
+ Object.keys(builderContext.providers)?.length
49
+ ) {
50
+ const useShorthandCtor = input.options?.useShorthandCtor;
51
+ files.push(
52
+ write(
53
+ input.outPath,
54
+ "contractContextBase.ts",
55
+ useShorthandCtor
56
+ ? contractContextBaseShortHandCtor
57
+ : contractContextBase
58
+ )
59
+ );
60
+ files.push(
61
+ write(
62
+ input.outPath,
63
+ "contracts-context.tsx",
64
+ contractsContextTSX,
65
+ "contractsContext"
66
+ )
67
+ );
27
68
  }
69
+
70
+ return files;
28
71
  };
@@ -47,24 +47,49 @@ export class ContractBase<
47
47
  TQuery = IEmptyClient,
48
48
  TMsgComposer = IEmptyClient
49
49
  > {
50
+
51
+ address: string | undefined;
52
+ cosmWasmClient: CosmWasmClient | undefined;
53
+ signingCosmWasmClient: SigningCosmWasmClient | undefined;
54
+ TSign?: new (
55
+ client: SigningCosmWasmClient,
56
+ sender: string,
57
+ contractAddress: string
58
+ ) => TSign;
59
+ TQuery?: new (
60
+ client: CosmWasmClient,
61
+ contractAddress: string
62
+ ) => TQuery;
63
+ TMsgComposer?: new (
64
+ sender: string,
65
+ contractAddress: string
66
+ ) => TMsgComposer;
67
+
50
68
  constructor(
51
- protected address: string | undefined,
52
- protected cosmWasmClient: CosmWasmClient | undefined,
53
- protected signingCosmWasmClient: SigningCosmWasmClient | undefined,
54
- private TSign?: new (
69
+ address: string | undefined,
70
+ cosmWasmClient: CosmWasmClient | undefined,
71
+ signingCosmWasmClient: SigningCosmWasmClient | undefined,
72
+ TSign?: new (
55
73
  client: SigningCosmWasmClient,
56
74
  sender: string,
57
75
  contractAddress: string
58
76
  ) => TSign,
59
- private TQuery?: new (
77
+ TQuery?: new (
60
78
  client: CosmWasmClient,
61
79
  contractAddress: string
62
80
  ) => TQuery,
63
- private TMsgComposer?: new (
81
+ TMsgComposer?: new (
64
82
  sender: string,
65
83
  contractAddress: string
66
84
  ) => TMsgComposer
67
- ) {}
85
+ ) {
86
+ this.address = address;
87
+ this.cosmWasmClient = cosmWasmClient;
88
+ this.signingCosmWasmClient = signingCosmWasmClient;
89
+ this.TSign = TSign;
90
+ this.TQuery = TQuery;
91
+ this.TMsgComposer = TMsgComposer;
92
+ }
68
93
 
69
94
  public getSigningClient(contractAddr: string): TSign {
70
95
  if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);
@@ -0,0 +1,92 @@
1
+ export const contractContextBaseShortHandCtor = `
2
+ import {
3
+ CosmWasmClient,
4
+ SigningCosmWasmClient,
5
+ } from '@cosmjs/cosmwasm-stargate';
6
+
7
+ export interface IContractConstructor {
8
+ address: string | undefined;
9
+ cosmWasmClient: CosmWasmClient | undefined;
10
+ signingCosmWasmClient: SigningCosmWasmClient | undefined;
11
+ }
12
+
13
+ export const NO_SINGING_ERROR_MESSAGE = 'signingCosmWasmClient not connected';
14
+
15
+ export const NO_COSMWASW_CLIENT_ERROR_MESSAGE = 'cosmWasmClient not connected';
16
+
17
+ export const NO_ADDRESS_ERROR_MESSAGE = "address doesn't exist";
18
+
19
+ export const NO_SIGNING_CLIENT_ERROR_MESSAGE =
20
+ 'Signing client is not generated. Please check ts-codegen config';
21
+
22
+ export const NO_QUERY_CLIENT_ERROR_MESSAGE =
23
+ 'Query client is not generated. Please check ts-codegen config';
24
+
25
+ export const NO_MESSAGE_COMPOSER_ERROR_MESSAGE =
26
+ 'Message composer client is not generated. Please check ts-codegen config';
27
+
28
+ /**
29
+ * a placeholder for non-generated classes
30
+ */
31
+ export interface IEmptyClient {}
32
+
33
+ export interface ISigningClientProvider<T> {
34
+ getSigningClient(contractAddr: string): T;
35
+ }
36
+
37
+ export interface IQueryClientProvider<T> {
38
+ getQueryClient(contractAddr: string): T;
39
+ }
40
+
41
+ export interface IMessageComposerProvider<T> {
42
+ getMessageComposer(contractAddr: string): T;
43
+ }
44
+
45
+ export class ContractBase<
46
+ TSign = IEmptyClient,
47
+ TQuery = IEmptyClient,
48
+ TMsgComposer = IEmptyClient
49
+ > {
50
+ constructor(
51
+ protected address: string | undefined,
52
+ protected cosmWasmClient: CosmWasmClient | undefined,
53
+ protected signingCosmWasmClient: SigningCosmWasmClient | undefined,
54
+ private TSign?: new (
55
+ client: SigningCosmWasmClient,
56
+ sender: string,
57
+ contractAddress: string
58
+ ) => TSign,
59
+ private TQuery?: new (
60
+ client: CosmWasmClient,
61
+ contractAddress: string
62
+ ) => TQuery,
63
+ private TMsgComposer?: new (
64
+ sender: string,
65
+ contractAddress: string
66
+ ) => TMsgComposer
67
+ ) {}
68
+
69
+ public getSigningClient(contractAddr: string): TSign {
70
+ if (!this.signingCosmWasmClient) throw new Error(NO_SINGING_ERROR_MESSAGE);
71
+ if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);
72
+ if (!this.TSign) throw new Error(NO_SIGNING_CLIENT_ERROR_MESSAGE);
73
+ return new this.TSign(
74
+ this.signingCosmWasmClient,
75
+ this.address,
76
+ contractAddr
77
+ );
78
+ }
79
+
80
+ public getQueryClient(contractAddr: string): TQuery {
81
+ if (!this.cosmWasmClient) throw new Error(NO_COSMWASW_CLIENT_ERROR_MESSAGE);
82
+ if (!this.TQuery) throw new Error(NO_QUERY_CLIENT_ERROR_MESSAGE);
83
+ return new this.TQuery(this.cosmWasmClient, contractAddr);
84
+ }
85
+
86
+ public getMessageComposer(contractAddr: string): TMsgComposer {
87
+ if (!this.address) throw new Error(NO_ADDRESS_ERROR_MESSAGE);
88
+ if (!this.TMsgComposer) throw new Error(NO_MESSAGE_COMPOSER_ERROR_MESSAGE);
89
+ return new this.TMsgComposer(this.address, contractAddr);
90
+ }
91
+ }
92
+ `;
@@ -64,7 +64,7 @@ export const ContractsProvider = ({
64
64
  };
65
65
 
66
66
  export const useContracts = () => {
67
- const contracts = useContext(ContractsContext);
67
+ const contracts: IContractsContext = useContext(ContractsContext);
68
68
  if (contracts === null) {
69
69
  throw new Error('useContracts must be used within a ContractsProvider');
70
70
  }
@@ -1,2 +1,3 @@
1
1
  export * from './contractContextBase';
2
+ export * from './contractContextBaseShortHandCtor';
2
3
  export * from './contractsContextTSX';
@@ -89,6 +89,7 @@ export class ContractsProviderBundlePlugin extends BuilderPluginBase<TSBuilderOp
89
89
  return [
90
90
  {
91
91
  type: "plugin",
92
+ pluginType: "contractContextProviders",
92
93
  localname,
93
94
  body,
94
95
  },
@@ -106,6 +106,7 @@ export class ContractsContextProviderPlugin extends BuilderPluginBase<TSBuilderO
106
106
  return [
107
107
  {
108
108
  type: "plugin",
109
+ pluginType: "provider",
109
110
  localname,
110
111
  body,
111
112
  },
@@ -14,13 +14,16 @@ export interface BundleOptions {
14
14
  }
15
15
  export interface UseContractsOptions {
16
16
  enabled?: boolean;
17
- filename?: string;
18
17
  }
19
- export type TSBuilderOptions = {
18
+ export declare type TSBuilderOptions = {
20
19
  bundle?: BundleOptions;
20
+ /**
21
+ * Enable using shorthand constructor.
22
+ */
23
+ useShorthandCtor?: boolean;
21
24
  useContractsHooks?: UseContractsOptions;
22
25
  } & RenderOptions;
23
- export type BuilderFileType = 'type' | 'client' | 'recoil' | 'react-query' | 'message-composer' | 'msg-builder' | 'plugin';
26
+ export declare type BuilderFileType = 'type' | 'client' | 'recoil' | 'react-query' | 'message-composer' | 'msg-builder' | 'plugin';
24
27
  export interface BuilderFile {
25
28
  type: BuilderFileType;
26
29
  pluginType?: string;
@@ -1,3 +1,3 @@
1
- import { TSBuilderInput } from "../builder";
1
+ import { BuilderFile, TSBuilderInput } from "../builder";
2
2
  import { BuilderContext } from "wasm-ast-types";
3
- export declare const createHelpers: (input: TSBuilderInput, builderContext: BuilderContext) => void;
3
+ export declare const createHelpers: (input: TSBuilderInput, builderContext: BuilderContext) => BuilderFile[];
@@ -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 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 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";
@@ -0,0 +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 +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\ninterface 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 = 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 CosmWasmClient,\n SigningCosmWasmClient,\n} from '@cosmjs/cosmwasm-stargate';\n\nimport { IContractsContext, getProviders } from './contractContextProviders';\n\ninterface 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,2 +1,3 @@
1
1
  export * from './contractContextBase';
2
+ export * from './contractContextBaseShortHandCtor';
2
3
  export * from './contractsContextTSX';