diff --git a/examples/one_shot/aiken.toml b/examples/one_shot/aiken.toml
index e75d33c9..1d19f44b 100644
--- a/examples/one_shot/aiken.toml
+++ b/examples/one_shot/aiken.toml
@@ -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]]
diff --git a/examples/one_shot/islands/App.tsx b/examples/one_shot/islands/App.tsx
index 46fc4e05..be127c7d 100644
--- a/examples/one_shot/islands/App.tsx
+++ b/examples/one_shot/islands/App.tsx
@@ -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 && (
<>
-
Lock
+ Redeem
- {parameterizedContracts.lock.script}
+ {parameterizedContracts.redeem.script}
- Mint
+ Gift Card
- {parameterizedContracts.mint.script}
+ {parameterizedContracts.giftCard.script}
@@ -256,12 +264,12 @@ export default function App({ validators }: AppProps) {
{lockTxHash && (
@@ -277,7 +285,7 @@ export default function App({ validators }: AppProps) {
diff --git a/examples/one_shot/utils.ts b/examples/one_shot/utils.ts
index 0d68f0eb..c374cf91 100644
--- a/examples/one_shot/utils.ts
+++ b/examples/one_shot/utils.ts
@@ -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 {
- 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,
};
diff --git a/examples/one_shot/validators/main.ak b/examples/one_shot/validators/main.ak
index 7dea01b2..1b6f7f62 100644
--- a/examples/one_shot/validators/main.ak
+++ b/examples/one_shot/validators/main.ak
@@ -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