Skip to content

Commit 03a2410

Browse files
authored
refactor: aztec new and init creating 2 crates (#20681)
2 parents d647094 + 5f9bee9 commit 03a2410

29 files changed

Lines changed: 421 additions & 230 deletions

File tree

aztec-up/test/counter_contract.sh

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,37 @@ export LOG_LEVEL=silent
55

66
# Execute commands as per: https://docs.aztec.network/tutorials/codealong/contract_tutorials/counter_contract
77
aztec new counter_contract
8-
if [ ! -f counter_contract/Nargo.toml ] || [ ! -f counter_contract/src/main.nr ]; then
9-
echo "Failed to create contract."
8+
9+
# Verify workspace structure
10+
if [ ! -f counter_contract/Nargo.toml ]; then
11+
echo "Failed to create workspace Nargo.toml."
12+
exit 1
13+
fi
14+
if [ ! -f counter_contract/contract/Nargo.toml ] || [ ! -f counter_contract/contract/src/main.nr ]; then
15+
echo "Failed to create contract crate."
16+
exit 1
17+
fi
18+
if [ ! -f counter_contract/test/Nargo.toml ] || [ ! -f counter_contract/test/src/lib.nr ]; then
19+
echo "Failed to create test crate."
1020
exit 1
1121
fi
1222

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

19-
# "Write" our contract.
20-
cp -Rf ./aztec-packages/noir-projects/noir-contracts/contracts/test/counter_contract .
29+
# "Write" our contract over the scaffold.
30+
cp -Rf ./aztec-packages/noir-projects/noir-contracts/contracts/test/counter_contract/* counter_contract/
2131
cd counter_contract
22-
sed -i 's|\.\./\.\./\.\./\.\./|/home/ubuntu/aztec-packages/noir-projects/|g' Nargo.toml
32+
sed -i 's|\.\./\.\./\.\./\.\./\.\./|/home/ubuntu/aztec-packages/noir-projects/|g' contract/Nargo.toml test/Nargo.toml
2333

2434
# Compile the contract.
2535
aztec compile
2636
# Codegen
27-
aztec codegen -o src/artifacts target
28-
if [ ! -d src/artifacts ]; then
37+
aztec codegen -o contract/src/artifacts target
38+
if [ ! -d contract/src/artifacts ]; then
2939
echo "Failed to codegen TypeScript."
3040
exit 1
3141
fi

boxes/init/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target/
2+
codegenCache.json

boxes/init/Nargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
[package]
2-
name = "init"
3-
type = "contract"
4-
5-
[dependencies]
6-
aztec = { path = "../../noir-projects/aztec-nr/aztec" }
1+
[workspace]
2+
members = ["contract", "test"]

boxes/init/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# init
2+
3+
An Aztec Noir contract project.
4+
5+
## Compile
6+
7+
```bash
8+
aztec compile
9+
```
10+
11+
This compiles the contract in `contract/` and outputs artifacts to `target/`.
12+
13+
## Test
14+
15+
```bash
16+
aztec test
17+
```
18+
19+
This runs the tests in `test/`.
20+
21+
## Generate TypeScript bindings
22+
23+
```bash
24+
aztec codegen target -o src/artifacts
25+
```
26+
27+
This generates TypeScript contract artifacts from the compiled output in `target/` into `src/artifacts/`.

boxes/init/contract/Nargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "init"
3+
type = "contract"
4+
5+
[dependencies]
6+
aztec = { path = "../../../noir-projects/aztec-nr/aztec" }

boxes/init/contract/src/main.nr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use aztec::macros::aztec;
2+
3+
#[aztec]
4+
pub contract Main {
5+
use aztec::macros::functions::{external, initializer};
6+
7+
#[initializer]
8+
#[external("private")]
9+
fn constructor() {}
10+
}

boxes/init/src/main.nr

Lines changed: 0 additions & 9 deletions
This file was deleted.

boxes/init/test/Nargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "init_test"
3+
type = "lib"
4+
5+
[dependencies]
6+
aztec = { path = "../../../noir-projects/aztec-nr/aztec" }
7+
init = { path = "../contract" }

boxes/init/test/src/lib.nr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use aztec::test::helpers::test_environment::TestEnvironment;
2+
use init::Main;
3+
4+
#[test]
5+
unconstrained fn test_constructor() {
6+
let mut env = TestEnvironment::new();
7+
let deployer = env.create_light_account();
8+
9+
// Deploy the contract with the default constructor:
10+
let contract_address = env.deploy("@init/Main").with_private_initializer(
11+
deployer,
12+
Main::interface().constructor(),
13+
);
14+
15+
// Deploy without an initializer:
16+
let contract_address = env.deploy("@init/Main").without_initializer();
17+
}

docs/docs-developers/docs/aztec-nr/framework-description/contract_structure.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ High-level structure of how Aztec smart contracts including the different compon
1010

1111
## Directory structure
1212

13-
Here's a common layout for a basic Aztec.nr Contract project:
13+
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.
1414

1515
```text title="layout of an aztec contract project"
1616
─── my_aztec_contract_project
17-
├── src
18-
│ └── main.nr <-- your contract
19-
└── Nargo.toml <-- package and dependency management
17+
├── Nargo.toml <-- workspace root
18+
├── contract
19+
│ ├── src
20+
│ │ └── main.nr <-- your contract
21+
│ └── Nargo.toml <-- contract package and dependencies
22+
└── test
23+
├── src
24+
│ └── lib.nr <-- your tests
25+
└── Nargo.toml <-- test package and dependencies
2026
```
2127

28+
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.
29+
2230
See the vanilla Noir docs for [more info on packages](https://noir-lang.org/docs/noir/modules_packages_crates/crates_and_packages).
2331

2432
## Contract block

0 commit comments

Comments
 (0)