Cardano Academy
Search…
⌃K

Minting Native tokens

Make sure you have done steps in previous chapter.

Generate protocol parameters

Generate protocol parameters and store it in ./protocol-parameters file:
cardano-cli query protocol-parameters \
--testnet-magic $TESTNET_MAGIC \
--out-file ./protocol-parameters

Generate key hash

Generate key hash and store it in ./key-hash file:
cardano-cli address key-hash \
--payment-verification-key-file ./verification-key \
> ./key-hash
Print your key hash to console:
cat ./key-hash

Create minting policy

Create minting policy file:
touch minting-policy
Add following lines to minting-policy file, where your_key_hash is the key hash generated above:
{
"keyHash": "your_key_hash",
"type": "sig"
}
Generate policy ID, and store it in ./minting-policy-id file:
cardano-cli transaction policyid \
--script-file ./minting-policy \
> ./minting-policy-id

Define your token name and amount

We are gonna define our token name and the amount we want to mint, please replace your_token_name and your_token_amount to your token name and minting amount.
export TOKEN_NAME=your_token_name
export TOKEN_AMOUNT=your_token_mount

Build transaction

Run this command to query the utxo in your address:
cardano-cli query utxo \
--address $(< ./address) \
--testnet-magic $TESTNET_MAGIC
You should get something like this:
TxHash TxIx Amount
--------------------------------------------------------------------------------------
1c7e8dd3bd6e463099e99bb104020435fcf5fe83579b7d9c3fb6b836e5b47c3a 0 1000000000 lovelace
we need to store these values in the environment variables so we can use it later, please replace the_tx_hash , the_tx_ix and the_amount to the TxHash, TxIx and Amount printed on your console:
export TX_HASH=the_tx_hash
export TX_IX=the_tx_ix
export TX_AMOUNT=the_amount
Build raw transaction:
cardano-cli transaction build-raw \
--fee 0 \
--tx-in $TX_HASH#$TX_IX \
--tx-out $(< ./address)+$TX_AMOUNT+"$TOKEN_AMOUNT $(< ./minting-policy-id).$TOKEN_NAME" \
--mint="$TOKEN_AMOUNT $(< ./minting-policy-id).$TOKEN_NAME" \
--out-file tx.raw
Calculate the minimum fee for this transaction:
cardano-cli transaction calculate-min-fee \
--tx-body-file tx.raw \
--tx-in-count 1 \
--tx-out-count 1 \
--witness-count 1 \
--testnet-magic $TESTNET_MAGIC \
--protocol-params-file ./protocol-parameters
For me, I got the output 174785 Lovelace, I'm gonna set the TX_FEE to 174785, you need to set it to the integer value printed on your console:
export TX_FEE=the_tx_fee
Now rebuild the transaction with fee:
cardano-cli transaction build-raw \
--fee $TX_FEE \
--tx-in $TX_HASH#$TX_IX \
--tx-out $(< ./address)+$(($TX_AMOUNT - $TX_FEE))+"$TOKEN_AMOUNT $(< ./minting-policy-id).$TOKEN_NAME" \
--mint="$TOKEN_AMOUNT $(< ./minting-policy-id).$TOKEN_NAME" \
--out-file tx.raw

Sign and submit the transaction

Sign this transaction:
cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file ./signing-key \
--script-file ./minting-policy \
--testnet-magic $TESTNET_MAGIC \
--out-file tx.signed
Submit the transaction:
cardano-cli transaction submit \
--tx-file tx.signed \
--testnet-magic $TESTNET_MAGIC
Now, recheck funds in our address:
cardano-cli query utxo \
--address $(< ./address) \
--testnet-magic $TESTNET_MAGIC
The output I got:
TxHash TxIx Amount
--------------------------------------------------------------------------------------
856f84d765b8c4705392a3f04908e67f243344d9d6a25536c6df1443f37c323e 0 999825215 lovelace + 1000000000000 ab2a37c3b78d602a5c0dfc58cbfe9ab481e3325278e9999d7e78e326.ALVINSTOKEN
Great! Now we minted our own token! 😀🎉🎉🎉