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 00000000..1cfaaa21 Binary files /dev/null and b/examples/one_shot/static/favicon.ico differ 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, []), + }; +}