@taqueria/plugin-contract-types 0.28.6 → 0.28.17
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/LICENSE +201 -0
- package/README.md +1 -1
- package/_readme.eta +1 -1
- package/{chunk-B5MX5VNS.js → chunk-BKLWHK3J.js} +14 -7
- package/chunk-BKLWHK3J.js.map +1 -0
- package/{chunk-E6YGOZCV.js → chunk-FJB62W6D.js} +2 -2
- package/chunk-RD5ZALXH.js +641 -0
- package/chunk-RD5ZALXH.js.map +1 -0
- package/chunk-UZ6TI22M.js +242 -0
- package/chunk-UZ6TI22M.js.map +1 -0
- package/example/contracts/example-contract-optionals.tz +10 -0
- package/index.cjs +13 -10
- package/index.cjs.map +1 -1
- package/index.js +2 -2
- package/package.json +14 -13
- package/src/cli-process.cjs +13 -10
- package/src/cli-process.cjs.map +1 -1
- package/src/cli-process.js +2 -2
- package/src/generator/contract-parser.ts +3 -1
- package/src/generator/testing-code-generator.cjs +13 -10
- package/src/generator/testing-code-generator.cjs.map +1 -1
- package/src/generator/testing-code-generator.js +1 -1
- package/src/generator/typescript-output.ts +13 -5
- package/chunk-B5MX5VNS.js.map +0 -1
- /package/{chunk-E6YGOZCV.js.map → chunk-FJB62W6D.js.map} +0 -0
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
// src/generator/contract-name.ts
|
|
2
|
+
var normalizeContractName = (text) => text.replace(/[^A-Za-z0-9]/g, "_").split("_").filter((x) => x).map((x) => x[0].toUpperCase() + x.substring(1)).join("");
|
|
3
|
+
|
|
4
|
+
// src/generator/common.ts
|
|
5
|
+
var GenerateApiError = class {
|
|
6
|
+
constructor(message, data) {
|
|
7
|
+
this.message = message;
|
|
8
|
+
this.data = data;
|
|
9
|
+
this.name = `GenerateApiError`;
|
|
10
|
+
console.error(`\u274C GenerateApiError: ${message}`, data);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var assertExhaustive = (value, message) => {
|
|
14
|
+
console.error(message, { value });
|
|
15
|
+
};
|
|
16
|
+
var reduceFlatMap = (out, x) => {
|
|
17
|
+
out.push(...x);
|
|
18
|
+
return out;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/generator/typescript-output.ts
|
|
22
|
+
var createTypescriptCodeGenerator = (options) => {
|
|
23
|
+
const usedStrictTypes = [];
|
|
24
|
+
const addTypeAlias = (strictType) => {
|
|
25
|
+
if (!usedStrictTypes.some((x) => x.aliasType === strictType.aliasType)) {
|
|
26
|
+
usedStrictTypes.push(strictType);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const tabs = (indent) => Array(indent).fill(` `).join(``);
|
|
30
|
+
const toIndentedItems = (indent, delimiters, items) => {
|
|
31
|
+
return `
|
|
32
|
+
${tabs(indent + 1)}${items.join(`${delimiters.afterItem ?? ``}
|
|
33
|
+
${tabs(indent + 1)}${delimiters.beforeItem ?? ``}`)}
|
|
34
|
+
${tabs(indent)}`;
|
|
35
|
+
};
|
|
36
|
+
const typeToCode = (t, indent) => {
|
|
37
|
+
if ((options == null ? void 0 : options.mode) === "defaultValue") {
|
|
38
|
+
return typeToCode_defaultValue(t, indent);
|
|
39
|
+
}
|
|
40
|
+
if (t.kind === `value`) {
|
|
41
|
+
const prim = `prim` in t.raw ? t.raw.prim : `unknown`;
|
|
42
|
+
if (t.typescriptType === `boolean` || t.typescriptType === `string` && prim === `string`) {
|
|
43
|
+
return `${t.typescriptType}`;
|
|
44
|
+
}
|
|
45
|
+
if (t.typescriptType === "number") {
|
|
46
|
+
const simpleBaseType2 = `string | BigNumber | number`;
|
|
47
|
+
const typeAlias2 = {
|
|
48
|
+
aliasType: prim,
|
|
49
|
+
simpleTypeDefinition: `type ${prim} = ${simpleBaseType2};`,
|
|
50
|
+
simpleTypeImports: [{ name: "BigNumber", isDefault: true, from: "bignumber.js" }]
|
|
51
|
+
};
|
|
52
|
+
addTypeAlias(typeAlias2);
|
|
53
|
+
return typeAlias2.aliasType;
|
|
54
|
+
}
|
|
55
|
+
const simpleBaseType = t.typescriptType === "Date" ? "Date | string" : t.typescriptType;
|
|
56
|
+
const typeAlias = { aliasType: prim, simpleTypeDefinition: `type ${prim} = ${simpleBaseType};` };
|
|
57
|
+
addTypeAlias(typeAlias);
|
|
58
|
+
return typeAlias.aliasType;
|
|
59
|
+
}
|
|
60
|
+
if (t.kind === `array`) {
|
|
61
|
+
return `Array<${typeToCode(t.array.item, indent)}>`;
|
|
62
|
+
}
|
|
63
|
+
if (t.kind === `map`) {
|
|
64
|
+
const typeAlias = t.map.isBigMap ? {
|
|
65
|
+
aliasType: `BigMap`,
|
|
66
|
+
simpleTypeDefinition: "type BigMap<K, T> = MichelsonMap<K, T>;",
|
|
67
|
+
simpleTypeImports: [{ name: "MichelsonMap", from: "@taquito/taquito" }]
|
|
68
|
+
} : {
|
|
69
|
+
aliasType: `MMap`,
|
|
70
|
+
simpleTypeDefinition: "type MMap<K, T> = MichelsonMap<K, T>;",
|
|
71
|
+
simpleTypeImports: [{ name: "MichelsonMap", from: "@taquito/taquito" }]
|
|
72
|
+
};
|
|
73
|
+
addTypeAlias(typeAlias);
|
|
74
|
+
return `${typeAlias.aliasType}<${typeToCode(t.map.key, indent)}, ${typeToCode(t.map.value, indent)}>`;
|
|
75
|
+
}
|
|
76
|
+
if (t.kind === `lambda`) {
|
|
77
|
+
const typeAlias = {
|
|
78
|
+
aliasType: "Instruction",
|
|
79
|
+
simpleTypeDefinition: `type Instruction = MichelsonInstruction;`,
|
|
80
|
+
simpleTypeImports: [{ name: "MichelsonInstruction", isDefault: false, from: "@taquito/michel-codec" }]
|
|
81
|
+
};
|
|
82
|
+
addTypeAlias(typeAlias);
|
|
83
|
+
return `Instruction[]`;
|
|
84
|
+
}
|
|
85
|
+
if (t.kind === `object`) {
|
|
86
|
+
return `{${toIndentedItems(indent, {}, t.fields.map((a, i) => varToCode(a, i, indent + 1) + `;`))}}`;
|
|
87
|
+
}
|
|
88
|
+
if (t.kind === `union`) {
|
|
89
|
+
const getUnionItem = (a, i) => {
|
|
90
|
+
const itemCode = `${varToCode(a, i, indent + 1)}`;
|
|
91
|
+
if (!itemCode.includes(`
|
|
92
|
+
`)) {
|
|
93
|
+
return `{ ${itemCode} }`;
|
|
94
|
+
}
|
|
95
|
+
return `{${toIndentedItems(indent + 1, {}, [`${varToCode(a, i, indent + 2)}`])}}`;
|
|
96
|
+
};
|
|
97
|
+
return `(${toIndentedItems(indent, { beforeItem: `| ` }, t.union.map(getUnionItem))})`;
|
|
98
|
+
}
|
|
99
|
+
if (t.kind === `unit`) {
|
|
100
|
+
const typeAlias = {
|
|
101
|
+
aliasType: `unit`,
|
|
102
|
+
simpleTypeDefinition: `type unit = (true | undefined);`
|
|
103
|
+
};
|
|
104
|
+
addTypeAlias(typeAlias);
|
|
105
|
+
return typeAlias.aliasType;
|
|
106
|
+
}
|
|
107
|
+
if (t.kind === `never`) {
|
|
108
|
+
return `never`;
|
|
109
|
+
}
|
|
110
|
+
if (t.kind === `unknown`) {
|
|
111
|
+
return `unknown`;
|
|
112
|
+
}
|
|
113
|
+
assertExhaustive(t, `Unknown type`);
|
|
114
|
+
throw new GenerateApiError(`Unknown type node`, { t });
|
|
115
|
+
};
|
|
116
|
+
const typeToCode_defaultValue = (t, indent) => {
|
|
117
|
+
if (t.kind === `value`) {
|
|
118
|
+
const prim = `prim` in t.raw ? t.raw.prim : `unknown`;
|
|
119
|
+
if (t.typescriptType === "boolean") {
|
|
120
|
+
return `true`;
|
|
121
|
+
}
|
|
122
|
+
if (t.typescriptType === `string` && prim === `string`) {
|
|
123
|
+
return `'VALUE'`;
|
|
124
|
+
}
|
|
125
|
+
if (t.typescriptType === "number") {
|
|
126
|
+
return `tas.${prim}('42')`;
|
|
127
|
+
}
|
|
128
|
+
if (t.typescriptType === "Date") {
|
|
129
|
+
return `tas.timestamp(new Date())`;
|
|
130
|
+
}
|
|
131
|
+
if (prim === "address" || prim === "contract") {
|
|
132
|
+
return `tas.${prim}('tz1ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456')`;
|
|
133
|
+
}
|
|
134
|
+
if (prim === "bytes") {
|
|
135
|
+
return `tas.${prim}(char2Bytes('DATA'))`;
|
|
136
|
+
}
|
|
137
|
+
if (t.typescriptType === "string") {
|
|
138
|
+
return `tas.${prim}('VALUE')`;
|
|
139
|
+
}
|
|
140
|
+
assertExhaustive(t.typescriptType, `Unknown value type`);
|
|
141
|
+
return prim;
|
|
142
|
+
}
|
|
143
|
+
if (t.kind === `array`) {
|
|
144
|
+
return `[${typeToCode(t.array.item, indent)}]`;
|
|
145
|
+
}
|
|
146
|
+
if (t.kind === `map`) {
|
|
147
|
+
const keyPrim = `prim` in t.map.key.raw ? t.map.key.raw.prim : `unknown`;
|
|
148
|
+
const isStringBasedKey = t.map.key.kind === "value" && keyPrim === "string";
|
|
149
|
+
if (isStringBasedKey) {
|
|
150
|
+
return `tas.${t.map.isBigMap ? "bigMap" : "map"}({
|
|
151
|
+
${tabs(indent + 1)}${typeToCode(t.map.key, indent)}: ${typeToCode(t.map.value, indent)},
|
|
152
|
+
${tabs(indent)}})`;
|
|
153
|
+
}
|
|
154
|
+
return `tas.${t.map.isBigMap ? "bigMap" : "map"}([{
|
|
155
|
+
${tabs(indent + 1)}key: ${typeToCode(t.map.key, indent)},
|
|
156
|
+
${tabs(indent + 1)}value: ${typeToCode(t.map.value, indent)},
|
|
157
|
+
${tabs(indent)}}])`;
|
|
158
|
+
}
|
|
159
|
+
if (t.kind === `object`) {
|
|
160
|
+
const delimiter = (options == null ? void 0 : options.mode) === "defaultValue" ? "," : `;`;
|
|
161
|
+
return `{${toIndentedItems(indent, {}, t.fields.map((a, i) => varToCode(a, i, indent + 1) + delimiter))}}`;
|
|
162
|
+
}
|
|
163
|
+
if (t.kind === `union`) {
|
|
164
|
+
const getUnionItem = (a, i) => {
|
|
165
|
+
const itemCode = `${varToCode(a, i, indent + 1)}`;
|
|
166
|
+
if (!itemCode.includes(`
|
|
167
|
+
`)) {
|
|
168
|
+
return `{ ${itemCode} }`;
|
|
169
|
+
}
|
|
170
|
+
return `{${toIndentedItems(indent + 1, {}, [`${varToCode(a, i, indent + 2)}`])}}`;
|
|
171
|
+
};
|
|
172
|
+
return `(${toIndentedItems(indent, { beforeItem: `| ` }, t.union.map(getUnionItem))})`;
|
|
173
|
+
}
|
|
174
|
+
if (t.kind === `unit`) {
|
|
175
|
+
return `tas.unit()`;
|
|
176
|
+
}
|
|
177
|
+
if (t.kind === `never`) {
|
|
178
|
+
return `never`;
|
|
179
|
+
}
|
|
180
|
+
if (t.kind === `unknown`) {
|
|
181
|
+
return `unknown`;
|
|
182
|
+
}
|
|
183
|
+
if (t.kind === "lambda") {
|
|
184
|
+
return `tas.lambda([])`;
|
|
185
|
+
}
|
|
186
|
+
assertExhaustive(t, `Unknown type`);
|
|
187
|
+
throw new GenerateApiError(`Unknown type node`, { t });
|
|
188
|
+
};
|
|
189
|
+
const varToCode = (t, i, indent, numberVarNamePrefix = "") => {
|
|
190
|
+
return `${t.name ?? `${numberVarNamePrefix}${i}`}${t.type.optional && (options == null ? void 0 : options.mode) !== "defaultValue" ? `?` : ``}: ${typeToCode(t.type, indent)}`;
|
|
191
|
+
};
|
|
192
|
+
const argsToCode = (args, indent, asObject) => {
|
|
193
|
+
const sortedArgs = args.sort((a, b) => {
|
|
194
|
+
if (a.type.optional && !b.type.optional)
|
|
195
|
+
return 1;
|
|
196
|
+
if (!a.type.optional && b.type.optional)
|
|
197
|
+
return -1;
|
|
198
|
+
return 0;
|
|
199
|
+
});
|
|
200
|
+
if (sortedArgs.length === 1) {
|
|
201
|
+
if (sortedArgs[0].type.kind === `unit`)
|
|
202
|
+
return ``;
|
|
203
|
+
if ((options == null ? void 0 : options.mode) === "defaultValue") {
|
|
204
|
+
return typeToCode(sortedArgs[0].type, indent + 1);
|
|
205
|
+
}
|
|
206
|
+
return `${sortedArgs[0].name ?? `param`}: ${typeToCode(sortedArgs[0].type, indent + 1)}`;
|
|
207
|
+
}
|
|
208
|
+
const result = `${toIndentedItems(
|
|
209
|
+
indent,
|
|
210
|
+
{},
|
|
211
|
+
sortedArgs.filter((x) => x.name || x.type.kind !== `unit`).map((a, i) => {
|
|
212
|
+
if (!asObject && (options == null ? void 0 : options.mode) === "defaultValue") {
|
|
213
|
+
return typeToCode(a.type, indent + 1) + `,`;
|
|
214
|
+
}
|
|
215
|
+
return varToCode(a, i, indent + 1, asObject ? "" : "_") + `,`;
|
|
216
|
+
})
|
|
217
|
+
)}`;
|
|
218
|
+
if (asObject) {
|
|
219
|
+
if ((options == null ? void 0 : options.mode) === "defaultValue") {
|
|
220
|
+
return `{${result}}`;
|
|
221
|
+
}
|
|
222
|
+
return `params: {${result}}`;
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
};
|
|
226
|
+
return {
|
|
227
|
+
usedStrictTypes,
|
|
228
|
+
tabs,
|
|
229
|
+
toIndentedItems,
|
|
230
|
+
typeToCode,
|
|
231
|
+
argsToCode
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
var toTypescriptCode = (storage, methods, contractName, parsedContract, protocol, typeAliasData, typeUtilsData) => {
|
|
235
|
+
const {
|
|
236
|
+
usedStrictTypes,
|
|
237
|
+
toIndentedItems,
|
|
238
|
+
typeToCode,
|
|
239
|
+
argsToCode
|
|
240
|
+
} = createTypescriptCodeGenerator();
|
|
241
|
+
const methodsToCode = (indent) => {
|
|
242
|
+
const methodFields = methods.map((x) => {
|
|
243
|
+
const methodCode = `${x.name}: (${argsToCode(x.args, indent + 1, false)}) => Promise<void>;`;
|
|
244
|
+
return methodCode;
|
|
245
|
+
});
|
|
246
|
+
const methodsTypeCode = `type Methods = {${toIndentedItems(indent, {}, methodFields)}};`;
|
|
247
|
+
return methodsTypeCode;
|
|
248
|
+
};
|
|
249
|
+
const methodsObjectToCode = (indent) => {
|
|
250
|
+
const methodFields = methods.map((x) => {
|
|
251
|
+
const methodCode = `${x.name}: (${argsToCode(x.args, indent + 1, true)}) => Promise<void>;`;
|
|
252
|
+
return methodCode;
|
|
253
|
+
});
|
|
254
|
+
const methodsTypeCode = `type MethodsObject = {${toIndentedItems(indent, {}, methodFields)}};`;
|
|
255
|
+
return methodsTypeCode;
|
|
256
|
+
};
|
|
257
|
+
const storageToCode = (indent) => {
|
|
258
|
+
const storageTypeCode = `export type Storage = ${typeToCode(storage.storage, indent)};`;
|
|
259
|
+
return storageTypeCode;
|
|
260
|
+
};
|
|
261
|
+
const methodsCode = methodsToCode(0);
|
|
262
|
+
const methodsObjectCode = methodsObjectToCode(0);
|
|
263
|
+
const storageCode = storageToCode(0);
|
|
264
|
+
const simpleTypeMappingImportsAll = new Map(
|
|
265
|
+
usedStrictTypes.map((x) => x.simpleTypeImports ?? []).reduce(reduceFlatMap, []).map(
|
|
266
|
+
(x) => [`${x == null ? void 0 : x.from}:${x == null ? void 0 : x.name}:${x == null ? void 0 : x.isDefault}`, x]
|
|
267
|
+
)
|
|
268
|
+
);
|
|
269
|
+
const simpleTypeMappingImportsFrom = [...simpleTypeMappingImportsAll.values()].reduce((out, x) => {
|
|
270
|
+
const entry = out[x.from] ?? (out[x.from] = { names: [] });
|
|
271
|
+
if (x.isDefault) {
|
|
272
|
+
entry.default = x.name;
|
|
273
|
+
} else {
|
|
274
|
+
entry.names.push(x.name);
|
|
275
|
+
}
|
|
276
|
+
entry.names.sort((a, b) => a.localeCompare(b));
|
|
277
|
+
return out;
|
|
278
|
+
}, {});
|
|
279
|
+
const simpleTypeMappingImportsText = Object.keys(simpleTypeMappingImportsFrom).map((k) => {
|
|
280
|
+
const entry = simpleTypeMappingImportsFrom[k];
|
|
281
|
+
const items = [entry.default, entry.names.length ? `{ ${entry.names.join(", ")} }` : ""].filter((x) => x);
|
|
282
|
+
return `import ${items.join(", ")} from '${k}';
|
|
283
|
+
`;
|
|
284
|
+
}).join("");
|
|
285
|
+
const simpleTypeMapping = usedStrictTypes.sort((a, b) => a.aliasType.localeCompare(b.aliasType)).map((x) => x.simpleTypeDefinition).join(`
|
|
286
|
+
`);
|
|
287
|
+
const typeUtilsDefinitions = `import { ContractAbstractionFromContractType, WalletContractAbstractionFromContractType } from '${typeUtilsData.importPath}';`;
|
|
288
|
+
const typeAliasesDefinitions = typeAliasData.mode === "simple" ? `${simpleTypeMappingImportsText}${simpleTypeMapping}` : typeAliasData.mode === "local" ? typeAliasData.fileContent : `import { ${usedStrictTypes.map((x) => x.aliasType).join(`, `)} } from '${typeAliasData.importPath}';`;
|
|
289
|
+
const contractTypeName = `${contractName}ContractType`;
|
|
290
|
+
const walletTypeName = `${contractName}WalletType`;
|
|
291
|
+
const codeName = `${contractName}Code`;
|
|
292
|
+
const typesFileContent = `
|
|
293
|
+
${typeUtilsDefinitions}
|
|
294
|
+
${typeAliasesDefinitions}
|
|
295
|
+
|
|
296
|
+
${storageCode}
|
|
297
|
+
|
|
298
|
+
${methodsCode}
|
|
299
|
+
|
|
300
|
+
${methodsObjectCode}
|
|
301
|
+
|
|
302
|
+
type contractTypes = { methods: Methods, methodsObject: MethodsObject, storage: Storage, code: { __type: '${codeName}', protocol: string, code: object[] } };
|
|
303
|
+
export type ${contractTypeName} = ContractAbstractionFromContractType<contractTypes>;
|
|
304
|
+
export type ${walletTypeName} = WalletContractAbstractionFromContractType<contractTypes>;
|
|
305
|
+
`;
|
|
306
|
+
const contractCodeFileContent = `
|
|
307
|
+
export const ${codeName}: { __type: '${codeName}', protocol: string, code: object[] } = {
|
|
308
|
+
__type: '${codeName}',
|
|
309
|
+
protocol: '${protocol.key}',
|
|
310
|
+
code: JSON.parse(\`${JSON.stringify(parsedContract)}\`)
|
|
311
|
+
};
|
|
312
|
+
`;
|
|
313
|
+
return {
|
|
314
|
+
typesFileContent,
|
|
315
|
+
contractCodeFileContent,
|
|
316
|
+
storage: storageCode,
|
|
317
|
+
methods: methodsCode,
|
|
318
|
+
methodsObject: methodsObjectCode
|
|
319
|
+
};
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// src/generator/process.ts
|
|
323
|
+
import * as M from "@taquito/michel-codec";
|
|
324
|
+
|
|
325
|
+
// src/generator/contract-parser.ts
|
|
326
|
+
var parseContractStorage = (storage) => {
|
|
327
|
+
const fields = storage.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []);
|
|
328
|
+
if (fields.length === 1 && !fields[0].name) {
|
|
329
|
+
return {
|
|
330
|
+
storage: fields[0].type
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
storage: {
|
|
335
|
+
kind: `object`,
|
|
336
|
+
raw: storage,
|
|
337
|
+
fields
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
};
|
|
341
|
+
var parseContractParameter = (parameter) => {
|
|
342
|
+
return {
|
|
343
|
+
methods: parameter.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, [])
|
|
344
|
+
};
|
|
345
|
+
};
|
|
346
|
+
var visitContractParameterEndpoint = (node) => {
|
|
347
|
+
var _a, _b;
|
|
348
|
+
if (node.prim === `or`) {
|
|
349
|
+
return node.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, []);
|
|
350
|
+
}
|
|
351
|
+
if (node.prim === `list` && node.args.length === 1 && ((_a = node.args[0]) == null ? void 0 : _a.prim) === `or`) {
|
|
352
|
+
return node.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, []);
|
|
353
|
+
}
|
|
354
|
+
const nameRaw = (_b = node.annots) == null ? void 0 : _b[0];
|
|
355
|
+
const name = (nameRaw == null ? void 0 : nameRaw.startsWith("%")) ? nameRaw.substr(1) : "default";
|
|
356
|
+
if (!name) {
|
|
357
|
+
console.warn(`Unknown method: ${node.prim}`, { node, args: node.args });
|
|
358
|
+
return [];
|
|
359
|
+
}
|
|
360
|
+
const nodeType = visitType(node, { ignorePairName: node.prim === "pair" });
|
|
361
|
+
if (nodeType.kind === "object") {
|
|
362
|
+
return [{ name, args: nodeType.fields }];
|
|
363
|
+
}
|
|
364
|
+
return [{
|
|
365
|
+
name,
|
|
366
|
+
args: [{ type: nodeType }]
|
|
367
|
+
}];
|
|
368
|
+
};
|
|
369
|
+
var visitVar = (node) => {
|
|
370
|
+
var _a;
|
|
371
|
+
const name = `annots` in node && ((_a = node.annots) == null ? void 0 : _a.length) === 1 ? node.annots[0].substr(1) : void 0;
|
|
372
|
+
const type = visitType(node);
|
|
373
|
+
return [{
|
|
374
|
+
name,
|
|
375
|
+
type
|
|
376
|
+
}];
|
|
377
|
+
};
|
|
378
|
+
var visitType = (node, options) => {
|
|
379
|
+
if (!(`prim` in node)) {
|
|
380
|
+
console.error(`visitType no prim`, { node });
|
|
381
|
+
return { kind: `unknown`, raw: node };
|
|
382
|
+
}
|
|
383
|
+
if (node.prim === `or`) {
|
|
384
|
+
const unionVars = node.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []).map((x) => x);
|
|
385
|
+
const union = unionVars.map((x) => !x.name && x.type.kind === "union" ? x.type.union : [x]).reduce(reduceFlatMap, []);
|
|
386
|
+
if (union.some((x) => !x)) {
|
|
387
|
+
throw new GenerateApiError(`or: Some fields are null`, { node });
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
kind: `union`,
|
|
391
|
+
raw: node,
|
|
392
|
+
union
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
if (node.prim === `pair`) {
|
|
396
|
+
const fields = node.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []);
|
|
397
|
+
if (fields.some((x) => !x)) {
|
|
398
|
+
throw new GenerateApiError(`pair: Some fields are null`, { node, args: node.args, fields });
|
|
399
|
+
}
|
|
400
|
+
const fieldsFlat = fields.map(
|
|
401
|
+
(x) => (!x.name || (options == null ? void 0 : options.ignorePairName)) && x.type.kind === "object" ? x.type.fields : [x]
|
|
402
|
+
).reduce(reduceFlatMap, []);
|
|
403
|
+
return {
|
|
404
|
+
kind: `object`,
|
|
405
|
+
raw: node,
|
|
406
|
+
fields: fieldsFlat
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
if (node.prim === `list` || node.prim === `set`) {
|
|
410
|
+
if (node.args.length !== 1) {
|
|
411
|
+
throw new GenerateApiError(`list does not have 1 arg`, { node, args: node.args });
|
|
412
|
+
}
|
|
413
|
+
const arrayItem = visitType(node.args[0]);
|
|
414
|
+
if (!arrayItem) {
|
|
415
|
+
throw new GenerateApiError(`arrayItem are null`, { node, args: node.args, arrayItem });
|
|
416
|
+
}
|
|
417
|
+
return {
|
|
418
|
+
kind: `array`,
|
|
419
|
+
raw: node,
|
|
420
|
+
array: { item: arrayItem }
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
if (node.prim === `map` || node.prim === `big_map`) {
|
|
424
|
+
if (node.args.length !== 2) {
|
|
425
|
+
throw new GenerateApiError(`map does not have 2 args`, { node, args: node.args });
|
|
426
|
+
}
|
|
427
|
+
const mapKey = visitType(node.args[0]);
|
|
428
|
+
const mapValue = visitType(node.args[1]);
|
|
429
|
+
if (!mapKey || !mapValue) {
|
|
430
|
+
throw new GenerateApiError(`map is missing key or value`, { node, args: node.args, mapKey, mapValue });
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
kind: `map`,
|
|
434
|
+
raw: node,
|
|
435
|
+
map: {
|
|
436
|
+
key: mapKey,
|
|
437
|
+
value: mapValue,
|
|
438
|
+
isBigMap: node.prim === `big_map`
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
if (node.prim === `option`) {
|
|
443
|
+
return {
|
|
444
|
+
...visitType(node.args[0]),
|
|
445
|
+
optional: true
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
if (node.prim === `bool`) {
|
|
449
|
+
return {
|
|
450
|
+
kind: `value`,
|
|
451
|
+
raw: node,
|
|
452
|
+
value: node.prim,
|
|
453
|
+
typescriptType: `boolean`
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
if (node.prim === `nat` || node.prim === `int` || node.prim === `mutez`) {
|
|
457
|
+
return {
|
|
458
|
+
kind: `value`,
|
|
459
|
+
raw: node,
|
|
460
|
+
value: node.prim,
|
|
461
|
+
typescriptType: `number`
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
if (node.prim === `timestamp`) {
|
|
465
|
+
return {
|
|
466
|
+
kind: `value`,
|
|
467
|
+
raw: node,
|
|
468
|
+
value: node.prim,
|
|
469
|
+
typescriptType: `Date`
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
if (node.prim === `address` || node.prim === `key` || node.prim === `key_hash` || node.prim === `chain_id` || node.prim === `string` || node.prim === `signature` || node.prim === `ticket` || node.prim === `bls12_381_fr` || node.prim === `bls12_381_g1` || node.prim === `bls12_381_g2` || node.prim === `sapling_state` || node.prim === `sapling_transaction` || node.prim === `contract`) {
|
|
473
|
+
return {
|
|
474
|
+
kind: `value`,
|
|
475
|
+
raw: node,
|
|
476
|
+
value: node.prim,
|
|
477
|
+
typescriptType: `string`
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
if (node.prim === `unit`) {
|
|
481
|
+
return {
|
|
482
|
+
kind: `unit`,
|
|
483
|
+
raw: node
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
if (node.prim === `bytes`) {
|
|
487
|
+
return {
|
|
488
|
+
kind: `value`,
|
|
489
|
+
raw: node,
|
|
490
|
+
value: node.prim,
|
|
491
|
+
typescriptType: `string`
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
if (node.prim === `lambda`) {
|
|
495
|
+
if (node.args.length !== 2) {
|
|
496
|
+
throw new GenerateApiError(`lambda does not have 2 args`, { node, args: node.args });
|
|
497
|
+
}
|
|
498
|
+
const argType = visitType(node.args[0]);
|
|
499
|
+
const retType = visitType(node.args[1]);
|
|
500
|
+
if (!argType || !retType) {
|
|
501
|
+
throw new GenerateApiError(`lambda is missing arg or return`, { node, args: node.args, argType, retType });
|
|
502
|
+
}
|
|
503
|
+
return {
|
|
504
|
+
kind: `lambda`,
|
|
505
|
+
raw: node,
|
|
506
|
+
lambda: {
|
|
507
|
+
arg: argType,
|
|
508
|
+
ret: retType
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
if (node.prim === `operation`) {
|
|
513
|
+
return {
|
|
514
|
+
kind: `value`,
|
|
515
|
+
raw: node,
|
|
516
|
+
value: node.prim,
|
|
517
|
+
typescriptType: `string`
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
if (node.prim === "chest" || node.prim === "chest_key") {
|
|
521
|
+
return {
|
|
522
|
+
kind: `value`,
|
|
523
|
+
raw: node,
|
|
524
|
+
value: node.prim,
|
|
525
|
+
typescriptType: `string`
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
if (node.prim === `never`) {
|
|
529
|
+
return {
|
|
530
|
+
kind: `never`,
|
|
531
|
+
raw: node
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
assertExhaustive(node, `Unknown type`);
|
|
535
|
+
throw new GenerateApiError(`Unknown type`, { node });
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
// src/generator/schema-output.ts
|
|
539
|
+
var toSchema = (methods, storage) => {
|
|
540
|
+
const getSchemaObjectType = (vars) => {
|
|
541
|
+
if (vars.some((x) => !x)) {
|
|
542
|
+
throw new GenerateApiError(`getSchemaObjectType has null vars`, { vars });
|
|
543
|
+
}
|
|
544
|
+
return vars.reduce((out, x, i) => {
|
|
545
|
+
out[x.name ?? i] = getSchemaType(x.type);
|
|
546
|
+
return out;
|
|
547
|
+
}, {});
|
|
548
|
+
};
|
|
549
|
+
const getSchemaType = (t) => {
|
|
550
|
+
return (t.kind === `value` && t.value ? t.value : null) ?? (t.kind === `array` && t.array ? [getSchemaType(t.array.item)] : null) ?? (t.kind === `map` && t.map ? [`map`, getSchemaType(t.map.key), getSchemaType(t.map.value)] : null) ?? (t.kind === `object` && t.fields ? getSchemaObjectType(t.fields) : null) ?? (t.kind === `unit` ? `unit` : null) ?? (t.kind === `never` ? `never` : null) ?? (t.kind === "lambda" ? ["lambda", getSchemaType(t.lambda.arg), getSchemaType(t.lambda.ret)] : null) ?? `${t.raw}`;
|
|
551
|
+
};
|
|
552
|
+
const schemaMethods = methods.reduce((out, x) => {
|
|
553
|
+
out[x.name] = {
|
|
554
|
+
params: x.args.length === 1 && !x.args[0].name ? getSchemaType(x.args[0].type) : getSchemaObjectType(x.args ?? [])
|
|
555
|
+
};
|
|
556
|
+
return out;
|
|
557
|
+
}, {});
|
|
558
|
+
const schemaStorage = getSchemaType(storage.storage);
|
|
559
|
+
return {
|
|
560
|
+
methods: schemaMethods,
|
|
561
|
+
storage: schemaStorage
|
|
562
|
+
};
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
// src/generator/process.ts
|
|
566
|
+
var parseContractWithMinimalProtocolLevel = (contractScript, format, contractLevelIndex) => {
|
|
567
|
+
const contractLevels = [
|
|
568
|
+
{ name: "PsDELPH1", key: M.Protocol.PsDELPH1 },
|
|
569
|
+
{ name: "PtEdo2Zk", key: M.Protocol.PtEdo2Zk },
|
|
570
|
+
{ name: "PsFLorena", key: M.Protocol.PsFLorena }
|
|
571
|
+
];
|
|
572
|
+
const protocol = contractLevels[contractLevelIndex];
|
|
573
|
+
if (!protocol) {
|
|
574
|
+
throw new GenerateApiError(`Could not parse contract script`, contractScript);
|
|
575
|
+
}
|
|
576
|
+
const p = new M.Parser({ protocol: protocol.key });
|
|
577
|
+
try {
|
|
578
|
+
const contract = format === "tz" ? p.parseScript(contractScript) : p.parseJSON(JSON.parse(contractScript));
|
|
579
|
+
if (contract) {
|
|
580
|
+
return {
|
|
581
|
+
contract,
|
|
582
|
+
protocol
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
} catch {
|
|
586
|
+
}
|
|
587
|
+
return parseContractWithMinimalProtocolLevel(contractScript, format, contractLevelIndex + 1);
|
|
588
|
+
};
|
|
589
|
+
var parseContractInterface = (contractScript, format) => {
|
|
590
|
+
const p = new M.Parser({ protocol: M.Protocol.PsFLorena });
|
|
591
|
+
const { contract, protocol } = parseContractWithMinimalProtocolLevel(contractScript, format, 0);
|
|
592
|
+
const contractStorage = contract.find((x) => x.prim === `storage`);
|
|
593
|
+
const contractParameter = contract.find((x) => x.prim === `parameter`);
|
|
594
|
+
const storageResult = contractStorage && parseContractStorage(contractStorage);
|
|
595
|
+
const storage = storageResult ?? { storage: { kind: `object`, raw: { prim: `never` }, fields: [] } };
|
|
596
|
+
const parameterResult = contractParameter && parseContractParameter(contractParameter);
|
|
597
|
+
const methods = (parameterResult == null ? void 0 : parameterResult.methods) ?? [];
|
|
598
|
+
return {
|
|
599
|
+
storage,
|
|
600
|
+
methods,
|
|
601
|
+
contract,
|
|
602
|
+
protocol
|
|
603
|
+
};
|
|
604
|
+
};
|
|
605
|
+
var generateContractTypesFromMichelsonCode = (contractScript, contractName, format, typeAliasData, typeUtilsData) => {
|
|
606
|
+
const {
|
|
607
|
+
storage,
|
|
608
|
+
methods,
|
|
609
|
+
contract,
|
|
610
|
+
protocol
|
|
611
|
+
} = parseContractInterface(
|
|
612
|
+
contractScript,
|
|
613
|
+
format
|
|
614
|
+
);
|
|
615
|
+
if (methods.length === 1)
|
|
616
|
+
methods[0].name = `default`;
|
|
617
|
+
const schemaOutput = toSchema(methods, storage);
|
|
618
|
+
const typescriptCode = toTypescriptCode(
|
|
619
|
+
storage,
|
|
620
|
+
methods,
|
|
621
|
+
contractName,
|
|
622
|
+
contract,
|
|
623
|
+
protocol,
|
|
624
|
+
typeAliasData,
|
|
625
|
+
typeUtilsData
|
|
626
|
+
);
|
|
627
|
+
return {
|
|
628
|
+
schemaOutput,
|
|
629
|
+
typescriptCodeOutput: typescriptCode,
|
|
630
|
+
parsedContract: contract,
|
|
631
|
+
minimalProtocol: protocol.key
|
|
632
|
+
};
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
export {
|
|
636
|
+
normalizeContractName,
|
|
637
|
+
createTypescriptCodeGenerator,
|
|
638
|
+
parseContractInterface,
|
|
639
|
+
generateContractTypesFromMichelsonCode
|
|
640
|
+
};
|
|
641
|
+
//# sourceMappingURL=chunk-RD5ZALXH.js.map
|