@taqueria/plugin-contract-types 0.3.0 → 0.4.0-rc2
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/README.md +125 -0
- package/_readme.eta +128 -0
- package/example/example-usage.ts +2 -2
- package/index.js +124 -152
- package/index.js.map +1 -1
- package/index.ts +32 -33
- package/package.json +53 -53
- package/run.ts +2 -2
- package/src/cli-process.ts +102 -95
- package/src/cli.ts +30 -19
- package/src/generator/common.ts +8 -9
- package/src/generator/contract-name.ts +7 -6
- package/src/generator/contract-parser.ts +324 -320
- package/src/generator/process.ts +71 -47
- package/src/generator/schema-output.ts +48 -47
- package/src/generator/typescript-output.ts +238 -205
- package/src/taquito-contract-type-generator.ts +3 -3
- package/src/type-aliases-file-content.ts +5 -1
- package/src/type-aliases.ts +40 -36
- package/src/type-utils-file-content.ts +1 -1
- package/src/type-utils.ts +23 -25
- package/tasks.ts +48 -52
- package/test/generator.spec.ts +61 -51
- package/tsconfig.json +12 -12
- package/Readme.md +0 -212
package/src/generator/process.ts
CHANGED
|
@@ -1,66 +1,90 @@
|
|
|
1
1
|
import * as M from '@taquito/michel-codec';
|
|
2
2
|
import { GenerateApiError } from './common';
|
|
3
|
-
import {
|
|
3
|
+
import { parseContractParameter, parseContractStorage } from './contract-parser';
|
|
4
4
|
import { SchemaOutput, toSchema } from './schema-output';
|
|
5
|
-
import {
|
|
5
|
+
import { toTypescriptCode, TypeAliasData, TypescriptCodeOutput, TypeUtilsData } from './typescript-output';
|
|
6
6
|
|
|
7
|
-
const parseContractWithMinimalProtocolLevel = (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
const parseContractWithMinimalProtocolLevel = (
|
|
8
|
+
contractScript: string,
|
|
9
|
+
format: 'tz' | 'json',
|
|
10
|
+
contractLevelIndex: number,
|
|
11
|
+
): { contract: M.MichelsonContract; protocol: { name: string; key: string } } => {
|
|
12
|
+
const contractLevels = [
|
|
13
|
+
{ name: 'PsDELPH1', key: M.Protocol.PsDELPH1 },
|
|
14
|
+
{ name: 'PtEdo2Zk', key: M.Protocol.PtEdo2Zk },
|
|
15
|
+
{ name: 'PsFLorena', key: M.Protocol.PsFLorena },
|
|
16
|
+
];
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
const protocol = contractLevels[contractLevelIndex];
|
|
19
|
+
if (!protocol) {
|
|
20
|
+
throw new GenerateApiError(`Could not parse contract script`, contractScript);
|
|
21
|
+
}
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
const p = new M.Parser({ protocol: protocol.key });
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
try {
|
|
26
|
+
const contract = (format === 'tz'
|
|
27
|
+
? p.parseScript(contractScript)
|
|
28
|
+
: p.parseJSON(JSON.parse(contractScript))) as M.MichelsonContract;
|
|
29
|
+
if (contract) {
|
|
30
|
+
return {
|
|
31
|
+
contract,
|
|
32
|
+
protocol,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
} catch {
|
|
36
|
+
// Ignore parse errors
|
|
37
|
+
}
|
|
32
38
|
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
// Try again with next level
|
|
40
|
+
return parseContractWithMinimalProtocolLevel(contractScript, format, contractLevelIndex + 1);
|
|
35
41
|
};
|
|
36
42
|
|
|
37
|
-
export const generateContractTypesFromMichelsonCode = (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
export const generateContractTypesFromMichelsonCode = (
|
|
44
|
+
contractScript: string,
|
|
45
|
+
contractName: string,
|
|
46
|
+
format: 'tz' | 'json',
|
|
47
|
+
typeAliasData: TypeAliasData,
|
|
48
|
+
typeUtilsData: TypeUtilsData,
|
|
49
|
+
): {
|
|
50
|
+
schemaOutput: SchemaOutput;
|
|
51
|
+
typescriptCodeOutput: TypescriptCodeOutput;
|
|
52
|
+
parsedContract: M.MichelsonContract;
|
|
53
|
+
minimalProtocol: string;
|
|
42
54
|
} => {
|
|
55
|
+
const p = new M.Parser({ protocol: M.Protocol.PsFLorena });
|
|
43
56
|
|
|
44
|
-
|
|
57
|
+
const { contract, protocol } = parseContractWithMinimalProtocolLevel(contractScript, format, 0);
|
|
45
58
|
|
|
46
|
-
|
|
59
|
+
const contractStorage = contract.find(x => x.prim === `storage`) as undefined | M.MichelsonContractStorage;
|
|
60
|
+
const contractParameter = contract.find(x => x.prim === `parameter`) as undefined | M.MichelsonContractParameter;
|
|
47
61
|
|
|
48
|
-
|
|
49
|
-
|
|
62
|
+
const storageResult = contractStorage && parseContractStorage(contractStorage);
|
|
63
|
+
const storage = storageResult
|
|
64
|
+
?? { storage: { kind: `object`, raw: { prim: `never` } as M.MichelsonType, fields: [] } };
|
|
50
65
|
|
|
51
|
-
|
|
52
|
-
|
|
66
|
+
const parameterResult = contractParameter && parseContractParameter(contractParameter);
|
|
67
|
+
const methods = parameterResult?.methods ?? [];
|
|
53
68
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const schemaOutput = toSchema(methods, storage);
|
|
69
|
+
// If there's only one entrypoint, then we call it "default"
|
|
70
|
+
if (methods.length === 1) methods[0].name = `default`;
|
|
57
71
|
|
|
58
|
-
|
|
72
|
+
const schemaOutput = toSchema(methods, storage);
|
|
59
73
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
const typescriptCode = toTypescriptCode(
|
|
75
|
+
storage,
|
|
76
|
+
methods,
|
|
77
|
+
contractName,
|
|
78
|
+
contract,
|
|
79
|
+
protocol,
|
|
80
|
+
typeAliasData,
|
|
81
|
+
typeUtilsData,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
schemaOutput,
|
|
86
|
+
typescriptCodeOutput: typescriptCode,
|
|
87
|
+
parsedContract: contract,
|
|
88
|
+
minimalProtocol: protocol.key,
|
|
89
|
+
};
|
|
66
90
|
};
|
|
@@ -1,58 +1,59 @@
|
|
|
1
1
|
import { GenerateApiError } from './common';
|
|
2
|
-
import { TypedMethod,
|
|
2
|
+
import { TypedMethod, TypedStorage, TypedType, TypedVar } from './contract-parser';
|
|
3
3
|
|
|
4
4
|
type SchemaObjectType = { [name: string]: SchemaType };
|
|
5
5
|
type SchemaType = string | SchemaType[] | SchemaObjectType;
|
|
6
6
|
type SchemaMethods = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
[name: string]: {
|
|
8
|
+
params: SchemaType;
|
|
9
|
+
};
|
|
10
10
|
};
|
|
11
11
|
export type SchemaOutput = {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
methods: SchemaMethods;
|
|
13
|
+
storage: SchemaType;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
export const toSchema = (methods: TypedMethod[], storage: TypedStorage): SchemaOutput => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
17
|
+
const getSchemaObjectType = (vars: TypedVar[]) => {
|
|
18
|
+
// console.log('getSchemaObjectType', { vars });
|
|
19
|
+
|
|
20
|
+
if (vars.some(x => !x)) {
|
|
21
|
+
throw new GenerateApiError(`getSchemaObjectType has null vars`, { vars });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return vars.reduce((out, x, i) => {
|
|
25
|
+
out[x.name ?? i] = getSchemaType(x.type);
|
|
26
|
+
return out;
|
|
27
|
+
}, {} as SchemaObjectType);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const getSchemaType = (t: TypedType): SchemaType => {
|
|
31
|
+
// console.log('getSchemaType', { t });
|
|
32
|
+
|
|
33
|
+
return (t.kind === `value` && t.value ? t.value : null)
|
|
34
|
+
?? (t.kind === `array` && t.array ? [getSchemaType(t.array.item)] : null)
|
|
35
|
+
?? (t.kind === `map` && t.map ? [`map`, getSchemaType(t.map.key), getSchemaType(t.map.value)] : null)
|
|
36
|
+
?? (t.kind === `object` && t.fields ? getSchemaObjectType(t.fields) : null)
|
|
37
|
+
?? (t.kind === `unit` ? `unit` : null)
|
|
38
|
+
?? (t.kind === `never` ? `never` : null)
|
|
39
|
+
?? `${t.raw as unknown as string}`;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const schemaMethods = methods.reduce((out, x) => {
|
|
43
|
+
// console.log('schemaMethods', { x });
|
|
44
|
+
|
|
45
|
+
out[x.name] = {
|
|
46
|
+
params: x.args.length === 1 && !x.args[0].name
|
|
47
|
+
? getSchemaType(x.args[0].type)
|
|
48
|
+
: getSchemaObjectType(x.args ?? []),
|
|
49
|
+
};
|
|
50
|
+
return out;
|
|
51
|
+
}, {} as SchemaMethods);
|
|
52
|
+
|
|
53
|
+
const schemaStorage = getSchemaType(storage.storage);
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
methods: schemaMethods,
|
|
57
|
+
storage: schemaStorage,
|
|
58
|
+
};
|
|
58
59
|
};
|