Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions aztec-up/test/counter_contract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,37 @@ export LOG_LEVEL=silent

# Execute commands as per: https://docs.aztec.network/tutorials/codealong/contract_tutorials/counter_contract
aztec new counter_contract
if [ ! -f counter_contract/Nargo.toml ] || [ ! -f counter_contract/src/main.nr ]; then
echo "Failed to create contract."

# Verify workspace structure
if [ ! -f counter_contract/Nargo.toml ]; then
echo "Failed to create workspace Nargo.toml."
exit 1
fi
if [ ! -f counter_contract/contract/Nargo.toml ] || [ ! -f counter_contract/contract/src/main.nr ]; then
echo "Failed to create contract crate."
exit 1
fi
if [ ! -f counter_contract/test/Nargo.toml ] || [ ! -f counter_contract/test/src/lib.nr ]; then
echo "Failed to create test crate."
exit 1
fi

# Check counter_contract dir is owned by aztec-dev.
# Check counter_contract dir is owned by ubuntu.
if [ "$(stat -c %U counter_contract)" != "ubuntu" ]; then
echo "counter_contract dir is not owned by ubuntu."
exit 1
fi

# "Write" our contract.
cp -Rf ./aztec-packages/noir-projects/noir-contracts/contracts/test/counter_contract .
# "Write" our contract over the scaffold.
cp -Rf ./aztec-packages/noir-projects/noir-contracts/contracts/test/counter_contract/* counter_contract/
cd counter_contract
sed -i 's|\.\./\.\./\.\./\.\./|/home/ubuntu/aztec-packages/noir-projects/|g' Nargo.toml
sed -i 's|\.\./\.\./\.\./\.\./\.\./|/home/ubuntu/aztec-packages/noir-projects/|g' contract/Nargo.toml test/Nargo.toml

# Compile the contract.
aztec compile
# Codegen
aztec codegen -o src/artifacts target
if [ ! -d src/artifacts ]; then
aztec codegen -o contract/src/artifacts target
if [ ! -d contract/src/artifacts ]; then
echo "Failed to codegen TypeScript."
exit 1
fi
Expand Down
2 changes: 2 additions & 0 deletions boxes/init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
codegenCache.json
8 changes: 2 additions & 6 deletions boxes/init/Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
[package]
name = "init"
type = "contract"

[dependencies]
aztec = { path = "../../noir-projects/aztec-nr/aztec" }
[workspace]
members = ["contract", "test"]
27 changes: 27 additions & 0 deletions boxes/init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# init

An Aztec Noir contract project.

## Compile

```bash
aztec compile
```

This compiles the contract in `contract/` and outputs artifacts to `target/`.

## Test

```bash
aztec test
```

This runs the tests in `test/`.

## Generate TypeScript bindings

```bash
aztec codegen target -o src/artifacts
```

This generates TypeScript contract artifacts from the compiled output in `target/` into `src/artifacts/`.
6 changes: 6 additions & 0 deletions boxes/init/contract/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "init"
type = "contract"

[dependencies]
aztec = { path = "../../../noir-projects/aztec-nr/aztec" }
10 changes: 10 additions & 0 deletions boxes/init/contract/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use aztec::macros::aztec;

#[aztec]
pub contract Main {
use aztec::macros::functions::{external, initializer};

#[initializer]
#[external("private")]
fn constructor() {}
}
9 changes: 0 additions & 9 deletions boxes/init/src/main.nr

This file was deleted.

7 changes: 7 additions & 0 deletions boxes/init/test/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "init_test"
type = "lib"

[dependencies]
aztec = { path = "../../../noir-projects/aztec-nr/aztec" }
init = { path = "../contract" }
17 changes: 17 additions & 0 deletions boxes/init/test/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use aztec::test::helpers::test_environment::TestEnvironment;
use init::Main;

#[test]
unconstrained fn test_constructor() {
let mut env = TestEnvironment::new();
let deployer = env.create_light_account();

// Deploy the contract with the default constructor:
let contract_address = env.deploy("@init/Main").with_private_initializer(
deployer,
Main::interface().constructor(),
);

// Deploy without an initializer:
let contract_address = env.deploy("@init/Main").without_initializer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ High-level structure of how Aztec smart contracts including the different compon

## Directory structure

Here's a common layout for a basic Aztec.nr Contract project:
When you create a new project with `aztec new`, it generates a workspace with two crates: a `contract` crate for your smart contract and a `test` crate for Noir tests.

```text title="layout of an aztec contract project"
─── my_aztec_contract_project
├── src
│ └── main.nr <-- your contract
└── Nargo.toml <-- package and dependency management
├── Nargo.toml <-- workspace root
├── contract
│ ├── src
│ │ └── main.nr <-- your contract
│ └── Nargo.toml <-- contract package and dependencies
└── test
├── src
│ └── lib.nr <-- your tests
└── Nargo.toml <-- test package and dependencies
```

The workspace root `Nargo.toml` declares both crates as workspace members. The contract code lives in `contract/src/main.nr`, and tests live in a separate `test` crate that depends on the contract crate.

See the vanilla Noir docs for [more info on packages](https://noir-lang.org/docs/noir/modules_packages_crates/crates_and_packages).

## Contract block
Expand Down
7 changes: 3 additions & 4 deletions docs/docs-developers/docs/aztec-nr/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ storage.votes.insert(new_vote).deliver(vote_counter); // the vote counter accoun

### 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
1. Write your contract and specify your contract dependencies. Create a new project with `aztec new my_project`, which sets up a workspace with a `contract` crate and a `test` crate, with the `aztec` dependency already configured. If you need additional dependencies, add them to `contract/Nargo.toml`:

```toml
# Nargo.toml
# contract/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.
Update your `contract/src/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

Expand Down
13 changes: 7 additions & 6 deletions docs/docs-developers/docs/aztec-nr/testing_contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ Always use `aztec test` instead of `nargo test`. The `TestEnvironment` requires

## Basic test structure

When you create a project with `aztec new` or `aztec init`, a separate `test` crate is created alongside the `contract` crate. Tests live in `test/src/lib.nr` and import the contract crate by name (not `crate::`):

```rust
use crate::MyContract;
use my_contract::MyContract;
use aztec::{
protocol::address::AztecAddress,
test::helpers::test_environment::TestEnvironment,
Expand All @@ -72,12 +74,11 @@ unconstrained fn test_basic_flow() {
:::

:::tip Organizing test files
You can organize tests in separate files:
Tests live in the separate `test` crate that `aztec new` creates. You can organize them into modules:

- Create `src/test.nr` with `mod utils;` to import helper functions
- Split tests into modules like `src/test/transfer_tests.nr`, `src/test/auth_tests.nr`
- Import the test module in `src/main.nr` with `mod test;`
- Share setup functions in `src/test/utils.nr`
- Split tests into modules like `test/src/transfer_tests.nr`, `test/src/auth_tests.nr`
- Import them in `test/src/lib.nr` with `mod transfer_tests;`, `mod auth_tests;`
- Share setup functions in `test/src/utils.nr`
:::

## Deploying contracts
Expand Down
26 changes: 26 additions & 0 deletions docs/docs-developers/docs/resources/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ Aztec is in active development. Each version may introduce breaking changes that

## TBD

### `aztec new` and `aztec init` now create a 2-crate workspace

`aztec new` and `aztec init` now create a workspace with two crates instead of a single contract crate:

- A `contract` crate (type = "contract") for your smart contract code
- A `test` crate (type = "lib") for Noir tests, which depends on the contract crate

The new project structure looks like:

```
my_project/
├── Nargo.toml # [workspace] members = ["contract", "test"]
├── contract/
│ ├── src/main.nr
│ └── Nargo.toml # type = "contract"
└── test/
├── src/lib.nr
└── Nargo.toml # type = "lib"
```

**What changed:**
- The `--contract` and `--lib` flags have been removed from `aztec new` and `aztec init`. These commands now always create a contract workspace.
- Contract code is now at `contract/src/main.nr` instead of `src/main.nr`.
- The `Nargo.toml` in the project root is now a workspace file. Contract dependencies go in `contract/Nargo.toml`.
- Tests should be written in the separate `test` crate (`test/src/lib.nr`) and import the contract by package name (e.g., `use my_contract::MyContract;`) instead of using `crate::`.

### Scope enforcement for private state access (TXE and PXE)

Scope enforcement is now active across both TXE (test environment) and PXE (client). Previously, private execution could implicitly access any account's keys and notes. Now, only the caller (`from`) address is in scope by default, and accessing another address's private state requires explicitly granting scope.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ This tutorial is compatible with the Aztec version `#include_aztec_version`. Ins
Run this to create a new contract project:

```bash
aztec new --contract counter
aztec new counter
```

Your structure should look like this:

```tree
.
|-counter
| |-src
| | |-main.nr
| |-Nargo.toml
| |-Nargo.toml <-- workspace root
| |-contract
| | |-src
| | | |-main.nr
| | |-Nargo.toml <-- contract package config
| |-test
| | |-src
| | | |-lib.nr
| | |-Nargo.toml <-- test package config
```

The file `main.nr` will soon turn into our smart contract!
The `aztec new` command creates a workspace with two crates: a `contract` crate for your smart contract code and a `test` crate for Noir tests. The file `contract/src/main.nr` will soon turn into our smart contract!

Add the following dependencies to `Nargo.toml` under the autogenerated content:
Add the following dependency to `contract/Nargo.toml` under the existing `aztec` dependency:

```toml
[dependencies]
Expand All @@ -47,7 +53,7 @@ balance_set = { git="https://github.com/AztecProtocol/aztec-nr/", tag="#include_

## Define the functions

Go to `main.nr`, and replace the boilerplate code with this contract initialization:
Go to `contract/src/main.nr`, and replace the boilerplate code with this contract initialization:

```rust
#include_code setup /docs/examples/contracts/counter_contract/src/main.nr raw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,30 @@ The contract demonstrates several important patterns:

### Create the Contract Project

Use `aztec init` to generate the contract project structure:
Use `aztec new` to generate the contract project structure:

```bash
aztec init --contract contract
aztec new contract --name ValueNotEqual
```

This creates:
This creates a workspace with two crates:

```tree
contract/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: using contract as root and having a contract inside might confusing. Might make sense to rename the root as something else

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think root is better and I can't think of anything else so leaving it as is. Also main came to my mind but I don't think it's less confusing than just having it be a contract. If you come up with a cute name I am open to renaming.

├── src/
│ └── main.nr # Contract code
└── Nargo.toml # Contract configuration
├── Nargo.toml # Workspace root
├── contract/
│ ├── src/
│ │ └── main.nr # Contract code
│ └── Nargo.toml # Contract configuration
└── test/
├── src/
│ └── lib.nr # Test code
└── Nargo.toml # Test configuration
```

### Contract Configuration

Update `contract/Nargo.toml` with the required dependencies:
Update `contract/contract/Nargo.toml` with the required dependencies:

```toml
[package]
Expand All @@ -240,15 +246,15 @@ aztec = { git = "https://github.com/AztecProtocol/aztec-nr/", tag = "#include_az
bb_proof_verification = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "#include_aztec_version", directory = "barretenberg/noir/bb_proof_verification" }
```

**Key differences from the circuit's Nargo.toml**:
**Key differences from the circuit's Nargo.toml** (in `contract/contract/Nargo.toml`):

- `type = "contract"` (not `"bin"`)
- Depends on `aztec` for Aztec-specific features
- Depends on `bb_proof_verification` for `verify_honk_proof`

### Contract Structure

Replace the contents of `contract/src/main.nr` with:
Replace the contents of `contract/contract/src/main.nr` with:

#include_code full_contract /docs/examples/contracts/recursive_verification_contract/src/main.nr rust

Expand Down Expand Up @@ -380,7 +386,7 @@ Create the following files in your project root directory.
"name": "recursive-verification-tutorial",
"type": "module",
"scripts": {
"ccc": "cd contract && aztec compile && aztec codegen target -o artifacts",
"ccc": "cd contract && aztec compile && aztec codegen target -o contract/artifacts",
"data": "tsx scripts/generate_data.ts",
"recursion": "tsx scripts/run_recursion.ts"
},
Expand Down Expand Up @@ -451,7 +457,7 @@ yarn ccc
This generates:

- `contract/target/ValueNotEqual.json` - Contract artifact (bytecode, ABI, etc.)
- `contract/artifacts/ValueNotEqual.ts` - TypeScript class for deploying and interacting with the contract
- `contract/contract/artifacts/ValueNotEqual.ts` - TypeScript class for deploying and interacting with the contract

### Proof Generation Script

Expand Down Expand Up @@ -584,7 +590,7 @@ import { SponsoredFeePaymentMethod } from "@aztec/aztec.js/fee";
import type { FieldLike } from "@aztec/aztec.js/abi";
import { getSponsoredFPCInstance } from "./sponsored_fpc.ts";
import { SponsoredFPCContract } from "@aztec/noir-contracts.js/SponsoredFPC";
import { ValueNotEqualContract } from "../contract/artifacts/ValueNotEqual";
import { ValueNotEqualContract } from "../contract/contract/artifacts/ValueNotEqual";
import data from "../data.json";
import { EmbeddedWallet } from "@aztec/wallets/embedded";
import { AztecAddress } from "@aztec/aztec.js/addresses";
Expand Down
Loading
Loading