zkApp programmability is not yet available on the Mina Mainnet, but zkApps can now be deployed on 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.
This tutorial reuses the Square
contract that you created in Tutorial 1: Hello World.
Prerequisites
This tutorial has been tested with:
Ensure your environment meets the Prerequisites for zkApp Developer Tutorials.
If you have earlier versions of the zkApp CLI and o1js installed, be sure to Update the zkApp CLI to the latest version:
npm update -g zkapp-cli
Create a project
Create or change to a directory where you have write privileges.
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, selectnone
:? Create an accompanying UI project too? …
next
svelte
nuxt
empty
❯ noneThe 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 mainThe
zk project
command creates the03-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.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.
Delete the default generated files:
$ rm src/Add.ts
$ rm src/Add.test.ts
$ rm src/interact.tsNow, create a new file for your smart contract:
$ zk file src/Square
Copy the
src/Square.ts
andsrc/index.ts
files from the example files for first tutorial 01-hello-world/src to your local03-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.
zkApp CLI
You installed the zkApp CLI as part of the Prerequisites, so you already 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 contains the configuration to deploy your zkApp. This 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 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 that 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.
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.Target network:
testnet
Mina GraphQL API URL:
https://proxy.berkeley.minaexplorer.com/graphql
Transaction fee to use when deploying (in MINA):
0.1
When prompted to choose an account to pay transaction fees, select:
Use a different account (select to see options)
If this is the first time you are running the
zk config
command, you see these options:❯ Recover fee payer account from an existing base58 private key
Create a new fee payer key pairA third option to choose another saved fee payer account is shown only if you have a cached fee payer account.
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.A fee payer account is a developer account that can always pay fees immediately for local testing. Do not use an account that holds a substantial amount of MINA.
When prompted to create an alias for this account, give an alias to your new fee payer key pair:
testing-feepayer
Your key pairs and deploy alias are created.
When prompted, give an alias to your new fee payer key pair. For this tutorial, use
03-deploy
:Create an alias for this account: berkeley
Your key pairs and deploy alias are created:
✔ Create fee payer key pair at ${HOME}/.cache/zkapp-cli/keys/03-deploy.json
✔ Create zkApp key pair at keys/berkeley.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`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).
To deploy your project:
$ zk deploy
At the interactive prompt, select the
berkeley
deploy alias:? Which deploy alias would you like to deploy to? …
❯ berkeleyA 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 transactionReview and confirm the details of the transaction:
✖ Confirm to send transaction
┌─────────────────┬──────────────────────────────────────────────────┐
│ Deploy Alias │ berkeley │
├─────────────────┼──────────────────────────────────────────────────┤
│ Fee-Payer Alias │ testing-feepayer │
├─────────────────┼──────────────────────────────────────────────────┤
│ URL │ https://proxy.berkeley.minaexplorer.com/graphql │
├─────────────────┼──────────────────────────────────────────────────┤
│ Smart Contract │ Square │
└─────────────────┴──────────────────────────────────────────────────┘When prompted, type
yes
to confirm and send the transaction to the network.✔ 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:To see the zkApp transaction, go to
https://minascan.io/berkeley/tx/<txn-hash>
, where<txn-hash>
is the transaction hash that is output to your terminal.- The zkApp Pending Transaction (Transaction Type
zkApp Tx
) shows until the transaction is included in the next block. - The zkApp Transaction shows after the transaction is included in a block.
- The zkApp Pending Transaction (Transaction Type
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
${HOME}/.cache/zkapp-cli/keys/03-deploy.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.