From ab1ee17ad4f435bbedb3a11ce0285eafdc05804c Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 10 Mar 2023 12:43:53 -0500 Subject: [PATCH] feat(one_shot): some deno boilerplate --- examples/one_shot/.gitignore | 2 + examples/one_shot/README.md | 63 ++---------- examples/one_shot/aiken.toml | 4 +- examples/one_shot/blueprint.ts | 28 ++++++ examples/one_shot/components/Button.tsx | 12 +++ examples/one_shot/deno.json | 10 ++ examples/one_shot/deno.lock | 127 ++++++++++++++++++++++++ examples/one_shot/dev.ts | 5 + examples/one_shot/fresh.gen.ts | 24 +++++ examples/one_shot/import_map.json | 15 +++ examples/one_shot/islands/Counter.tsx | 17 ++++ examples/one_shot/main.ts | 13 +++ 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 | 24 +++++ examples/one_shot/static/favicon.ico | Bin 0 -> 22382 bytes examples/one_shot/static/logo.svg | 6 ++ examples/one_shot/twind.config.ts | 5 + examples/one_shot/utils.ts | 62 ++++++++++++ 20 files changed, 385 insertions(+), 60 deletions(-) create mode 100644 examples/one_shot/blueprint.ts create mode 100644 examples/one_shot/components/Button.tsx create mode 100644 examples/one_shot/deno.json create mode 100644 examples/one_shot/deno.lock create mode 100755 examples/one_shot/dev.ts create mode 100644 examples/one_shot/fresh.gen.ts create mode 100644 examples/one_shot/import_map.json create mode 100644 examples/one_shot/islands/Counter.tsx create mode 100644 examples/one_shot/main.ts create mode 100644 examples/one_shot/routes/[name].tsx create mode 100644 examples/one_shot/routes/api/joke.ts create mode 100644 examples/one_shot/routes/index.tsx create mode 100644 examples/one_shot/static/favicon.ico create mode 100644 examples/one_shot/static/logo.svg create mode 100644 examples/one_shot/twind.config.ts create mode 100644 examples/one_shot/utils.ts diff --git a/examples/one_shot/.gitignore b/examples/one_shot/.gitignore index ff7811b1..ca7ce639 100644 --- a/examples/one_shot/.gitignore +++ b/examples/one_shot/.gitignore @@ -4,3 +4,5 @@ artifacts/ build/ # Aiken's default documentation export docs/ +# generated by deno fresh +.vscode/ diff --git a/examples/one_shot/README.md b/examples/one_shot/README.md index 895e65d7..f605bd96 100644 --- a/examples/one_shot/README.md +++ b/examples/one_shot/README.md @@ -1,62 +1,11 @@ -# mint_lock_burn +# fresh project -Write validators in the `validators` folder, and supporting functions in the `lib` folder using `.ak` as a file extension. +### Usage -For example, as `validators/always_true.ak` +Start the project: -```gleam -validator spend { - pub fn spend(_datum: Data, _redeemer: Data, _context: Data) -> Bool { - True - } -} +``` +deno task start ``` -Validators are named after their purpose, so one of: - -- `spent` -- `mint` -- `withdraw` -- `publish` - -## Building - -```sh -aiken build -``` - -## Testing - -You can write tests in any module using the `test` keyword. For example: - -```gleam -test foo() { - 1 + 1 == 2 -} -``` - -To run all tests, simply do: - -```sh -aiken check -``` - -To run only tests matching the string `foo`, do: - -```sh -aiken check -m foo -``` - -## Documentation - -If you're writing a library, you might want to generate an HTML documentation for it. - -Use: - -```sh -aiken docs -``` - -## Resources - -Find more on the [Aiken's user manual](https://aiken-lang.org). +This will watch the project directory and restart as necessary. diff --git a/examples/one_shot/aiken.toml b/examples/one_shot/aiken.toml index c7316808..e75d33c9 100644 --- a/examples/one_shot/aiken.toml +++ b/examples/one_shot/aiken.toml @@ -1,11 +1,11 @@ name = 'aiken-lang/one_shot' version = '0.0.0' license = 'Apache-2.0' -description = "Aiken contracts for project 'aiken-lang/mint_lock_burn'" +description = "One shot parameterized minting and burning of tokens" [repository] user = 'aiken-lang' -project = 'mint_lock_burn' +project = 'one_shot' platform = 'github' [[dependencies]] diff --git a/examples/one_shot/blueprint.ts b/examples/one_shot/blueprint.ts new file mode 100644 index 00000000..1d4dd692 --- /dev/null +++ b/examples/one_shot/blueprint.ts @@ -0,0 +1,28 @@ +export type Preamble = { + title: string; + description?: string; + version: string; + plutusVersion: string; + license?: string; +}; + +export type Argument = { + title?: string; + description?: string; + // schema: Record; +}; + +export type Validator = { + title: string; + description?: string; + datum?: Argument; + redeemer: Argument; + parameters?: Argument[]; + compiledCode: string; + hash: string; +}; + +export type Blueprint = { + preamble: Preamble; + validators: Validator[]; +}; diff --git a/examples/one_shot/components/Button.tsx b/examples/one_shot/components/Button.tsx new file mode 100644 index 00000000..909d3807 --- /dev/null +++ b/examples/one_shot/components/Button.tsx @@ -0,0 +1,12 @@ +import { JSX } from "preact"; +import { IS_BROWSER } from "$fresh/runtime.ts"; + +export function Button(props: JSX.HTMLAttributes) { + return ( + + + + ); +} diff --git a/examples/one_shot/main.ts b/examples/one_shot/main.ts new file mode 100644 index 00000000..bb00964d --- /dev/null +++ b/examples/one_shot/main.ts @@ -0,0 +1,13 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import manifest from "./fresh.gen.ts"; + +import twindPlugin from "$fresh/plugins/twind.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 3c652cda..6708c2f4 100644 --- a/examples/one_shot/plutus.json +++ b/examples/one_shot/plutus.json @@ -1,7 +1,7 @@ { "preamble": { "title": "aiken-lang/one_shot", - "description": "Aiken contracts for project 'aiken-lang/mint_lock_burn'", + "description": "One shot parameterized minting and burning of tokens", "version": "0.0.0", "plutusVersion": "v2", "license": "Apache-2.0" diff --git a/examples/one_shot/routes/[name].tsx b/examples/one_shot/routes/[name].tsx new file mode 100644 index 00000000..9c068270 --- /dev/null +++ b/examples/one_shot/routes/[name].tsx @@ -0,0 +1,5 @@ +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 new file mode 100644 index 00000000..a3f4243d --- /dev/null +++ b/examples/one_shot/routes/api/joke.ts @@ -0,0 +1,21 @@ +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 new file mode 100644 index 00000000..42e62689 --- /dev/null +++ b/examples/one_shot/routes/index.tsx @@ -0,0 +1,24 @@ +import { Head } from "$fresh/runtime.ts"; +import Counter from "~/islands/Counter.tsx"; + +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. +

