From 14cdac7bfae6ba39c5d13e2f151c8d46f1da28f7 Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 10 Mar 2023 23:08:41 -0500 Subject: [PATCH] feat(one_shot): some deno and lucid stuff --- examples/one_shot/.gitignore | 5 + examples/one_shot/components/Button.tsx | 2 +- examples/one_shot/components/Input.tsx | 21 +++ examples/one_shot/deno.lock | 173 ++++++++++++++++++++++++ examples/one_shot/fresh.gen.ts | 12 +- examples/one_shot/import_map.json | 4 +- examples/one_shot/islands/App.tsx | 71 ++++++++++ examples/one_shot/islands/Counter.tsx | 17 --- examples/one_shot/main.ts | 4 +- examples/one_shot/plutus.json | 2 +- examples/one_shot/routes/[name].tsx | 5 - examples/one_shot/routes/api/joke.ts | 21 --- examples/one_shot/routes/index.tsx | 52 +++++-- examples/one_shot/utils.ts | 45 +++--- 14 files changed, 345 insertions(+), 89 deletions(-) create mode 100644 examples/one_shot/components/Input.tsx create mode 100644 examples/one_shot/islands/App.tsx delete mode 100644 examples/one_shot/islands/Counter.tsx delete mode 100644 examples/one_shot/routes/[name].tsx delete mode 100644 examples/one_shot/routes/api/joke.ts diff --git a/examples/one_shot/.gitignore b/examples/one_shot/.gitignore index ca7ce639..9b3bec67 100644 --- a/examples/one_shot/.gitignore +++ b/examples/one_shot/.gitignore @@ -6,3 +6,8 @@ build/ docs/ # generated by deno fresh .vscode/ + +vendor/ + +static/cardano_message_signing_bg.wasm +static/cardano_multiplatform_lib_bg.wasm diff --git a/examples/one_shot/components/Button.tsx b/examples/one_shot/components/Button.tsx index 909d3807..74051aca 100644 --- a/examples/one_shot/components/Button.tsx +++ b/examples/one_shot/components/Button.tsx @@ -6,7 +6,7 @@ export function Button(props: JSX.HTMLAttributes) { + + )} + + ); +} diff --git a/examples/one_shot/islands/Counter.tsx b/examples/one_shot/islands/Counter.tsx deleted file mode 100644 index 1b437bf2..00000000 --- a/examples/one_shot/islands/Counter.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import { useState } from "preact/hooks"; -import { Button } from "../components/Button.tsx"; - -interface CounterProps { - start: number; -} - -export default function Counter(props: CounterProps) { - const [count, setCount] = useState(props.start); - return ( -
-

{count}

