chore: better names for the validators and some functions

This commit is contained in:
rvcas 2023-03-11 22:44:49 -05:00 committed by Lucas
parent 5e062c130d
commit a522cd1e7d
6 changed files with 124 additions and 115 deletions

View File

@ -1,11 +1,11 @@
name = 'aiken-lang/one_shot' name = 'aiken-lang/gift_card'
version = '0.0.0' version = '0.0.0'
license = 'Apache-2.0' license = 'Apache-2.0'
description = "One shot parameterized minting and burning of tokens" description = "Create a gift card that can be used to redeem locked assets"
[repository] [repository]
user = 'aiken-lang' user = 'aiken-lang'
project = 'one_shot' project = 'gift_card'
platform = 'github' platform = 'github'
[[dependencies]] [[dependencies]]

View File

@ -95,11 +95,12 @@ export default function App({ validators }: AppProps) {
setParameterizedContracts(contracts); setParameterizedContracts(contracts);
}; };
const submitADAGift = async (e: Event) => { const createGiftCard = async (e: Event) => {
e.preventDefault(); e.preventDefault();
setWaitingLockTx(true); setWaitingLockTx(true);
try {
const lovelace = Number(giftADA) * 1000000; const lovelace = Number(giftADA) * 1000000;
const assetName = `${parameterizedContracts!.policyId}${ const assetName = `${parameterizedContracts!.policyId}${
@ -115,7 +116,7 @@ export default function App({ validators }: AppProps) {
const tx = await lucid! const tx = await lucid!
.newTx() .newTx()
.collectFrom([utxo]) .collectFrom([utxo])
.attachMintingPolicy(parameterizedContracts!.mint) .attachMintingPolicy(parameterizedContracts!.giftCard)
.mintAssets( .mintAssets(
{ [assetName]: BigInt(1) }, { [assetName]: BigInt(1) },
mintRedeemer, mintRedeemer,
@ -152,13 +153,17 @@ export default function App({ validators }: AppProps) {
setLockTxHash(txHash); setLockTxHash(txHash);
} }
}, 3000); }, 3000);
} catch {
setWaitingLockTx(false);
}
}; };
const submitRedeemADAGift = async (e: Event) => { const redeemGiftCard = async (e: Event) => {
e.preventDefault(); e.preventDefault();
setWaitingUnlockTx(true); setWaitingUnlockTx(true);
try {
const utxos = await lucid!.utxosAt(parameterizedContracts!.lockAddress); const utxos = await lucid!.utxosAt(parameterizedContracts!.lockAddress);
const assetName = `${parameterizedContracts!.policyId}${ const assetName = `${parameterizedContracts!.policyId}${
@ -171,8 +176,8 @@ export default function App({ validators }: AppProps) {
const tx = await lucid! const tx = await lucid!
.newTx() .newTx()
.collectFrom(utxos, Data.void()) .collectFrom(utxos, Data.void())
.attachMintingPolicy(parameterizedContracts!.mint) .attachMintingPolicy(parameterizedContracts!.giftCard)
.attachSpendingValidator(parameterizedContracts!.lock) .attachSpendingValidator(parameterizedContracts!.redeem)
.mintAssets( .mintAssets(
{ [assetName]: BigInt(-1) }, { [assetName]: BigInt(-1) },
burnRedeemer, burnRedeemer,
@ -192,6 +197,9 @@ export default function App({ validators }: AppProps) {
setUnlockTxHash(txHash); setUnlockTxHash(txHash);
} }
} catch {
setWaitingUnlockTx(false);
}
}; };
return ( return (
@ -234,14 +242,14 @@ export default function App({ validators }: AppProps) {
)} )}
{lucid && parameterizedContracts && ( {lucid && parameterizedContracts && (
<> <>
<h3 class="mt-4 mb-2">Lock</h3> <h3 class="mt-4 mb-2">Redeem</h3>
<pre class="bg-gray-200 p-2 rounded overflow-x-scroll"> <pre class="bg-gray-200 p-2 rounded overflow-x-scroll">
{parameterizedContracts.lock.script} {parameterizedContracts.redeem.script}
</pre> </pre>
<h3 class="mt-4 mb-2">Mint</h3> <h3 class="mt-4 mb-2">Gift Card</h3>
<pre class="bg-gray-200 p-2 rounded overflow-x-scroll"> <pre class="bg-gray-200 p-2 rounded overflow-x-scroll">
{parameterizedContracts.mint.script} {parameterizedContracts.giftCard.script}
</pre> </pre>
<div class="mt-10 grid grid-cols-1 gap-y-8"> <div class="mt-10 grid grid-cols-1 gap-y-8">
@ -256,12 +264,12 @@ export default function App({ validators }: AppProps) {
</Input> </Input>
<Button <Button
onClick={submitADAGift} onClick={createGiftCard}
disabled={waitingLockTx || !!lockTxHash} disabled={waitingLockTx || !!lockTxHash}
> >
{waitingLockTx {waitingLockTx
? "Waiting for Tx..." ? "Waiting for Tx..."
: "Send Gift Card (Locks ADA)"} : "Create Gift Card (Locks ADA)"}
</Button> </Button>
{lockTxHash && ( {lockTxHash && (
@ -277,7 +285,7 @@ export default function App({ validators }: AppProps) {
</a> </a>
<Button <Button
onClick={submitRedeemADAGift} onClick={redeemGiftCard}
disabled={waitingLockTx || !!unlockTxHash} disabled={waitingLockTx || !!unlockTxHash}
> >
{waitingUnlockTx {waitingUnlockTx

View File

@ -1,14 +1,14 @@
{ {
"preamble": { "preamble": {
"title": "aiken-lang/one_shot", "title": "aiken-lang/gift_card",
"description": "One shot parameterized minting and burning of tokens", "description": "Create a gift card that can be used to redeem locked assets",
"version": "0.0.0", "version": "0.0.0",
"plutusVersion": "v2", "plutusVersion": "v2",
"license": "Apache-2.0" "license": "Apache-2.0"
}, },
"validators": [ "validators": [
{ {
"title": "main.mint", "title": "main.gift_card",
"redeemer": { "redeemer": {
"title": "Action", "title": "Action",
"schema": { "schema": {
@ -76,7 +76,7 @@
"hash": "9185bf20c9bbdd7062d9b2dc11a03290bfb256938395d56855baee81" "hash": "9185bf20c9bbdd7062d9b2dc11a03290bfb256938395d56855baee81"
}, },
{ {
"title": "main.lock", "title": "main.redeem",
"datum": { "datum": {
"title": "Unit", "title": "Unit",
"description": "The nullary constructor.", "description": "The nullary constructor.",

View File

@ -10,8 +10,8 @@ interface Data {
} }
export const handler: Handlers<Data> = { export const handler: Handlers<Data> = {
async GET(_req, ctx) { GET(_req, ctx) {
const validators = await readValidators(); const validators = readValidators();
return ctx.render({ validators }); return ctx.render({ validators });
}, },
@ -32,14 +32,14 @@ export default function Home({ data }: PageProps<Data>) {
Make a one shot minting and lock contract Make a one shot minting and lock contract
</h2> </h2>
<h3 class="mt-4 mb-2">Lock</h3> <h3 class="mt-4 mb-2">Redeem</h3>
<pre class="bg-gray-200 p-2 rounded overflow-x-scroll"> <pre class="bg-gray-200 p-2 rounded overflow-x-scroll">
{validators.lock.script} {validators.redeem.script}
</pre> </pre>
<h3 class="mt-4 mb-2">Mint</h3> <h3 class="mt-4 mb-2">Gift Card</h3>
<pre class="bg-gray-200 p-2 rounded overflow-x-scroll"> <pre class="bg-gray-200 p-2 rounded overflow-x-scroll">
{validators.mint.script} {validators.giftCard.script}
</pre> </pre>
</div> </div>

View File

@ -10,10 +10,11 @@ import {
} from "~/vendor/lucid@0.9.4/mod.ts"; } from "~/vendor/lucid@0.9.4/mod.ts";
import { Blueprint } from "~/blueprint.ts"; import { Blueprint } from "~/blueprint.ts";
import blueprint from "~/plutus.json" assert { type: "json" };
export type Validators = { export type Validators = {
lock: SpendingValidator; redeem: SpendingValidator;
mint: MintingPolicy; giftCard: MintingPolicy;
}; };
export type LocalCache = { export type LocalCache = {
@ -23,38 +24,38 @@ export type LocalCache = {
parameterizedValidators: AppliedValidators; parameterizedValidators: AppliedValidators;
}; };
export async function readValidators(): Promise<Validators> { export function readValidators(): Validators {
const blueprint: Blueprint = JSON.parse( const redeem = (blueprint as Blueprint).validators.find((v) =>
await Deno.readTextFile("plutus.json"), v.title === "main.redeem"
); );
const lock = blueprint.validators.find((v) => v.title === "main.lock"); if (!redeem) {
throw new Error("Redeem validator not found");
if (!lock) {
throw new Error("Lock validator not found");
} }
const mint = blueprint.validators.find((v) => v.title === "main.mint"); const giftCard = (blueprint as Blueprint).validators.find((v) =>
v.title === "main.gift_card"
);
if (!mint) { if (!giftCard) {
throw new Error("Mint validator not found"); throw new Error("Gift Card validator not found");
} }
return { return {
lock: { redeem: {
type: "PlutusV2", type: "PlutusV2",
script: lock.compiledCode, script: redeem.compiledCode,
}, },
mint: { giftCard: {
type: "PlutusV2", type: "PlutusV2",
script: mint.compiledCode, script: giftCard.compiledCode,
}, },
}; };
} }
export type AppliedValidators = { export type AppliedValidators = {
lock: SpendingValidator; redeem: SpendingValidator;
mint: MintingPolicy; giftCard: MintingPolicy;
policyId: string; policyId: string;
lockAddress: string; lockAddress: string;
}; };
@ -70,29 +71,29 @@ export function applyParams(
BigInt(outputReference.outputIndex), BigInt(outputReference.outputIndex),
]); ]);
const mint = applyParamsToScript(validators.mint.script, [ const giftCard = applyParamsToScript(validators.giftCard.script, [
fromText(tokenName), fromText(tokenName),
outRef, outRef,
]); ]);
const policyId = lucid.utils.validatorToScriptHash({ const policyId = lucid.utils.validatorToScriptHash({
type: "PlutusV2", type: "PlutusV2",
script: mint, script: giftCard,
}); });
const lock = applyParamsToScript(validators.lock.script, [ const redeem = applyParamsToScript(validators.redeem.script, [
fromText(tokenName), fromText(tokenName),
policyId, policyId,
]); ]);
const lockAddress = lucid.utils.validatorToAddress({ const lockAddress = lucid.utils.validatorToAddress({
type: "PlutusV2", type: "PlutusV2",
script: lock, script: redeem,
}); });
return { return {
lock: { type: "PlutusV2", script: applyDoubleCborEncoding(lock) }, redeem: { type: "PlutusV2", script: applyDoubleCborEncoding(redeem) },
mint: { type: "PlutusV2", script: applyDoubleCborEncoding(mint) }, giftCard: { type: "PlutusV2", script: applyDoubleCborEncoding(giftCard) },
policyId, policyId,
lockAddress, lockAddress,
}; };

View File

@ -8,7 +8,7 @@ type Action {
Burn Burn
} }
validator mint(token_name: ByteArray, utxo_ref: OutputReference) { validator gift_card(token_name: ByteArray, utxo_ref: OutputReference) {
fn(rdmr: Action, ctx: ScriptContext) -> Bool { fn(rdmr: Action, ctx: ScriptContext) -> Bool {
let ScriptContext { transaction, purpose } = ctx let ScriptContext { transaction, purpose } = ctx
@ -32,7 +32,7 @@ validator mint(token_name: ByteArray, utxo_ref: OutputReference) {
} }
} }
validator lock(token_name: ByteArray, policy_id: ByteArray) { validator redeem(token_name: ByteArray, policy_id: ByteArray) {
fn(_d: Void, _r: Void, ctx: ScriptContext) -> Bool { fn(_d: Void, _r: Void, ctx: ScriptContext) -> Bool {
let ScriptContext { transaction, .. } = ctx let ScriptContext { transaction, .. } = ctx