@dedot/codegen 0.17.0 → 0.17.1-next.6d398d60.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 (55) hide show
  1. package/cjs/index.js +1 -0
  2. package/cjs/sol/generator/ConstructorQueryGen.js +55 -0
  3. package/cjs/sol/generator/ConstructorTxGen.js +21 -0
  4. package/cjs/sol/generator/EventsGen.js +38 -0
  5. package/cjs/sol/generator/IndexGen.js +27 -0
  6. package/cjs/sol/generator/QueryGen.js +50 -0
  7. package/cjs/sol/generator/TxGen.js +21 -0
  8. package/cjs/sol/generator/TypesGen.js +216 -0
  9. package/cjs/sol/generator/index.js +23 -0
  10. package/cjs/sol/index.js +42 -0
  11. package/cjs/typink/generator/ConstructorQueryGen.js +2 -2
  12. package/cjs/typink/generator/ConstructorTxGen.js +2 -2
  13. package/cjs/typink/generator/EventsGen.js +2 -2
  14. package/cjs/typink/generator/IndexGen.js +1 -1
  15. package/cjs/typink/generator/QueryGen.js +2 -2
  16. package/cjs/typink/generator/TxGen.js +2 -2
  17. package/cjs/typink/templates/constructor-query.hbs +1 -1
  18. package/cjs/typink/templates/constructor-tx.hbs +3 -2
  19. package/cjs/typink/templates/events.hbs +1 -1
  20. package/cjs/typink/templates/index.hbs +8 -7
  21. package/cjs/typink/templates/query.hbs +1 -1
  22. package/cjs/typink/templates/tx.hbs +1 -1
  23. package/index.d.ts +1 -0
  24. package/index.js +1 -0
  25. package/package.json +11 -10
  26. package/sol/generator/ConstructorQueryGen.d.ts +12 -0
  27. package/sol/generator/ConstructorQueryGen.js +51 -0
  28. package/sol/generator/ConstructorTxGen.d.ts +6 -0
  29. package/sol/generator/ConstructorTxGen.js +17 -0
  30. package/sol/generator/EventsGen.d.ts +10 -0
  31. package/sol/generator/EventsGen.js +34 -0
  32. package/sol/generator/IndexGen.d.ts +7 -0
  33. package/sol/generator/IndexGen.js +23 -0
  34. package/sol/generator/QueryGen.d.ts +11 -0
  35. package/sol/generator/QueryGen.js +46 -0
  36. package/sol/generator/TxGen.d.ts +6 -0
  37. package/sol/generator/TxGen.js +17 -0
  38. package/sol/generator/TypesGen.d.ts +15 -0
  39. package/sol/generator/TypesGen.js +212 -0
  40. package/sol/generator/index.d.ts +7 -0
  41. package/sol/generator/index.js +7 -0
  42. package/sol/index.d.ts +3 -0
  43. package/sol/index.js +35 -0
  44. package/typink/generator/ConstructorQueryGen.js +2 -2
  45. package/typink/generator/ConstructorTxGen.js +2 -2
  46. package/typink/generator/EventsGen.js +2 -2
  47. package/typink/generator/IndexGen.js +1 -1
  48. package/typink/generator/QueryGen.js +2 -2
  49. package/typink/generator/TxGen.js +2 -2
  50. package/typink/templates/constructor-query.hbs +1 -1
  51. package/typink/templates/constructor-tx.hbs +3 -2
  52. package/typink/templates/events.hbs +1 -1
  53. package/typink/templates/index.hbs +8 -7
  54. package/typink/templates/query.hbs +1 -1
  55. package/typink/templates/tx.hbs +1 -1
package/cjs/index.js CHANGED
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./types.js"), exports);
18
18
  __exportStar(require("./chaintypes/index.js"), exports);
19
19
  __exportStar(require("./typink/index.js"), exports);
