Skip to main content
Version: Devnet (v3.0.0-devnet.6-patch.1)

Overview

Aztec.nr is a Noir framework used to develop and test Aztec smart contracts. It contains both high-level abstractions (state variables, messages) and low-level protocol primitives, providing granular control to developers if they want custom contracts.

tip

If you are already familiar with writing Aztec smart contracts and Aztec.nr, visit the API reference.

Motivation

Noir can be used to write circuits, but Aztec contracts are more complex than this. They include multiple external functions, each of a different type: circuits for private functions, AVM bytecode for public functions, and brillig bytecode for utility functions. The circuits for private functions are also need to interact with the protocol's kernel circuits in specific ways, so manually writing them, and then combining everything into a contract artifact is involved work. Aztec.nr takes care of all of this heavy lifting and makes writing contracts as simple as marking functions with the corresponding attributes e.g. #[external(private)].

It allows safe and easy implementation of well understood design patterns, such as the multiple kinds of private state variables, meaning developers don't need to understand the low-levels of how the protocol works. These features are optional, however, advanced developers are not prevented from building their own custom solutions.

Design principles

  • Make it hard to shoot yourself in the foot by making it clear when something is unsafe.
  • Dangerous actions should be easy to spot. e.g. ignoring return values or calling functions with the _unsafe prefix.
  • This is achieved by having rails that intentionally trigger a developer's "WTF?" response, to ensure they understand what they're doing.

A good example of this is writing to private state variables. These functions return a NoteMessage struct, which results in a compiler error unless used. This is because writing to private state also requires sending an encrypted message with the new state to the people that need to access it - otherwise, because it is private, they will not even know the state changed.

storage.votes.insert(new_vote); // compiler error - unused NoteMessagePendingDelivery return value
storage.votes.insert(new_vote).deliver(vote_counter); // the vote counter account will now be notified of the new vote

Contract Development

Prerequisites

Flow

  1. Write your contract and specify your contract dependencies. Every contract written for Aztec will have aztec-nr as a dependency. Add it to your Nargo.toml with
# Nargo.toml
[dependencies]
aztec = { git="https://github.com/AztecProtocol/aztec-nr/", tag="#include_aztec_version", directory="aztec" }

Update your main.nr contract file to use the Aztec.nr macros for writing contracts.

#include_code setup /docs/examples/contracts/counter_contract/src/main.nr rust

and import dependencies from the Aztec.nr library.

#include_code imports /docs/examples/contracts/counter_contract/src/main.nr rust

info

You can see a complete example of a simple counter contract written with Aztec.nr here.

  1. Profile the private functions in your contract to get a sense of how long generating client side proofs will take
  2. Write unit tests directly in Noir and end-to-end tests with TypeScript
  3. Compile your contract
  4. Deploy your contract with Aztec.js

Section Contents