-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.ts
274 lines (255 loc) · 9.19 KB
/
type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import type { MichelsonJSON, MichelsonMicheline, PairsOfKeys, IType, PrimType } from './typings';
import { composeRightCombLayout, parenthesis } from './misc/utils';
import { Prim } from './enums/prim';
export class Michelson_Type implements IType {
#annotation?: string;
private innerTypes: IType[];
constructor(private type: PrimType, ...innerTypes: IType[]) {
this.innerTypes = innerTypes;
}
/**
* @description Set field annotation
* @link https://tezos.gitlab.io/active/michelson.html#field-and-constructor-annotations
* @param {string} annotation field annotation
*/
public setAnnotation(annotation: string) {
this.#annotation = annotation;
return this;
}
/**
* @description Generate the Micheline representation of the type
* @returns {MichelsonMicheline} Micheline representation
*/
toMicheline(): MichelsonMicheline {
const annot = this.#annotation ? `%${this.#annotation}` : '';
switch (this.type) {
case Prim.unit:
case Prim.int:
case Prim.nat:
case Prim.mutez:
case Prim.timestamp:
case Prim.string:
case Prim.address:
case Prim.chain_id:
case Prim.bool:
case Prim.bytes:
case Prim.bls12_381_fr:
case Prim.bls12_381_g1:
case Prim.bls12_381_g2:
case Prim.key:
case Prim.key_hash:
case Prim.signature:
case Prim.operation:
case Prim.never:
return annot ? parenthesis(`${this.type} ${annot}`) : this.type;
case Prim.list:
case Prim.set:
case Prim.pair:
case Prim.or:
case Prim.option:
case Prim.map:
case Prim.big_map:
case Prim.lambda:
case Prim.ticket:
case Prim.contract:
case Prim.sapling_state:
case Prim.sapling_transaction:
return parenthesis(
[this.type, ...(annot ? [annot] : []), ...this.innerTypes.map((t) => t.toMicheline())].join(' '),
);
}
}
/**
* @description Generate the JSON representation of the type
* @returns {MichelsonMicheline} JSON representation
*/
toJSON(): MichelsonJSON {
const obj = this.#annotation ? { annots: [`%${this.#annotation}`] } : {};
switch (this.type) {
case Prim.unit:
case Prim.int:
case Prim.nat:
case Prim.mutez:
case Prim.timestamp:
case Prim.string:
case Prim.address:
case Prim.chain_id:
case Prim.bool:
case Prim.bytes:
case Prim.bls12_381_fr:
case Prim.bls12_381_g1:
case Prim.bls12_381_g2:
case Prim.key:
case Prim.key_hash:
case Prim.signature:
case Prim.operation:
case Prim.never:
return {
...obj,
prim: this.type,
};
case Prim.list:
case Prim.set:
case Prim.pair:
case Prim.or:
case Prim.option:
case Prim.map:
case Prim.big_map:
case Prim.lambda:
case Prim.ticket:
case Prim.contract:
case Prim.sapling_state:
case Prim.sapling_transaction:
return {
...obj,
prim: this.type,
args: this.innerTypes.map((t) => t.toJSON()),
};
}
}
/**
* @description Resolve type instance to a primitive
* @return {MichelsonJSON} Michelson JSON format
*/
[Symbol.toPrimitive](): MichelsonJSON {
return this.toJSON();
}
}
export class Michelson_Type_With_Param implements IType {
#annotation?: string;
#params: number[];
constructor(private type: PrimType, ...params: number[]) {
this.#params = params;
}
/**
* @description Set field annotation
* @link https://tezos.gitlab.io/active/michelson.html#field-and-constructor-annotations
* @param {string} annotation field annotation
*/
public setAnnotation(annotation: string) {
this.#annotation = annotation;
return this;
}
/**
* @description Generate the Micheline representation of the type
* @returns {MichelsonMicheline} Micheline representation
*/
toMicheline(): MichelsonMicheline {
return `(${[this.type, ...this.#params.map(String)].join(' ')})`;
}
/**
* @description Generate the JSON representation of the type
* @returns {MichelsonMicheline} JSON representation
*/
toJSON(): MichelsonJSON {
const obj = this.#annotation ? { annots: [`%${this.#annotation}`] } : {};
return {
...obj,
prim: this.type,
args: this.#params.map((p) => ({
int: String(p),
})),
};
}
/**
* @description Resolve type instance to a primitive
* @return {MichelsonJSON} Michelson JSON format
*/
[Symbol.toPrimitive](): MichelsonJSON {
return this.toJSON();
}
}
export interface IRecordVariant extends IType {
fields: Record<string, IType>;
layout: PairsOfKeys<string>;
}
export const buildRecordVariantType = (
fields: Record<string, IType>,
layout: PairsOfKeys<string>,
container: (leftType: IType, rightType: IType) => IType,
): IRecordVariant => {
const buildBranch = (branch: string | PairsOfKeys<string>): IType => {
if (typeof branch === 'string') {
return fields[branch].setAnnotation(branch);
}
const [left, right] = branch;
return container(buildBranch(left), buildBranch(right));
};
return Object.assign(buildBranch(layout), { fields, layout });
};
// Singleton types
export const TNat = () => new Michelson_Type(Prim.nat);
export const TInt = () => new Michelson_Type(Prim.int);
export const TMutez = () => new Michelson_Type(Prim.mutez);
export const TString = () => new Michelson_Type(Prim.string);
export const TBool = () => new Michelson_Type(Prim.bool);
export const TAddress = () => new Michelson_Type(Prim.address);
export const TTimestamp = () => new Michelson_Type(Prim.timestamp);
export const TChain_id = () => new Michelson_Type(Prim.chain_id);
export const TBytes = () => new Michelson_Type(Prim.bytes);
export const TBls12_381_fr = () => new Michelson_Type(Prim.bls12_381_fr);
export const TBls12_381_g1 = () => new Michelson_Type(Prim.bls12_381_g1);
export const TBls12_381_g2 = () => new Michelson_Type(Prim.bls12_381_g2);
export const TKey = () => new Michelson_Type(Prim.key);
export const TKey_hash = () => new Michelson_Type(Prim.key_hash);
export const TSignature = () => new Michelson_Type(Prim.signature);
export const TUnit = () => new Michelson_Type(Prim.unit);
export const TOperation = () => new Michelson_Type(Prim.operation);
export const TNever = () => new Michelson_Type(Prim.never);
// Container types
export const TList = (innerType: IType) => new Michelson_Type(Prim.list, innerType);
export const TSet = (innerType: IType) => new Michelson_Type(Prim.set, innerType);
export const TOption = (innerType: IType) => new Michelson_Type(Prim.option, innerType);
export const TPair = (leftType: IType, rightType: IType) => new Michelson_Type(Prim.pair, leftType, rightType);
export const TOr = (leftType: IType, rightType: IType) => new Michelson_Type(Prim.or, leftType, rightType);
export const TMap = (keyType: IType, valueType: IType) => new Michelson_Type(Prim.map, keyType, valueType);
export const TBig_map = (keyType: IType, valueType: IType) => new Michelson_Type(Prim.big_map, keyType, valueType);
export const TLambda = (inType: IType, outType: IType) => new Michelson_Type(Prim.lambda, inType, outType);
export const TTicket = (innerType: IType) => new Michelson_Type(Prim.ticket, innerType);
export const TContract = (innerType: IType) => new Michelson_Type(Prim.contract, innerType);
export const TSapling_state = (memoSize: number) => new Michelson_Type_With_Param(Prim.sapling_state, memoSize);
export const TSapling_transaction = (memoSize: number) =>
new Michelson_Type_With_Param(Prim.sapling_transaction, memoSize);
// Artificial Types
export const TRecord = (fields: Record<string, IType>, layout?: PairsOfKeys<string>) =>
buildRecordVariantType(fields, layout || composeRightCombLayout(Object.keys(fields)), TPair);
export const TVariant = (fields: Record<string, IType>, layout?: PairsOfKeys<string>) =>
buildRecordVariantType(fields, layout || composeRightCombLayout(Object.keys(fields)), TOr);
const Types = {
// Singleton types
TUnit,
TNat,
TInt,
TMutez,
TString,
TBool,
TBytes,
TAddress,
TTimestamp,
TChain_id,
TBls12_381_fr,
TBls12_381_g1,
TBls12_381_g2,
TKey,
TKey_hash,
TSignature,
TOperation,
TNever,
// Container types
TList,
TSet,
TOption,
TPair,
TOr,
TMap,
TBig_map,
TLambda,
TTicket,
TContract,
TSapling_state,
TSapling_transaction,
// Artificial Types
TRecord,
TVariant,
};
export default Types;