20
+ __exportStar(require("./sol/index.js"), exports);
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConstructorQueryGen = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const utils_js_1 = require("../../utils.js");
6
+ class ConstructorQueryGen {
7
+ abi;
8
+ typesGen;
9
+ constructor(abi, typesGen) {
10
+ this.abi = abi;
11
+ this.typesGen = typesGen;
12
+ }
13
+ generate(useSubPaths = false) {
14
+ this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
15
+ this.typesGen.typeImports.addContractType('GenericConstructorQuery', 'GenericConstructorQueryCall', 'GenericConstructorCallResult', 'ConstructorCallOptions', 'ContractInstantiateResult', 'MetadataType');
16
+ let constructor = this.findConstructor();
17
+ const constructorsOut = this.doGenerateConstructorFragment(constructor, 'ConstructorCallOptions');
18
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
19
+ const template = (0, utils_js_1.compileTemplate)('sol/templates/constructor-query.hbs');
20
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, constructorsOut }));
21
+ }
22
+ doGenerateConstructorFragment(abiItem, optionsTypeName) {
23
+ let callsOut = '';
24
+ const { inputs = [] } = abiItem;
25
+ // In case there is an arg has label `options`,
26
+ // we use the name `_options` for the last options param
27
+ // This is just and edge case, so this approach works for now
28
+ const optionsParamName = inputs.some(({ name }) => name === 'options') ? '_options' : 'options';
29
+ callsOut += `${(0, utils_js_1.commentBlock)(inputs.map((input, idx) => `@param {${this.typesGen.generateType(input, abiItem, 1)}} ${(0, utils_1.stringCamelCase)(input.name || `arg${idx}`)}`), optionsTypeName ? `@param {${optionsTypeName}} ${optionsParamName}` : '')}`;
30
+ callsOut += `new: ${this.generateMethodDef(abiItem, optionsParamName)};\n\n`;
31
+ return callsOut;
32
+ }
33
+ generateMethodDef(abiItem, optionsParamName = 'options') {
34
+ const paramsOut = this.generateParamsOut(abiItem);
35
+ return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<[], ContractInstantiateResult<ChainApi>>>, Type>`;
36
+ }
37
+ generateParamsOut(abiItem) {
38
+ return abiItem.inputs
39
+ .map((input, idx) => `${(0, utils_1.stringCamelCase)(input.name || `arg${idx}`)}: ${this.typesGen.generateType(input, abiItem, 1)}`)
40
+ .join(', ');
41
+ }
42
+ findConstructor() {
43
+ let constructor = this.abi.find((item) => item.type === 'constructor');
44
+ // Fallback to default constructor
45
+ if (!constructor) {
46
+ constructor = {
47
+ inputs: [],
48
+ stateMutability: 'nonpayable',
49
+ type: 'constructor',
50
+ };
51
+ }
52
+ return constructor;
53
+ }
54
+ }
55
+ exports.ConstructorQueryGen = ConstructorQueryGen;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConstructorTxGen = void 0;
4
+ const utils_js_1 = require("../../utils.js");
5
+ const ConstructorQueryGen_js_1 = require("./ConstructorQueryGen.js");
6
+ class ConstructorTxGen extends ConstructorQueryGen_js_1.ConstructorQueryGen {
7
+ generate(useSubPaths = false) {
8
+ this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
9
+ this.typesGen.typeImports.addContractType('GenericConstructorTx', 'SolGenericContractApi', 'GenericConstructorTxCall', 'ConstructorTxOptions', 'GenericInstantiateSubmittableExtrinsic', 'MetadataType');
10
+ let constructor = this.findConstructor();
11
+ const constructorsOut = this.doGenerateConstructorFragment(constructor, 'ConstructorTxOptions');
12
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
13
+ const template = (0, utils_js_1.compileTemplate)('sol/templates/constructor-tx.hbs');
14
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, constructorsOut }));
15
+ }
16
+ generateMethodDef(abiItem, optionsParamName = 'options') {
17
+ const paramsOut = this.generateParamsOut(abiItem);
18
+ return `GenericConstructorTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorTxOptions) => GenericInstantiateSubmittableExtrinsic<ChainApi, ContractApi>, Type>`;
19
+ }
20
+ }
21
+ exports.ConstructorTxGen = ConstructorTxGen;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventsGen = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const utils_js_1 = require("../../utils.js");
6
+ class EventsGen {
7
+ abi;
8
+ typesGen;
9
+ constructor(abi, typesGen) {
10
+ this.abi = abi;
11
+ this.typesGen = typesGen;
12
+ }
13
+ generate(useSubPaths = false) {
14
+ this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
15
+ this.typesGen.typeImports.addContractType('GenericContractEvents', 'GenericContractEvent', 'MetadataType');
16
+ const events = this.abi.filter((o) => o.type === 'event');
17
+ let eventsOut = '';
18
+ events.forEach((event) => {
19
+ const { name, anonymous } = event;
20
+ eventsOut += `${(0, utils_1.stringPascalCase)(name)}: ${this.#generateEventDef(event)};\n\n`;
21
+ });
22
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
23
+ const template = (0, utils_js_1.compileTemplate)('sol/templates/events.hbs');
24
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, eventsOut }));
25
+ }
26
+ #generateEventDef(abiItem) {
27
+ const { name } = abiItem;
28
+ const paramsOut = this.generateParamsOut(abiItem);
29
+ return `GenericContractEvent<'${(0, utils_1.stringPascalCase)(name)}', {${paramsOut}}, Type>`;
30
+ }
31
+ generateParamsOut(abiItem) {
32
+ const { inputs } = abiItem;
33
+ return inputs
34
+ .map((o, idx) => `${(0, utils_js_1.commentBlock)(`@indexed: ${o.indexed}`)}${(0, utils_1.stringCamelCase)(o.name || `arg${idx}`)}: ${this.typesGen.generateType(o, abiItem, 1)}`)
35
+ .join(', ');
36
+ }
37
+ }
38
+ exports.EventsGen = EventsGen;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IndexGen = void 0;
4
+ const index_js_1 = require("../../shared/index.js");
5
+ const utils_js_1 = require("../../utils.js");
6
+ class IndexGen {
7
+ interfaceName;
8
+ typesGen;
9
+ constructor(interfaceName, typesGen) {
10
+ this.interfaceName = interfaceName;
11
+ this.typesGen = typesGen;
12
+ }
13
+ generate(useSubPaths = false) {
14
+ const interfaceName = this.interfaceName;
15
+ const typeImports = new index_js_1.TypeImports();
16
+ typeImports.addKnownType('VersionedGenericSubstrateApi', 'RpcVersion', 'RpcV2');
17
+ typeImports.addContractType('SolGenericContractApi', 'SolRegistry');
18
+ typeImports.addChainType('SubstrateApi');
19
+ const importTypes = typeImports.toImports({ useSubPaths });
20
+ const template = (0, utils_js_1.compileTemplate)('sol/templates/index.hbs');
21
+ return (0, utils_js_1.beautifySourceCode)(template({
22
+ interfaceName,
23
+ importTypes,
24
+ }));
25
+ }
26
+ }
27
+ exports.IndexGen = IndexGen;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QueryGen = void 0;
4
+ const contracts_1 = require("@dedot/contracts");
5
+ const utils_1 = require("@dedot/utils");
6
+ const utils_js_1 = require("../../utils.js");
7
+ class QueryGen {
8
+ abi;
9
+ typesGen;
10
+ constructor(abi, typesGen) {
11
+ this.abi = abi;
12
+ this.typesGen = typesGen;
13
+ }
14
+ generate(useSubPaths = false) {
15
+ this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
16
+ this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult', 'MetadataType');
17
+ const functions = this.abi.filter((item) => item.type === 'function');
18
+ const queryCallsOut = this.doGenerate(functions, 'ContractCallOptions');
19
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
20
+ const template = (0, utils_js_1.compileTemplate)('sol/templates/query.hbs');
21
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, queryCallsOut }));
22
+ }
23
+ doGenerate(functions, optionsTypeName) {
24
+ let callsOut = '';
25
+ functions.forEach((def) => {
26
+ const { inputs, name } = def;
27
+ // In case there is an arg has label `options`,
28
+ // we use the name `_options` for the last options param
29
+ // This is just and edge case, so this approach works for now
30
+ const optionsParamName = inputs.some(({ name }) => name === 'options') ? '_options' : 'options';
31
+ callsOut += `${(0, utils_js_1.commentBlock)(inputs.map((input, idx) => `@param {${this.typesGen.generateType(input, def, 1)}} ${(0, utils_1.stringCamelCase)(input.name || `arg${idx}`)}`), optionsTypeName ? `@param {${optionsTypeName}} ${optionsParamName}` : '')}`;
32
+ callsOut += `${(0, contracts_1.normalizeLabel)(name)}: ${this.generateMethodDef(def, optionsParamName)};\n\n`;
33
+ });
34
+ return callsOut;
35
+ }
36
+ generateMethodDef(abiItem, optionsParamName = 'options') {
37
+ const { outputs } = abiItem;
38
+ const paramsOut = this.generateParamsOut(abiItem);
39
+ const typeOutInner = `${outputs.map((o) => this.typesGen.generateType(o, abiItem, 1, true)).join(',')}`;
40
+ // If there is only one output, we don't need to wrap it in a tuple, for user-friendly
41
+ const typeOut = outputs.length === 1 ? typeOutInner : `[${typeOutInner}]`;
42
+ return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult<ChainApi>>>, Type>`;
43
+ }
44
+ generateParamsOut(abiItem) {
45
+ return abiItem.inputs
46
+ .map((input, idx) => `${(0, utils_1.stringCamelCase)(input.name || `arg${idx}`)}: ${this.typesGen.generateType(input, abiItem, 1)}`)
47
+ .join(', ');
48
+ }
49
+ }
50
+ exports.QueryGen = QueryGen;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TxGen = void 0;
4
+ const utils_js_1 = require("../../utils.js");
5
+ const QueryGen_js_1 = require("./QueryGen.js");
6
+ class TxGen extends QueryGen_js_1.QueryGen {
7
+ generate(useSubPaths = false) {
8
+ this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
9
+ this.typesGen.typeImports.addContractType('GenericContractTx', 'GenericContractTxCall', 'ContractTxOptions', 'ContractSubmittableExtrinsic', 'MetadataType');
10
+ const txFunctions = this.abi.filter((item) => item.type === 'function' && item.stateMutability !== 'view');
11
+ const txCallsOut = this.doGenerate(txFunctions, 'ContractTxOptions');
12
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
13
+ const template = (0, utils_js_1.compileTemplate)('sol/templates/tx.hbs');
14
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, txCallsOut }));
15
+ }
16
+ generateMethodDef(abiItem, optionsParamName = 'options') {
17
+ const paramsOut = this.generateParamsOut(abiItem);
18
+ return `GenericContractTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractTxOptions) => ContractSubmittableExtrinsic<ChainApi>, Type>`;
19
+ }
20
+ }
21
+ exports.TxGen = TxGen;
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypesGen = exports.BASIC_KNOWN_TYPES = exports.SUPPORTED_SOLIDITY_TYPES = void 0;
4
+ const utils_1 = require("@dedot/utils");
5
+ const TypeImports_js_1 = require("../../shared/TypeImports.js");
6
+ const utils_js_1 = require("../../utils.js");
7
+ const INT_TYPES = /^int(\d+)?(\[(\d+)?])*?$/;
8
+ const UINT_TYPES = /^uint(\d+)?(\[(\d+)?])*?$/;
9
+ const BYTES_TYPES = /^bytes(\d+)?(\[(\d+)?])*?$/;
10
+ const FIXED_TYPES = /^fixed(\d+x\d+)?(\[(\d+)?])*?$/;
11
+ const UNFIXED_TYPES = /^ufixed(\d+x\d+)?(\[(\d+)?])*?$/;
12
+ const STRING_TYPES = /^string(\[(\d+)?])*?$/;
13
+ const BOOL_TYPES = /^bool(\[(\d+)?])*?$/;
14
+ const ADDRESS_TYPES = /^address(\[(\d+)?])*?$/;
15
+ const FUNCTION_TYPES = /^function(\[(\d+)?])*?$/;
16
+ const COMPONENT_TYPES = /^tuple(\[(\d+)?])*?$/;
17
+ const ARRAY_DIM = /\[(\d+)?]/g;
18
+ const ONLY_ARRAY_DIM = /^\[(\d+)?]$/;
19
+ exports.SUPPORTED_SOLIDITY_TYPES = [
20
+ INT_TYPES,
21
+ UINT_TYPES,
22
+ BYTES_TYPES,
23
+ FIXED_TYPES,
24
+ UNFIXED_TYPES,
25
+ STRING_TYPES,
26
+ BOOL_TYPES,
27
+ ADDRESS_TYPES,
28
+ FUNCTION_TYPES,
29
+ COMPONENT_TYPES,
30
+ ];
31
+ exports.BASIC_KNOWN_TYPES = [
32
+ /^(H160)$/,
33
+ /^(FixedBytes)<(\d+)>$/,
34
+ /^(Bytes)$/,
35
+ /^(BytesLike)$/,
36
+ /^(Fixed)<(\d+),(\d+)>$/,
37
+ /^(UFixed)<(\d+),(\d+)>$/,
38
+ /^(FixedArray)$/,
39
+ ];
40
+ class TypesGen {
41
+ abi;
42
+ typeImports;
43
+ constructor(abi) {
44
+ this.abi = abi;
45
+ this.typeImports = new TypeImports_js_1.TypeImports();
46
+ }
47
+ generate(useSubPaths = false) {
48
+ let defTypeOut = '';
49
+ this.abi.forEach((abiItem) => {
50
+ if (abiItem.type === 'fallback' || abiItem.type === 'receive')
51
+ return;
52
+ if (abiItem.type === 'function' || abiItem.type === 'constructor' || abiItem.type === 'event') {
53
+ const { inputs } = abiItem;
54
+ inputs
55
+ .filter((o) => o.components && o.components.length > 0)
56
+ .forEach((o) => {
57
+ defTypeOut += `export type ${this.generateTypeName(o, abiItem)} = ${this.generateType(o, abiItem, 0)};\n\n`;
58
+ });
59
+ }
60
+ if (abiItem.type === 'function') {
61
+ const { outputs } = abiItem;
62
+ outputs
63
+ .filter((o) => o.components && o.components.length > 0)
64
+ .forEach((o) => {
65
+ defTypeOut += `export type ${this.generateTypeName(o, abiItem, true)} = ${this.generateType(o, abiItem, 0, true)};\n\n`;
66
+ });
67
+ }
68
+ });
69
+ const importTypes = this.typeImports.toImports({ excludeModules: ['./types.js'], useSubPaths });
70
+ const template = (0, utils_js_1.compileTemplate)('sol/templates/types.hbs');
71
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
72
+ }
73
+ generateTypeName(typeDef, abiItem, typeOut = false) {
74
+ if (abiItem.type === 'fallback' || abiItem.type === 'receive')
75
+ return '';
76
+ if (COMPONENT_TYPES.test(typeDef.type)) {
77
+ const baseName = abiItem.type === 'constructor'
78
+ ? `${typeDef.name}_input`
79
+ : `${abiItem.name}_${typeDef.name}_${typeOut ? 'output' : 'input'}`;
80
+ return (0, utils_1.stringPascalCase)(baseName);
81
+ }
82
+ return '';
83
+ }
84
+ generateType(typeDef, abiItem, nestedLevel = 0, typeOut = false) {
85
+ if (nestedLevel > 0 && abiItem) {
86
+ let typeName = this.generateTypeName(typeDef, abiItem, typeOut);
87
+ if (typeName.length === 0) {
88
+ typeName = this.#generateType(typeDef, nestedLevel, typeOut);
89
+ return typeName;
90
+ }
91
+ this.addTypeImport(typeName);
92
+ return typeName;
93
+ }
94
+ return this.#generateType(typeDef, nestedLevel, typeOut);
95
+ }
96
+ #generateType(typeDef, nestedLevel = 0, typeOut = false) {
97
+ const { type } = typeDef;
98
+ let baseType = this.#generateBaseType(typeDef, nestedLevel, typeOut);
99
+ if (!COMPONENT_TYPES.test(type)) {
100
+ // baseType of component types are all generated types, eg: { a: bigint, b: string }
101
+ // So we don't import them here.
102
+ this.addTypeImport(baseType);
103
+ }
104
+ // Match all array dimensions
105
+ const dimensions = type.match(ARRAY_DIM);
106
+ dimensions
107
+ // Match each dimension to see if fixed array or dynamic array (eg: [3] vs [])
108
+ ?.map((o) => o.match(ONLY_ARRAY_DIM))
109
+ .forEach(([_, n]) => {
110
+ if (n) {
111
+ this.addTypeImport('FixedArray');
112
+ baseType = `FixedArray<${baseType}, ${n}>`;
113
+ }
114
+ else {
115
+ baseType = (0, utils_js_1.isNativeType)(baseType.replaceAll('[]', '')) ? `${baseType}[]` : `Array<${baseType}>`;
116
+ }
117
+ });
118
+ return baseType;
119
+ }
120
+ #generateBaseType(typeDef, nestedLevel = 0, typeOut = false) {
121
+ const { type } = typeDef;
122
+ if (INT_TYPES.test(type)) {
123
+ const [_, bitsStr] = type.match(INT_TYPES);
124
+ // Default to 256 when unspecified
125
+ const bits = bitsStr ? parseInt(bitsStr) : 256;
126
+ const isSafeNumber = bits <= 48;
127
+ return isSafeNumber ? `number` : 'bigint';
128
+ }
129
+ else if (UINT_TYPES.test(type)) {
130
+ const [_, bitsStr] = type.match(UINT_TYPES);
131
+ // Default to 256 when unspecified
132
+ const bits = bitsStr ? parseInt(bitsStr) : 256;
133
+ const isSafeNumber = bits <= 48;
134
+ return isSafeNumber ? `number` : 'bigint';
135
+ }
136
+ else if (BYTES_TYPES.test(type)) {
137
+ const [_, n] = type.match(BYTES_TYPES);
138
+ if (n) {
139
+ return `FixedBytes<${n}>`;
140
+ }
141
+ if (typeOut) {
142
+ return `Bytes`;
143
+ }
144
+ return `BytesLike`;
145
+ }
146
+ else if (BOOL_TYPES.test(type)) {
147
+ return 'boolean';
148
+ }
149
+ else if (STRING_TYPES.test(type)) {
150
+ return 'string';
151
+ }
152
+ else if (COMPONENT_TYPES.test(type)) {
153
+ const { components = [] } = typeDef;
154
+ if (components.length === 0) {
155
+ return '[]';
156
+ }
157
+ else {
158
+ const objectType = this.generateObjectType(components, nestedLevel + 1, typeOut);
159
+ return `${objectType}`;
160
+ }
161
+ }
162
+ else if (ADDRESS_TYPES.test(type)) {
163
+ return `H160`;
164
+ }
165
+ else if (FUNCTION_TYPES.test(type)) {
166
+ // Function type is an address (20 bytes) followed by a function selector (4 bytes).
167
+ // Ref: https://docs.soliditylang.org/en/latest/abi-spec.html#:~:text=function%3A%20an%20address%20(20%20bytes)%20followed%20by%20a%20function%20selector%20(4%20bytes).%20Encoded%20identical%20to%20bytes24.
168
+ return `FixedBytes<24>`;
169
+ }
170
+ else if (FIXED_TYPES.test(type)) {
171
+ const [_, denotation] = type.match(FIXED_TYPES);
172
+ // Ref: https://docs.soliditylang.org/en/latest/abi-spec.html#:~:text=fixed%2C%20ufixed%3A%20synonyms%20for%20fixed128x18%2C%20ufixed128x18%20respectively.%20For%20computing%20the%20function%20selector%2C%20fixed128x18%20and%20ufixed128x18%20have%20to%20be%20used.
173
+ const [m, n] = denotation ? denotation.split('x').map((s) => parseInt(s)) : [128, 18];
174
+ return `Fixed<${m},${n}>`;
175
+ }
176
+ else if (UNFIXED_TYPES.test(type)) {
177
+ const [_, denotation] = type.match(UNFIXED_TYPES);
178
+ // Ref: https://docs.soliditylang.org/en/latest/abi-spec.html#:~:text=fixed%2C%20ufixed%3A%20synonyms%20for%20fixed128x18%2C%20ufixed128x18%20respectively.%20For%20computing%20the%20function%20selector%2C%20fixed128x18%20and%20ufixed128x18%20have%20to%20be%20used.
179
+ const [m, n] = denotation ? denotation.split('x').map((s) => parseInt(s)) : [128, 18];
180
+ return `UFixed<${m},${n}>`;
181
+ }
182
+ else {
183
+ throw new Error(`Unsupported Solidity type: ${type}`);
184
+ }
185
+ }
186
+ generateObjectType(components, nestedLevel = 0, typeOut = false) {
187
+ const props = components.map((typeDef) => {
188
+ const type = this.generateType(typeDef, undefined, nestedLevel + 1, typeOut);
189
+ return {
190
+ name: typeDef.name,
191
+ type,
192
+ };
193
+ });
194
+ if (props.length > 0 && props.at(0).name.length === 0) {
195
+ return `[${props.map(({ type }) => `${type}`).join(', ')}]`;
196
+ }
197
+ return `{${props.map(({ name, type }) => `${name}: ${type}`).join(',\n')}}`;
198
+ }
199
+ addTypeImport(typeName) {
200
+ if (Array.isArray(typeName)) {
201
+ typeName.forEach((one) => this.addTypeImport(one));
202
+ return;
203
+ }
204
+ if ((0, utils_js_1.isNativeType)(typeName)) {
205
+ return;
206
+ }
207
+ const re = exports.BASIC_KNOWN_TYPES.find((re) => typeName.match(re));
208
+ if (re) {
209
+ const typeBase = typeName.match(re)[1];
210
+ this.typeImports.addCodecType(typeBase);
211
+ return;
212
+ }
213
+ this.typeImports.addPortableType(typeName);
214
+ }
215
+ }
216
+ exports.TypesGen = TypesGen;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./IndexGen.js"), exports);
18
+ __exportStar(require("./QueryGen.js"), exports);
19
+ __exportStar(require("./TypesGen.js"), exports);
20
+ __exportStar(require("./TxGen.js"), exports);
21
+ __exportStar(require("./ConstructorTxGen.js"), exports);
22
+ __exportStar(require("./ConstructorQueryGen.js"), exports);
23
+ __exportStar(require("./EventsGen.js"), exports);
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateSolContractTypes = void 0;
7
+ const utils_1 = require("@dedot/utils");
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
10
+ const index_js_1 = require("./generator/index.js");
11
+ async function generateSolContractTypes(abi, contract = 'contract', outDir = '.', extension = 'd.ts', useSubPaths = false) {
12
+ let abiItems = typeof abi === 'string' ? JSON.parse(abi) : abi;
13
+ const contractName = contract;
14
+ const dirPath = path_1.default.resolve(outDir, (0, utils_1.stringDashCase)(contractName));
15
+ const typesFileName = path_1.default.join(dirPath, `types.${extension}`);
16
+ const queryTypesFileName = path_1.default.join(dirPath, `query.${extension}`);
17
+ const eventsTypesFileName = path_1.default.join(dirPath, `events.${extension}`);
18
+ const txTypesFileName = path_1.default.join(dirPath, `tx.${extension}`);
19
+ const constructorTxTypesFileName = path_1.default.join(dirPath, `constructor-tx.${extension}`);
20
+ const constructorQueryTypesFileName = path_1.default.join(dirPath, `constructor-query.${extension}`);
21
+ const indexTypesFileName = path_1.default.join(dirPath, `index.${extension}`);
22
+ if (!fs_1.default.existsSync(dirPath)) {
23
+ fs_1.default.mkdirSync(dirPath, { recursive: true });
24
+ }
25
+ const interfaceName = `${(0, utils_1.stringPascalCase)(`${contractName}`)}ContractApi`;
26
+ const typesGen = new index_js_1.TypesGen(abiItems);
27
+ const queryGen = new index_js_1.QueryGen(abiItems, typesGen);
28
+ const eventsGen = new index_js_1.EventsGen(abiItems, typesGen);
29
+ const txGen = new index_js_1.TxGen(abiItems, typesGen);
30
+ const constructorTxGen = new index_js_1.ConstructorTxGen(abiItems, typesGen);
31
+ const constructorQueryGen = new index_js_1.ConstructorQueryGen(abiItems, typesGen);
32
+ const indexGen = new index_js_1.IndexGen(interfaceName, typesGen);
33
+ fs_1.default.writeFileSync(typesFileName, await typesGen.generate(useSubPaths));
34
+ fs_1.default.writeFileSync(queryTypesFileName, await queryGen.generate(useSubPaths));
35
+ fs_1.default.writeFileSync(eventsTypesFileName, await eventsGen.generate(useSubPaths));
36
+ fs_1.default.writeFileSync(txTypesFileName, await txGen.generate(useSubPaths));
37
+ fs_1.default.writeFileSync(constructorQueryTypesFileName, await constructorQueryGen.generate(useSubPaths));
38
+ fs_1.default.writeFileSync(constructorTxTypesFileName, await constructorTxGen.generate(useSubPaths));
39
+ fs_1.default.writeFileSync(indexTypesFileName, await indexGen.generate(useSubPaths));
40
+ return { interfaceName, outputFolder: dirPath };
41
+ }
42
+ exports.generateSolContractTypes = generateSolContractTypes;
@@ -7,7 +7,7 @@ class ConstructorQueryGen extends QueryGen_js_1.QueryGen {
7
7
  generate(useSubPaths = false) {
8
8
  this.typesGen.clearCache();
9
9
  this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
10
- this.typesGen.typeImports.addContractType('GenericConstructorQuery', 'GenericConstructorQueryCall', 'GenericConstructorCallResult', 'ConstructorCallOptions', 'ContractInstantiateResult');
10
+ this.typesGen.typeImports.addContractType('GenericConstructorQuery', 'GenericConstructorQueryCall', 'GenericConstructorCallResult', 'ConstructorCallOptions', 'ContractInstantiateResult', 'MetadataType');
11
11
  const { constructors } = this.contractMetadata.spec;
12
12
  const constructorsOut = this.doGenerate(constructors, 'ConstructorCallOptions');
13
13
  const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
@@ -20,7 +20,7 @@ class ConstructorQueryGen extends QueryGen_js_1.QueryGen {
20
20
  const typeOutRaw = this.typesGen.generateType(returnType.type, 0, true);
21
21
  // Unwrap langError result
22
22
  const typeOut = typeOutRaw.match(/^(\w+)<(.*), (.*)>$/).at(2);
23
- return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<${typeOut}, ContractInstantiateResult<ChainApi>>>>`;
23
+ return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<${typeOut}, ContractInstantiateResult<ChainApi>>>, Type>`;
24
24
  }
25
25
  }
26
26
  exports.ConstructorQueryGen = ConstructorQueryGen;
@@ -7,7 +7,7 @@ class ConstructorTxGen extends QueryGen_js_1.QueryGen {
7
7
  generate(useSubPaths = false) {
8
8
  this.typesGen.clearCache();
9
9
  this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
10
- this.typesGen.typeImports.addContractType('GenericConstructorTx', 'GenericConstructorTxCall', 'ConstructorTxOptions', 'GenericContractApi', 'GenericInstantiateSubmittableExtrinsic');
10
+ this.typesGen.typeImports.addContractType('GenericConstructorTx', 'GenericConstructorTxCall', 'ConstructorTxOptions', 'InkGenericContractApi', 'GenericInstantiateSubmittableExtrinsic', 'MetadataType');
11
11
  const { constructors } = this.contractMetadata.spec;
12
12
  const constructorsOut = this.doGenerate(constructors, 'ConstructorTxOptions');
13
13
  const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
@@ -16,7 +16,7 @@ class ConstructorTxGen extends QueryGen_js_1.QueryGen {
16
16
  }
17
17
  generateMethodDef(def, optionsParamName = 'options') {
18
18
  const paramsOut = this.generateParamsOut(def.args);
19
- return `GenericConstructorTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorTxOptions) => GenericInstantiateSubmittableExtrinsic<ChainApi, ContractApi>>`;
19
+ return `GenericConstructorTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorTxOptions) => GenericInstantiateSubmittableExtrinsic<ChainApi, ContractApi>, Type>`;
20
20
  }
21
21
  }
22
22
  exports.ConstructorTxGen = ConstructorTxGen;
@@ -8,7 +8,7 @@ class EventsGen extends QueryGen_js_1.QueryGen {
8
8
  generate(useSubPaths = false) {
9
9
  this.typesGen.clearCache();
10
10
  this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
11
- this.typesGen.typeImports.addContractType('GenericContractEvents', 'GenericContractEvent');
11
+ this.typesGen.typeImports.addContractType('GenericContractEvents', 'GenericContractEvent', 'MetadataType');
12
12
  const { events } = this.contractMetadata.spec;
13
13
  let eventsOut = '';
14
14
  const isV5 = this.contractMetadata.version === 5;
@@ -24,7 +24,7 @@ class EventsGen extends QueryGen_js_1.QueryGen {
24
24
  #generateEventDef(event) {
25
25
  const { args, label } = event;
26
26
  const paramsOut = this.generateParamsOut(args);
27
- return `GenericContractEvent<'${(0, utils_1.stringPascalCase)(label)}', {${paramsOut}}>`;
27
+ return `GenericContractEvent<'${(0, utils_1.stringPascalCase)(label)}', {${paramsOut}}, Type>`;
28
28
  }
29
29
  generateParamsOut(args) {
30
30
  return args
@@ -19,7 +19,7 @@ class IndexGen {
19
19
  const langErrorName = this.typesGen.cleanPath(this.contractMetadata.types[langErrorId].type.path);
20
20
  const typeImports = new index_js_1.TypeImports();
21
21
  typeImports.addKnownType('VersionedGenericSubstrateApi', 'RpcVersion', 'RpcV2');
22
- typeImports.addContractType('GenericContractApi', 'WithLazyStorage');
22
+ typeImports.addContractType('InkGenericContractApi', 'WithLazyStorage');
23
23
  typeImports.addChainType('SubstrateApi');
24
24
  typeImports.addPortableType(langErrorName);
25
25
  const [rootStorageName, lazyStorageName] = this.#extractRootStorageNames(typeImports);
@@ -14,7 +14,7 @@ class QueryGen {
14
14
  generate(useSubPaths = false) {
15
15
  this.typesGen.clearCache();
16
16
  this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
17
- this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult');
17
+ this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult', 'MetadataType');
18
18
  const { messages } = this.contractMetadata.spec;
19
19
  const queryCallsOut = this.doGenerate(messages, 'ContractCallOptions');
20
20
  const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
@@ -40,7 +40,7 @@ class QueryGen {
40
40
  const typeOutRaw = this.typesGen.generateType(returnType.type, 0, true);
41
41
  // Unwrap langError result
42
42
  const typeOut = typeOutRaw.match(/^(\w+)<(.*), (.*)>$/).at(2);
43
- return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult<ChainApi>>>>`;
43
+ return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult<ChainApi>>>, Type>`;
44
44
  }
45
45
  generateParamsOut(args) {
46
46
  return args
@@ -7,7 +7,7 @@ class TxGen extends QueryGen_js_1.QueryGen {
7
7
  generate(useSubPaths = false) {
8
8
  this.typesGen.clearCache();
9
9
  this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
10
- this.typesGen.typeImports.addContractType('GenericContractTx', 'GenericContractTxCall', 'ContractTxOptions', 'ContractSubmittableExtrinsic');
10
+ this.typesGen.typeImports.addContractType('GenericContractTx', 'GenericContractTxCall', 'ContractTxOptions', 'ContractSubmittableExtrinsic', 'MetadataType');
11
11
  const { messages } = this.contractMetadata.spec;
12
12
  const txMessages = messages.filter((one) => one.mutates);
13
13
  const txCallsOut = this.doGenerate(txMessages, 'ContractTxOptions');
@@ -17,7 +17,7 @@ class TxGen extends QueryGen_js_1.QueryGen {
17
17
  }
18
18
  generateMethodDef(def, optionsParamName = 'options') {
19
19
  const paramsOut = this.generateParamsOut(def.args);
20
- return `GenericContractTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractTxOptions) => ContractSubmittableExtrinsic<ChainApi>>`;
20
+ return `GenericContractTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractTxOptions) => ContractSubmittableExtrinsic<ChainApi>, Type>`;
21
21
  }
22
22
  }
23
23
  exports.TxGen = TxGen;
@@ -2,6 +2,6 @@
2
2
 
3
3
  {{{ importTypes }}}
4
4
 
5
- export interface ConstructorQuery<ChainApi extends GenericSubstrateApi> extends GenericConstructorQuery<ChainApi> {
5
+ export interface ConstructorQuery<ChainApi extends GenericSubstrateApi, Type extends MetadataType> extends GenericConstructorQuery<ChainApi, Type> {
6
6
  {{{ constructorsOut }}}
7
7
  }
@@ -4,7 +4,8 @@
4
4
 
5
5
  export interface ConstructorTx<
6
6
  ChainApi extends GenericSubstrateApi,
7
- ContractApi extends GenericContractApi = GenericContractApi,
8
- > extends GenericConstructorTx<ChainApi> {
7
+ ContractApi extends InkGenericContractApi,
8
+ Type extends MetadataType
9
+ > extends GenericConstructorTx<ChainApi, Type> {
9
10
  {{{ constructorsOut }}}
10
11
  }
@@ -2,6 +2,6 @@
2
2
 
3
3
  {{{ importTypes }}}
4
4
 
5
- export interface ContractEvents<ChainApi extends GenericSubstrateApi> extends GenericContractEvents<ChainApi> {
5
+ export interface ContractEvents <ChainApi extends GenericSubstrateApi, Type extends MetadataType> extends GenericContractEvents <ChainApi, Type> {
6
6
  {{{ eventsOut }}}
7
7
  }