Skip to content

Instantly share code, notes, and snippets.

@ElGatoLoco
Created March 26, 2021 12:29
Show Gist options
  • Select an option

  • Save ElGatoLoco/2f25a7eb1d9de45d8c54abad6e1158fd to your computer and use it in GitHub Desktop.

Select an option

Save ElGatoLoco/2f25a7eb1d9de45d8c54abad6e1158fd to your computer and use it in GitHub Desktop.

Revisions

  1. ElGatoLoco created this gist Mar 26, 2021.
    141 changes: 141 additions & 0 deletions minting-native-tokens-on-cardano.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,141 @@
    # This guide assumes that you're running MacOS and already have a fully synced Daedalus testnet wallet
    # For mainnet some minor tweaks are required

    # The line below assumes the default location for the testnet wallet at the time of writing this guide
    # If your node socket is located somewhere else, you should be able to finding by inspecting the running processes on your machine
    export CARDANO_NODE_SOCKET_PATH=~/Library/Application\ Support/Daedalus\ Testnet/cardano-node.socket

    # Make testnet id available as environment variable; not needed for the mainnet
    # This one is active at the time of writing this guide
    export TESTNET_ID=1097911063

    # If we’re already using Daedalus’ node, why not use the cli as well? :)
    # This line is just to make an alias and have a simple command available globally
    # If you wish, you could put it into your bash profile, so it stays available for the future sessions
    alias cardano-cli="/Applications/Daedalus\ Testnet.app/Contents/MacOS/cardano-cli"

    # Save your token name and amount you want to mint as environment variables
    export TOKEN_NAME=NeonCat
    export TOKEN_AMOUNT=1

    # Check that everything is working so far
    # This command should return you a list of all transaction on the network
    cardano-cli query utxo \
    --testnet-magic $TESTNET_ID \
    --mary-era

    # Generate stake keys
    cardano-cli stake-address key-gen \
    --verification-key-file stake.vkey \
    --signing-key-file stake.skey

    # Generate payment keys
    cardano-cli address key-gen \
    --verification-key-file payment.vkey \
    --signing-key-file payment.skey

    # Use payment and stake verification keys to generate a payment address
    cardano-cli address build \
    --payment-verification-key-file payment.vkey \
    --stake-verification-key-file stake.vkey \
    --out-file payment.addr \
    --testnet-magic $TESTNET_ID

    # Request test funds to be sent to your address - obviously not working on the mainnet (:
    https://developers.cardano.org/en/testnets/cardano/tools/faucet/

    # Check address balance
    cardano-cli query utxo --address $(< payment.addr) \
    --testnet-magic $TESTNET_ID \
    --mary-era

    # Export the protocol parameters to a file for later use
    cardano-cli query protocol-parameters \
    --testnet-magic $TESTNET_ID \
    --mary-era \
    --out-file protocol.json

    # Create directory for policy files
    mkdir policy

    # Generate policy signing and verification keys
    cardano-cli address key-gen \
    --verification-key-file policy/policy.vkey \
    --signing-key-file policy/policy.skey

    # Create policy script file
    touch policy/policy.script && echo "" > policy/policy.script

    # Generate policy script and put it into the newly created file
    echo "{" >> policy/policy.script
    echo " \"keyHash\": \"$(cardano-cli address key-hash --payment-verification-key-file policy/policy.vkey)\"," >> policy/policy.script
    echo " \"type\": \"sig\"" >> policy/policy.script
    echo "}" >> policy/policy.script

    # Generate policy ID
    cardano-cli transaction policyid --script-file ./policy/policy.script >> policy/policyId

    # Use the command for checking the address balance (defined above) to see transaction hash and index and the lovelace amount you have available
    #
    # If you were successful in funding your address, it should return something like this:
    #
    # TxHash TxIx Amount
    # --------------------------------------------------------------------------------------
    # a8a166c59f8753a01f4e57d8aea2427b5c7de50147e058ba6c0f8e5bfb785094 0 1000000000 lovelace

    # Save this data as environment variables
    export TX_HASH=a8a166c59f8753a01f4e57d8aea2427b5c7de50147e058ba6c0f8e5bfb785094
    export TX_IX=0
    export AVAILABLE_LOVELACE=1000000000

    # Build raw transaction using this data
    cardano-cli transaction build-raw \
    --mary-era \
    --fee 0 \
    --tx-in $TX_HASH#$TX_IX \
    --tx-out $(< payment.addr)+$AVAILABLE_LOVELACE+"$TOKEN_AMOUNT $(< policy/policyId).$TOKEN_NAME" \
    --mint="$TOKEN_AMOUNT $(< policy/policyId).$TOKEN_NAME" \
    --out-file matx.raw

    # Calculate the minimum fee
    cardano-cli transaction calculate-min-fee \
    --tx-body-file matx.raw \
    --tx-in-count 1 \
    --tx-out-count 1 \
    --witness-count 1 \
    --testnet-magic $TESTNET_ID \
    --protocol-params-file protocol.json

    # Make your fee available as an environment variable
    export TX_FEE=174961 # Replace with value returned by running the previous command

    # Use the calculated fee to build the transaction with proper fee amount
    cardano-cli transaction build-raw \
    --mary-era \
    --fee $TX_FEE \
    --tx-in $TX_HASH#$TX_IX \
    --tx-out $(< payment.addr)+$(($AVAILABLE_LOVELACE - $TX_FEE))+"$TOKEN_AMOUNT $(< policy/policyId).$TOKEN_NAME" \
    --mint="$TOKEN_AMOUNT $(< policy/policyId).$TOKEN_NAME" \
    --out-file matx.raw

    # Sign the transaction
    cardano-cli transaction sign \
    --signing-key-file payment.skey \
    --signing-key-file policy/policy.skey \
    --script-file policy/policy.script \
    --testnet-magic $TESTNET_ID \
    --tx-body-file matx.raw \
    --out-file matx.signed

    # Submit the transaction
    cardano-cli transaction submit --tx-file matx.signed --testnet-magic $TESTNET_ID

    # If you don’t see anything in the terminal it means everything went smoothly
    # So, you should now be able to query your address and see your token there
    cardano-cli query utxo --address $(< payment.addr) \
    --testnet-magic $TESTNET_ID \
    --mary-era

    # If you don’t see it immediately, don’t panic, it might take a bit for the transaction to be confirmed

    # CONGRATS! YOU HAVE JUST MINTED YOUR FIRST TOKEN(S) ON THE CARDANO TESTNET