From b08f3ee08c31d8e568e6f23934e3201b01705fde Mon Sep 17 00:00:00 2001 From: Ian Burzynski <23251244+iburzynski@users.noreply.github.com> Date: Sun, 28 Jan 2024 05:58:57 -0500 Subject: [PATCH] Add minting sample --- validators/samples/minting.ak | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 validators/samples/minting.ak diff --git a/validators/samples/minting.ak b/validators/samples/minting.ak new file mode 100644 index 0000000..9b9fd32 --- /dev/null +++ b/validators/samples/minting.ak @@ -0,0 +1,42 @@ +use aiken/dict as dict +use aiken/list as list +use aiken/transaction.{Input, OutputReference, ScriptContext, Transaction} as tx +use aiken/transaction/value.{AssetName, MintedValue, PolicyId} + +validator { + fn free_minting(_redeemer: Void, _ctx: ScriptContext) -> Bool { + True + } +} + +type Mode { + Minting + Burning +} + +validator(token_name: ByteArray, oref: OutputReference) { + fn nft(redeemer: Mode, ctx: ScriptContext) -> Bool { + expect tx.Mint(policy_id) = ctx.purpose + + let Transaction { inputs, mint, .. } = ctx.transaction + + when redeemer is { + Minting -> + has_utxo(oref, inputs)? && check_mint(policy_id, token_name, mint, 1)? + Burning -> check_mint(policy_id, token_name, mint, -1)? + } + } +} + +fn has_utxo(oref: OutputReference, inputs: List) { + list.any(inputs, fn(input) { input.output_reference == oref }) +} + +fn check_mint(cs: PolicyId, tn: AssetName, mint: MintedValue, mint_qty: Int) { + expect [(asset_name, qty)] = + mint + |> value.from_minted_value + |> value.tokens(cs) + |> dict.to_list() + (qty == mint_qty)? && (asset_name == tn)? +}