Mint Assets
Create a Minting Policy
First, we need to create a minting policy for the assets we want to mint. In this example, we'll use a native script time-locking policy with our wallet as the required signer:
const address = await lucid.wallet().address();
const mintingPolicy = scriptFromNative({
type: "all",
scripts: [
{ type: "sig", keyHash: paymentCredentialOf(address).hash },
{
type: "before",
slot: unixTimeToSlot(lucid.config().network, Date.now() + 1000000),
},
],
});
Next we derive the policy id from the minting policy script:
const policyID = mintingPolicyToId(mintingPolicy);
Mint Assets
Now we can mint our desired tokens:
const tx = await lucid
.newTx()
.mintAssets({
[policyId + fromText("MyToken")]: 1n,
[policyId + fromText("MyOtherToken")]: 1n,
})
.pay.ToAddress(address, { [policyId + fromText("MyToken")]: 1n })
.validTo(Date.now() + 900000)
.attach.MintingPolicy(mintingPolicy)
.complete();
const signed = await tx.sign.withWallet().complete();
const txHash = await signed.submit();
âšī¸
When working with script parameters (involving nested structures or custom data types):
const scriptWithParams = applyParamsToScript(
yourCompiledScript,
[param1, param2, ...]
);
- Minting tokens creates them, but it doesn't automatically assign them to any address. After minting, the tokens are technically "owned" by the transaction itself.
- Purpose of
pay.ToAddress()
is to send the newly minted tokens to a specific address
Burn
To burn tokens, we use a similar process to minting, but with negative values for the assets we want to burn:
const tx = await lucid
.newTx()
.mintAssets({
[policyId + fromText("MyToken")]: -1n,
[policyId + fromText("MyOtherToken")]: -1n,
})
.validTo(Date.now() + 900000)
.attach.MintingPolicy(mintingPolicy)
.complete();
const signed = await tx.sign.withWallet().complete();
const txHash = await signed.submit();
đĄ
- All assets minted in a single
mintAssets
call should be of the same policy id. You can chain multiplemintAssets
calls if you need to mint assets with different policy ids - The minting policy must be attached to the transaction using
attach.MintingPolicy
- Lucid Evolution supports
Native
,PlutusV1 / V2 / V3
minting policies. The appropriate script type will be used based on the policy you attach. - When using Plutus scripts (V1 / V2 / V3), make sure to provide an appropriate redeemer.
- The
validTo
field is important for time-locked minting policies to ensure the transaction is submitted within the valid time range