Mint assets

Mint and burn native tokens.

Note: You need to have a wallet and a provider selected in order to build and submit transactions.

Mint

First we need to create a minting policy for the assets we want to mint. In this example we utilize a native script time-locking policy with our wallet as required signer:

import { Addresses } from "https://deno.land/x/lucid/mod.ts";

const { payment } = Addresses.inspect(
  await lucid.wallet.address(),
);

const mintingPolicy = lucid.newScript(
  {
    type: "All",
    scripts: [
      { type: "Sig", keyHash: paymentCredential.hash },
      {
        type: "Before",
        slot: lucid.utils.unixTimeToSlots(Date.now() + 1000000),
      },
    ],
  },
);

Next we derive the policy id from the minting policy script:

const policyId = mintingPolicy.toHash();

Now we can mint our desired tokens:

const unit = policyId + fromText("MyMintedToken");

const tx = await lucid.newTx()
  .mint({ [unit]: 1n })
  .validTo(Date.now() + 200000)
  .attachScript(mintingPolicy)
  .commit();

const signedTx = await tx.sign().commit();

const txHash = await signedTx.submit();

Burn

const unit = policyId + fromText("MyMintedToken");

const tx = await lucid
  .newTx()
  .mint({ [unit]: -1n })
  .validTo(Date.now() + 200000)
  .attachScript(mintingPolicy)
  .commit();

const signedTx = await tx.sign().commit();

const txHash = await signedTx.submit();