- - -
- ); -} diff --git a/examples/one_shot/main.ts b/examples/one_shot/main.ts index bb00964d..e5d0f606 100644 --- a/examples/one_shot/main.ts +++ b/examples/one_shot/main.ts @@ -5,9 +5,9 @@ /// import { start } from "$fresh/server.ts"; -import manifest from "./fresh.gen.ts"; +import manifest from "~/fresh.gen.ts"; import twindPlugin from "$fresh/plugins/twind.ts"; -import twindConfig from "./twind.config.ts"; +import twindConfig from "~/twind.config.ts"; await start(manifest, { plugins: [twindPlugin(twindConfig)] }); diff --git a/examples/one_shot/plutus.json b/examples/one_shot/plutus.json index 6708c2f4..8920d784 100644 --- a/examples/one_shot/plutus.json +++ b/examples/one_shot/plutus.json @@ -121,4 +121,4 @@ "hash": "e1c247d17b275dc889a0437d23f7791a2ea60a6d1b84612572668ec2" } ] -} \ No newline at end of file +} diff --git a/examples/one_shot/routes/[name].tsx b/examples/one_shot/routes/[name].tsx deleted file mode 100644 index 9c068270..00000000 --- a/examples/one_shot/routes/[name].tsx +++ /dev/null @@ -1,5 +0,0 @@ -import { PageProps } from "$fresh/server.ts"; - -export default function Greet(props: PageProps) { - return
Hello {props.params.name}
; -} diff --git a/examples/one_shot/routes/api/joke.ts b/examples/one_shot/routes/api/joke.ts deleted file mode 100644 index a3f4243d..00000000 --- a/examples/one_shot/routes/api/joke.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { HandlerContext } from "$fresh/server.ts"; - -// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/ -const JOKES = [ - "Why do Java developers often wear glasses? They can't C#.", - "A SQL query walks into a bar, goes up to two tables and says “can I join you?”", - "Wasn't hard to crack Forrest Gump's password. 1forrest1.", - "I love pressing the F5 key. It's refreshing.", - "Called IT support and a chap from Australia came to fix my network connection. I asked “Do you come from a LAN down under?”", - "There are 10 types of people in the world. Those who understand binary and those who don't.", - "Why are assembly programmers often wet? They work below C level.", - "My favourite computer based band is the Black IPs.", - "What programme do you use to predict the music tastes of former US presidential candidates? An Al Gore Rhythm.", - "An SEO expert walked into a bar, pub, inn, tavern, hostelry, public house.", -]; - -export const handler = (_req: Request, _ctx: HandlerContext): Response => { - const randomIndex = Math.floor(Math.random() * JOKES.length); - const body = JOKES[randomIndex]; - return new Response(body); -}; diff --git a/examples/one_shot/routes/index.tsx b/examples/one_shot/routes/index.tsx index 42e62689..84632217 100644 --- a/examples/one_shot/routes/index.tsx +++ b/examples/one_shot/routes/index.tsx @@ -1,23 +1,49 @@ import { Head } from "$fresh/runtime.ts"; -import Counter from "~/islands/Counter.tsx"; +import { Handlers, PageProps } from "$fresh/server.ts"; + +import App from "~/islands/App.tsx"; + +import { readValidators, Validators } from "~/utils.ts"; + +interface Data { + validators: Validators; +} + +export const handler: Handlers = { + async GET(_req, ctx) { + const validators = await readValidators(); + + return ctx.render({ validators }); + }, +}; + +export default function Home({ data }: PageProps) { + const { validators } = data; -export default function Home() { return ( <> One Shot -
- the fresh logo: a sliced lemon dripping with juice -

- Welcome to `fresh`. Try updating this message in the - ./routes/index.tsx file, and refresh. -

- + +
+
+

+ Make a one shot minting and lock contract +

+ +

Lock

+
+            {validators.lock.script}
+          
+ +

Mint

+
+            {validators.mint.script}
+          
+
+ +
); diff --git a/examples/one_shot/utils.ts b/examples/one_shot/utils.ts index 475ccccf..be2e0346 100644 --- a/examples/one_shot/utils.ts +++ b/examples/one_shot/utils.ts @@ -1,12 +1,12 @@ import { + applyDoubleCborEncoding, applyParamsToScript, Data, - fromHex, + Lucid, MintingPolicy, + OutRef, SpendingValidator, - toHex, -} from "lucid"; -import * as cbor from "cbor"; +} from "~/vendor/lucid@0.9.4/mod.ts"; import { Blueprint } from "~/blueprint.ts"; @@ -15,9 +15,10 @@ export type Validators = { mint: MintingPolicy; }; -async function readValidators(): Promise { - const blueprint: Blueprint = JSON - .parse(await Deno.readTextFile("plutus.json")); +export async function readValidators(): Promise { + const blueprint: Blueprint = JSON.parse( + await Deno.readTextFile("plutus.json"), + ); const lock = blueprint.validators.find((v) => v.title === "main.lock"); @@ -34,29 +35,37 @@ async function readValidators(): Promise { return { lock: { type: "PlutusV2", - script: toHex(cbor.encode(fromHex(lock.compiledCode))), + script: lock.compiledCode, }, mint: { type: "PlutusV2", - script: toHex(cbor.encode(fromHex(mint.compiledCode))), + script: mint.compiledCode, }, }; } export async function applyParams( - // bytes tokenName: string, - // bytes - policyId: string, - // aiken/transaction.{OutputReference} - outputReference: any, + outputReference: OutRef, ): Promise<{ lock: string; mint: string }> { const validators = await readValidators(); + const mint = applyParamsToScript(validators.mint.script, [ + tokenName, + outputReference, + ]); + + const lucid = new Lucid(); + + const policyId = lucid.utils.validatorToScriptHash(validators.mint); + + const lock = applyParamsToScript(validators.lock.script, [ + tokenName, + policyId, + ]); + return { - // TODO: apply tokenName and policyId - lock: applyParamsToScript(validators.lock.script, []), - // TODO: apply tokenName and outputReference - mint: applyParamsToScript(validators.mint.script, []), + lock: applyDoubleCborEncoding(lock), + mint: applyDoubleCborEncoding(mint), }; }