Please note that zkApp programmability is not yet available on Mina Mainnet, but zkApps can now be deployed to Berkeley Testnet.
How to Write a zkApp
A zkApp consists of a smart contract and a UI to interact with it. First, install the Mina zkApp CLI and write a smart contract.
Write a smart contract
Write your smart contract using the Mina zkApp CLI.
Mina zkApp CLI makes it easy to follow recommended best practices by providing project scaffolding including dependencies such as SnarkyJS, a test framework (Jest), code auto-formatting (Prettier), linting (ES Lint), & more.
Install Mina zkApp CLI
npm install -g zkapp-cli
Dependencies:
- NodeJS 16+ (or 14 using
node --experimental-wasm-threads
) - NPM 6+
- Git 2+
If you have an older version installed, install a newer version using the package manager for your system: Homebrew (Mac), Chocolatey (Windows), or apt/yum/etc (Linux). On Linux, you may need to install a recent NodeJS version via NodeSource (deb or rpm), as recommended by the NodeJS Project.
Start a project
Now that you have Mina zkApp CLI installed, you can start with an example or start your own project.
Option A: Start with an example (recommended)
Examples are based on the standard project structure, but with additional files in the /src
directory as the only difference.
- Install: Run
zk example sudoku
. This creates a new project and includes the example files (i.e. the smart contract) inside the project’ssrc/
directory. Typels
& hit enter to see the files that were created or open the directory in a code editor such as VS Code. - Run tests: Run
npm run test
. Tests are written using Jest. After running this command, you see all tests pass. You can also runnpm run testw
to run tests in watch mode, so tests are automatically re-run tests when you save changes to your code. - Build the example: Run
npm run build
to compile your TypeScript into JavaScript inside the project’s/build
directory. - Deploy to Testnet: Run
zk config
. The command walks you through adding a network alias to your project’sconfig.json
. For Berkeley Testnet, useberkeley
as the name,0.1
for the fee, andhttps://proxy.berkeley.minaexplorer.com/graphql
for the url. Then runzk deploy
and follow the prompts. See the how to deploy a zkApp page for further details.
You can view a list of all available examples here.
Option B: Start your own project
- Install: Run
zk project <myproj>
. Typels
and hit enter to see the newly created project structure. Note: If you want to directly scaffold an UI with your new zkApp, you can do so by adding the--ui
flag to the previous command, likezk project <myproj> --ui=<framework>
. You can currently choose from the following frameworks:svelte
,next
ornuxt
- Run tests: Run
npm run test
. Tests are written using Jest. After running this command, you can see that all tests pass. You can also runnpm run testw
to run tests in watch mode, so tests are automatically re-run you save changes to your code. - Build: Run
npm run build
to compile your TypeScript code into JavaScript inside the project’s/build
. - Deploy to Testnet: Run
zk config
. The command walks you through adding a network alias to your project’sconfig.json
. For Berkeley Testnet, useberkeley
as the name,0.1
for the fee, andhttps://proxy.berkeley.minaexplorer.com/graphql
for the url. Then runzk deploy
and follow the prompts. See the how to deploy a zkApp page for further details. - Deploy to Mainnet: (Coming soon.)
Writing your smart contract
zkApps are written in TypeScript using SnarkyJS. SnarkyJS is a TypeScript library for writing smart contracts based on zero-knowledge proofs for the Mina Protocol. It is included automatically when creating a new project using the Mina zkApp CLI.
See the SnarkyJS docs to learn more. The first 3 subsections are enough to get you started writing zkApps:
Start with Tutorial 1: Hello World, a hands-on walkthrough that teaches you to create your first zkApp.
For a comprehensive documentation of the SnarkyJS API, please see the SnarkyJS reference.
Next Steps
Now that you've learned how to write and operate a basic smart contract, you can learn how to test your zkApp.