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

feat(docs_tutorials): Token Portal & Uniswap Tutorial #2726

Merged
merged 13 commits into from
Oct 13, 2023
13 changes: 7 additions & 6 deletions docs/docs/dev_docs/tutorials/token_portal/depositing_to_aztec.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ In this step, we will write our token portal contract on L1.

## Initialize Solidity contract

In `l1-contracts/contracts` create a new file called `TokenPortal.sol` and paste this:
In `l1-contracts/contracts` in your file called `TokenPortal.sol` paste this:

```solidity
pragma solidity >=0.8.18;
pragma solidity ^0.8.20;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Copy link
Contributor

Choose a reason for hiding this comment

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

I am getting import errors here. I also tried installing the contracts with yarn add -D @openzeppelin/contracts in ./l1-contracts/ but still seeing the error.
image

import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
Expand Down Expand Up @@ -38,12 +38,12 @@ contract TokenPortal {
This imports relevant files including the interfaces used by the Aztec rollup. And initializes the contract with the following parameters:

- rollup registry address (that stores the current rollup, inbox and outbox contract addresses)
- The erc20 token the portal corresponds to
- The ERC20 token the portal corresponds to
- The address of the sister contract on Aztec to where the token will send messages to (for depositing tokens or from where to withdraw the tokens)

Let's also create a dummy ERC20 contract that can mint tokens to anyone. This will make it easier to test our code:
Create a basic ERC20 contract that can mint tokens to anyone. We will use this to test.

Let's create a file `PortalERC20.sol` in the same folder and add:
Create a file `PortalERC20.sol` in the same folder and add:

```solidity
// SPDX-License-Identifier: Apache-2.0
Expand All @@ -59,11 +59,12 @@ contract PortalERC20 is ERC20 {
}
}
```

## 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.

Paste this in TokenPortal.sol
Paste this in `TokenPortal.sol`

#include_code deposit_public /l1-contracts/test/portals/TokenPortal.sol solidity

Expand Down
19 changes: 13 additions & 6 deletions docs/docs/dev_docs/tutorials/token_portal/minting_on_aztec.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ In this step we will start writing our Aztec.nr bridge smart contract and write

In our `token-bridge` nargo project in `aztec-contracts`, under `src` there is an example `main.nr` file. Delete all the code in here and paste this to define imports and initialize the constructor:

```rust
```rust
mod util;
catmcgee marked this conversation as resolved.
Show resolved Hide resolved
```

#include_code token_bridge_imports /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr rust
use crate::token_interface::Token;
use crate::util::{get_mint_public_content_hash, get_mint_private_content_hash, get_withdraw_content_hash};

#include_code token_bridge_storage_and_constructor /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr rust
```rust
use crate::token_interface::Token;
use crate::util::{get_mint_public_content_hash, get_mint_private_content_hash, get_withdraw_content_hash};
```

