Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: deploying contracts using aztec-cli #1592

Merged
merged 12 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
61 changes: 60 additions & 1 deletion docs/docs/dev_docs/contracts/deploying.md
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
See sandbox section?
# Deploying contracts

Once you have [compiled](./compiling.md) your contracts you can proceed to deploying them using `aztec-cli`.

## Prerequisites
- aztec-cli installed (go to [CLI main section](./main.md) for installation instructions)
- contract artifacts ready (go to [Compiling contracts section](../contracts/compiling.md) for instructions on how to compile contracts)
- aztec-sandbox running (go to [Sandbox section](../sandbox/main.md) for instructions on how to install and run the sandbox)

## Deploy

To deploy the contracts we use the `deploy` command form `aztec-cli`:
benesjan marked this conversation as resolved.
Show resolved Hide resolved

```bash
aztec-cli deploy /path/to/contract/abi.json
```

### Arguments
This command takes 1 mandatory positional argument which is the path to the contract ABI file in a JSON format (e.g. `build/contracts/PrivateToken.json`). Alternatively you can pass here a name of a contract ABI as exported by `@aztec/noir-contracts` (run `aztec-cli example-contracts` to get the list).
benesjan marked this conversation as resolved.
Show resolved Hide resolved

The command also takes the following optional arguments:
- `-args <constructorArgs...>` (default: `[]`): Arguments to pass to the contract constructor.
- `--rpc-url <string>` (default: `http://localhost:8080`): URL of the Aztec node to connect to.
- `--public-key <string>` (default: `undefined`): Optional encryption public key for this contract.
Set this only if this contract is expected to receive private notes (in such a case the public key is used during the note encryption).
> **NOTE**: Contracts which are expected to receive notes are called account contracts.
benesjan marked this conversation as resolved.
Show resolved Hide resolved
- `--salt <string>` (default: random value): Hexadecimal string used when computing the contract address of the contract being deployed.
By default is set to a random value.
Set it, if you need a deterministic contract address (same functionality as Ethereum's `CREATE2` opcode).

To give you a more complete example we will deploy the `PrivateToken` contract whose artifacts are included in the `@aztec/noir-contracts` package.

### Deploying private token contract
The contract has `initial_supply` and `owner` as constructor arguments.
Because the contract sends a note to the owner specified inside the constructor, we first need to register the owner as a recipient inside the Aztec RPC with the following command:
benesjan marked this conversation as resolved.
Show resolved Hide resolved

```bash
aztec-cli register-recipient --address 0x147392a39e593189902458f4303bc6e0a39128c5a1c1612f76527a162d36d529 --public-key 0x26e193aef4f83c70651485b5526c6d01a36d763223ab24efd1f9ff91b394ac0c20ad99d0ef669dc0dde8d5f5996c63105de8e15c2c87d8260b9e6f02f72af622 --partial-address 0x200e9a6c2d2e8352012e51c6637659713d336405c29386c7c4ac56779ab54fa7
```

> **NOTE**: If we didn't register owner as a recipient we could not encrypt a note for the owner and the contract deployment would fail because constructor execution would fail (we need owner's public key to encrypt a note).

Once the recipient is registered we can deploy the contract:

```bash
aztec-cli deploy PrivateTokenContractAbi --args 1000 0x147392a39e593189902458f4303bc6e0a39128c5a1c1612f76527a162d36d529
```

If everything went as expected you should see the following output (with a different address):
> Contract deployed at 0x151de6120ae6628129ee852c5fc7bcbc8531055f76d4347cdc86003bbea96906

If we pass the salt as an argument:

```bash
aztec-cli deploy PrivateTokenContractAbi --args 1000 0x147392a39e593189902458f4303bc6e0a39128c5a1c1612f76527a162d36d529 --salt 0x123
```

the resulting address will be deterministic.

> **NOTE**: You can try running the deployment with the same salt the second time in which case the transaction will fail because it will be considered a double spend.
benesjan marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion yarn-project/aztec-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
"formatting:fix": "run -T prettier -w ./src",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests",
"start": "node --no-warnings ./dest/index.js"
"start": "node --no-warnings ./dest/bin/index.js"
},
"inherits": [
"../package.common.json"
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/aztec-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
program
.command('deploy')
.description('Deploys a compiled Noir contract to Aztec.')
.requiredOption(
'-c, --contract-abi <file>',
.argument(
'<abi>',
Comment on lines +149 to +150
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like this change, but heads up this impacts other guides, as well as the one in the up-dev page. We'll need to update those as well. Let's include them in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is maybe a good argument for writing all code snippets as part of testable code files (like you did earlier this week for some other docs, Santiago), so that the CI can then test that the code snippets are all up-to-date?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree. I did fail to get them into the CI though (build-system beat me again), but can revisit it next week.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 9dea419

I will send a PR to the sandbox website as well.

"A compiled Noir contract's ABI in JSON format or name of a contract ABI exported by @aztec/noir-contracts",
)
.option('-a, --args <constructorArgs...>', 'Contract constructor arguments', [])
Expand All @@ -157,8 +157,8 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
'Optional encryption public key for this address. Set this value only if this contract is expected to receive private notes, which will be encrypted using this public key.',
)
.option('-s, --salt <string>', 'Optional deployment salt as a hex string for generating the deployment address.')
.action(async (options: any) => {
const contractAbi = await getContractAbi(options.contractAbi, log);
.action(async (abiPath, options: any) => {
const contractAbi = await getContractAbi(abiPath, log);
const constructorAbi = contractAbi.functions.find(({ name }) => name === 'constructor');

const client = createClient(options.rpcUrl);
Expand Down Expand Up @@ -279,7 +279,7 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
const partialAddress = Fr.fromString(options.partialAddress);

await client.registerRecipient(await CompleteAddress.create(address, publicKey, partialAddress));
log(`\nRegistered details for Address: ${options.address}\n`);
log(`\nRegistered details for account with address: ${options.address}\n`);
});

program
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function hashConstructor(
* Computes a contract address.
* @param wasm - A module providing low-level wasm access.
* @param deployerPubKey - The pubkey of the contract deployer.
* @param contractAddrSalt - The salt used as 1 one of the inputs of the contract address computation.
* @param contractAddrSalt - The salt used as one of the inputs of the contract address computation.
* @param fnTreeRoot - The function tree root of the contract being deployed.
* @param constructorHash - The hash of the constructor.
* @returns The contract address.
Expand All @@ -208,7 +208,7 @@ export function computeContractAddress(
/**
* Computes a partial address. Consists of all contract address components except the deployer public key.
* @param wasm - A module providing low-level wasm access.
* @param contractAddrSalt - The salt used as 1 one of the inputs of the contract address computation.
* @param contractAddrSalt - The salt used as one of the inputs of the contract address computation.
* @param fnTreeRoot - The function tree root of the contract being deployed.
* @param constructorHash - The hash of the constructor.
* @returns The partially constructed contract address.
Expand All @@ -232,7 +232,7 @@ export function computePartialAddress(
/**
* Computes a contract address from its partial address and the pubkey.
* @param wasm - A module providing low-level wasm access.
* @param partial - The salt used as 1 one of the inputs of the contract address computation.
* @param partial - The salt used as one of the inputs of the contract address computation.
* @param fnTreeRoot - The function tree root of the contract being deployed.
* @param constructorHash - The hash of the constructor.
* @returns The partially constructed contract address.
Expand Down