+ +
+ + ); +} diff --git a/examples/one_shot/static/favicon.ico b/examples/one_shot/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..1cfaaa2193b0f210107a559f7421569f57a25388 GIT binary patch literal 22382 zcmeI4dw7?{mB%N97z7oqA|OH{6p11r>cU#lM7K(nW`t#!NG z`qUAy{#t>9K|!BwH6TqGo5?%XehL;`0&-}m=Ue0llhcL@pl$8VmT z%zK+TmpOCh%*>geb9pY`9euPTFLpO|c5Z}ouDCdHKbPk-c~(}IxG%ZDxr=%@SHd^E zqD103nR9%XEERoVu3rrLu0HUY|1MgG%1x{{_pcwC`)FSxKQUHUyl&n5r0WaUnLDS_ zO1@EJ-yc$bGez?bM z=RUI!pyBE&vtsb~Nlt_6nbdbp$ix3y;iH@E#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%oiClr@+E;3V$3T2m5Zafg2!6iTF zIGBzUQb1p*pOI_LtBQe3(2Gg*k!O&{n?NPk8+o=J*a_&jGwOi9!}nZdC%#XN)RWO# ze@F6{P2KX%qO?b@U%1Iz6ft&<#639s)CxM&8D($iiPS z`4rnXm5kiNe6McZI7{TiY+rES)A(%zQnxTa()hgt(qXnS$U7Oofk4We!fz);a7v(y&DRt~7zy75O|tmn&+X8hls8Z!IVlSy`CR4)Ri4 z8s>?LhlK=}8ow<`Dm8wnA;=RIjN=zlbx%G+IRXhdGgifPzmOU3B69BS4)IC8#<@<) bck@HGWY%2idMme??%p8ZW3z(%VE+9-Ofn0d literal 0 HcmV?d00001 diff --git a/examples/one_shot/static/logo.svg b/examples/one_shot/static/logo.svg new file mode 100644 index 00000000..ef2fbe4c --- /dev/null +++ b/examples/one_shot/static/logo.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/examples/one_shot/twind.config.ts b/examples/one_shot/twind.config.ts new file mode 100644 index 00000000..2a7ac27d --- /dev/null +++ b/examples/one_shot/twind.config.ts @@ -0,0 +1,5 @@ +import { Options } from "$fresh/plugins/twind.ts"; + +export default { + selfURL: import.meta.url, +} as Options; diff --git a/examples/one_shot/utils.ts b/examples/one_shot/utils.ts new file mode 100644 index 00000000..475ccccf --- /dev/null +++ b/examples/one_shot/utils.ts @@ -0,0 +1,62 @@ +import { + applyParamsToScript, + Data, + fromHex, + MintingPolicy, + SpendingValidator, + toHex, +} from "lucid"; +import * as cbor from "cbor"; + +import { Blueprint } from "~/blueprint.ts"; + +export type Validators = { + lock: SpendingValidator; + mint: MintingPolicy; +}; + +async function readValidators(): Promise { + const blueprint: Blueprint = JSON + .parse(await Deno.readTextFile("plutus.json")); + + const lock = blueprint.validators.find((v) => v.title === "main.lock"); + + if (!lock) { + throw new Error("Lock validator not found"); + } + + const mint = blueprint.validators.find((v) => v.title === "main.mint"); + + if (!mint) { + throw new Error("Mint validator not found"); + } + + return { + lock: { + type: "PlutusV2", + script: toHex(cbor.encode(fromHex(lock.compiledCode))), + }, + mint: { + type: "PlutusV2", + script: toHex(cbor.encode(fromHex(mint.compiledCode))), + }, + }; +} + +export async function applyParams( + // bytes + tokenName: string, + // bytes + policyId: string, + // aiken/transaction.{OutputReference} + outputReference: any, +): Promise<{ lock: string; mint: string }> { + const validators = await readValidators(); + + return { + // TODO: apply tokenName and policyId + lock: applyParamsToScript(validators.lock.script, []), + // TODO: apply tokenName and outputReference + mint: applyParamsToScript(validators.mint.script, []), + }; +}