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'
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]
user = 'aiken-lang'
project = 'one_shot'
project = 'gift_card'
platform = 'github'
[[dependencies]]

View File

@ -95,102 +95,110 @@ export default function App({ validators }: AppProps) {
setParameterizedContracts(contracts);
};
const submitADAGift = async (e: Event) => {
const createGiftCard = async (e: Event) => {
e.preventDefault();
setWaitingLockTx(true);
const lovelace = Number(giftADA) * 1000000;
try {
const lovelace = Number(giftADA) * 1000000;
const assetName = `${parameterizedContracts!.policyId}${
fromText(tokenName)
}`;
const assetName = `${parameterizedContracts!.policyId}${
fromText(tokenName)
}`;
// Action::Mint
const mintRedeemer = Data.to(new Constr(0, []));
// Action::Mint
const mintRedeemer = Data.to(new Constr(0, []));
const utxos = await lucid?.wallet.getUtxos()!;
const utxo = utxos[0];
const utxos = await lucid?.wallet.getUtxos()!;
const utxo = utxos[0];
const tx = await lucid!
.newTx()
.collectFrom([utxo])
.attachMintingPolicy(parameterizedContracts!.mint)
.mintAssets(
{ [assetName]: BigInt(1) },
mintRedeemer,
)
.payToContract(
parameterizedContracts!.lockAddress,
{ inline: Data.void() },
{ "lovelace": BigInt(lovelace) },
)
.complete();
const tx = await lucid!
.newTx()
.collectFrom([utxo])
.attachMintingPolicy(parameterizedContracts!.giftCard)
.mintAssets(
{ [assetName]: BigInt(1) },
mintRedeemer,
)
.payToContract(
parameterizedContracts!.lockAddress,
{ inline: Data.void() },
{ "lovelace": BigInt(lovelace) },
)
.complete();
const txSigned = await tx.sign().complete();
const txSigned = await tx.sign().complete();
const txHash = await txSigned.submit();
const txHash = await txSigned.submit();
const success = await lucid!.awaitTx(txHash);
const success = await lucid!.awaitTx(txHash);
// Wait a little bit longer so ExhuastedUTxOError doesn't happen
// in the next Tx
setTimeout(() => {
// Wait a little bit longer so ExhuastedUTxOError doesn't happen
// in the next Tx
setTimeout(() => {
setWaitingLockTx(false);
if (success) {
localStorage.setItem(
"cache",
JSON.stringify({
tokenName,
giftADA,
parameterizedValidators: parameterizedContracts,
lockTxHash: txHash,
}),
);
setLockTxHash(txHash);
}
}, 3000);
} catch {
setWaitingLockTx(false);
if (success) {
localStorage.setItem(
"cache",
JSON.stringify({
tokenName,
giftADA,
parameterizedValidators: parameterizedContracts,
lockTxHash: txHash,
}),
);
setLockTxHash(txHash);
}
}, 3000);
}
};
const submitRedeemADAGift = async (e: Event) => {
const redeemGiftCard = async (e: Event) => {
e.preventDefault();
setWaitingUnlockTx(true);
const utxos = await lucid!.utxosAt(parameterizedContracts!.lockAddress);
try {
const utxos = await lucid!.utxosAt(parameterizedContracts!.lockAddress);
const assetName = `${parameterizedContracts!.policyId}${
fromText(tokenName)
}`;
const assetName = `${parameterizedContracts!.policyId}${
fromText(tokenName)
}`;
// Action::Burn
const burnRedeemer = Data.to(new Constr(1, []));
// Action::Burn
const burnRedeemer = Data.to(new Constr(1, []));
const tx = await lucid!
.newTx()
.collectFrom(utxos, Data.void())
.attachMintingPolicy(parameterizedContracts!.mint)
.attachSpendingValidator(parameterizedContracts!.lock)
.mintAssets(
{ [assetName]: BigInt(-1) },
burnRedeemer,
)
.complete();
const tx = await lucid!
.newTx()
.collectFrom(utxos, Data.void())
.attachMintingPolicy(parameterizedContracts!.giftCard)
.attachSpendingValidator(parameterizedContracts!.redeem)
.mintAssets(
{ [assetName]: BigInt(-1) },
burnRedeemer,
)
.complete();
const txSigned = await tx.sign().complete();
const txSigned = await tx.sign().complete();
const txHash = await txSigned.submit();
const txHash = await txSigned.submit();
const success = await lucid!.awaitTx(txHash);
const success = await lucid!.awaitTx(txHash);
setWaitingUnlockTx(false);
setWaitingUnlockTx(false);
if (success) {
localStorage.removeItem("cache");
if (success) {
localStorage.removeItem("cache");
setUnlockTxHash(txHash);
setUnlockTxHash(txHash);
}
} catch {
setWaitingUnlockTx(false);
}
};
@ -234,14 +242,14 @@ export default function App({ validators }: AppProps) {
)}
{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">
{parameterizedContracts.lock.script}
{parameterizedContracts.redeem.script}
</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">
{parameterizedContracts.mint.script}
{parameterizedContracts.giftCard.script}
</pre>
<div class="mt-10 grid grid-cols-1 gap-y-8">
@ -256,12 +264,12 @@ export default function App({ validators }: AppProps) {
</Input>
<Button
onClick={submitADAGift}
onClick={createGiftCard}
disabled={waitingLockTx || !!lockTxHash}
>
{waitingLockTx
? "Waiting for Tx..."
: "Send Gift Card (Locks ADA)"}
: "Create Gift Card (Locks ADA)"}
</Button>
{lockTxHash && (
@ -277,7 +285,7 @@ export default function App({ validators }: AppProps) {
</a>
<Button
onClick={submitRedeemADAGift}
onClick={redeemGiftCard}
disabled={waitingLockTx || !!unlockTxHash}
>
{waitingUnlockTx

View File

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

View File

@ -10,8 +10,8 @@ interface Data {
}
export const handler: Handlers<Data> = {
async GET(_req, ctx) {
const validators = await readValidators();
GET(_req, ctx) {
const validators = readValidators();
return ctx.render({ validators });
},
@ -32,14 +32,14 @@ export default function Home({ data }: PageProps<Data>) {
Make a one shot minting and lock contract
</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">
{validators.lock.script}
{validators.redeem.script}
</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">
{validators.mint.script}
{validators.giftCard.script}
</pre>
</div>

View File

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

View File

@ -8,7 +8,7 @@ type Action {
Burn
}
validator mint(token_name: ByteArray, utxo_ref: OutputReference) {
validator gift_card(token_name: ByteArray, utxo_ref: OutputReference) {
fn(rdmr: Action, ctx: ScriptContext) -> Bool {
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 {
let ScriptContext { transaction, .. } = ctx