This imports aztec related dependencies and also our two helper files `token_interface.nr` and `util.nr`.
(The code above will give errors right now - this is because we haven't implemented util and token_interface yet.)

In `token_interface.nr`, add the follows:
#include_code token_bridge_storage_and_constructor /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr rust

This imports Aztec-related dependencies and our two helper files `token_interface.nr` and `util.nr`.

In `token_interface.nr`, add this:
#include_code token_brodge_token_interface /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/token_interface.nr rust

We will write `util.nr` as needed.

## Consume the L1 message

In the previous step, we have moved our funds to the portal and created a L1->L2 message. Upon building the next rollup, the sequencer asks the inbox for any incoming messages and adds them to Aztec’s L1->L2 message tree, so an application on L2 can prove that the message exists and consumes it.
Expand Down
122 changes: 63 additions & 59 deletions docs/docs/dev_docs/tutorials/token_portal/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,12 @@ curl -L https://raw.githubusercontent.com/noir-lang/noirup/main/install | sh
noirup -v #include_noir_version
```

# Create a yarn project
# Create the root project

Our root yarn project will house everything ✨
Our root project will house everything ✨

```sh
mkdir aztec-token-bridge && cd aztec-token-bridge && yarn init
```

In your `package.json` add this

```json
"type": "module",
"scripts": {
"build": "yarn clean && tsc -b",
"build:dev": "tsc -b --watch",
"clean": "rm -rf ./dest tsconfig.tsbuildinfo",
"start": "yarn build && node ./dest/src/index.js"
},
```

Create a `tsconfig.json` and paste this into it:

```json
{
"compilerOptions": {
"rootDir": "./packages",
"outDir": "./dest",
"target": "es2020",
"lib": ["dom", "esnext", "es2017.object"],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"declaration": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"downlevelIteration": true,
"inlineSourceMap": true,
"declarationMap": true,
"importHelpers": true,
"resolveJsonModule": true,
"composite": true,
"skipLibCheck": true
},
"include": [
"packages/src/**/*",
"contracts/**/*.json",
"packages/src/**/*",
"packages/aztec-contracts/token_bridge/target/*.json"
],
"exclude": ["node_modules", "**/*.spec.ts", "contracts/**/*.ts"],
"references": []
}
mkdir aztec-token-bridge
```

# Create a nargo project
Expand Down Expand Up @@ -119,7 +73,7 @@ safe_math = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#incl

Inside `src` you will see a `main.nr` file. This is where our main smart contract will go.

We will also be writing some helper functions that should exist elsewhere so we don't overcomplicated our contract. In `src` create another file called `util.nr` so your dir structure should now look like this:
We will also be writing some helper functions that should exist elsewhere so we don't overcomplicated our contract. In `src` create two more files - one called `util.nr` and one called `token_interface` - so your dir structure should now look like this:

```
aztec-contracts
Expand All @@ -140,6 +94,7 @@ mkdir l1-contracts
cd l1-contracts
npx hardhat init
```

Once you have a hardhat project set up, delete the existing contracts and create a `TokenPortal.sol`:

```sh
Expand All @@ -148,10 +103,11 @@ rm *.sol
touch TokenPortal.sol
```

Also add Aztec's L1-contracts that includes the interfaces to Aztec Inbox, Outbox and Registry smart contracts, which we will need to send messages between L1 and L2.
Now add dependencies that are required. These include interfaces to Aztec Inbox, Outbox and Registry smart contracts, OpenZeppelin contracts, and NomicFoundation.

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

```
yarn add @aztec/l1-contracts
```

This is what your `l1-contracts` should look like:
Expand All @@ -166,11 +122,17 @@ This is what your `l1-contracts` should look like:
└── package.json
```

We will need to ensure we are using the correct Solidity version. Inside your `hardhat.config.js` update `solidity` version to this:

```json
solidity: "0.8.20",
```

catmcgee marked this conversation as resolved.
Show resolved Hide resolved
# Create src yarn project

In this package, we will write TS code that will interact with our ethereum and aztec-nr contracts and run them against the sandbox.
In this directory, we will write TS code that will interact with our L1 and L2 contracts and run them against the sandbox.

We will use `viem` instead of `ethers.js` although ethers works fine too! We will also use `jest` to test our code.
We will use `viem` in this tutorial and `jest` for testing.

Inside the root directory, run

Expand All @@ -189,7 +151,7 @@ In `package.json`, add:
}
```

Your `package.json` should look like so:
Your `package.json` should look something like this:

```json
{
Expand Down Expand Up @@ -218,7 +180,43 @@ Your `package.json` should look like so:
}
```

In this package we will also add a jest config file: `jest.config.json`
Create a `tsconfig.json` and paste this:

```json
{
"compilerOptions": {
"rootDir": "../",
"outDir": "./dest",
"target": "es2020",
"lib": ["dom", "esnext", "es2017.object"],
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"declaration": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"downlevelIteration": true,
"inlineSourceMap": true,
"declarationMap": true,
"importHelpers": true,
"resolveJsonModule": true,
"composite": true,
"skipLibCheck": true
},
"include": [
"packages/src/**/*",
"contracts/**/*.json",
"packages/src/**/*",
"packages/aztec-contracts/token_bridge/target/*.json"
],
"exclude": ["node_modules", "**/*.spec.ts", "contracts/**/*.ts"],
"references": []
}
```

The main thing this will allow us to do is to access TS artifacts that we generate later from our test.

Then create a jest config file: `jest.config.json`

```json
{
Expand All @@ -236,14 +234,20 @@ In this package we will also add a jest config file: `jest.config.json`
}
```

Finally, we will create a test file, in the `src` package:
You will also need to install some dependencies:

```sh
yarn add --dev typescript @types/jest ts-jest
```

Finally, we will create a test file. Run this in the `src` directory.:

```sh
mkdir test && cd test
touch cross_chain_messaging.test.ts
```

Your `src` package should look like:
Your `src` dir should look like:

```tree
src
Expand Down
Loading