Skip to content

Commit

Permalink
Reformatted and resolved dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ilap authored and micahkendall committed Dec 28, 2023
1 parent 7a99a03 commit ffd17fb
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 143 deletions.
150 changes: 87 additions & 63 deletions blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ type Blueprint = {
compiledCode: string;
hash: string;
}[];
definitions: Record<string, {
title: string;
schema: {
$ref: string;
};
}>;
definitions: Record<
string,
{
title: string;
schema: {
$ref: string;
};
}
>;
};

export async function parseBlueprint(blueprint: string, plutusTs: string) {
const plutusJson: Blueprint = JSON.parse(
await Bun.file(blueprint).text(),
);
const plutusJson: Blueprint = JSON.parse(await Bun.file(blueprint).text());

const plutusVersion = "Plutus" +
plutusJson.preamble.plutusVersion.toUpperCase();
const plutusVersion =
"Plutus" + plutusJson.preamble.plutusVersion.toUpperCase();

const definitions = plutusJson.definitions;

Expand All @@ -69,33 +70,35 @@ import { applyParamsToScript, Data, Validator } from "../translucent/index.ts"`;
dataType: "list",
items: params.map((param) => resolveSchema(param.schema, definitions)),
};
const paramsArgs = params.map((
param,
index,
) => [snakeToCamel(param.title), schemaToType(paramsSchema.items[index])]);
const paramsArgs = params.map((param, index) => [
snakeToCamel(param.title),
schemaToType(paramsSchema.items[index]),
]);

const script = validator.compiledCode;

return `export interface ${name} {
new (${paramsArgs.map((param) => param.join(":")).join(",")}): Validator;${datum ? `\n${datumTitle}: ${schemaToType(datumSchema)};` : ""
}
new (${paramsArgs.map((param) => param.join(":")).join(",")}): Validator;${
datum ? `\n${datumTitle}: ${schemaToType(datumSchema)};` : ""
}
${redeemerTitle}: ${schemaToType(redeemerSchema)};
};
export const ${name} = Object.assign(
function (${paramsArgs.map((param) => param.join(":")).join(",")}) {${paramsArgs.length > 0
? `return { type: "${plutusVersion}", script: applyParamsToScript("${script}", [${paramsArgs.map((param) => param[0]).join(",")
}], ${JSON.stringify(paramsSchema)} as any) };`
function (${paramsArgs.map((param) => param.join(":")).join(",")}) {${
paramsArgs.length > 0
? `return { type: "${plutusVersion}", script: applyParamsToScript("${script}", [${paramsArgs
.map((param) => param[0])
.join(",")}], ${JSON.stringify(paramsSchema)} as any) };`
: `return {type: "${plutusVersion}", script: "${script}"};`
}},
}},
${datum ? `{${datumTitle}: ${JSON.stringify(datumSchema)}},` : ""}
{${redeemerTitle}: ${JSON.stringify(redeemerSchema)}},
) as unknown as ${name};`;
});

const plutus = imports + "\n\n" + validators.join("\n\n");


await Bun.write(plutusTs, plutus);

console.log(
Expand All @@ -110,7 +113,7 @@ import { applyParamsToScript, Data, Validator } from "../translucent/index.ts"`;
return {
...schema,
items: schema.items.map((item: any) =>
resolveSchema(item, definitions)
resolveSchema(item, definitions),
),
};
} else {
Expand Down Expand Up @@ -138,14 +141,19 @@ import { applyParamsToScript, Data, Validator } from "../translucent/index.ts"`;
};
} else {
if (schema["$ref"]) {
const refKey =
schema["$ref"].replaceAll("~1", "/").split("#/definitions/")[1];
const refKey = schema["$ref"]
.replaceAll("~1", "/")
.split("#/definitions/")[1];

if (refKey === refName) {
return schema;
} else {
refName = refKey
const resolved = resolveSchema(definitions[refKey], definitions, refName);
refName = refKey;
const resolved = resolveSchema(
definitions[refKey],
definitions,
refName,
);
return resolved;
}
} else {
Expand All @@ -169,10 +177,12 @@ import { applyParamsToScript, Data, Validator } from "../translucent/index.ts"`;
if (isVoid(schema)) {
return "undefined";
} else {
return `{${schema.fields.map((field: any) =>
`${field.title || "wrapper"}:${schemaToType(field)}`
).join(";")
}}`;
return `{${schema.fields
.map(
(field: any) =>
`${field.title || "wrapper"}:${schemaToType(field)}`,
)
.join(";")}}`;
}
}
case "enum": {
Expand All @@ -186,30 +196,37 @@ import { applyParamsToScript, Data, Validator } from "../translucent/index.ts"`;
if (isNullable(schema)) {
return `${schemaToType(schema.anyOf[0].fields[0])} | null`;
}
return schema.anyOf.map((entry: any) =>
entry.fields.length === 0
? `"${entry.title}"`
: `{${entry.title}: ${entry.fields[0].title
? `{${entry.fields.map((field: any) =>
[field.title, schemaToType(field)].join(":")
).join(",")
}}}`
: `[${entry.fields.map((field: any) => schemaToType(field)).join(",")
}]}`
}`
).join(" | ");
return schema.anyOf
.map((entry: any) =>
entry.fields.length === 0
? `"${entry.title}"`
: `{${entry.title}: ${
entry.fields[0].title
? `{${entry.fields
.map((field: any) =>
[field.title, schemaToType(field)].join(":"),
)
.join(",")}}}`
: `[${entry.fields
.map((field: any) => schemaToType(field))
.join(",")}]}`
}`,
)
.join(" | ");
}
case "list": {
if (schema.items instanceof Array) {
return `[${schema.items.map((item: any) => schemaToType(item)).join(",")
}]`;
return `[${schema.items
.map((item: any) => schemaToType(item))
.join(",")}]`;
} else {
return `Array<${schemaToType(schema.items)}>`;
}
}
case "map": {
return `Map<${schemaToType(schema.keys)}, ${schemaToType(schema.values)
}>`;
return `Map<${schemaToType(schema.keys)}, ${schemaToType(
schema.values,
)}>`;
}
case undefined: {
return "Data";
Expand All @@ -219,38 +236,45 @@ import { applyParamsToScript, Data, Validator } from "../translucent/index.ts"`;
}

function isBoolean(shape: any): boolean {
return shape.anyOf && shape.anyOf[0]?.title === "False" &&
shape.anyOf[1]?.title === "True";
return (
shape.anyOf &&
shape.anyOf[0]?.title === "False" &&
shape.anyOf[1]?.title === "True"
);
}

function isVoid(shape: any): boolean {
return shape.index === 0 && shape.fields.length === 0;
}

function isNullable(shape: any): boolean {
return shape.anyOf && shape.anyOf[0]?.title === "Some" &&
shape.anyOf[1]?.title === "None";
return (
shape.anyOf &&
shape.anyOf[0]?.title === "Some" &&
shape.anyOf[1]?.title === "None"
);
}

function snakeToCamel(s: string): string {
const withUnderscore = s.charAt(0) === "_" ? s.charAt(0) : "";
return withUnderscore +
(withUnderscore ? s.slice(1) : s).toLowerCase().replace(
/([-_][a-z])/g,
(group) =>
group
.toUpperCase()
.replace("-", "")
.replace("_", ""),
);
return (
withUnderscore +
(withUnderscore ? s.slice(1) : s)
.toLowerCase()
.replace(/([-_][a-z])/g, (group) =>
group.toUpperCase().replace("-", "").replace("_", ""),
)
);
}

function upperFirst(s: string): string {
const withUnderscore = s.charAt(0) === "_" ? s.charAt(0) : "";
return withUnderscore +
return (
withUnderscore +
s.charAt(withUnderscore ? 1 : 0).toUpperCase() +
s.slice((withUnderscore ? 1 : 0) + 1);
s.slice((withUnderscore ? 1 : 0) + 1)
);
}
}

parseBlueprint("../../../on-chain/aiken/plutus.json", "plutus.ts")
parseBlueprint("../../../on-chain/aiken/plutus.json", "plutus.ts");
Binary file modified bun.lockb
Binary file not shown.
Loading

0 comments on commit ffd17fb

Please sign in to comment.