Skip to content

Commit 1fcb964

Browse files
authored
chore: Rename generate-ts/nr commands to codegen. (AztecProtocol#3843)
* Get rid of generate-* commands on cli, in favour of `codegen` command with optional `--ts` or `--nr` flags. * Update docs to explain how to do a contract compilation now.
1 parent c489727 commit 1fcb964

File tree

10 files changed

+58
-103
lines changed

10 files changed

+58
-103
lines changed

aztec-up/bin/aztec-sandbox

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -euo pipefail
55
CMD="docker compose"
66
$CMD &>/dev/null || CMD="docker-compose"
77

8-
ARGS="-f ~/.aztec/docker-compose.yml -p sandbox"
8+
ARGS="-f $HOME/.aztec/docker-compose.yml -p sandbox"
99

1010
# Function to be executed when SIGINT is received.
1111
cleanup() {

boxes/blank-react/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"formatting": "prettier --check ./src && eslint ./src",
1313
"formatting:fix": "prettier -w ./src",
1414
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile",
15-
"codegen": "${AZTEC_CLI:-aztec-cli} generate-typescript src/contracts/target --outdir src/artifacts",
15+
"codegen": "${AZTEC_CLI:-aztec-cli} codegen src/contracts/target -o src/artifacts --ts",
1616
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --runInBand"
1717
},
1818
"jest": {

boxes/blank/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"formatting": "prettier --check ./src && eslint ./src",
1313
"formatting:fix": "prettier -w ./src",
1414
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile",
15-
"codegen": "${AZTEC_CLI:-aztec-cli} generate-typescript src/contracts/target --outdir src/artifacts",
15+
"codegen": "${AZTEC_CLI:-aztec-cli} codegen src/contracts/target -o src/artifacts --ts",
1616
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --runInBand"
1717
},
1818
"jest": {

boxes/token/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"formatting": "prettier --check ./src && eslint ./src",
1313
"formatting:fix": "prettier -w ./src",
1414
"compile": "cd src/contracts && ${AZTEC_NARGO:-aztec-nargo} compile",
15-
"codegen": "${AZTEC_CLI:-aztec-cli} generate-typescript src/contracts/target --outdir src/artifacts",
15+
"codegen": "${AZTEC_CLI:-aztec-cli} codegen src/contracts/target -o src/artifacts --ts",
1616
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --runInBand"
1717
},
1818
"jest": {

docs/docs/dev_docs/contracts/compiling.md

+20-28
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,38 @@ In this guide we will cover how to do so, both using the CLI and programmaticall
66

77
We'll also cover how to generate a helper [TypeScript interface](#typescript-interfaces) and an [Aztec.nr interface](#noir-interfaces) for easily interacting with your contract from your typescript app and from other Aztec.nr contracts, respectively.
88

9-
## Compile using the CLI
9+
## Compile using aztec-nargo
1010

11-
To compile a contract using the Aztec CLI, first [install it](../cli/cli-commands#installation).
11+
To compile a contract using the Aztec's build of nargo.
1212

13-
Then run the `compile` command with the path to your [contract project folder](./layout.md#directory-structure), which is the one that contains the `Nargo.toml` file:
13+
Run the `aztec-nargo compile` command within your [contract project folder](./layout.md#directory-structure), which is the one that contains the `Nargo.toml` file:
1414

15-
```
16-
aztec-cli compile ./path/to/my_aztec_contract_project
15+
```bash
16+
aztec-nargo compile
1717
```
1818

19-
This will output a JSON [artifact](./artifacts.md) for each contract in the project to a `target` folder containing their artifact, which you can use for deploying or interacting with your contracts.
19+
This will output a JSON [artifact](./artifacts.md) for each contract in the project to a `target` folder containing the Noir ABI artifacts.
2020

21-
`aztec-cli` uses `noir_wasm` by default for compiling contracts. This helps reduce the developer overhead of installation and maintaining the noir compiler, `nargo`. However, if you prefer, you can use `nargo` to compile contracts with `aztec-cli` as so:
21+
Before you can use the ABI it currently needs to be validated as being Aztec compatible, and transformed to an Aztec compatible ABI using `aztec-cli codegen`, passing a Noir ABI file or folder, and output location, e.g:
2222

2323
```bash
24-
aztec-cli compile my-contract --compiler nargo # switches compiler to nargo
24+
aztec-cli codegen ./aztec-nargo/output/target/path -o src/artifacts
2525
```
2626

27-
When you specify `nargo` as your compiler, you need to make sure that you are using the correct version. You can find the [latest version information here](../updating.md#updating-nargo).
27+
It can be useful to perform this compilation, validation and transformation in one go, so you may wish to chain the commands and perhaps add them to a package.json script. The below assumes your contract is in a folder called `contract` at your project root:
2828

29-
### Typescript Interfaces
30-
31-
You can use the compiler to autogenerate type-safe typescript classes for each of your contracts. These classes define type-safe methods for deploying and interacting with your contract based on their artifact.
29+
```bash
30+
(cd contract && aztec-nargo compile && aztec-cli codegen target -o ../src/artifacts)
31+
```
3232

33-
To generate them, include a `--typescript` option in the compile command with a path to the target folder for the typescript files:
33+
### Typescript Interfaces
3434

35-
```
36-
aztec-cli compile --typescript ./path/to/typescript/src ./path/to/my_aztec_contract_project
37-
```
35+
You can use the codegenerator to autogenerate type-safe typescript classes for each of your contracts. These classes define type-safe methods for deploying and interacting with your contract based on their artifact.
3836

39-
You can also generate these interfaces from prebuilt artifacts using the `generate-typescript` command:
37+
To generate them, include a `--ts` option in the codegen command with a path to the target folder for the typescript files:
4038

41-
```
42-
aztec-cli generate-typescript ./path/to/my_aztec_contract_project
39+
```bash
40+
aztec-cli codegen ./aztec-nargo/output/target/path -o src/artifacts --ts
4341
```
4442

4543
Below is typescript code generated from the [Token](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/contracts/token_contract/src/main.nr) contract:
@@ -133,16 +131,10 @@ An Aztec.nr contract can [call a function](./syntax/functions.md) in another con
133131

134132
To make this easier, the compiler can generate contract interface structs that expose a convenience method for each function listed in a given contract artifact. These structs are intended to be used from another contract project that calls into the current one. For each contract, two interface structs are generated: one to be used from private functions with a `PrivateContext`, and one to be used from open functions with a `PublicContext`.
135133

136-
To generate them, include a `--interface` option in the compile command with a path to the target folder for the generated Aztec.nr interface files:
137-
138-
```
139-
aztec-cli compile --interface ./path/to/another_aztec_contract_project/src ./path/to/my_aztec_contract_project
140-
```
141-
142-
You can also generate these interfaces from prebuilt artifacts using the `generate-noir-interface` command:
134+
To generate them, include a `--nr` option in the `codegen` command with a path to the target folder for the generated Aztec.nr interface files:
143135

144-
```
145-
aztec-cli generate-noir-interface ./path/to/my_aztec_contract_project
136+
```bash
137+
aztec-cli codegen ./aztec-nargo/output/target/path -o ./path/to/output/folder --nr
146138
```
147139

148140
Below is an example interface, also generated from the [Token](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/noir-contracts/contracts/token_contract/src/main.nr) contract:

yarn-project/cli/src/index.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { DebugLogger, LogFn } from '@aztec/foundation/log';
22
import { fileURLToPath } from '@aztec/foundation/url';
3-
import {
4-
addGenerateNoirInterfaceCommanderAction,
5-
addGenerateTypescriptCommanderAction,
6-
} from '@aztec/noir-compiler/cli';
3+
import { addCodegenCommanderAction } from '@aztec/noir-compiler/cli';
74

85
import { Command, Option } from 'commander';
96
import { lookup } from 'dns/promises';
@@ -493,8 +490,7 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
493490
await update(projectPath, contract, rpcUrl, aztecVersion, log);
494491
});
495492

496-
addGenerateTypescriptCommanderAction(program, log);
497-
addGenerateNoirInterfaceCommanderAction(program, log);
493+
addCodegenCommanderAction(program, log);
498494

499495
return program;
500496
}

yarn-project/noir-compiler/src/cli/add_noir_compiler_commander_actions.ts

+13-25
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ interface Options {
2121
*
2222
*/
2323
export function addNoirCompilerCommanderActions(program: Command, log: LogFn = () => {}) {
24-
// addCompileCommanderAction(program, log);
25-
addGenerateTypescriptCommanderAction(program, log);
26-
addGenerateNoirInterfaceCommanderAction(program, log);
24+
addCodegenCommanderAction(program, log);
2725
}
2826

2927
/**
@@ -48,30 +46,20 @@ export function addCompileCommanderAction(program: Command, log: LogFn = () => {
4846
/**
4947
*
5048
*/
51-
export function addGenerateTypescriptCommanderAction(program: Command, _: LogFn = () => {}) {
49+
export function addCodegenCommanderAction(program: Command, _: LogFn = () => {}) {
5250
program
53-
.command('generate-typescript')
51+
.command('codegen')
5452
.argument('<noir-abi-path>', 'Path to the Noir ABI or project dir.')
55-
.option('-o, --outdir <path>', 'Output folder for the generated typescript.')
53+
.option('-o, --outdir <path>', 'Output folder for the generated code.')
5654
.option('-d, --debug', 'Include debug info.')
57-
.description('Generates TypeScript interface from the given abi.')
58-
.action(async (noirAbiPath: string, { debug, outdir }) => {
59-
const { generateTypescriptInterface } = await import('./generate_typescript_interface.js');
60-
generateTypescriptInterface(outdir || dirname(noirAbiPath), noirAbiPath, debug);
61-
});
62-
}
63-
64-
/**
65-
*
66-
*/
67-
export function addGenerateNoirInterfaceCommanderAction(program: Command, _: LogFn = () => {}) {
68-
return program
69-
.command('generate-noir-interface')
70-
.argument('<noir-abi-path>', 'Path to the Noir ABI or project dir.')
71-
.option('-o, --outdir <path>', 'Output folder for the generated noir.')
72-
.description('Generates Noir interfaces from the artifacts in the given project')
73-
.action(async (noirAbiPath: string, { outdir }) => {
74-
const { generateNoirInterface } = await import('./generate_noir_interface.js');
75-
generateNoirInterface(outdir || dirname(noirAbiPath), noirAbiPath);
55+
.option('--ts', 'Generate TypeScript wrapper.')
56+
.option('--nr', 'Generate Noir interface.')
57+
.description('Validates and generates an Aztec Contract ABI from Noir ABI.')
58+
.action(async (noirAbiPath: string, { debug, outdir, ts, nr }) => {
59+
if (ts && nr) {
60+
throw new Error('--ts and --nr are mutually exclusive.');
61+
}
62+
const { generateCode } = await import('./codegen.js');
63+
generateCode(outdir || dirname(noirAbiPath), noirAbiPath, debug, ts, nr);
7664
});
7765
}

yarn-project/noir-compiler/src/cli/generate_typescript_interface.ts yarn-project/noir-compiler/src/cli/codegen.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,48 @@ import path from 'path';
33

44
import { generateContractArtifact } from '../contract-interface-gen/abi.js';
55
import { generateTypescriptContractInterface } from '../contract-interface-gen/contractTypescript.js';
6+
import { generateNoirContractInterface } from '../contract-interface-gen/noir.js';
67

78
/**
89
*
910
*/
10-
export function generateTypescriptInterface(outputPath: string, fileOrDirPath: string, includeDebug = false) {
11+
export function generateCode(outputPath: string, fileOrDirPath: string, includeDebug = false, ts = false, nr = false) {
1112
const stats = statSync(fileOrDirPath);
1213

1314
if (stats.isDirectory()) {
1415
const files = readdirSync(fileOrDirPath).filter(file => file.endsWith('.json') && !file.startsWith('debug_'));
1516
for (const file of files) {
1617
const fullPath = path.join(fileOrDirPath, file);
17-
generateTypescriptInterfaceFromNoirAbi(outputPath, fullPath, includeDebug);
18+
generateFromNoirAbi(outputPath, fullPath, includeDebug, ts, nr);
1819
}
1920
} else if (stats.isFile()) {
20-
generateTypescriptInterfaceFromNoirAbi(outputPath, fileOrDirPath, includeDebug);
21+
generateFromNoirAbi(outputPath, fileOrDirPath, includeDebug, ts, nr);
2122
}
2223
}
2324

2425
/**
2526
*
2627
*/
27-
function generateTypescriptInterfaceFromNoirAbi(outputPath: string, noirAbiPath: string, includeDebug: boolean) {
28+
function generateFromNoirAbi(outputPath: string, noirAbiPath: string, includeDebug: boolean, ts: boolean, nr: boolean) {
2829
const contract = JSON.parse(readFileSync(noirAbiPath, 'utf8'));
2930
const noirDebugPath = includeDebug ? getDebugFilePath(noirAbiPath) : undefined;
3031
const debug = noirDebugPath ? JSON.parse(readFileSync(noirDebugPath, 'utf8')) : undefined;
3132
const aztecAbi = generateContractArtifact({ contract, debug });
32-
const tsWrapper = generateTypescriptContractInterface(aztecAbi, `./${aztecAbi.name}.json`);
33+
3334
mkdirSync(outputPath, { recursive: true });
34-
writeFileSync(`${outputPath}/${aztecAbi.name}.ts`, tsWrapper);
35+
36+
if (nr) {
37+
const noirContract = generateNoirContractInterface(aztecAbi);
38+
writeFileSync(`${outputPath}/${aztecAbi.name}.nr`, noirContract);
39+
return;
40+
}
41+
3542
writeFileSync(`${outputPath}/${aztecAbi.name}.json`, JSON.stringify(aztecAbi, undefined, 2));
43+
44+
if (ts) {
45+
const tsWrapper = generateTypescriptContractInterface(aztecAbi, `./${aztecAbi.name}.json`);
46+
writeFileSync(`${outputPath}/${aztecAbi.name}.ts`, tsWrapper);
47+
}
3648
}
3749

3850
/**

yarn-project/noir-compiler/src/cli/generate_noir_interface.ts

-33
This file was deleted.

yarn-project/noir-contracts/scripts/generate-types.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ for ABI in $(find target -maxdepth 1 -type f ! -name 'debug_*' -name '*.json');
1414
DEBUG_INFO="$(dirname $ABI)/debug_$(basename $ABI)"
1515

1616
echo "Creating types for $CONTRACT in $ABI..."
17-
node --no-warnings ../noir-compiler/dest/cli.js generate-typescript -o $OUT_DIR -d $ABI
17+
node --no-warnings ../noir-compiler/dest/cli.js codegen -o $OUT_DIR -d --ts $ABI
1818

1919
# Add contract import/export to index.ts.
2020
echo "export * from './${CONTRACT}.js';" >> $INDEX

0 commit comments

Comments
 (0)