@taqueria/plugin-contract-types 0.8.2 → 0.10.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.
- package/chunk-GC2KSB5D.js +226 -0
- package/chunk-GC2KSB5D.js.map +1 -0
- package/chunk-T4SGVAIL.js +604 -0
- package/chunk-T4SGVAIL.js.map +1 -0
- package/example/test-generation/example-contract-0.jest-demo.ts +78 -0
- package/example/test-generation/example-contract-1.jest-demo.ts +196 -0
- package/example/test-generation/example-contract-10.jest-demo.ts +47 -0
- package/example/test-generation/example-contract-11.jest-demo.ts +48 -0
- package/example/test-generation/example-contract-2.jest-demo.ts +277 -0
- package/example/test-generation/example-contract-4.jest-demo.ts +203 -0
- package/example/test-generation/example-contract-5.jest-demo.ts +235 -0
- package/example/test-generation/example-contract-6.jest-demo.ts +277 -0
- package/example/test-generation/example-contract-7.jest-demo.ts +170 -0
- package/example/test-generation/example-contract-8.jest-demo.ts +227 -0
- package/example/test-generation/example-contract-9.jest-demo.ts +42 -0
- package/example/types-file/example-contract-10.types.ts +2 -2
- package/example/types-file/example-contract-11.types.ts +2 -2
- package/example/types-file/type-aliases.ts +4 -0
- package/index.cjs +920 -0
- package/index.cjs.map +1 -0
- package/index.d.ts +1 -0
- package/index.js +78 -882
- package/index.js.map +1 -1
- package/index.ts +1 -0
- package/package.json +27 -12
- package/src/cli-process.cjs +848 -0
- package/src/cli-process.cjs.map +1 -0
- package/src/cli-process.d.ts +9 -0
- package/src/cli-process.js +8 -0
- package/src/cli-process.js.map +1 -0
- package/src/generator/process.ts +37 -13
- package/src/generator/testing-code-generator-jest-demo.ts +75 -0
- package/src/generator/testing-code-generator.cjs +613 -0
- package/src/generator/testing-code-generator.cjs.map +1 -0
- package/src/generator/testing-code-generator.d.ts +79 -0
- package/src/generator/testing-code-generator.js +131 -0
- package/src/generator/testing-code-generator.js.map +1 -0
- package/src/generator/testing-code-generator.ts +282 -0
- package/src/generator/typescript-output.ts +136 -14
- package/src/type-aliases.ts +1 -0
- package/tsconfig.json +4 -1
- package/lib/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
|
|
26
|
+
// src/generator/testing-code-generator.ts
|
|
27
|
+
var testing_code_generator_exports = {};
|
|
28
|
+
__export(testing_code_generator_exports, {
|
|
29
|
+
createTestingCodeGenerator: () => createTestingCodeGenerator,
|
|
30
|
+
normalizeContractName: () => normalizeContractName
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(testing_code_generator_exports);
|
|
33
|
+
|
|
34
|
+
// src/generator/contract-name.ts
|
|
35
|
+
var normalizeContractName = (text) => text.replace(/[^A-Za-z0-9]/g, "_").split("_").filter((x) => x).map((x) => x[0].toUpperCase() + x.substring(1)).join("");
|
|
36
|
+
|
|
37
|
+
// src/generator/process.ts
|
|
38
|
+
var M = __toESM(require("@taquito/michel-codec"), 1);
|
|
39
|
+
|
|
40
|
+
// src/generator/common.ts
|
|
41
|
+
var GenerateApiError = class {
|
|
42
|
+
constructor(message, data) {
|
|
43
|
+
this.message = message;
|
|
44
|
+
this.data = data;
|
|
45
|
+
this.name = `GenerateApiError`;
|
|
46
|
+
console.error(`\u274C GenerateApiError: ${message}`, data);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var assertExhaustive = (value, message) => {
|
|
50
|
+
console.error(message, { value });
|
|
51
|
+
};
|
|
52
|
+
var reduceFlatMap = (out, x) => {
|
|
53
|
+
out.push(...x);
|
|
54
|
+
return out;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/generator/contract-parser.ts
|
|
58
|
+
var parseContractStorage = (storage) => {
|
|
59
|
+
const fields = storage.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []);
|
|
60
|
+
if (fields.length === 1 && !fields[0].name) {
|
|
61
|
+
return {
|
|
62
|
+
storage: fields[0].type
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
storage: {
|
|
67
|
+
kind: `object`,
|
|
68
|
+
raw: storage,
|
|
69
|
+
fields
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
var parseContractParameter = (parameter) => {
|
|
74
|
+
return {
|
|
75
|
+
methods: parameter.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, [])
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
var visitContractParameterEndpoint = (node) => {
|
|
79
|
+
var _a, _b;
|
|
80
|
+
if (node.prim === `or`) {
|
|
81
|
+
return node.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, []);
|
|
82
|
+
}
|
|
83
|
+
if (node.prim === `list` && node.args.length === 1 && ((_a = node.args[0]) == null ? void 0 : _a.prim) === `or`) {
|
|
84
|
+
return node.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, []);
|
|
85
|
+
}
|
|
86
|
+
const nameRaw = (_b = node.annots) == null ? void 0 : _b[0];
|
|
87
|
+
const name = (nameRaw == null ? void 0 : nameRaw.startsWith("%")) ? nameRaw.substr(1) : null;
|
|
88
|
+
if (!name) {
|
|
89
|
+
console.warn(`Unknown method: ${node.prim}`, { node, args: node.args });
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
const nodeType = visitType(node, { ignorePairName: node.prim === "pair" });
|
|
93
|
+
if (nodeType.kind === "object") {
|
|
94
|
+
return [{ name, args: nodeType.fields }];
|
|
95
|
+
}
|
|
96
|
+
return [{
|
|
97
|
+
name,
|
|
98
|
+
args: [{ type: nodeType }]
|
|
99
|
+
}];
|
|
100
|
+
};
|
|
101
|
+
var visitVar = (node) => {
|
|
102
|
+
var _a;
|
|
103
|
+
const name = `annots` in node && ((_a = node.annots) == null ? void 0 : _a.length) === 1 ? node.annots[0].substr(1) : void 0;
|
|
104
|
+
const type = visitType(node);
|
|
105
|
+
return [{
|
|
106
|
+
name,
|
|
107
|
+
type
|
|
108
|
+
}];
|
|
109
|
+
};
|
|
110
|
+
var visitType = (node, options) => {
|
|
111
|
+
if (!(`prim` in node)) {
|
|
112
|
+
console.error(`visitType no prim`, { node });
|
|
113
|
+
return { kind: `unknown`, raw: node };
|
|
114
|
+
}
|
|
115
|
+
if (node.prim === `or`) {
|
|
116
|
+
const unionVars = node.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []).map((x) => x);
|
|
117
|
+
const union = unionVars.map((x) => !x.name && x.type.kind === "union" ? x.type.union : [x]).reduce(reduceFlatMap, []);
|
|
118
|
+
if (union.some((x) => !x)) {
|
|
119
|
+
throw new GenerateApiError(`or: Some fields are null`, { node });
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
kind: `union`,
|
|
123
|
+
raw: node,
|
|
124
|
+
union
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (node.prim === `pair`) {
|
|
128
|
+
const fields = node.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []);
|
|
129
|
+
if (fields.some((x) => !x)) {
|
|
130
|
+
throw new GenerateApiError(`pair: Some fields are null`, { node, args: node.args, fields });
|
|
131
|
+
}
|
|
132
|
+
const fieldsFlat = fields.map(
|
|
133
|
+
(x) => (!x.name || (options == null ? void 0 : options.ignorePairName)) && x.type.kind === "object" ? x.type.fields : [x]
|
|
134
|
+
).reduce(reduceFlatMap, []);
|
|
135
|
+
return {
|
|
136
|
+
kind: `object`,
|
|
137
|
+
raw: node,
|
|
138
|
+
fields: fieldsFlat
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
if (node.prim === `list` || node.prim === `set`) {
|
|
142
|
+
if (node.args.length !== 1) {
|
|
143
|
+
throw new GenerateApiError(`list does not have 1 arg`, { node, args: node.args });
|
|
144
|
+
}
|
|
145
|
+
const arrayItem = visitType(node.args[0]);
|
|
146
|
+
if (!arrayItem) {
|
|
147
|
+
throw new GenerateApiError(`arrayItem are null`, { node, args: node.args, arrayItem });
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
kind: `array`,
|
|
151
|
+
raw: node,
|
|
152
|
+
array: { item: arrayItem }
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (node.prim === `map` || node.prim === `big_map`) {
|
|
156
|
+
if (node.args.length !== 2) {
|
|
157
|
+
throw new GenerateApiError(`map does not have 2 args`, { node, args: node.args });
|
|
158
|
+
}
|
|
159
|
+
const mapKey = visitType(node.args[0]);
|
|
160
|
+
const mapValue = visitType(node.args[1]);
|
|
161
|
+
if (!mapKey || !mapValue) {
|
|
162
|
+
throw new GenerateApiError(`map is missing key or value`, { node, args: node.args, mapKey, mapValue });
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
kind: `map`,
|
|
166
|
+
raw: node,
|
|
167
|
+
map: {
|
|
168
|
+
key: mapKey,
|
|
169
|
+
value: mapValue,
|
|
170
|
+
isBigMap: node.prim === `big_map`
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (node.prim === `option`) {
|
|
175
|
+
return {
|
|
176
|
+
...visitType(node.args[0]),
|
|
177
|
+
optional: true
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
if (node.prim === `bool`) {
|
|
181
|
+
return {
|
|
182
|
+
kind: `value`,
|
|
183
|
+
raw: node,
|
|
184
|
+
value: node.prim,
|
|
185
|
+
typescriptType: `boolean`
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
if (node.prim === `nat` || node.prim === `int` || node.prim === `mutez`) {
|
|
189
|
+
return {
|
|
190
|
+
kind: `value`,
|
|
191
|
+
raw: node,
|
|
192
|
+
value: node.prim,
|
|
193
|
+
typescriptType: `number`
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
if (node.prim === `timestamp`) {
|
|
197
|
+
return {
|
|
198
|
+
kind: `value`,
|
|
199
|
+
raw: node,
|
|
200
|
+
value: node.prim,
|
|
201
|
+
typescriptType: `Date`
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
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`) {
|
|
205
|
+
return {
|
|
206
|
+
kind: `value`,
|
|
207
|
+
raw: node,
|
|
208
|
+
value: node.prim,
|
|
209
|
+
typescriptType: `string`
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
if (node.prim === `unit`) {
|
|
213
|
+
return {
|
|
214
|
+
kind: `unit`,
|
|
215
|
+
raw: node
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
if (node.prim === `bytes`) {
|
|
219
|
+
return {
|
|
220
|
+
kind: `value`,
|
|
221
|
+
raw: node,
|
|
222
|
+
value: node.prim,
|
|
223
|
+
typescriptType: `string`
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if (node.prim === `lambda` || node.prim === `operation`) {
|
|
227
|
+
return {
|
|
228
|
+
kind: `value`,
|
|
229
|
+
raw: node,
|
|
230
|
+
value: node.prim,
|
|
231
|
+
typescriptType: `string`
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if (node.prim === "chest" || node.prim === "chest_key") {
|
|
235
|
+
return {
|
|
236
|
+
kind: `value`,
|
|
237
|
+
raw: node,
|
|
238
|
+
value: node.prim,
|
|
239
|
+
typescriptType: `string`
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if (node.prim === `never`) {
|
|
243
|
+
return {
|
|
244
|
+
kind: `never`,
|
|
245
|
+
raw: node
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
assertExhaustive(node, `Unknown type`);
|
|
249
|
+
throw new GenerateApiError(`Unknown type`, { node });
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// src/generator/typescript-output.ts
|
|
253
|
+
var createTypescriptCodeGenerator = (options) => {
|
|
254
|
+
const usedStrictTypes = [];
|
|
255
|
+
const addTypeAlias = (strictType) => {
|
|
256
|
+
if (!usedStrictTypes.some((x) => x.aliasType === strictType.aliasType)) {
|
|
257
|
+
usedStrictTypes.push(strictType);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const tabs = (indent) => Array(indent).fill(` `).join(``);
|
|
261
|
+
const toIndentedItems = (indent, delimeters, items) => {
|
|
262
|
+
return `
|
|
263
|
+
${tabs(indent + 1)}${items.join(`${delimeters.afterItem ?? ``}
|
|
264
|
+
${tabs(indent + 1)}${delimeters.beforeItem ?? ``}`)}
|
|
265
|
+
${tabs(indent)}`;
|
|
266
|
+
};
|
|
267
|
+
const typeToCode = (t, indent) => {
|
|
268
|
+
if ((options == null ? void 0 : options.mode) === "defaultValue") {
|
|
269
|
+
return typeToCode_defaultValue(t, indent);
|
|
270
|
+
}
|
|
271
|
+
if (t.kind === `value`) {
|
|
272
|
+
const prim = `prim` in t.raw ? t.raw.prim : `unknown`;
|
|
273
|
+
if (t.typescriptType === `boolean` || t.typescriptType === `string` && prim === `string`) {
|
|
274
|
+
return `${t.typescriptType}`;
|
|
275
|
+
}
|
|
276
|
+
if (t.typescriptType === "number") {
|
|
277
|
+
const simpleBaseType2 = `string | BigNumber | number`;
|
|
278
|
+
const typeAlias2 = {
|
|
279
|
+
aliasType: prim,
|
|
280
|
+
simpleTypeDefinition: `type ${prim} = ${simpleBaseType2};`,
|
|
281
|
+
simpleTypeImports: [{ name: "BigNumber", isDefault: true, from: "bignumber.js" }]
|
|
282
|
+
};
|
|
283
|
+
addTypeAlias(typeAlias2);
|
|
284
|
+
return typeAlias2.aliasType;
|
|
285
|
+
}
|
|
286
|
+
const simpleBaseType = t.typescriptType === "Date" ? "Date | string" : t.typescriptType;
|
|
287
|
+
const typeAlias = { aliasType: prim, simpleTypeDefinition: `type ${prim} = ${simpleBaseType};` };
|
|
288
|
+
addTypeAlias(typeAlias);
|
|
289
|
+
return typeAlias.aliasType;
|
|
290
|
+
}
|
|
291
|
+
if (t.kind === `array`) {
|
|
292
|
+
return `Array<${typeToCode(t.array.item, indent)}>`;
|
|
293
|
+
}
|
|
294
|
+
if (t.kind === `map`) {
|
|
295
|
+
const typeAlias = t.map.isBigMap ? {
|
|
296
|
+
aliasType: `BigMap`,
|
|
297
|
+
simpleTypeDefinition: "type BigMap<K, T> = MichelsonMap<K, T>;",
|
|
298
|
+
simpleTypeImports: [{ name: "MichelsonMap", from: "@taquito/taquito" }]
|
|
299
|
+
} : {
|
|
300
|
+
aliasType: `MMap`,
|
|
301
|
+
simpleTypeDefinition: "type MMap<K, T> = MichelsonMap<K, T>;",
|
|
302
|
+
simpleTypeImports: [{ name: "MichelsonMap", from: "@taquito/taquito" }]
|
|
303
|
+
};
|
|
304
|
+
addTypeAlias(typeAlias);
|
|
305
|
+
return `${typeAlias.aliasType}<${typeToCode(t.map.key, indent)}, ${typeToCode(t.map.value, indent)}>`;
|
|
306
|
+
}
|
|
307
|
+
if (t.kind === `object`) {
|
|
308
|
+
return `{${toIndentedItems(indent, {}, t.fields.map((a, i) => varToCode(a, i, indent + 1) + `;`))}}`;
|
|
309
|
+
}
|
|
310
|
+
if (t.kind === `union`) {
|
|
311
|
+
const getUnionItem = (a, i) => {
|
|
312
|
+
const itemCode = `${varToCode(a, i, indent + 1)}`;
|
|
313
|
+
if (!itemCode.includes(`
|
|
314
|
+
`)) {
|
|
315
|
+
return `{ ${itemCode} }`;
|
|
316
|
+
}
|
|
317
|
+
return `{${toIndentedItems(indent + 1, {}, [`${varToCode(a, i, indent + 2)}`])}}`;
|
|
318
|
+
};
|
|
319
|
+
return `(${toIndentedItems(indent, { beforeItem: `| ` }, t.union.map(getUnionItem))})`;
|
|
320
|
+
}
|
|
321
|
+
if (t.kind === `unit`) {
|
|
322
|
+
const typeAlias = {
|
|
323
|
+
aliasType: `unit`,
|
|
324
|
+
simpleTypeDefinition: `type unit = (true | undefined);`
|
|
325
|
+
};
|
|
326
|
+
addTypeAlias(typeAlias);
|
|
327
|
+
return typeAlias.aliasType;
|
|
328
|
+
}
|
|
329
|
+
if (t.kind === `never`) {
|
|
330
|
+
return `never`;
|
|
331
|
+
}
|
|
332
|
+
if (t.kind === `unknown`) {
|
|
333
|
+
return `unknown`;
|
|
334
|
+
}
|
|
335
|
+
assertExhaustive(t, `Unknown type`);
|
|
336
|
+
throw new GenerateApiError(`Unknown type node`, { t });
|
|
337
|
+
};
|
|
338
|
+
const typeToCode_defaultValue = (t, indent) => {
|
|
339
|
+
if (t.kind === `value`) {
|
|
340
|
+
const prim = `prim` in t.raw ? t.raw.prim : `unknown`;
|
|
341
|
+
if (t.typescriptType === "boolean") {
|
|
342
|
+
return `true`;
|
|
343
|
+
}
|
|
344
|
+
if (t.typescriptType === `string` && prim === `string`) {
|
|
345
|
+
return `'VALUE'`;
|
|
346
|
+
}
|
|
347
|
+
if (t.typescriptType === "number") {
|
|
348
|
+
return `tas.${prim}('42')`;
|
|
349
|
+
}
|
|
350
|
+
if (t.typescriptType === "Date") {
|
|
351
|
+
return `tas.timestamp(new Date())`;
|
|
352
|
+
}
|
|
353
|
+
if (prim === "address" || prim === "contract") {
|
|
354
|
+
return `tas.${prim}('tz1ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456')`;
|
|
355
|
+
}
|
|
356
|
+
if (prim === "bytes") {
|
|
357
|
+
return `tas.${prim}(char2Bytes('DATA'))`;
|
|
358
|
+
}
|
|
359
|
+
if (t.typescriptType === "string") {
|
|
360
|
+
return `tas.${prim}('VALUE')`;
|
|
361
|
+
}
|
|
362
|
+
assertExhaustive(t.typescriptType, `Unknown value type`);
|
|
363
|
+
return prim;
|
|
364
|
+
}
|
|
365
|
+
if (t.kind === `array`) {
|
|
366
|
+
return `[${typeToCode(t.array.item, indent)}]`;
|
|
367
|
+
}
|
|
368
|
+
if (t.kind === `map`) {
|
|
369
|
+
const keyPrim = `prim` in t.map.key.raw ? t.map.key.raw.prim : `unknown`;
|
|
370
|
+
const isStringBasedKey = t.map.key.kind === "value" && keyPrim === "string";
|
|
371
|
+
if (isStringBasedKey) {
|
|
372
|
+
return `tas.${t.map.isBigMap ? "bigMap" : "map"}({
|
|
373
|
+
${tabs(indent + 1)}${typeToCode(t.map.key, indent)}: ${typeToCode(t.map.value, indent)},
|
|
374
|
+
${tabs(indent)}})`;
|
|
375
|
+
}
|
|
376
|
+
return `tas.${t.map.isBigMap ? "bigMap" : "map"}([{
|
|
377
|
+
${tabs(indent + 1)}key: ${typeToCode(t.map.key, indent)},
|
|
378
|
+
${tabs(indent + 1)}value: ${typeToCode(t.map.value, indent)},
|
|
379
|
+
${tabs(indent)}}])`;
|
|
380
|
+
}
|
|
381
|
+
if (t.kind === `object`) {
|
|
382
|
+
const delimeter = (options == null ? void 0 : options.mode) === "defaultValue" ? "," : `;`;
|
|
383
|
+
return `{${toIndentedItems(indent, {}, t.fields.map((a, i) => varToCode(a, i, indent + 1) + delimeter))}}`;
|
|
384
|
+
}
|
|
385
|
+
if (t.kind === `union`) {
|
|
386
|
+
const getUnionItem = (a, i) => {
|
|
387
|
+
const itemCode = `${varToCode(a, i, indent + 1)}`;
|
|
388
|
+
if (!itemCode.includes(`
|
|
389
|
+
`)) {
|
|
390
|
+
return `{ ${itemCode} }`;
|
|
391
|
+
}
|
|
392
|
+
return `{${toIndentedItems(indent + 1, {}, [`${varToCode(a, i, indent + 2)}`])}}`;
|
|
393
|
+
};
|
|
394
|
+
return `(${toIndentedItems(indent, { beforeItem: `| ` }, t.union.map(getUnionItem))})`;
|
|
395
|
+
}
|
|
396
|
+
if (t.kind === `unit`) {
|
|
397
|
+
return `tas.unit()`;
|
|
398
|
+
}
|
|
399
|
+
if (t.kind === `never`) {
|
|
400
|
+
return `never`;
|
|
401
|
+
}
|
|
402
|
+
if (t.kind === `unknown`) {
|
|
403
|
+
return `unknown`;
|
|
404
|
+
}
|
|
405
|
+
assertExhaustive(t, `Unknown type`);
|
|
406
|
+
throw new GenerateApiError(`Unknown type node`, { t });
|
|
407
|
+
};
|
|
408
|
+
const varToCode = (t, i, indent, numberVarNamePrefix = "") => {
|
|
409
|
+
return `${t.name ?? `${numberVarNamePrefix}${i}`}${t.type.optional && (options == null ? void 0 : options.mode) !== "defaultValue" ? `?` : ``}: ${typeToCode(t.type, indent)}`;
|
|
410
|
+
};
|
|
411
|
+
const argsToCode = (args, indent, asObject) => {
|
|
412
|
+
if (args.length === 1) {
|
|
413
|
+
if (args[0].type.kind === `unit`)
|
|
414
|
+
return ``;
|
|
415
|
+
if ((options == null ? void 0 : options.mode) === "defaultValue") {
|
|
416
|
+
return typeToCode(args[0].type, indent + 1);
|
|
417
|
+
}
|
|
418
|
+
return `${args[0].name ?? `param`}: ${typeToCode(args[0].type, indent + 1)}`;
|
|
419
|
+
}
|
|
420
|
+
const result = `${toIndentedItems(
|
|
421
|
+
indent,
|
|
422
|
+
{},
|
|
423
|
+
args.filter((x) => x.name || x.type.kind !== `unit`).map((a, i) => {
|
|
424
|
+
if (!asObject && (options == null ? void 0 : options.mode) === "defaultValue") {
|
|
425
|
+
return typeToCode(a.type, indent + 1) + `,`;
|
|
426
|
+
}
|
|
427
|
+
return varToCode(a, i, indent + 1, asObject ? "" : "_") + `,`;
|
|
428
|
+
})
|
|
429
|
+
)}`;
|
|
430
|
+
if (asObject) {
|
|
431
|
+
if ((options == null ? void 0 : options.mode) === "defaultValue") {
|
|
432
|
+
return `{${result}}`;
|
|
433
|
+
}
|
|
434
|
+
return `params: {${result}}`;
|
|
435
|
+
}
|
|
436
|
+
return result;
|
|
437
|
+
};
|
|
438
|
+
return {
|
|
439
|
+
usedStrictTypes,
|
|
440
|
+
tabs,
|
|
441
|
+
toIndentedItems,
|
|
442
|
+
typeToCode,
|
|
443
|
+
argsToCode
|
|
444
|
+
};
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
// src/generator/process.ts
|
|
448
|
+
var parseContractWithMinimalProtocolLevel = (contractScript, format, contractLevelIndex) => {
|
|
449
|
+
const contractLevels = [
|
|
450
|
+
{ name: "PsDELPH1", key: M.Protocol.PsDELPH1 },
|
|
451
|
+
{ name: "PtEdo2Zk", key: M.Protocol.PtEdo2Zk },
|
|
452
|
+
{ name: "PsFLorena", key: M.Protocol.PsFLorena }
|
|
453
|
+
];
|
|
454
|
+
const protocol = contractLevels[contractLevelIndex];
|
|
455
|
+
if (!protocol) {
|
|
456
|
+
throw new GenerateApiError(`Could not parse contract script`, contractScript);
|
|
457
|
+
}
|
|
458
|
+
const p = new M.Parser({ protocol: protocol.key });
|
|
459
|
+
try {
|
|
460
|
+
const contract = format === "tz" ? p.parseScript(contractScript) : p.parseJSON(JSON.parse(contractScript));
|
|
461
|
+
if (contract) {
|
|
462
|
+
return {
|
|
463
|
+
contract,
|
|
464
|
+
protocol
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
} catch {
|
|
468
|
+
}
|
|
469
|
+
return parseContractWithMinimalProtocolLevel(contractScript, format, contractLevelIndex + 1);
|
|
470
|
+
};
|
|
471
|
+
var parseContractInterface = (contractScript, format) => {
|
|
472
|
+
const p = new M.Parser({ protocol: M.Protocol.PsFLorena });
|
|
473
|
+
const { contract, protocol } = parseContractWithMinimalProtocolLevel(contractScript, format, 0);
|
|
474
|
+
const contractStorage = contract.find((x) => x.prim === `storage`);
|
|
475
|
+
const contractParameter = contract.find((x) => x.prim === `parameter`);
|
|
476
|
+
const storageResult = contractStorage && parseContractStorage(contractStorage);
|
|
477
|
+
const storage = storageResult ?? { storage: { kind: `object`, raw: { prim: `never` }, fields: [] } };
|
|
478
|
+
const parameterResult = contractParameter && parseContractParameter(contractParameter);
|
|
479
|
+
const methods = (parameterResult == null ? void 0 : parameterResult.methods) ?? [];
|
|
480
|
+
return {
|
|
481
|
+
storage,
|
|
482
|
+
methods,
|
|
483
|
+
contract,
|
|
484
|
+
protocol
|
|
485
|
+
};
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
// src/generator/testing-code-generator.ts
|
|
489
|
+
var createTestingCodeGenerator = ({
|
|
490
|
+
contractSource,
|
|
491
|
+
contractFormat
|
|
492
|
+
}) => {
|
|
493
|
+
const {
|
|
494
|
+
storage,
|
|
495
|
+
methods
|
|
496
|
+
} = parseContractInterface(contractSource, contractFormat);
|
|
497
|
+
const codeGenerator = createTypescriptCodeGenerator({ mode: "defaultValue" });
|
|
498
|
+
const getMethodByName = (methodName) => {
|
|
499
|
+
const method = methods.find((x) => x.name === methodName);
|
|
500
|
+
if (!method) {
|
|
501
|
+
throw new Error(`Failed to fine method: ${methodName}`);
|
|
502
|
+
}
|
|
503
|
+
return method;
|
|
504
|
+
};
|
|
505
|
+
return {
|
|
506
|
+
storage,
|
|
507
|
+
methods,
|
|
508
|
+
generateOrigination: ({
|
|
509
|
+
contractVarName,
|
|
510
|
+
formatting
|
|
511
|
+
}) => generateOrigination({
|
|
512
|
+
codeGenerator,
|
|
513
|
+
storage,
|
|
514
|
+
indent: formatting == null ? void 0 : formatting.indent
|
|
515
|
+
}),
|
|
516
|
+
generateMethodCall: ({
|
|
517
|
+
methodName,
|
|
518
|
+
formatting
|
|
519
|
+
}) => generateMethodCall({
|
|
520
|
+
codeGenerator,
|
|
521
|
+
method: getMethodByName(methodName),
|
|
522
|
+
indent: formatting == null ? void 0 : formatting.indent
|
|
523
|
+
}),
|
|
524
|
+
generateStorageAccess: ({
|
|
525
|
+
storagePath
|
|
526
|
+
}) => generateStorageAccess({
|
|
527
|
+
codeGenerator,
|
|
528
|
+
storagePath
|
|
529
|
+
})
|
|
530
|
+
};
|
|
531
|
+
};
|
|
532
|
+
var generateOrigination = ({
|
|
533
|
+
codeGenerator,
|
|
534
|
+
storage,
|
|
535
|
+
indent = 2,
|
|
536
|
+
useSemicolons = true,
|
|
537
|
+
contractVarName = "contract",
|
|
538
|
+
minConfirmations = 3
|
|
539
|
+
}) => {
|
|
540
|
+
const {
|
|
541
|
+
tabs,
|
|
542
|
+
argsToCode
|
|
543
|
+
} = codeGenerator;
|
|
544
|
+
const semi = useSemicolons ? ";" : "";
|
|
545
|
+
const code = `
|
|
546
|
+
${tabs(indent)}const newContractOrigination = await Tezos.contract.originate<ContractType>({
|
|
547
|
+
${tabs(indent + 1)}code: ContractCode.code,
|
|
548
|
+
${tabs(indent + 1)}storage: ${argsToCode([{ type: storage.storage }], indent + 1, true)},
|
|
549
|
+
${tabs(indent)}})${semi}
|
|
550
|
+
${tabs(indent)}const newContractResult = await newContractOrigination.contract()${semi}
|
|
551
|
+
${tabs(indent)}const newContractAddress = newContractResult.address${semi}
|
|
552
|
+
${tabs(indent)}${contractVarName} = await Tezos.contract.at<ContractType>(newContractAddress)${semi}
|
|
553
|
+
${tabs(indent)}`;
|
|
554
|
+
return {
|
|
555
|
+
code
|
|
556
|
+
};
|
|
557
|
+
};
|
|
558
|
+
var generateMethodCall = ({
|
|
559
|
+
codeGenerator,
|
|
560
|
+
method,
|
|
561
|
+
indent = 2,
|
|
562
|
+
useSemicolons = true,
|
|
563
|
+
contractVarName = "contract",
|
|
564
|
+
minConfirmations = 3
|
|
565
|
+
}) => {
|
|
566
|
+
const {
|
|
567
|
+
tabs,
|
|
568
|
+
argsToCode
|
|
569
|
+
} = codeGenerator;
|
|
570
|
+
const semi = useSemicolons ? ";" : "";
|
|
571
|
+
const code = `
|
|
572
|
+
${tabs(indent)}const ${method.name}Request = await ${contractVarName}.methodsObject.${method.name}(${argsToCode(method.args, indent + 1, true)}).send()${semi}
|
|
573
|
+
${tabs(indent)}await ${method.name}Request.confirmation(${minConfirmations})${semi}
|
|
574
|
+
${tabs(indent)}`;
|
|
575
|
+
return {
|
|
576
|
+
code
|
|
577
|
+
};
|
|
578
|
+
};
|
|
579
|
+
var generateStorageAccess = ({
|
|
580
|
+
codeGenerator,
|
|
581
|
+
storagePath,
|
|
582
|
+
indent = 2,
|
|
583
|
+
useSemicolons = true,
|
|
584
|
+
contractVarName = "contract",
|
|
585
|
+
getStorageValueFunctionName,
|
|
586
|
+
storageVarName = "storage",
|
|
587
|
+
storageValueVarName = "value"
|
|
588
|
+
}) => {
|
|
589
|
+
const {
|
|
590
|
+
tabs,
|
|
591
|
+
argsToCode
|
|
592
|
+
} = codeGenerator;
|
|
593
|
+
const semi = useSemicolons ? ";" : "";
|
|
594
|
+
const funcName = getStorageValueFunctionName ?? `getStorageValue${storagePath.split(".").map((x) => x.replace(/[^A-Za-z0-9]/g, "")).map((x) => `${x.substring(0, 1).toLocaleUpperCase()}${x.substring(1)}`).join("")}`;
|
|
595
|
+
const getStorageValueExpr = `${storageVarName}`;
|
|
596
|
+
const funcCode = `
|
|
597
|
+
${tabs(indent)}const ${funcName} = async () => {
|
|
598
|
+
${tabs(indent + 1)}const ${storageVarName} = await ${contractVarName}.storage()${semi}
|
|
599
|
+
${tabs(indent + 1)}const ${storageValueVarName} = ${getStorageValueExpr}${semi}
|
|
600
|
+
${tabs(indent + 1)}return ${storageValueVarName}${semi}
|
|
601
|
+
${tabs(indent)}}${semi}
|
|
602
|
+
`;
|
|
603
|
+
return {
|
|
604
|
+
getStorageValueFunctionCode: funcCode,
|
|
605
|
+
getStorageValueFunctionName: funcName
|
|
606
|
+
};
|
|
607
|
+
};
|
|
608
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
609
|
+
0 && (module.exports = {
|
|
610
|
+
createTestingCodeGenerator,
|
|
611
|
+
normalizeContractName
|
|
612
|
+
});
|
|
613
|
+
//# sourceMappingURL=testing-code-generator.cjs.map
|