Cardano Academy
Search…
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:
1
cardano-cli query protocol-parameters \
2
--testnet-magic $TESTNET_MAGIC \
3
--out-file ./protocol-parameters
Copied!

Generate key hash

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

Create minting policy

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

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.
1
export TOKEN_NAME=your_token_name
2
export TOKEN_AMOUNT=your_token_mount
Copied!

Build transaction

Run this command to query the utxo in your address:
1
cardano-cli query utxo \
2
--address $(< ./address) \
3
--testnet-magic $TESTNET_MAGIC
Copied!
You should get something like this:
1
TxHash TxIx Amount
2
--------------------------------------------------------------------------------------
3
1c7e8dd3bd6e463099e99bb104020435fcf5fe83579b7d9c3fb6b836e5b47c3a 0 1000000000 lovelace
Copied!
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:
1
export TX_HASH=the_tx_hash
2
export TX_IX=the_tx_ix
3
export TX_AMOUNT=the_amount
Copied!
Build raw transaction:
1
cardano-cli transaction build-raw \
2
--fee 0 \
3
--tx-in $TX_HASH#$TX_IX \
4
--tx-out $(< ./address)+$TX_AMOUNT+"$TOKEN_AMOUNT $(< ./minting-policy-id).$TOKEN_NAME" \
5
--mint="$TOKEN_AMOUNT $(< ./minting-policy-id).$TOKEN_NAME" \
6
--out-file tx.raw
Copied!
Calculate the minimum fee for this transaction:
1
cardano-cli transaction calculate-min-fee \
2
--tx-body-file tx.raw \
3
--tx-in-count 1 \
4
--tx-out-count 1 \
5
--witness-count 1 \
6
--testnet-magic $TESTNET_MAGIC \
7
--protocol-params-file ./protocol-parameters
Copied!
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:
1
export TX_FEE=the_tx_fee
Copied!
Now rebuild the transaction with fee:
1
cardano-cli transaction build-raw \
2
--fee $TX_FEE \
3
--tx-in $TX_HASH#$TX_IX \
4
--tx-out $(< ./address)+$(($TX_AMOUNT - $TX_FEE))+"$TOKEN_AMOUNT $(< ./minting-policy-id).$TOKEN_NAME" \
5
--mint="$TOKEN_AMOUNT $(< ./minting-policy-id).$TOKEN_NAME" \
6
--out-file tx.raw
Copied!

Sign and submit the transaction

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