Skip to main content
info

zkApp programmability is not yet available on the Mina Mainnet. You can get started now by deploying zkApps to the Berkeley Testnet.

Tutorial 3: Deploy to a Live Network

In previous tutorials, you learned how to deploy and run transactions on a local network.

In this tutorial, you use the zk config command to create a deploy alias, request tMINA funds to pay for transaction fees, and deploy a project to a live network.

Mina zkApps are available only on feature-complete Berkeley, Mina's public Testnet. This tutorial reuses the Square contract that you created in Tutorial 1: Hello World.

Prerequisites

This tutorial has been tested with Mina zkApp CLI version 0.10.0 and SnarkyJS 0.11.3.

Ensure your environment meets the Prerequisites for zkApp Developer Tutorials.

Create a project

  1. Create or change to a directory where you have write privileges.

  2. Create a project by using the zk project command:

    $ zk project 03-deploying-to-a-live-network

    The zk project command has the ability to scaffold the UI for your project. For this tutorial, select none:

    ? Create an accompanying UI project too? …
    next
    svelte
    nuxt
    empty
    ❯ none

    The expected output is:

    ✔ Create an accompanying UI project too? · none
    ✔ UI: Set up project
    ✔ Initialize Git repo
    ✔ Set up project
    ✔ NPM install
    ✔ NPM build contract
    ✔ Set project name
    ✔ Git init commit

    Success!

    Next steps:
    cd 03-deploying-to-a-live-network
    git remote add origin <your-repo-url>
    git push -u origin main

    The zk project command creates the 03-deploying-to-a-live-network directory that contains the scaffolding for your project, including tools such as the Prettier code formatting tool, the ESLint static code analysis tool, and the Jest JavaScript testing framework.

  3. Change into the 03-deploying-to-a-live-network directory.

For this tutorial, you run commands from the root of the 03-deploying-to-a-live-network directory as you work in the src directory on files that contain the TypeScript code for the smart contract.

Each time you make updates, then build or deploy, the TypeScript code is compiled into JavaScript in the build directory.

Prepare the project

Start by deleting the default files that come with the new project.

  1. Delete the default generated files:

    $ rm src/Add.ts
    $ rm src/Add.test.ts
    $ rm src/interact.ts
  2. Now, create a new file for your smart contract:

    $ zk file src/Square
  3. Copy the src/Square.ts and src/index.ts files from the example files for first tutorial 01-hello-world/src to your local 03-deploying-to-a-live-network/src directory. If prompted, replace existing files.

Now that your smart contract is in place, you are ready to deploy your smart contract to Berkeley Testnet.

Mina zkApp CLI

You already installed the Mina zkApp CLI as part of the Prerequisites, so you have the tools to manage deployments.

In some cases, you might need to create a custom account for your zkApp, for example, to deploy a zkApp to a different key than the fee payer key, programmatically parameterize a zkApp before you initialize it, or create a smart contract programmatically for users as part of an application. For details, see Interacting with zkApps server-side.

Deploy the smart contract

The config.json configuration file was automatically scaffolded when you created your project with the zk project command. However, the generated configuration file does not yet contain the deploy alias.

Deploy alias

The zk config command prompts guide you to create or update a deploy alias in your project config.json file.

You can have one or more deploy aliases for your project.

