@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
|
@@ -1,210 +1,246 @@
|
|
|
1
1
|
import { assertExhaustive, GenerateApiError, reduceFlatMap } from './common';
|
|
2
|
-
import {
|
|
2
|
+
import { TypedMethod, TypedStorage, TypedType, TypedVar } from './contract-parser';
|
|
3
3
|
|
|
4
4
|
export type TypescriptCodeOutput = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
typesFileContent: string;
|
|
6
|
+
contractCodeFileContent: string;
|
|
7
|
+
storage: string;
|
|
8
|
+
methods: string;
|
|
9
|
+
methodsObject: string;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
export type TypeAliasData = {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
mode: 'local';
|
|
14
|
+
fileContent?: string;
|
|
15
15
|
} | {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
mode: 'file' | 'library';
|
|
17
|
+
importPath?: string;
|
|
18
18
|
} | {
|
|
19
|
-
|
|
19
|
+
mode: 'simple';
|
|
20
20
|
};
|
|
21
21
|
export type TypeUtilsData = {
|
|
22
|
-
|
|
22
|
+
importPath: string;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
export const toTypescriptCode = (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
export const toTypescriptCode = (
|
|
26
|
+
storage: TypedStorage,
|
|
27
|
+
methods: TypedMethod[],
|
|
28
|
+
contractName: string,
|
|
29
|
+
parsedContract: unknown,
|
|
30
|
+
protocol: { name: string; key: string },
|
|
31
|
+
typeAliasData: TypeAliasData,
|
|
32
|
+
typeUtilsData: TypeUtilsData,
|
|
33
|
+
): TypescriptCodeOutput => {
|
|
34
|
+
type TypeAlias = {
|
|
35
|
+
aliasType: string;
|
|
36
|
+
simpleTypeDefinition: string;
|
|
37
|
+
simpleTypeImports?: { name: string; isDefault?: boolean; from: string }[];
|
|
38
|
+
};
|
|
39
|
+
const usedStrictTypes = [] as TypeAlias[];
|
|
40
|
+
const addTypeAlias = (strictType: TypeAlias) => {
|
|
41
|
+
if (!usedStrictTypes.some(x => x.aliasType === strictType.aliasType)) {
|
|
42
|
+
usedStrictTypes.push(strictType);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Not really tabs :)
|
|
47
|
+
const tabs = (indent: number) => Array(indent).fill(` `).join(``);
|
|
48
|
+
const toIndentedItems = (
|
|
49
|
+
indent: number,
|
|
50
|
+
delimeters: { afterItem?: string; beforeItem?: string },
|
|
51
|
+
items: string[],
|
|
52
|
+
) => {
|
|
53
|
+
return `
|
|
54
|
+
${tabs(indent + 1)}${
|
|
55
|
+
items.join(`${delimeters.afterItem ?? ``}
|
|
56
|
+
${tabs(indent + 1)}${delimeters.beforeItem ?? ``}`)
|
|
57
|
+
}
|
|
40
58
|
${tabs(indent)}`;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const typeToCode = (t: TypedType, indent: number): string => {
|
|
62
|
+
if (t.kind === `value`) {
|
|
63
|
+
// return `${t.typescriptType}`;
|
|
64
|
+
|
|
65
|
+
const prim = `prim` in t.raw ? t.raw.prim : `unknown`;
|
|
66
|
+
|
|
67
|
+
// Strict mode
|
|
68
|
+
if (
|
|
69
|
+
t.typescriptType === `boolean`
|
|
70
|
+
|| t.typescriptType === `string` && prim === `string`
|
|
71
|
+
) {
|
|
72
|
+
return `${t.typescriptType}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (t.typescriptType === 'number') {
|
|
76
|
+
const simpleBaseType = `string | BigNumber | number`;
|
|
77
|
+
const typeAlias: TypeAlias = {
|
|
78
|
+
aliasType: prim,
|
|
79
|
+
simpleTypeDefinition: `type ${prim} = ${simpleBaseType};`,
|
|
80
|
+
simpleTypeImports: [{ name: 'BigNumber', isDefault: true, from: 'bignumber.js' }],
|
|
81
|
+
};
|
|
82
|
+
addTypeAlias(typeAlias);
|
|
83
|
+
|
|
84
|
+
return typeAlias.aliasType;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const simpleBaseType = t.typescriptType === 'Date' ? 'Date | string' : t.typescriptType;
|
|
88
|
+
const typeAlias: TypeAlias = { aliasType: prim, simpleTypeDefinition: `type ${prim} = ${simpleBaseType};` };
|
|
89
|
+
addTypeAlias(typeAlias);
|
|
90
|
+
|
|
91
|
+
return typeAlias.aliasType;
|
|
92
|
+
}
|
|
93
|
+
if (t.kind === `array`) {
|
|
94
|
+
return `Array<${typeToCode(t.array.item, indent)}>`;
|
|
95
|
+
}
|
|
96
|
+
if (t.kind === `map`) {
|
|
97
|
+
const typeAlias: TypeAlias = t.map.isBigMap
|
|
98
|
+
? {
|
|
99
|
+
aliasType: `BigMap`,
|
|
100
|
+
simpleTypeDefinition: 'type BigMap<K, T> = MichelsonMap<K, T>;',
|
|
101
|
+
simpleTypeImports: [{ name: 'MichelsonMap', from: '@taquito/taquito' }],
|
|
102
|
+
}
|
|
103
|
+
: {
|
|
104
|
+
aliasType: `MMap`,
|
|
105
|
+
simpleTypeDefinition: 'type MMap<K, T> = MichelsonMap<K, T>;',
|
|
106
|
+
simpleTypeImports: [{ name: 'MichelsonMap', from: '@taquito/taquito' }],
|
|
107
|
+
};
|
|
108
|
+
addTypeAlias(typeAlias);
|
|
109
|
+
|
|
110
|
+
return `${typeAlias.aliasType}<${typeToCode(t.map.key, indent)}, ${typeToCode(t.map.value, indent)}>`;
|
|
111
|
+
}
|
|
112
|
+
if (t.kind === `object`) {
|
|
113
|
+
return `{${toIndentedItems(indent, {}, t.fields.map((a, i) => varToCode(a, i, indent + 1) + `;`))}}`;
|
|
114
|
+
}
|
|
115
|
+
if (t.kind === `union`) {
|
|
116
|
+
const getUnionItem = (a: TypedVar, i: number) => {
|
|
117
|
+
const itemCode = `${varToCode(a, i, indent + 1)}`;
|
|
118
|
+
|
|
119
|
+
// Keep on single line if already on single line
|
|
120
|
+
if (!itemCode.includes(`\n`)) {
|
|
121
|
+
return `{ ${itemCode} }`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Indent if multi-line (and remake with extra indent)
|
|
125
|
+
return `{${toIndentedItems(indent + 1, {}, [`${varToCode(a, i, indent + 2)}`])}}`;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return `(${toIndentedItems(indent, { beforeItem: `| ` }, t.union.map(getUnionItem))})`;
|
|
129
|
+
}
|
|
130
|
+
if (t.kind === `unit`) {
|
|
131
|
+
const typeAlias: TypeAlias = { aliasType: `unit`, simpleTypeDefinition: `type unit = (true | undefined);` };
|
|
132
|
+
addTypeAlias(typeAlias);
|
|
133
|
+
return typeAlias.aliasType;
|
|
134
|
+
}
|
|
135
|
+
if (t.kind === `never`) {
|
|
136
|
+
return `never`;
|
|
137
|
+
}
|
|
138
|
+
if (t.kind === `unknown`) {
|
|
139
|
+
return `unknown`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
assertExhaustive(t, `Unknown type`);
|
|
143
|
+
throw new GenerateApiError(`Unknown type node`, { t });
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const varToCode = (t: TypedVar, i: number, indent: number, numberVarNamePrefix = ''): string => {
|
|
147
|
+
return `${t.name ?? `${numberVarNamePrefix}${i}`}${t.type.optional ? `?` : ``}: ${typeToCode(t.type, indent)}`;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const argsToCode = (args: TypedVar[], indent: number, asObject: boolean): string => {
|
|
151
|
+
if (args.length === 1) {
|
|
152
|
+
if (args[0].type.kind === `unit`) return ``;
|
|
153
|
+
return `${args[0].name ?? `param`}: ${typeToCode(args[0].type, indent + 1)}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const result = `${
|
|
157
|
+
toIndentedItems(
|
|
158
|
+
indent,
|
|
159
|
+
{},
|
|
160
|
+
args.filter(x => x.name || x.type.kind !== `unit`).map((a, i) =>
|
|
161
|
+
varToCode(a, i, indent + 1, asObject ? '' : '_') + `,`
|
|
162
|
+
),
|
|
163
|
+
)
|
|
164
|
+
}`;
|
|
165
|
+
|
|
166
|
+
if (asObject) {
|
|
167
|
+
return `params: {${result}}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return result;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const methodsToCode = (indent: number) => {
|
|
174
|
+
const methodFields = methods.map(x => {
|
|
175
|
+
const methodCode = `${x.name}: (${argsToCode(x.args, indent + 1, false)}) => Promise<void>;`;
|
|
176
|
+
return methodCode;
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const methodsTypeCode = `type Methods = {${toIndentedItems(indent, {}, methodFields)}};`;
|
|
180
|
+
return methodsTypeCode;
|
|
181
|
+
};
|
|
182
|
+
const methodsObjectToCode = (indent: number) => {
|
|
183
|
+
const methodFields = methods.map(x => {
|
|
184
|
+
const methodCode = `${x.name}: (${argsToCode(x.args, indent + 1, true)}) => Promise<void>;`;
|
|
185
|
+
return methodCode;
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const methodsTypeCode = `type MethodsObject = {${toIndentedItems(indent, {}, methodFields)}};`;
|
|
189
|
+
return methodsTypeCode;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const storageToCode = (indent: number) => {
|
|
193
|
+
const storageTypeCode = `type Storage = ${typeToCode(storage.storage, indent)};`;
|
|
194
|
+
return storageTypeCode;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const methodsCode = methodsToCode(0);
|
|
198
|
+
const methodsObjectCode = methodsObjectToCode(0);
|
|
199
|
+
const storageCode = storageToCode(0);
|
|
200
|
+
|
|
201
|
+
// Simple type aliases
|
|
202
|
+
const simpleTypeMappingImportsAll = new Map(
|
|
203
|
+
usedStrictTypes.map(x => x.simpleTypeImports ?? []).reduce(reduceFlatMap, []).map(
|
|
204
|
+
x => [`${x?.from}:${x?.name}:${x?.isDefault}`, x],
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
const simpleTypeMappingImportsFrom = [...simpleTypeMappingImportsAll.values()].reduce((out, x) => {
|
|
208
|
+
const entry = out[x.from] ?? (out[x.from] = { names: [] });
|
|
209
|
+
if (x.isDefault) {
|
|
210
|
+
entry.default = x.name;
|
|
211
|
+
} else {
|
|
212
|
+
entry.names.push(x.name);
|
|
213
|
+
}
|
|
214
|
+
entry.names.sort((a, b) => a.localeCompare(b));
|
|
215
|
+
return out;
|
|
216
|
+
}, {} as { [from: string]: { names: string[]; default?: string } });
|
|
217
|
+
|
|
218
|
+
const simpleTypeMappingImportsText = Object.keys(simpleTypeMappingImportsFrom)
|
|
219
|
+
.map(k => {
|
|
220
|
+
const entry = simpleTypeMappingImportsFrom[k];
|
|
221
|
+
const items = [entry.default, entry.names.length ? `{ ${entry.names.join(', ')} }` : ''].filter(x => x);
|
|
222
|
+
return `import ${items.join(', ')} from '${k}';\n`;
|
|
223
|
+
})
|
|
224
|
+
.join('');
|
|
225
|
+
|
|
226
|
+
const simpleTypeMapping = usedStrictTypes
|
|
227
|
+
.sort((a, b) => a.aliasType.localeCompare(b.aliasType))
|
|
228
|
+
.map(x => x.simpleTypeDefinition).join(`\n`);
|
|
229
|
+
|
|
230
|
+
const typeUtilsDefinitions =
|
|
231
|
+
`import { ContractAbstractionFromContractType, WalletContractAbstractionFromContractType } from '${typeUtilsData.importPath}';`;
|
|
232
|
+
|
|
233
|
+
const typeAliasesDefinitions = typeAliasData.mode === 'simple'
|
|
234
|
+
? `${simpleTypeMappingImportsText}${simpleTypeMapping}`
|
|
235
|
+
: typeAliasData.mode === 'local'
|
|
236
|
+
? typeAliasData.fileContent
|
|
237
|
+
: `import { ${usedStrictTypes.map(x => x.aliasType).join(`, `)} } from '${typeAliasData.importPath}';`;
|
|
238
|
+
|
|
239
|
+
const contractTypeName = `${contractName}ContractType`;
|
|
240
|
+
const walletTypeName = `${contractName}WalletType`;
|
|
241
|
+
const codeName = `${contractName}Code`;
|
|
242
|
+
|
|
243
|
+
const typesFileContent = `
|
|
208
244
|
${typeUtilsDefinitions}
|
|
209
245
|
${typeAliasesDefinitions}
|
|
210
246
|
|
|
@@ -219,21 +255,18 @@ export type ${contractTypeName} = ContractAbstractionFromContractType<contractTy
|
|
|
219
255
|
export type ${walletTypeName} = WalletContractAbstractionFromContractType<contractTypes>;
|
|
220
256
|
`;
|
|
221
257
|
|
|
222
|
-
|
|
258
|
+
const contractCodeFileContent = `
|
|
223
259
|
export const ${codeName}: { __type: '${codeName}', protocol: string, code: object[] } = {
|
|
224
260
|
__type: '${codeName}',
|
|
225
261
|
protocol: '${protocol.key}',
|
|
226
262
|
code: JSON.parse(\`${JSON.stringify(parsedContract)}\`)
|
|
227
263
|
};
|
|
228
264
|
`;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
265
|
+
return {
|
|
266
|
+
typesFileContent,
|
|
267
|
+
contractCodeFileContent,
|
|
268
|
+
storage: storageCode,
|
|
269
|
+
methods: methodsCode,
|
|
270
|
+
methodsObject: methodsObjectCode,
|
|
271
|
+
};
|
|
237
272
|
};
|
|
238
|
-
|
|
239
|
-
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { generateContractTypesFromMichelsonCode } from './generator/process';
|
|
2
|
-
export { generateContractTypesProcessContractFiles as generateContractTypesProcessTzContractFiles } from './cli-process';
|
|
3
1
|
export { run } from './cli';
|
|
4
|
-
export
|
|
2
|
+
export { generateContractTypesProcessContractFiles as generateContractTypesProcessTzContractFiles } from './cli-process';
|
|
3
|
+
export { generateContractTypesFromMichelsonCode } from './generator/process';
|
|
4
|
+
export * from './type-aliases';
|
|
@@ -26,6 +26,8 @@ type MapKey = Array<any> | object | string | boolean | number;
|
|
|
26
26
|
export type MMap<K extends MapKey, V> = Omit<MichelsonMap<K, V>, 'get'> & { get: (key: K) => V };
|
|
27
27
|
export type BigMap<K extends MapKey, V> = Omit<MichelsonMap<K, V>, 'get'> & { get: (key: K) => Promise<V> };
|
|
28
28
|
|
|
29
|
+
export type chest = string & { __type: 'chest' };
|
|
30
|
+
export type chest_key = string & { __type: 'chest_key' };
|
|
29
31
|
|
|
30
32
|
const createStringTypeTas = <T extends string>() => {
|
|
31
33
|
return (value: string): T => value as T;
|
|
@@ -64,6 +66,8 @@ export const tas = {
|
|
|
64
66
|
address: createStringTypeTas<address>(),
|
|
65
67
|
bytes: createStringTypeTas<bytes>(),
|
|
66
68
|
contract: createStringTypeTas<contract>(),
|
|
69
|
+
chest: createStringTypeTas<chest>(),
|
|
70
|
+
chest_key: createStringTypeTas<chest_key>(),
|
|
67
71
|
timestamp: (value: string | Date): timestamp => new Date(value).toISOString() as timestamp,
|
|
68
72
|
|
|
69
73
|
int: createBigNumberTypeTas<int>(),
|
|
@@ -81,4 +85,4 @@ export const tas = {
|
|
|
81
85
|
// To number
|
|
82
86
|
number: (value: string | BigNumber) => Number(value + ''),
|
|
83
87
|
};
|
|
84
|
-
`;
|
|
88
|
+
`;
|
package/src/type-aliases.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BigNumber } from 'bignumber.js';
|
|
2
1
|
import { MichelsonMap } from '@taquito/taquito';
|
|
2
|
+
import { BigNumber } from 'bignumber.js';
|
|
3
3
|
|
|
4
4
|
export type unit = (true | undefined) & { __type: 'unit' };
|
|
5
5
|
|
|
@@ -24,58 +24,62 @@ type MapKey = Array<any> | object | string | boolean | number;
|
|
|
24
24
|
export type MMap<K extends MapKey, V> = Omit<MichelsonMap<K, V>, 'get'> & { get: (key: K) => V };
|
|
25
25
|
export type BigMap<K extends MapKey, V> = Omit<MichelsonMap<K, V>, 'get'> & { get: (key: K) => Promise<V> };
|
|
26
26
|
|
|
27
|
+
export type chest = string & { __type: 'chest' };
|
|
28
|
+
export type chest_key = string & { __type: 'chest_key' };
|
|
27
29
|
|
|
28
30
|
const createStringTypeTas = <T extends string>() => {
|
|
29
|
-
|
|
31
|
+
return (value: string): T => value as T;
|
|
30
32
|
};
|
|
31
33
|
|
|
32
34
|
const createBigNumberTypeTas = <T extends BigNumber>() => {
|
|
33
|
-
|
|
35
|
+
return (value: number | BigNumber | string): T => new BigNumber(value) as T;
|
|
34
36
|
};
|
|
35
37
|
|
|
36
|
-
type asMapParamOf<K, V> = K extends string ? { [key: string]: V } | Array<{ key: K
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
type asMapParamOf<K, V> = K extends string ? { [key: string]: V } | Array<{ key: K; value: V }>
|
|
39
|
+
: K extends number ? { [key: number]: V } | Array<{ key: K; value: V }>
|
|
40
|
+
: Array<{ key: K; value: V }>;
|
|
39
41
|
|
|
40
42
|
function asMap<K extends MapKey, V>(value: asMapParamOf<K, V>): MMap<K, V> {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
const m = new MichelsonMap<K, V>();
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
const vArray = value as Array<{ key: K; value: V }>;
|
|
46
|
+
vArray.forEach(x => m.set(x.key, x.value));
|
|
47
|
+
} else {
|
|
48
|
+
const vObject = value as { [key: string]: V };
|
|
49
|
+
Object.keys(vObject).forEach(key => m.set(key as unknown as K, vObject[key]));
|
|
50
|
+
}
|
|
51
|
+
return m as MMap<K, V>;
|
|
50
52
|
}
|
|
51
53
|
const asBigMap = <K extends MapKey, V>(value: asMapParamOf<K, V>) => asMap(value) as unknown as BigMap<K, V>;
|
|
52
54
|
|
|
53
55
|
function add<T extends BigNumber>(a: T, b: T): T {
|
|
54
|
-
|
|
56
|
+
return a.plus(b) as T;
|
|
55
57
|
}
|
|
56
58
|
function subtract<T extends BigNumber>(a: T, b: T): T {
|
|
57
|
-
|
|
59
|
+
return a.minus(b) as T;
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
/** tas: Tezos 'as' casting for strict types */
|
|
61
63
|
export const tas = {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
64
|
+
address: createStringTypeTas<address>(),
|
|
65
|
+
bytes: createStringTypeTas<bytes>(),
|
|
66
|
+
contract: createStringTypeTas<contract>(),
|
|
67
|
+
chest: createStringTypeTas<chest>(),
|
|
68
|
+
chest_key: createStringTypeTas<chest_key>(),
|
|
69
|
+
timestamp: (value: string | Date): timestamp => new Date(value).toISOString() as timestamp,
|
|
70
|
+
|
|
71
|
+
int: createBigNumberTypeTas<int>(),
|
|
72
|
+
nat: createBigNumberTypeTas<nat>(),
|
|
73
|
+
mutez: createBigNumberTypeTas<mutez>(),
|
|
74
|
+
tez: createBigNumberTypeTas<tez>(),
|
|
75
|
+
|
|
76
|
+
map: asMap,
|
|
77
|
+
bigMap: asBigMap,
|
|
78
|
+
|
|
79
|
+
// Operations
|
|
80
|
+
add,
|
|
81
|
+
subtract,
|
|
82
|
+
|
|
83
|
+
// To number
|
|
84
|
+
number: (value: string | BigNumber) => Number(value + ''),
|
|
81
85
|
};
|