-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
362 lines (339 loc) · 9.57 KB
/
index.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import * as fs from "fs";
type Pointer = {
$ref: string;
};
const genericLetters = "abcdefg".split("");
const plutusData: { title: "Data"; description: "Any Plutus data." } = {
title: "Data",
description: "Any Plutus data.",
};
type Definition =
| ({ title?: string; description?: string } & (
| // Pointer
{
schema: Pointer;
}
// Variants (Product)
| {
anyOf: Variants;
}
// Record (Sum)
| {
dataType: "constructor";
index: number;
fields: { $ref: string; title?: string }[];
}
// List
| {
dataType: "list";
items: Pointer;
}
// Dictionary
| {
dataType: "map";
keys?: Pointer;
values?: Pointer;
}
// Raw bytes
| {
dataType: "bytes";
}
// Raw integer
| {
dataType: "integer";
}
))
| typeof plutusData;
type Variants = Definition[];
type Blueprint = {
preamble: {
title: string;
description?: string;
version?: string;
plutusVersion?: string;
license?: string;
};
validators: {
title: string;
datum?: Definition;
redeemer: Definition;
parameters?: Definition[];
compiledCode: string;
hash: string;
}[];
definitions: Record<string, Definition>;
};
const plutusJson: Blueprint = JSON.parse(
fs.readFileSync("plutus.json", "utf8")
);
// Butane-Specific Changes
if (
plutusJson.validators.find((x) => x.title == "synthetics.validate") !==
undefined
) {
plutusJson.validators.find((x) => x.title ==
"synthetics.validate")!.redeemer =
plutusJson.validators.find((x) => x.title ==
"synthetics.types")!.redeemer;
plutusJson.validators.find((x) => x.title == "state.spend")!.datum =
plutusJson.validators.find((x) => x.title ==
"synthetics.types")!.datum;
plutusJson.validators = plutusJson.validators.filter(
(x) =>
[
"util.always_true",
"util.types",
"synthetics.types",
"price_feed.feed_type",
"price_feed.feed_inner_type",
].indexOf(x.title) == -1
);
}
//
const definitions = plutusJson.definitions;
function getTypstRef(ref: string) {
let res = ref;
// Generic
if (ref.includes("$")) {
let [main] = ref.split("$");
res = `${main}-a`;
}
return res.replaceAll("/", "-").replaceAll("_", "-").toLowerCase();
}
function renderRef(ref: string, generics?: string[], preprocess = true) {
if (!ref) {
return "";
}
let sliced;
if (preprocess) {
sliced = ref.slice("#/definitions/".length).replaceAll("~1", "/");
} else {
sliced = ref;
}
let links: string[] = [];
let fixed = sliced.split("$");
let almost: string;
let almostLink: string;
if (fixed.length == 1) {
almost = fixed[0];
almostLink = getTypstRef(almost);
} else if (generics === undefined) {
const numGenerics = fixed[1].split("_").length;
almost = `${fixed[0]}\\<${genericLetters
.slice(0, numGenerics)
.join(", ")}\\>`;
almostLink = getTypstRef(`${fixed[0]}-a`);
} else {
almost = fixed[0];
links = fixed[1].split("_");
almostLink = getTypstRef(`${fixed[0]}-a`);
}
const lastSlash = almost.lastIndexOf("/");
if (lastSlash !== -1) {
almost =
almost.substring(0, lastSlash) + "." + almost.substring(lastSlash +
1);
}
if (generics?.includes(almost)) {
return genericLetters[generics.indexOf(almost)];
}
let linkStr = "";
// console.log(`#link(<${getTypstRef(key)}>)[Click here]`)
if (links.length > 0) {
linkStr = links
.map((link) => {
// TODO: Properly handle recursive generic types
if (link == "aiken/transaction/credential/Referenced") {
link =
"aiken/transaction/credential/Referenced$aiken/transaction/credential/Credential";
} else if (link == "butane/types/Feed") {
link = "butane/types/Feed$butane/types/PriceFeed_ByteArray";
}
return renderRef(link, generics, false);
// console.log(link)
})
.join(", ");
}
linkStr = linkStr.length > 0 ? `\\<${linkStr}\\>` : "";
let almostStr = `#link(<${almostLink}>)[${almost}]`;
if (["int", "bytearray", "data"].includes(almostLink)) {
almostStr = almost;
} else if (almostLink == "tuple") {
almostStr = "(Int, Int)";
}
return `${almostStr}${linkStr}`;
}
function stringifyDefinitions(
depth: number,
definition: Definition,
generics?: string[]
) {
let out = "";
if ("schema" in definition && definition.title) {
//out += `${tabs}alias ${definition.title} =
${definition.schema.$ref}\n`
out += `${renderRef(definition.schema.$ref, generics)} \\ \n`;
} else if ("anyOf" in definition) {
out += `:= \\{ \\ \n#enum(indent: ${
depth * 16
}pt,numbering: (num)=>[#if num > 1 [|]],`;
for (const def of definition.anyOf) {
out += "[" + stringifyDefinitions(depth + 1, def, generics) + "],";
}
out = out.slice(0, -1);
out += `)\n\\}\n`;
} else if ("dataType" in definition && definition.dataType ==
"constructor") {
if (!definition.fields[0]) {
out += `${definition.title}`;
} else if (definition.fields[0].title) {
// Record
out += `${definition.title} \\{\\`;
for (const field of definition.fields) {
out += ` #h(16pt) ${field.title} := ${renderRef(
field.$ref,
generics
)}, \\`;
}
out += `\ \\}`;
} else {
// Enum
out += `${definition.title}\(`;
for (const field of definition.fields) {
out += `${renderRef(field.$ref, generics)},`;
}
out = out.slice(0, -1);
out += `\)#linebreak()`;
}
} else if ("dataType" in definition && definition.dataType == "list") {
out += `\${x_n in $ ${renderRef(
definition.items.$ref,
generics
)} $}_(n=0)^∞$\n`;
} else if ("dataType" in definition && definition.dataType == "map") {
out += `Map\\<${renderRef(
definition.keys!.$ref,
generics
)} $arrow.r$ ${renderRef(definition.values!.$ref, generics)}\\>\n`;
} else if ("dataType" in definition && definition.dataType == "bytes") {
//out += `${tabs}raw bytes\n`
} else if ("dataType" in definition && definition.dataType == "integer")
{
//out += `${tabs}an arbitrary sized integer\n`
} else if (JSON.stringify(definition) === JSON.stringify(plutusData)) {
//out += `${tabs}some raw plutus data\n`
}
return out;
}
function snakeToPascalCase(input: string): string {
return input.split("_").reduce((result, word) => {
return result + word.charAt(0).toUpperCase() +
word.slice(1).toLowerCase();
}, "");
}
console.log("#import sym");
console.log("#let blueprint_appendix = [");
console.log("== Validator Definitions");
{
let pScripts = `#table(
columns: (auto, auto),
inset: 10pt,
align: horizon,
[*Validator Name*], [*Parameters*],
`;
for (const validator of plutusJson.validators) {
const title = validator.title
.split(".")
.map(snakeToPascalCase)
.join(" $arrow.r$ ");
if (validator.parameters == undefined || validator.parameters.length
== 0) {
pScripts += `[${title}], [],`;
} else {
pScripts += `[${title}], [`;
for (const parameter of validator.parameters) {
const paramRef = renderRef(parameter.schema.$ref);
pScripts += `\`${
parameter.title.split(".").map(snakeToPascalCase) || ""
}\`: ${paramRef},#linebreak()`;
}
pScripts += `],`;
}
}
pScripts = pScripts.slice(0, -1);
pScripts += ")";
console.log(pScripts);
}
console.log("== Redeemers");
{
let redeemersString = `#table(
columns: (auto, auto),
inset: 10pt,
align: horizon,
[*Validator Name*], [*Redeemer*],
`;
for (const validator of plutusJson.validators) {
if (!("schema" in validator.redeemer)) {
throw new Error("Redeemer must be a schema/ref");
}
if (validator.datum && !("schema" in validator.datum)) {
throw new Error("Datum must be a schema/ref");
}
const title = validator.title
.split(".")
.map(snakeToPascalCase)
.join(" $arrow.r$ ");
const redeemerRef = renderRef(validator.redeemer.schema.$ref, [""]);
redeemersString += `[${title}], [${redeemerRef}],`;
}
redeemersString = redeemersString.slice(0, -1);
redeemersString += ")";
console.log(redeemersString);
}
console.log("== Datums");
{
let datumString = `#table(
columns: (auto, auto),
inset: 10pt,
align: horizon,
[*Validator Name*], [*Datum*],
`;
for (const validator of plutusJson.validators) {
if (validator.datum && !("schema" in validator.datum)) {
throw new Error("Datum must be a schema/ref");
}
const title = validator.title
.split(".")
.map(snakeToPascalCase)
.join(" $arrow.r$ ");
const datumRef = validator.datum?.schema.$ref
? renderRef(validator.datum?.schema.$ref, [""])
: undefined;
if (datumRef != undefined) {
datumString += `[${title}], [${datumRef}],`;
}
}
datumString = datumString.slice(0, -1);
datumString += ")";
console.log(datumString);
}
console.log(`== Definitions`);
const written = new Set();
for (const key of Object.keys(definitions)) {
const def = definitions[key];
const [keyNoGen, generics] = key.split("$");
const genericsList = generics?.split("_");
if (written.has(keyNoGen)) {
continue;
}
const ret = stringifyDefinitions(1, def, genericsList ?? [""]);
if (ret != "" && key) {
console.log(
`/ ${renderRef("#/definitions/" + key)} <${getTypstRef(key)}>:
${ret}`
);
written.add(keyNoGen);
// console.log(`#link(<${getTypstRef(key)}>)[Click here]`)
}
}
console.log("]");