Skip to content

Commit

Permalink
Update tut
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaki committed Apr 23, 2024
1 parent 4de414e commit 310b41e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
34 changes: 22 additions & 12 deletions docs/docs/developers/tutorials/crowdfunding/donations.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@ Along the way you will:
- Install Aztec developer tools
- Setup a new Noir contract project
- Add base Aztec dependencies
-
-

## Setup

### Install tools

Please ensure that the you already have [Installed the Sandbox](https://docs.aztec.network/developers/getting_started/quickstart#install-the-sandbox).

And if using VSCode, see [here](https://docs.aztec.network/developers/contracts/main#install-noir-lsp-recommended) to install Noir LSP and select `aztec-nargo`.
And if using VSCode, see [here](https://docs.aztec.network/developers/contracts/main#install-noir-lsp-recommended) to install Noir LSP, where you'll benefit from syntax highlighting, profiling, and more.

### Create an Aztec project

Create a new Aztec contract project named "crowdfunding":
Use `aztec-nargo` in a terminal to create a new Aztec contract project named "crowdfunding":

```sh
aztec-nargo new --contract crowdfunding
```

Inside the new `crowdfunding` directory you will have a base to implement the Aztec smart contract.

Use `aztec-nargo --help` to see other commands.

## Private donations

1. An "Operator" begins a Crowdfunding campaign (contract), specifying:
Expand All @@ -54,7 +56,9 @@ Inside the new `crowdfunding` directory you will have a base to implement the Az

#### Initialize

Rename the contract from `Main`, to `Crowdfunding`.
Open the project in your preferred editor. If using VSCode and the LSP, you'll be able to select the `aztec-nargo` binary to use (instead of `nargo`).

In `main.nr`, rename the contract from `Main`, to `Crowdfunding`.

#include_code empty-contract /noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust

Expand Down Expand Up @@ -91,28 +95,33 @@ A word about versions:

More about versions [here](https://docs.aztec.network/developers/versions-updating).

Inside the Crowdfunding contract definition, use the dependency that defines the address type `AztecAddress`
Inside the Crowdfunding contract definition, use the dependency that defines the address type `AztecAddress` (same syntax as Rust)

```rust
use dep::aztec::protocol_types::address::AztecAddress;
```

The `aztec::protocol_types` can be browsed [here](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir-projects/noir-protocol-circuits/crates/types/src).
The `aztec::protocol_types` can be browsed [here](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir-projects/noir-protocol-circuits/crates/types/src). And like rust dependencies, the relative path inside the dependency corresponds to `address::AztecAddress`.


#### Storage

To retain the initializer parameters in the contract's Storage, we'll need to declare them in a preceding `Storage` struct:

#include_code storage /noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust

There is an additional type, `ValueNote` that we will use later, so also include this at the start of your contract
The `ValueNote` type is in the top-level of the Aztec.nr framework, namely [noir-projects/aztec-nr](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir-projects/aztec-nr/value-note/src/value_note.nr). Like before, you'll need to add the crate to Nargo.toml

(See [here](https://docs.aztec.network/developers/contracts/resources/dependencies) for common dependencies).

---

Back in main.nr, reference `use` of the type

```rust
use dep::value_note::value_note::ValueNote;
```

This dependency is from the top-level of the Aztec.nr framework, namely [noir-projects/aztec-nr](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir-projects/aztec-nr/value-note/src/value_note.nr)

Now complete the initializer by setting the storage variables with the parameters:

#include_code init /noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust
Expand All @@ -123,7 +132,7 @@ Now complete the initializer by setting the storage variables with the parameter

To check that the donation occurs before the campaign deadline, we must access the public `timestamp`. It is one of several [Public Global Variables](https://docs.aztec.network/developers/contracts/references/globals#public-global-variables).

Declare a public (internal) function
Declare an Aztec function that is public and internal

```rust
#include_code deadline-header /noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr raw
Expand All @@ -135,16 +144,17 @@ Read the deadline from storage and assert that the `timestamp` from this context

#include_code deadline /noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust

---

Since donations are private, they will have the user's private context which has these [Private Global Variables](https://docs.aztec.network/developers/contracts/references/globals#private-global-variables). So from the private context we must do some extra work to call the (public internal) `_check_deadline` function.
Since donations are to be private, the donate function will have the user's private context which has these [Private Global Variables](https://docs.aztec.network/developers/contracts/references/globals#private-global-variables). So from the private context we must do some extra work to call the (public internal) `_check_deadline` function.

```rust
#include_code call-check-deadline /noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr raw
//...
}
```

From the private context we call `call_public_function` (defined in [private_context.nr](https://github.com/AztecProtocol/aztec-packages/tree/master/noir-projects/aztec-nr/aztec/src/context) ~[here](https://docs.aztec.network/developers/contracts/references/aztec-nr/aztec/context/private_context#call_public_function)~). Passing the address of the contract and the function signature (name and param types).
From the private context we call `call_public_function` (defined [here](https://docs.aztec.network/developers/contracts/references/aztec-nr/aztec/context/private_context#call_public_function)). Passing the address of the contract and the function signature (name and param types).

We've not yet added the `FunctionSelector` type, so do that now

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ contract Crowdfunding {

// docs:start:donate
// docs:start:call-check-deadline
// docs:start:call-check-deadline-header
#[aztec(private)]
fn donate(amount: u64) {
// docs:end:call-check-deadline-header
// 1) Check that the deadline has not passed
Crowdfunding::at(context.this_address())._check_deadline().enqueue(&mut context);
// docs:end:call-check-deadline
Expand Down

0 comments on commit 310b41e

Please sign in to comment.