@dedot/codegen 0.17.0 → 0.17.1-next.db98ff20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +1 -0
- package/cjs/sol/generator/ConstructorQueryGen.js +55 -0
- package/cjs/sol/generator/ConstructorTxGen.js +21 -0
- package/cjs/sol/generator/EventsGen.js +38 -0
- package/cjs/sol/generator/IndexGen.js +27 -0
- package/cjs/sol/generator/QueryGen.js +50 -0
- package/cjs/sol/generator/TxGen.js +21 -0
- package/cjs/sol/generator/TypesGen.js +216 -0
- package/cjs/sol/generator/index.js +23 -0
- package/cjs/sol/index.js +42 -0
- package/cjs/sol/templates/constructor-query.hbs +7 -0
- package/cjs/sol/templates/constructor-tx.hbs +11 -0
- package/cjs/sol/templates/events.hbs +7 -0
- package/cjs/sol/templates/index.hbs +23 -0
- package/cjs/sol/templates/query.hbs +7 -0
- package/cjs/sol/templates/tx.hbs +7 -0
- package/cjs/sol/templates/types.hbs +5 -0
- package/cjs/typink/generator/ConstructorQueryGen.js +2 -2
- package/cjs/typink/generator/ConstructorTxGen.js +2 -2
- package/cjs/typink/generator/EventsGen.js +2 -2
- package/cjs/typink/generator/IndexGen.js +1 -1
- package/cjs/typink/generator/QueryGen.js +2 -2
- package/cjs/typink/generator/TxGen.js +2 -2
- package/cjs/typink/templates/constructor-query.hbs +1 -1
- package/cjs/typink/templates/constructor-tx.hbs +3 -2
- package/cjs/typink/templates/events.hbs +1 -1
- package/cjs/typink/templates/index.hbs +8 -7
- package/cjs/typink/templates/query.hbs +1 -1
- package/cjs/typink/templates/tx.hbs +1 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +12 -11
- package/sol/generator/ConstructorQueryGen.d.ts +12 -0
- package/sol/generator/ConstructorQueryGen.js +51 -0
- package/sol/generator/ConstructorTxGen.d.ts +6 -0
- package/sol/generator/ConstructorTxGen.js +17 -0
- package/sol/generator/EventsGen.d.ts +10 -0
- package/sol/generator/EventsGen.js +34 -0
- package/sol/generator/IndexGen.d.ts +7 -0
- package/sol/generator/IndexGen.js +23 -0
- package/sol/generator/QueryGen.d.ts +11 -0
- package/sol/generator/QueryGen.js +46 -0
- package/sol/generator/TxGen.d.ts +6 -0
- package/sol/generator/TxGen.js +17 -0
- package/sol/generator/TypesGen.d.ts +15 -0
- package/sol/generator/TypesGen.js +212 -0
- package/sol/generator/index.d.ts +7 -0
- package/sol/generator/index.js +7 -0
- package/sol/index.d.ts +3 -0
- package/sol/index.js +35 -0
- package/sol/templates/constructor-query.hbs +7 -0
- package/sol/templates/constructor-tx.hbs +11 -0
- package/sol/templates/events.hbs +7 -0
- package/sol/templates/index.hbs +23 -0
- package/sol/templates/query.hbs +7 -0
- package/sol/templates/tx.hbs +7 -0
- package/sol/templates/types.hbs +5 -0
- package/typink/generator/ConstructorQueryGen.js +2 -2
- package/typink/generator/ConstructorTxGen.js +2 -2
- package/typink/generator/EventsGen.js +2 -2
- package/typink/generator/IndexGen.js +1 -1
- package/typink/generator/QueryGen.js +2 -2
- package/typink/generator/TxGen.js +2 -2
- package/typink/templates/constructor-query.hbs +1 -1
- package/typink/templates/constructor-tx.hbs +3 -2
- package/typink/templates/events.hbs +1 -1
- package/typink/templates/index.hbs +8 -7
- package/typink/templates/query.hbs +1 -1
- package/typink/templates/tx.hbs +1 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { TypeImports } from '../../shared/TypeImports.js';
|
|
3
|
+
import { beautifySourceCode, compileTemplate, isNativeType } from '../../utils.js';
|
|
4
|
+
const INT_TYPES = /^int(\d+)?(\[(\d+)?])*?$/;
|
|
5
|
+
const UINT_TYPES = /^uint(\d+)?(\[(\d+)?])*?$/;
|
|
6
|
+
const BYTES_TYPES = /^bytes(\d+)?(\[(\d+)?])*?$/;
|
|
7
|
+
const FIXED_TYPES = /^fixed(\d+x\d+)?(\[(\d+)?])*?$/;
|
|
8
|
+
const UNFIXED_TYPES = /^ufixed(\d+x\d+)?(\[(\d+)?])*?$/;
|
|
9
|
+
const STRING_TYPES = /^string(\[(\d+)?])*?$/;
|
|
10
|
+
const BOOL_TYPES = /^bool(\[(\d+)?])*?$/;
|
|
11
|
+
const ADDRESS_TYPES = /^address(\[(\d+)?])*?$/;
|
|
12
|
+
const FUNCTION_TYPES = /^function(\[(\d+)?])*?$/;
|
|
13
|
+
const COMPONENT_TYPES = /^tuple(\[(\d+)?])*?$/;
|
|
14
|
+
const ARRAY_DIM = /\[(\d+)?]/g;
|
|
15
|
+
const ONLY_ARRAY_DIM = /^\[(\d+)?]$/;
|
|
16
|
+
export const SUPPORTED_SOLIDITY_TYPES = [
|
|
17
|
+
INT_TYPES,
|
|
18
|
+
UINT_TYPES,
|
|
19
|
+
BYTES_TYPES,
|
|
20
|
+
FIXED_TYPES,
|
|
21
|
+
UNFIXED_TYPES,
|
|
22
|
+
STRING_TYPES,
|
|
23
|
+
BOOL_TYPES,
|
|
24
|
+
ADDRESS_TYPES,
|
|
25
|
+
FUNCTION_TYPES,
|
|
26
|
+
COMPONENT_TYPES,
|
|
27
|
+
];
|
|
28
|
+
export const BASIC_KNOWN_TYPES = [
|
|
29
|
+
/^(H160)$/,
|
|
30
|
+
/^(FixedBytes)<(\d+)>$/,
|
|
31
|
+
/^(Bytes)$/,
|
|
32
|
+
/^(BytesLike)$/,
|
|
33
|
+
/^(Fixed)<(\d+),(\d+)>$/,
|
|
34
|
+
/^(UFixed)<(\d+),(\d+)>$/,
|
|
35
|
+
/^(FixedArray)$/,
|
|
36
|
+
];
|
|
37
|
+
export class TypesGen {
|
|
38
|
+
abi;
|
|
39
|
+
typeImports;
|
|
40
|
+
constructor(abi) {
|
|
41
|
+
this.abi = abi;
|
|
42
|
+
this.typeImports = new TypeImports();
|
|
43
|
+
}
|
|
44
|
+
generate(useSubPaths = false) {
|
|
45
|
+
let defTypeOut = '';
|
|
46
|
+
this.abi.forEach((abiItem) => {
|
|
47
|
+
if (abiItem.type === 'fallback' || abiItem.type === 'receive')
|
|
48
|
+
return;
|
|
49
|
+
if (abiItem.type === 'function' || abiItem.type === 'constructor' || abiItem.type === 'event') {
|
|
50
|
+
const { inputs } = abiItem;
|
|
51
|
+
inputs
|
|
52
|
+
.filter((o) => o.components && o.components.length > 0)
|
|
53
|
+
.forEach((o) => {
|
|
54
|
+
defTypeOut += `export type ${this.generateTypeName(o, abiItem)} = ${this.generateType(o, abiItem, 0)};\n\n`;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (abiItem.type === 'function') {
|
|
58
|
+
const { outputs } = abiItem;
|
|
59
|
+
outputs
|
|
60
|
+
.filter((o) => o.components && o.components.length > 0)
|
|
61
|
+
.forEach((o) => {
|
|
62
|
+
defTypeOut += `export type ${this.generateTypeName(o, abiItem, true)} = ${this.generateType(o, abiItem, 0, true)};\n\n`;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
const importTypes = this.typeImports.toImports({ excludeModules: ['./types.js'], useSubPaths });
|
|
67
|
+
const template = compileTemplate('sol/templates/types.hbs');
|
|
68
|
+
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
69
|
+
}
|
|
70
|
+
generateTypeName(typeDef, abiItem, typeOut = false) {
|
|
71
|
+
if (abiItem.type === 'fallback' || abiItem.type === 'receive')
|
|
72
|
+
return '';
|
|
73
|
+
if (COMPONENT_TYPES.test(typeDef.type)) {
|
|
74
|
+
const baseName = abiItem.type === 'constructor'
|
|
75
|
+
? `${typeDef.name}_input`
|
|
76
|
+
: `${abiItem.name}_${typeDef.name}_${typeOut ? 'output' : 'input'}`;
|
|
77
|
+
return stringPascalCase(baseName);
|
|
78
|
+
}
|
|
79
|
+
return '';
|
|
80
|
+
}
|
|
81
|
+
generateType(typeDef, abiItem, nestedLevel = 0, typeOut = false) {
|
|
82
|
+
if (nestedLevel > 0 && abiItem) {
|
|
83
|
+
let typeName = this.generateTypeName(typeDef, abiItem, typeOut);
|
|
84
|
+
if (typeName.length === 0) {
|
|
85
|
+
typeName = this.#generateType(typeDef, nestedLevel, typeOut);
|
|
86
|
+
return typeName;
|
|
87
|
+
}
|
|
88
|
+
this.addTypeImport(typeName);
|
|
89
|
+
return typeName;
|
|
90
|
+
}
|
|
91
|
+
return this.#generateType(typeDef, nestedLevel, typeOut);
|
|
92
|
+
}
|
|
93
|
+
#generateType(typeDef, nestedLevel = 0, typeOut = false) {
|
|
94
|
+
const { type } = typeDef;
|
|
95
|
+
let baseType = this.#generateBaseType(typeDef, nestedLevel, typeOut);
|
|
96
|
+
if (!COMPONENT_TYPES.test(type)) {
|
|
97
|
+
// baseType of component types are all generated types, eg: { a: bigint, b: string }
|
|
98
|
+
// So we don't import them here.
|
|
99
|
+
this.addTypeImport(baseType);
|
|
100
|
+
}
|
|
101
|
+
// Match all array dimensions
|
|
102
|
+
const dimensions = type.match(ARRAY_DIM);
|
|
103
|
+
dimensions
|
|
104
|
+
// Match each dimension to see if fixed array or dynamic array (eg: [3] vs [])
|
|
105
|
+
?.map((o) => o.match(ONLY_ARRAY_DIM))
|
|
106
|
+
.forEach(([_, n]) => {
|
|
107
|
+
if (n) {
|
|
108
|
+
this.addTypeImport('FixedArray');
|
|
109
|
+
baseType = `FixedArray<${baseType}, ${n}>`;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
baseType = isNativeType(baseType.replaceAll('[]', '')) ? `${baseType}[]` : `Array<${baseType}>`;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
return baseType;
|
|
116
|
+
}
|
|
117
|
+
#generateBaseType(typeDef, nestedLevel = 0, typeOut = false) {
|
|
118
|
+
const { type } = typeDef;
|
|
119
|
+
if (INT_TYPES.test(type)) {
|
|
120
|
+
const [_, bitsStr] = type.match(INT_TYPES);
|
|
121
|
+
// Default to 256 when unspecified
|
|
122
|
+
const bits = bitsStr ? parseInt(bitsStr) : 256;
|
|
123
|
+
const isSafeNumber = bits <= 48;
|
|
124
|
+
return isSafeNumber ? `number` : 'bigint';
|
|
125
|
+
}
|
|
126
|
+
else if (UINT_TYPES.test(type)) {
|
|
127
|
+
const [_, bitsStr] = type.match(UINT_TYPES);
|
|
128
|
+
// Default to 256 when unspecified
|
|
129
|
+
const bits = bitsStr ? parseInt(bitsStr) : 256;
|
|
130
|
+
const isSafeNumber = bits <= 48;
|
|
131
|
+
return isSafeNumber ? `number` : 'bigint';
|
|
132
|
+
}
|
|
133
|
+
else if (BYTES_TYPES.test(type)) {
|
|
134
|
+
const [_, n] = type.match(BYTES_TYPES);
|
|
135
|
+
if (n) {
|
|
136
|
+
return `FixedBytes<${n}>`;
|
|
137
|
+
}
|
|
138
|
+
if (typeOut) {
|
|
139
|
+
return `Bytes`;
|
|
140
|
+
}
|
|
141
|
+
return `BytesLike`;
|
|
142
|
+
}
|
|
143
|
+
else if (BOOL_TYPES.test(type)) {
|
|
144
|
+
return 'boolean';
|
|
145
|
+
}
|
|
146
|
+
else if (STRING_TYPES.test(type)) {
|
|
147
|
+
return 'string';
|
|
148
|
+
}
|
|
149
|
+
else if (COMPONENT_TYPES.test(type)) {
|
|
150
|
+
const { components = [] } = typeDef;
|
|
151
|
+
if (components.length === 0) {
|
|
152
|
+
return '[]';
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
const objectType = this.generateObjectType(components, nestedLevel + 1, typeOut);
|
|
156
|
+
return `${objectType}`;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else if (ADDRESS_TYPES.test(type)) {
|
|
160
|
+
return `H160`;
|
|
161
|
+
}
|
|
162
|
+
else if (FUNCTION_TYPES.test(type)) {
|
|
163
|
+
// Function type is an address (20 bytes) followed by a function selector (4 bytes).
|
|
164
|
+
// 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.
|
|
165
|
+
return `FixedBytes<24>`;
|
|
166
|
+
}
|
|
167
|
+
else if (FIXED_TYPES.test(type)) {
|
|
168
|
+
const [_, denotation] = type.match(FIXED_TYPES);
|
|
169
|
+
// 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.
|
|
170
|
+
const [m, n] = denotation ? denotation.split('x').map((s) => parseInt(s)) : [128, 18];
|
|
171
|
+
return `Fixed<${m},${n}>`;
|
|
172
|
+
}
|
|
173
|
+
else if (UNFIXED_TYPES.test(type)) {
|
|
174
|
+
const [_, denotation] = type.match(UNFIXED_TYPES);
|
|
175
|
+
// 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.
|
|
176
|
+
const [m, n] = denotation ? denotation.split('x').map((s) => parseInt(s)) : [128, 18];
|
|
177
|
+
return `UFixed<${m},${n}>`;
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
throw new Error(`Unsupported Solidity type: ${type}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
generateObjectType(components, nestedLevel = 0, typeOut = false) {
|
|
184
|
+
const props = components.map((typeDef) => {
|
|
185
|
+
const type = this.generateType(typeDef, undefined, nestedLevel + 1, typeOut);
|
|
186
|
+
return {
|
|
187
|
+
name: typeDef.name,
|
|
188
|
+
type,
|
|
189
|
+
};
|
|
190
|
+
});
|
|
191
|
+
if (props.length > 0 && props.at(0).name.length === 0) {
|
|
192
|
+
return `[${props.map(({ type }) => `${type}`).join(', ')}]`;
|
|
193
|
+
}
|
|
194
|
+
return `{${props.map(({ name, type }) => `${name}: ${type}`).join(',\n')}}`;
|
|
195
|
+
}
|
|
196
|
+
addTypeImport(typeName) {
|
|
197
|
+
if (Array.isArray(typeName)) {
|
|
198
|
+
typeName.forEach((one) => this.addTypeImport(one));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (isNativeType(typeName)) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const re = BASIC_KNOWN_TYPES.find((re) => typeName.match(re));
|
|
205
|
+
if (re) {
|
|
206
|
+
const typeBase = typeName.match(re)[1];
|
|
207
|
+
this.typeImports.addCodecType(typeBase);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
this.typeImports.addPortableType(typeName);
|
|
211
|
+
}
|
|
212
|
+
}
|
package/sol/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { SolAbi } from '@dedot/contracts';
|
|
2
|
+
import { GeneratedResult } from '../types.js';
|
|
3
|
+
export declare function generateSolContractTypes(abi: SolAbi | string, contract?: string | undefined, outDir?: string, extension?: string, useSubPaths?: boolean): Promise<GeneratedResult>;
|
package/sol/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { stringDashCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import { ConstructorQueryGen, ConstructorTxGen, EventsGen, IndexGen, QueryGen, TxGen, TypesGen, } from './generator/index.js';
|
|
5
|
+
export async function generateSolContractTypes(abi, contract = 'contract', outDir = '.', extension = 'd.ts', useSubPaths = false) {
|
|
6
|
+
let abiItems = typeof abi === 'string' ? JSON.parse(abi) : abi;
|
|
7
|
+
const contractName = contract;
|
|
8
|
+
const dirPath = path.resolve(outDir, stringDashCase(contractName));
|
|
9
|
+
const typesFileName = path.join(dirPath, `types.${extension}`);
|
|
10
|
+
const queryTypesFileName = path.join(dirPath, `query.${extension}`);
|
|
11
|
+
const eventsTypesFileName = path.join(dirPath, `events.${extension}`);
|
|
12
|
+
const txTypesFileName = path.join(dirPath, `tx.${extension}`);
|
|
13
|
+
const constructorTxTypesFileName = path.join(dirPath, `constructor-tx.${extension}`);
|
|
14
|
+
const constructorQueryTypesFileName = path.join(dirPath, `constructor-query.${extension}`);
|
|
15
|
+
const indexTypesFileName = path.join(dirPath, `index.${extension}`);
|
|
16
|
+
if (!fs.existsSync(dirPath)) {
|
|
17
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
const interfaceName = `${stringPascalCase(`${contractName}`)}ContractApi`;
|
|
20
|
+
const typesGen = new TypesGen(abiItems);
|
|
21
|
+
const queryGen = new QueryGen(abiItems, typesGen);
|
|
22
|
+
const eventsGen = new EventsGen(abiItems, typesGen);
|
|
23
|
+
const txGen = new TxGen(abiItems, typesGen);
|
|
24
|
+
const constructorTxGen = new ConstructorTxGen(abiItems, typesGen);
|
|
25
|
+
const constructorQueryGen = new ConstructorQueryGen(abiItems, typesGen);
|
|
26
|
+
const indexGen = new IndexGen(interfaceName, typesGen);
|
|
27
|
+
fs.writeFileSync(typesFileName, await typesGen.generate(useSubPaths));
|
|
28
|
+
fs.writeFileSync(queryTypesFileName, await queryGen.generate(useSubPaths));
|
|
29
|
+
fs.writeFileSync(eventsTypesFileName, await eventsGen.generate(useSubPaths));
|
|
30
|
+
fs.writeFileSync(txTypesFileName, await txGen.generate(useSubPaths));
|
|
31
|
+
fs.writeFileSync(constructorQueryTypesFileName, await constructorQueryGen.generate(useSubPaths));
|
|
32
|
+
fs.writeFileSync(constructorTxTypesFileName, await constructorTxGen.generate(useSubPaths));
|
|
33
|
+
fs.writeFileSync(indexTypesFileName, await indexGen.generate(useSubPaths));
|
|
34
|
+
return { interfaceName, outputFolder: dirPath };
|
|
35
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Generated by dedot cli
|
|
2
|
+
|
|
3
|
+
{{{ importTypes }}}
|
|
4
|
+
|
|
5
|
+
export interface ConstructorTx<
|
|
6
|
+
ChainApi extends GenericSubstrateApi,
|
|
7
|
+
ContractApi extends SolGenericContractApi,
|
|
8
|
+
Type extends MetadataType
|
|
9
|
+
> extends GenericConstructorTx <ChainApi, Type> {
|
|
10
|
+
{{{ constructorsOut }}}
|
|
11
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Generated by dedot cli
|
|
2
|
+
|
|
3
|
+
{{{importTypes}}}
|
|
4
|
+
import { ContractQuery } from './query.js';
|
|
5
|
+
import { ContractTx } from './tx.js';
|
|
6
|
+
import { ConstructorQuery } from './constructor-query.js';
|
|
7
|
+
import { ConstructorTx } from './constructor-tx.js';
|
|
8
|
+
import { ContractEvents } from './events.js';
|
|
9
|
+
|
|
10
|
+
export * from './types.js';
|
|
11
|
+
|
|
12
|
+
{{{interfaceDocs}}}export interface {{{interfaceName}}}<Rv extends RpcVersion = RpcVersion, ChainApi extends VersionedGenericSubstrateApi = SubstrateApi> extends SolGenericContractApi<Rv, ChainApi> {
|
|
13
|
+
metadataType: 'sol';
|
|
14
|
+
query: ContractQuery<ChainApi[Rv], 'sol'>;
|
|
15
|
+
tx: ContractTx<ChainApi[Rv], 'sol'>;
|
|
16
|
+
constructorQuery: ConstructorQuery<ChainApi[Rv], 'sol'>;
|
|
17
|
+
events: ContractEvents<ChainApi[Rv], 'sol'>;
|
|
18
|
+
constructorTx: ConstructorTx<ChainApi[Rv], {{{interfaceName}}}, 'sol'>;
|
|
19
|
+
|
|
20
|
+
types: {
|
|
21
|
+
ChainApi: ChainApi[Rv];
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -4,7 +4,7 @@ export class ConstructorQueryGen extends QueryGen {
|
|
|
4
4
|
generate(useSubPaths = false) {
|
|
5
5
|
this.typesGen.clearCache();
|
|
6
6
|
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
7
|
-
this.typesGen.typeImports.addContractType('GenericConstructorQuery', 'GenericConstructorQueryCall', 'GenericConstructorCallResult', 'ConstructorCallOptions', 'ContractInstantiateResult');
|
|
7
|
+
this.typesGen.typeImports.addContractType('GenericConstructorQuery', 'GenericConstructorQueryCall', 'GenericConstructorCallResult', 'ConstructorCallOptions', 'ContractInstantiateResult', 'MetadataType');
|
|
8
8
|
const { constructors } = this.contractMetadata.spec;
|
|
9
9
|
const constructorsOut = this.doGenerate(constructors, 'ConstructorCallOptions');
|
|
10
10
|
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
@@ -17,6 +17,6 @@ export class ConstructorQueryGen extends QueryGen {
|
|
|
17
17
|
const typeOutRaw = this.typesGen.generateType(returnType.type, 0, true);
|
|
18
18
|
// Unwrap langError result
|
|
19
19
|
const typeOut = typeOutRaw.match(/^(\w+)<(.*), (.*)>$/).at(2);
|
|
20
|
-
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<${typeOut}, ContractInstantiateResult<ChainApi
|
|
20
|
+
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorCallOptions) => Promise<GenericConstructorCallResult<${typeOut}, ContractInstantiateResult<ChainApi>>>, Type>`;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -4,7 +4,7 @@ export class ConstructorTxGen extends QueryGen {
|
|
|
4
4
|
generate(useSubPaths = false) {
|
|
5
5
|
this.typesGen.clearCache();
|
|
6
6
|
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
7
|
-
this.typesGen.typeImports.addContractType('GenericConstructorTx', 'GenericConstructorTxCall', 'ConstructorTxOptions', '
|
|
7
|
+
this.typesGen.typeImports.addContractType('GenericConstructorTx', 'GenericConstructorTxCall', 'ConstructorTxOptions', 'InkGenericContractApi', 'GenericInstantiateSubmittableExtrinsic', 'MetadataType');
|
|
8
8
|
const { constructors } = this.contractMetadata.spec;
|
|
9
9
|
const constructorsOut = this.doGenerate(constructors, 'ConstructorTxOptions');
|
|
10
10
|
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
@@ -13,6 +13,6 @@ export class ConstructorTxGen extends QueryGen {
|
|
|
13
13
|
}
|
|
14
14
|
generateMethodDef(def, optionsParamName = 'options') {
|
|
15
15
|
const paramsOut = this.generateParamsOut(def.args);
|
|
16
|
-
return `GenericConstructorTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorTxOptions) => GenericInstantiateSubmittableExtrinsic<ChainApi, ContractApi
|
|
16
|
+
return `GenericConstructorTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ConstructorTxOptions) => GenericInstantiateSubmittableExtrinsic<ChainApi, ContractApi>, Type>`;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -5,7 +5,7 @@ export class EventsGen extends QueryGen {
|
|
|
5
5
|
generate(useSubPaths = false) {
|
|
6
6
|
this.typesGen.clearCache();
|
|
7
7
|
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
8
|
-
this.typesGen.typeImports.addContractType('GenericContractEvents', 'GenericContractEvent');
|
|
8
|
+
this.typesGen.typeImports.addContractType('GenericContractEvents', 'GenericContractEvent', 'MetadataType');
|
|
9
9
|
const { events } = this.contractMetadata.spec;
|
|
10
10
|
let eventsOut = '';
|
|
11
11
|
const isV5 = this.contractMetadata.version === 5;
|
|
@@ -21,7 +21,7 @@ export class EventsGen extends QueryGen {
|
|
|
21
21
|
#generateEventDef(event) {
|
|
22
22
|
const { args, label } = event;
|
|
23
23
|
const paramsOut = this.generateParamsOut(args);
|
|
24
|
-
return `GenericContractEvent<'${stringPascalCase(label)}', {${paramsOut}}>`;
|
|
24
|
+
return `GenericContractEvent<'${stringPascalCase(label)}', {${paramsOut}}, Type>`;
|
|
25
25
|
}
|
|
26
26
|
generateParamsOut(args) {
|
|
27
27
|
return args
|
|
@@ -16,7 +16,7 @@ export class IndexGen {
|
|
|
16
16
|
const langErrorName = this.typesGen.cleanPath(this.contractMetadata.types[langErrorId].type.path);
|
|
17
17
|
const typeImports = new TypeImports();
|
|
18
18
|
typeImports.addKnownType('VersionedGenericSubstrateApi', 'RpcVersion', 'RpcV2');
|
|
19
|
-
typeImports.addContractType('
|
|
19
|
+
typeImports.addContractType('InkGenericContractApi', 'WithLazyStorage');
|
|
20
20
|
typeImports.addChainType('SubstrateApi');
|
|
21
21
|
typeImports.addPortableType(langErrorName);
|
|
22
22
|
const [rootStorageName, lazyStorageName] = this.#extractRootStorageNames(typeImports);
|
|
@@ -11,7 +11,7 @@ export class QueryGen {
|
|
|
11
11
|
generate(useSubPaths = false) {
|
|
12
12
|
this.typesGen.clearCache();
|
|
13
13
|
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
14
|
-
this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult');
|
|
14
|
+
this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult', 'MetadataType');
|
|
15
15
|
const { messages } = this.contractMetadata.spec;
|
|
16
16
|
const queryCallsOut = this.doGenerate(messages, 'ContractCallOptions');
|
|
17
17
|
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
@@ -37,7 +37,7 @@ export class QueryGen {
|
|
|
37
37
|
const typeOutRaw = this.typesGen.generateType(returnType.type, 0, true);
|
|
38
38
|
// Unwrap langError result
|
|
39
39
|
const typeOut = typeOutRaw.match(/^(\w+)<(.*), (.*)>$/).at(2);
|
|
40
|
-
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult<ChainApi
|
|
40
|
+
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult<ChainApi>>>, Type>`;
|
|
41
41
|
}
|
|
42
42
|
generateParamsOut(args) {
|
|
43
43
|
return args
|
|
@@ -4,7 +4,7 @@ export class TxGen extends QueryGen {
|
|
|
4
4
|
generate(useSubPaths = false) {
|
|
5
5
|
this.typesGen.clearCache();
|
|
6
6
|
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
7
|
-
this.typesGen.typeImports.addContractType('GenericContractTx', 'GenericContractTxCall', 'ContractTxOptions', 'ContractSubmittableExtrinsic');
|
|
7
|
+
this.typesGen.typeImports.addContractType('GenericContractTx', 'GenericContractTxCall', 'ContractTxOptions', 'ContractSubmittableExtrinsic', 'MetadataType');
|
|
8
8
|
const { messages } = this.contractMetadata.spec;
|
|
9
9
|
const txMessages = messages.filter((one) => one.mutates);
|
|
10
10
|
const txCallsOut = this.doGenerate(txMessages, 'ContractTxOptions');
|
|
@@ -14,6 +14,6 @@ export class TxGen extends QueryGen {
|
|
|
14
14
|
}
|
|
15
15
|
generateMethodDef(def, optionsParamName = 'options') {
|
|
16
16
|
const paramsOut = this.generateParamsOut(def.args);
|
|
17
|
-
return `GenericContractTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractTxOptions) => ContractSubmittableExtrinsic<ChainApi
|
|
17
|
+
return `GenericContractTxCall<ChainApi, (${paramsOut && `${paramsOut},`} ${optionsParamName}?: ContractTxOptions) => ContractSubmittableExtrinsic<ChainApi>, Type>`;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
@@ -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
|
|
8
|
-
|
|
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
|
}
|
|
@@ -9,21 +9,22 @@ import { ContractEvents } from './events.js';
|
|
|
9
9
|
|
|
10
10
|
export * from './types.js';
|
|
11
11
|
|
|
12
|
-
{{{interfaceDocs}}}export interface {{{interfaceName}}}<Rv extends RpcVersion = RpcVersion, ChainApi extends VersionedGenericSubstrateApi = SubstrateApi> extends
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
{{{interfaceDocs}}}export interface {{{interfaceName}}}<Rv extends RpcVersion = RpcVersion, ChainApi extends VersionedGenericSubstrateApi = SubstrateApi> extends InkGenericContractApi<Rv, ChainApi> {
|
|
13
|
+
metadataType: 'ink';
|
|
14
|
+
query: ContractQuery<ChainApi[Rv], 'ink'>;
|
|
15
|
+
tx: ContractTx<ChainApi[Rv], 'ink'>;
|
|
16
|
+
constructorQuery: ConstructorQuery<ChainApi[Rv], 'ink'>;
|
|
17
|
+
constructorTx: ConstructorTx<ChainApi[Rv], {{{interfaceName}}}, 'ink'>;
|
|
18
|
+
events: ContractEvents<ChainApi[Rv], 'ink'>;
|
|
18
19
|
storage: {
|
|
19
20
|
root(): Promise<{{{rootStorageName}}}>;
|
|
20
21
|
lazy(): {{{lazyStorageName}}};
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
types: {
|
|
25
|
+
ChainApi: ChainApi[Rv];
|
|
24
26
|
RootStorage: {{{rootStorageName}}};
|
|
25
27
|
LazyStorage: {{{lazyStorageName}}};
|
|
26
28
|
LangError: {{{langErrorName}}};
|
|
27
|
-
ChainApi: ChainApi[Rv];
|
|
28
29
|
};
|
|
29
30
|
}
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
{{{ importTypes }}}
|
|
4
4
|
|
|
5
|
-
export interface ContractQuery<ChainApi extends GenericSubstrateApi> extends GenericContractQuery<ChainApi> {
|
|
5
|
+
export interface ContractQuery<ChainApi extends GenericSubstrateApi, Type extends MetadataType> extends GenericContractQuery<ChainApi, Type> {
|
|
6
6
|
{{{ queryCallsOut }}}
|
|
7
7
|
}
|
package/typink/templates/tx.hbs
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
{{{ importTypes }}}
|
|
4
4
|
|
|
5
|
-
export interface ContractTx<ChainApi extends GenericSubstrateApi> extends GenericContractTx<ChainApi> {
|
|
5
|
+
export interface ContractTx<ChainApi extends GenericSubstrateApi, Type extends MetadataType> extends GenericContractTx<ChainApi, Type> {
|
|
6
6
|
{{{ txCallsOut }}}
|
|
7
7
|
}
|