Skip to content

Commit

Permalink
fix(docs): Tutorial fixes (#5600)
Browse files Browse the repository at this point in the history
Closes #5781

This fixes all tutorials and removes uniswap - we will need to write our
own TS for this one because referencing the monorepo gets messy & has a
lot of unnecessary things

---------

Co-authored-by: Josh Crites <jc@joshcrites.com>
Co-authored-by: josh crites <critesjosh@gmail.com>
  • Loading branch information
3 people authored Apr 30, 2024
1 parent cfa850b commit 6421467
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 96 deletions.
2 changes: 1 addition & 1 deletion docs/docs/developers/tutorials/crowdfunding/donations.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ aztec = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_
```

A word about versions:
- Choose the aztec packages version to match your aztec tools as seen here - `aztec-cli -V`
- Choose the aztec packages version to match your aztec sandbox version
- Check that your `compiler_version` in Nargo.toml is satisified by your aztec compiler - `aztec-nargo -V`

More about versions [here](https://docs.aztec.network/developers/versions-updating).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol
// Messaging
import {IRegistry} from "@aztec/l1-contracts/src/core/interfaces/messagebridge/IRegistry.sol";
import {IInbox} from "@aztec/l1-contracts/src/core/interfaces/messagebridge/IInbox.sol";
import {IOutbox} from "@aztec/l1-contracts/src/core/interfaces/messagebridge/IOutbox.sol";
import {DataStructures} from "@aztec/l1-contracts/src/core/libraries/DataStructures.sol";
import {Hash} from "@aztec/l1-contracts/src/core/libraries/Hash.sol";
Expand All @@ -35,6 +36,12 @@ Create a file `PortalERC20.sol` in the same folder and add:

#include_code contract /l1-contracts/test/portals/PortalERC20.sol solidity

Replace the openzeppelin import with this:

```solidity
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
```

## Depositing tokens to Aztec publicly

Next, we will write a function that is used to deposit funds on L1 that a user may have into an Aztec portal and send a message to the Aztec rollup to mint tokens _publicly_ on Aztec.
Expand Down
40 changes: 22 additions & 18 deletions docs/docs/developers/tutorials/token_portal/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ We recommend going through this setup to fully understand where things live.

However if you’d rather skip this part, our dev-rels repo contains the starter code here.

# Prerequisites
## Prerequisites

- [node v18+](https://github.com/tj/n)
- [docker](https://docs.docker.com/)
Expand All @@ -26,7 +26,7 @@ However if you’d rather skip this part, our dev-rels repo contains the starter
/bin/sh -c "$(curl -fsSL 'https://sandbox.aztec.network')"
```

# Create the root project and packages
## Create the root project and packages

Our root project will house everything ✨

Expand All @@ -37,11 +37,21 @@ cd aztec-token-bridge && mkdir packages

We will hold our projects inside of `packages` to follow the design of the project in the [repo](https://github.com/AztecProtocol/dev-rel/tree/main/tutorials/token-bridge-e2e).

# Create a noir project
## Create a noir project

Now inside `packages` create a new directory called `aztec-contracts`
Inside `packages` create a new directory `aztec-contracts`:

Inside `aztec-contracts`, create the following file structure:
```bash
cd packages && mkdir aztec-contracts
```

Inside `aztec-contracts` create a new contract project like this:

```bash
aztec-nargo new --contract token_bridge
```

Your file structure should look something like this:

```tree
aztec-contracts
Expand All @@ -51,18 +61,13 @@ aztec-contracts
├── main.nr
```

Inside `Nargo.toml` add the following content:
Inside `Nargo.toml` add the following dependencies:

```toml
[package]
name = "token_bridge"
authors = [""]
compiler_version = ">=0.18.0"
type = "contract"

[dependencies]
aztec = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/aztec" }
token_portal_content_hash_lib = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/noir-contracts/contracts/token_portal_content_hash_lib" }
token = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/noir-contracts/contracts/token_contract" }
```

We will also be writing some helper functions that should exist elsewhere so we don't overcomplicated our contract. In `src` create one more file called `util.nr` - so your dir structure should now look like this:
Expand All @@ -76,7 +81,7 @@ aztec-contracts
├── util.nr
```

# Create a JS hardhat project
## Create a JS hardhat project

In the `packages` dir, create a new directory called `l1-contracts` and run `yarn init -yp &&
npx hardhat init` inside of it. Keep hitting enter so you get the default setup (Javascript project)
Expand All @@ -91,16 +96,15 @@ npx hardhat init
Once you have a hardhat project set up, delete the existing contracts, tests, and scripts, and create a `TokenPortal.sol`:

```bash
rm -rf contracts test scripts
rm -rf contracts test scripts ignition
mkdir contracts && cd contracts
touch TokenPortal.sol
```

Now add dependencies that are required. These include interfaces to Aztec Inbox, Outbox and Registry smart contracts, OpenZeppelin contracts, and NomicFoundation.

```bash
yarn add @aztec/foundation @aztec/l1-contracts @openzeppelin/contracts && yarn add --dev @nomicfoundation/hardhat-network-helpers @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers @nomiclabs/hardhat-etherscan @types/chai @types/mocha @typechain/ethers-v5 @typechain/hardhat chai@4.0.0 hardhat-gas-reporter solidity-coverage ts-node typechain typescript

yarn add @aztec/foundation @aztec/l1-contracts @openzeppelin/contracts ethers && yarn add --dev @nomicfoundation/hardhat-network-helpers @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers @nomiclabs/hardhat-etherscan @types/chai @types/mocha @typechain/ethers-v5 @typechain/hardhat chai@4.0.0 hardhat-gas-reporter solidity-coverage ts-node typechain typescript @nomicfoundation/hardhat-ignition @nomicfoundation/hardhat-ignition-ethers @nomicfoundation/hardhat-verify
```

This is what your `l1-contracts` should look like:
Expand Down Expand Up @@ -129,7 +133,7 @@ Inside the `packages` directory, run

```bash
mkdir src && cd src && yarn init -yp
yarn add typescript @aztec/aztec.js @aztec/accounts @aztec/noir-contracts.js @aztec/types @aztec/foundation @aztec/l1-artifacts viem@1.21.4 "@types/node@^20.8.2"
yarn add typescript @aztec/aztec.js @aztec/accounts @aztec/noir-contracts.js @aztec/types @aztec/foundation @aztec/l1-artifacts viem@^2.7.15 "@types/node@^20.8.2"
yarn add -D jest @jest/globals ts-jest
```

Expand All @@ -141,7 +145,7 @@ In `package.json`, add:
"type": "module",
"scripts": {
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest)"
}
},
```

Your `package.json` should look something like this (do not copy and paste):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ Open `cross_chain_messaging.test.ts` and paste the initial description of the te

```typescript
import { expect, jest} from '@jest/globals'
import { AccountWallet, AztecAddress, DebugLogger, EthAddress, Fr, computeAuthWitMessageHash, createDebugLogger, createPXEClient, waitForSandbox } from '@aztec/aztec.js';
import { getSandboxAccountsWallets } from '@aztec/accounts/testing';
import { AccountWallet, AztecAddress, DebugLogger, EthAddress, Fr, computeAuthWitMessageHash, createDebugLogger, createPXEClient, waitForPXE } from '@aztec/aztec.js';
import { getInitialTestAccountsWallets } from '@aztec/accounts/testing';
import { TokenContract } from '@aztec/noir-contracts.js/Token';
import { TokenBridgeContract } from '@aztec/noir-contracts.js/TokenBridge';
import { TokenBridgeContract } from './fixtures/TokenBridge.js';
import { createAztecNodeClient } from '@aztec/circuit-types';

import { CrossChainTestHarness } from './shared/cross_chain_test_harness.js';
import { mnemonicToAccount } from 'viem/accounts';
Expand All @@ -49,12 +50,12 @@ import { foundry } from 'viem/chains';
const { PXE_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env;
const MNEMONIC = 'test test test test test test test test test test test junk';
const hdAccount = mnemonicToAccount(MNEMONIC);
const aztecNode = createAztecNodeClient(PXE_URL);

describe('e2e_cross_chain_messaging', () => {
jest.setTimeout(90_000);

let logger: DebugLogger;
// include code:
let user1Wallet: AccountWallet;
let user2Wallet: AccountWallet;
let ethAccount: EthAddress;
Expand All @@ -68,8 +69,8 @@ describe('e2e_cross_chain_messaging', () => {
beforeEach(async () => {
logger = createDebugLogger('aztec:e2e_uniswap');
const pxe = createPXEClient(PXE_URL);
await waitForSandbox(pxe);
const wallets = await getSandboxAccountsWallets(pxe);
await waitForPXE(pxe);
const wallets = await getInitialTestAccountsWallets(pxe);

const walletClient = createWalletClient({
account: hdAccount,
Expand All @@ -82,6 +83,7 @@ describe('e2e_cross_chain_messaging', () => {
});

crossChainTestHarness = await CrossChainTestHarness.new(
aztecNode,
pxe,
publicClient,
walletClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ cd aztec-contracts/token_bridge
aztec-nargo compile
```

And generate the TypeScript interface for the contract and add it to the test dir:
You may get some unused variable warnings - you can ignore these.

And generate the TypeScript interface for the contract and add it to the test dir. Run this inside `aztec-contracts/token_bridge`:

```bash
aztec-builder target -o ../../src/test/fixtures
```

This will create a TS interface in our `src/test` folder!
This will create a TS interface inside `fixtures` dir in our `src/test` folder!

In the next step we will write the TypeScript code to deploy our contracts and call on both L1 and L2 so we can see how everything works together.
4 changes: 3 additions & 1 deletion docs/docs/developers/tutorials/uniswap/l1_portal.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ cd l1-contracts/contracts && touch UniswapPortal.sol
and paste this inside:

```solidity
pragma solidity ^0.8.20;
pragma solidity >=0.8.18;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IRegistry} from "@aztec/l1-contracts/src/core/interfaces/messagebridge/IRegistry.sol";
import {IOutbox} from "@aztec/l1-contracts/src/core/interfaces/messagebridge/IOutbox.sol";
import {DataStructures} from "@aztec/l1-contracts/src/core/libraries/DataStructures.sol";
import {DataStructures as PortalDataStructures} from "./DataStructures.sol";
import {Hash} from "@aztec/l1-contracts/src/core/libraries/Hash.sol";
#include_code setup l1-contracts/test/portals/UniswapPortal.sol raw
Expand Down
24 changes: 9 additions & 15 deletions docs/docs/developers/tutorials/writing_private_voting_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ Your file structure should look something like this:

The file `main.nr` will soon turn into our smart contract!

We will need the Aztec library to create this contract. Add the following content to `Nargo.toml`:
We will need the Aztec library to create this contract. In your `Nargo.toml` you should see `[dependencies]` - paste this bellow it.

```toml
[package]
name = "private_voting"
type = "contract"
authors = [""]
compiler_version = ">=0.18.0"

[dependencies]
aztec = { git="https://github.com/AztecProtocol/aztec-packages", tag="#include_aztec_version", directory="noir-projects/aztec-nr/aztec" }
```
Expand All @@ -70,13 +64,11 @@ Inside this, paste these imports:

We are using various utils within the Aztec library:

- `context` - exposes things such as the contract address, msg_sender, etc
- `context.request_nullifier_secret_key` - get your secret key to help us create a randomized nullifier
- `FunctionSelector::from_signature` - compute a function selector from signature so we can call functions from other functions
- `state_vars::{Map, PublicMutable}` - we will use a Map to store the votes (key = voteId, value = number of votes), and PublicMutable to hold our public values that we mentioned earlier
- `types::type_serialization::{..}` - various serialization methods for defining how to use these types
- `types::address::{AztecAddress},` - our admin will be held as an address
- `constants::EMPTY_NULLIFIED_COMMITMENT,` - this will come in useful when creating our nullifier
- `Context` and `PrivateContext` - exposes things such as the contract address, msg_sender, etc
- `AztecAddress` - A type for storing an address on Aztec
- `FunctionSelector` - Used for computing a selector to call a function
- `Map` - A data storage type for storing candidates with the number of votes they have
- `PublicMutable` - A type of storage, which holds a mutable public value. We'll store votes as PublicMutables

## Set up storage

Expand Down Expand Up @@ -155,7 +147,9 @@ The easiest way to compile the contract is with `aztec-nargo`. Run the following
aztec-nargo compile
```

This will create a new directory called `target` and a JSON artifact inside it. To optionally create a typescript interface, run:
This will create a new directory called `target` and a JSON artifact inside it.

Once it is compiled you can [deploy](../contracts/deploying_contracts/how_to_deploy_contract.md) it to the sandbox. Ensure your [sandbox is running](../sandbox/references/sandbox-reference.md).

```bash
aztec-builder target -o src/artifacts
Expand Down
48 changes: 24 additions & 24 deletions docs/docs/developers/tutorials/writing_token_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,41 @@ Check the [Dev Tools section](https://github.com/noir-lang/awesome-noir#dev-tool

## Project setup

Create a new directory called `token_contract_tutorial`
Create a new project with:

```bash
mkdir token_contract_tutorial
aztec-nargo new --contract token_contract
```

inside that directory, create a `contracts` folder for the Aztec contracts.

```bash
cd token_contract_tutorial && mkdir contracts && cd contracts
```

Create the following file structure
Your file structure should look something like this:

```tree
.
└── contracts
├── Nargo.toml
└── src
└── main.nr
|--private_voting
| |--src
| | |--main.nr
| |--Nargo.toml
```

Add the following content to Nargo.toml file:
Inside `Nargo.toml` paste the following:

```toml
[package]
name = "token_contract"
authors = [""]
compiler_version = ">=0.18.0"
type = "contract"

[dependencies]
aztec = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/aztec" }
authwit={ git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/authwit"}
compressed_string = {git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/compressed-string"}
```

We will be working within `main.nr` for the rest of the tutorial.

## Contract Interface

Remove everything from `main.nr` and paste this:

```rust
contract Token {
#[aztec(private)]
#[aztec(initializer)]
fn constructor() {}

#[aztec(public)]
Expand Down Expand Up @@ -111,11 +104,13 @@ contract Token {
#[aztec(public)]
fn _initialize(new_admin: AztecAddress) {}

#[aztec(internal)]
#[aztec(public)]
internal fn _increase_public_balance(to: AztecAddress, amount: Field) {}
fn _increase_public_balance(to: AztecAddress, amount: Field) {}

#[aztec(internal)]
#[aztec(public)]
internal fn _reduce_total_supply(amount: Field) {}
fn _reduce_total_supply(amount: Field) {}

// Unconstrained functions (read only)

Expand All @@ -131,7 +126,7 @@ contract Token {
}
```

This specifies the interface of the `Token` contract. Go ahead and copy and paste this interface into your `main.nr` file.
This specifies the interface of the `Token` contract. Don't worry if you get some warnings - we haven't imported our types yet.

Before we through the interface and implement each function, let's review the functions to get a sense of what the contract does.

Expand Down Expand Up @@ -204,7 +199,12 @@ Just below the contract definition, add the following imports:

#include_code imports /noir-projects/noir-contracts/contracts/token_contract/src/main.nr rust

We are importing the Option type, items from the `value_note` library to help manage private value storage, note utilities, context (for managing private and public execution contexts), `state_vars` for helping manage state, `types` for data manipulation and `oracle` for help passing data from the private to public execution context. We also import the `auth` [library](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir-projects/aztec-nr/aztec/src/auth.nr) to handle token authorizations from [Account Contracts](../../learn/concepts/accounts/main). Check out the Account Contract with AuthWitness [here](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir-projects/noir-contracts/contracts/schnorr_single_key_account_contract/src/main.nr).
We are importing:

- `CompressedString` to hold the token symbol
- Types from `aztec::prelude`
- `compute_secret_hash` that will help with the shielding and unshielding, allowing someone to claim a token from private to public
- Types for storing note types

For more detail on execution contexts, see [Contract Communication](../../learn/concepts/communication/main).

Expand Down
Loading

0 comments on commit 6421467

Please sign in to comment.