A deploy alias consists of:

  • A self-describing name (can be anything). Using naming patterns is helpful when you have more than one deploy alias.

  • The Mina GraphQL API URL that defines the network that receives your deploy transaction and broadcasts it to the appropriate Mina network (Testnet, Devnet, Mainnet, and so on)

  • The transaction fee (in MINA) to use when deploying

  • Two key pairs:

    • A key pair for the zkApp account. Public and private keys to use in your application are automatically generated in keys/berkeley.json.

    • A key pair to use as a fee payer account for updates and deployments. Public and private keys are stored on your local computer and can be used across multiple projects.

  • Fee payer account alias

    • A fee payer account is required, you can choose to use an existing account or create a new fee payer account.
  1. To configure your deployment, run the zk config command and respond to the prompts:

    $ zk config

    For this tutorial on Berkeley Testnet, use:

    • Deploy alias name: berkeley

      This tutorial uses berkeley, but the deploy alias name can be anything and does not have to match the network name.

    • Mina GraphQL API URL: https://proxy.berkeley.minaexplorer.com/graphql

    • Transaction fee to use when deploying: 0.1

    • Account to pay transaction fees: Create a new fee payer pair

  1. When prompted to choose an account to pay transaction feeds, select to use a different account:

    Use a different account (select to see options)
  2. Next, select to create a new fee payer key pair:

    Create a new fee payer key pair
    NOTE: the private key will be stored in plain text on this computer.
  3. When prompted, give an alias to your new fee payer key pair. For this tutorial, use 03-deploy:

    Create an alias for this account: 03-deploy

    Your key pairs and deploy alias are created:

    ✔ Create fee payer key pair at /Users/<username>/.cache/zkapp-cli/keys/03-deploy.json
    ✔ Create zkApp key pair at keys/berkeley2.json
    ✔ Add deploy alias to config.json

    Success!

    Next steps:
    - If this is a testnet, request tMINA at:
    https://faucet.minaprotocol.com/?address=B62qqK5JgYAtmh2DHsQfUjUSKwQ6CFSPkGvyMZd19j1BUHfEJEqpKGo&?explorer=minaexplorer
    - To deploy, run: `zk deploy berkeley`

  4. Request funds from the Testnet Faucet to fund your fee payer account.

    Follow the prompts to request tMINA. For this tutorial, your MINA address is populated on the Testnet Faucet. tMINA arrives at your address when the next block is produced (~3 minutes).

  5. To deploy your project:

    $ zk deploy
  6. At the interactive prompt, select the berkeley deploy alias:

    ? Which deploy alias would you like to deploy to? …
    ❯ berkeley

    A verification key for your smart contract is generated (takes 10-30 seconds).

    The deploy process is output:

    ✔ Build project
    ✔ Generate build.json
    ✔ Choose smart contract
    Only one smart contract exists in the project: Square
    Your config.json was updated to always use this
    smart contract when deploying to this deploy alias.
    ✔ Generate verification key (takes 10-30 sec)
    ⠋ Build transaction...(node:25066) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
    (Use `node --trace-warnings ...` to show where the warning was created)
    ✔ Build transaction
  7. Review and confirm the details of the transaction:

    ✖ Confirm to send transaction

    ┌─────────────────┬─────────────────────────────────────────────────┐
    │ Deploy Alias │ berkeley2 │
    ├─────────────────┼─────────────────────────────────────────────────┤
    │ Fee-Payer Alias │ 03-deploy │
    ├─────────────────┼─────────────────────────────────────────────────┤
    │ URL │ https://proxy.berkeley.minaexplorer.com/graphql │
    ├─────────────────┼─────────────────────────────────────────────────┤
    │ Smart Contract │ Square │
    └─────────────────┴─────────────────────────────────────────────────┘

    When prompted, type yes to confirm and send the transaction.

    ✔ Send to network

    Success! Deploy transaction sent.

    Next step:
    Your smart contract will be live (or updated)
    as soon as the transaction is included in a block:
  8. To see the zkApp transaction, go to https://berkeley.minaexplorer.com/transaction/<txn-hash>, where <txn-hash> is the transaction hash that is output to your terminal.

    • The zkApp Pending Transaction shows until the transaction is included in the next block.
    • The zkApp Transaction shows after the transaction is included in a block.

Success

After the transaction is included in a block, your smart contract is deployed!

  • The Mina account at this public key now contains the verification key associated with this smart contract.

You ran the zk config command to:

  • Create a deploy alias
  • Create a fee payer key pair at /Users/<username>/.cache/zkapp-cli/keys/03deploy.json
  • Create a zkApp key pair at keys/berkeley.json

You requested tMINA to fund your deploy transactions. Use the remaining tMINA to keep building and testing.

You ran the zk deploy command to:

  • Generate a verification key for your smart contract
  • Add the deploy alias to the project config.json

Congratulations!

About the Smart Contract Transactions

Because this tutorial used the smart contract for Tutorial 1: Hello World, the smart contract editState permissions require that a transaction must contain a valid zk proof that was created by the private key associated with this zkApp account.

  • When a user interacts with this smart contract by providing a proof, the proof is generated locally on the user's device and included in a transaction.
  • When the transaction is submitted to the network, the proof is checked to ensure it is correct and matches the on-chain verification key.
  • After the transaction is accepted, the proof and transaction are recursively proved and bundled into Mina's recursive zero knowledge proof.

When you change the smart contract code, the associated verification key also changes. Use the same steps to redeploy the smart contract.

For a typical smart contract, permissions are set to allow only proof authorization: the proof in zero knowledge proof. You learn more about setting permissions in a later tutorial.

Video

Watch this tutorial for a step-by-step guide and extra explanations on how to deploy a zkApp.

The video is provided for educational purposes and uses earlier versions of the zkApp CLI and o1js, so there are some differences. This tutorial is tested with a specific version of the zkApp CLI and o1js.

Conclusion

Congratulations! You have successfully deployed a smart contract to a live network.

Check out Tutorial 4: Build a zkApp UI in the Browser with React to implement a browser UI that interacts with a smart contract.