Skip to content

Commit

Permalink
Add minting sample
Browse files Browse the repository at this point in the history
  • Loading branch information
iburzynski committed Jan 28, 2024
1 parent 25e4a1d commit b08f3ee
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions validators/samples/minting.ak
Original file line number Diff line number Diff line change
@@ -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<Input>) {
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)?
}

0 comments on commit b08f3ee

Please sign in to comment.