@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.
@@ -1,66 +1,90 @@
1
1
  import * as M from '@taquito/michel-codec';
2
2
  import { GenerateApiError } from './common';
3
- import { parseContractStorage, parseContractParameter } from './contract-parser';
3
+ import { parseContractParameter, parseContractStorage } from './contract-parser';
4
4
  import { SchemaOutput, toSchema } from './schema-output';
5
- import { TypescriptCodeOutput, toTypescriptCode, TypeAliasData, TypeUtilsData } from './typescript-output';
5
+ import { toTypescriptCode, TypeAliasData, TypescriptCodeOutput, TypeUtilsData } from './typescript-output';
6
6
 
7
- const parseContractWithMinimalProtocolLevel = (contractScript: string, format: 'tz' | 'json', contractLevelIndex: number): { contract: M.MichelsonContract, protocol: { name: string, key: string } } => {
8
- const contractLevels = [
9
- { name: 'PsDELPH1', key: M.Protocol.PsDELPH1 },
10
- { name: 'PtEdo2Zk', key: M.Protocol.PtEdo2Zk },
11
- { name: 'PsFLorena', key: M.Protocol.PsFLorena },
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
- const protocol = contractLevels[contractLevelIndex];
15
- if (!protocol) {
16
- throw new GenerateApiError(`Could not parse contract script`, contractScript);
17
- }
18
+ const protocol = contractLevels[contractLevelIndex];
19
+ if (!protocol) {
20
+ throw new GenerateApiError(`Could not parse contract script`, contractScript);
21
+ }
18
22
 
19
- const p = new M.Parser({ protocol: protocol.key });
23
+ const p = new M.Parser({ protocol: protocol.key });
20
24
 
21
- try {
22
- const contract = (format === 'tz' ? p.parseScript(contractScript) : p.parseJSON(JSON.parse(contractScript))) as M.MichelsonContract;
23
- if (contract) {
24
- return {
25
- contract,
26
- protocol,
27
- };
28
- }
29
- } catch {
30
- // Ignore parse errors
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
- // Try again with next level
34
- return parseContractWithMinimalProtocolLevel(contractScript, format, contractLevelIndex + 1);
39
+ // Try again with next level
40
+ return parseContractWithMinimalProtocolLevel(contractScript, format, contractLevelIndex + 1);
35
41
  };
36
42
 
37
- export const generateContractTypesFromMichelsonCode = (contractScript: string, contractName: string, format: 'tz' | 'json', typeAliasData: TypeAliasData, typeUtilsData: TypeUtilsData): {
38
- schemaOutput: SchemaOutput;
39
- typescriptCodeOutput: TypescriptCodeOutput;
40
- parsedContract: M.MichelsonContract;
41
- minimalProtocol: string;
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
- const p = new M.Parser({ protocol: M.Protocol.PsFLorena });
57
+ const { contract, protocol } = parseContractWithMinimalProtocolLevel(contractScript, format, 0);
45
58
 
46
- const { contract, protocol } = parseContractWithMinimalProtocolLevel(contractScript, format, 0);
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
- const contractStorage = contract.find(x => x.prim === `storage`) as undefined | M.MichelsonContractStorage;
49
- const contractParameter = contract.find(x => x.prim === `parameter`) as undefined | M.MichelsonContractParameter;
62
+ const storageResult = contractStorage && parseContractStorage(contractStorage);
63
+ const storage = storageResult
64
+ ?? { storage: { kind: `object`, raw: { prim: `never` } as M.MichelsonType, fields: [] } };
50
65
 
51
- const storageResult = contractStorage && parseContractStorage(contractStorage);
52
- const storage = storageResult ?? { storage: { kind: `object`, raw: { prim: `never` } as M.MichelsonType, fields: [] } };
66
+ const parameterResult = contractParameter && parseContractParameter(contractParameter);
67
+ const methods = parameterResult?.methods ?? [];
53
68
 
54
- const parameterResult = contractParameter && parseContractParameter(contractParameter);
55
- const methods = parameterResult?.methods ?? [];
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
- const typescriptCode = toTypescriptCode(storage, methods, contractName, contract, protocol, typeAliasData, typeUtilsData);
72
+ const schemaOutput = toSchema(methods, storage);
59
73
 
60
- return {
61
- schemaOutput,
62
- typescriptCodeOutput: typescriptCode,
63
- parsedContract: contract,
64
- minimalProtocol: protocol.key,
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, TypedVar, TypedType, TypedStorage } from './contract-parser';
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
- [name: string]: {
8
- params: SchemaType;
9
- };
7
+ [name: string]: {
8
+ params: SchemaType;
9
+ };
10
10
  };
11
11
  export type SchemaOutput = {
12
- methods: SchemaMethods;
13
- storage: SchemaType;
12
+ methods: SchemaMethods;
13
+ storage: SchemaType;
14
14
  };
15
15
 
16
16
  export const toSchema = (methods: TypedMethod[], storage: TypedStorage): SchemaOutput => {
17
-
18
- const getSchemaObjectType = (vars: TypedVar[]) => {
19
- // console.log('getSchemaObjectType', { vars });
20
-
21
- if (vars.some(x => !x)) {
22
- throw new GenerateApiError(`getSchemaObjectType has null vars`, { vars });
23
- }
24
-
25
- return vars.reduce((out, x, i) => {
26
- out[x.name ?? i] = getSchemaType(x.type);
27
- return out;
28
- }, {} as SchemaObjectType);
29
- };
30
-
31
- const getSchemaType = (t: TypedType): SchemaType => {
32
- // console.log('getSchemaType', { t });
33
-
34
- return (t.kind === `value` && t.value ? t.value : null)
35
- ?? (t.kind === `array` && t.array ? [getSchemaType(t.array.item)] : null)
36
- ?? (t.kind === `map` && t.map ? [`map`, getSchemaType(t.map.key), getSchemaType(t.map.value)] : null)
37
- ?? (t.kind === `object` && t.fields ? getSchemaObjectType(t.fields) : null)
38
- ?? (t.kind === `unit` ? `unit` : null)
39
- ?? (t.kind === `never` ? `never` : null)
40
- ?? `${t.raw as unknown as string}`;
41
- };
42
-
43
- const schemaMethods = methods.reduce((out, x) => {
44
- // console.log('schemaMethods', { x });
45
-
46
- out[x.name] = {
47
- params: x.args.length === 1 && !x.args[0].name ? getSchemaType(x.args[0].type) : getSchemaObjectType(x.args ?? []),
48
- };
49
- return out;
50
- }, {} as SchemaMethods);
51
-
52
- const schemaStorage = getSchemaType(storage.storage);
53
-
54
- return {
55
- methods: schemaMethods,
56
- storage: schemaStorage,
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
  };