Skip to content

Commit e0abc28

Browse files
committed
details on function page
1 parent 8cf8fa6 commit e0abc28

File tree

9 files changed

+80
-68
lines changed

9 files changed

+80
-68
lines changed

docs/docs/aztec/smart_contracts/contract_creation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Aztec makes an important distinction between initialization and public deploymen
6161

6262
### Initialization
6363

64-
Contract constructors are not enshrined in the protocol, but handled at the application circuit level. Constructors are methods used for initializing a contract, either private or public, and contract classes may declare more than a single constructor. They can be declared by the `#[initializer]` macro. You can read more about how to use them on the [Defining Initializer Functions](../../developers/guides/smart_contracts/initializers.md) page.
64+
Contract constructors are not enshrined in the protocol, but handled at the application circuit level. Constructors are methods used for initializing a contract, either private or public, and contract classes may declare more than a single constructor. They can be declared by the `#[initializer]` macro. You can read more about how to use them on the [Defining Initializer Functions](../../developers/guides/smart_contracts/define_functions.md#initializer-functions) page.
6565

6666
A contract must ensure:
6767

docs/docs/aztec/smart_contracts/functions/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ There are also special oracle functions, which can get data from outside of the
2424
## Learn more about functions
2525

2626
- [How function visibility works in Aztec](./visibility.md)
27-
- How to write an [initializer function](../../../developers/guides/smart_contracts/initializers.md)
27+
- How to write an [initializer function](../../../developers/guides/smart_contracts/define_functions.md#initializer-functions)
2828
- [Oracles](../oracles/index.md) and how Aztec smart contracts might use them
2929
- [How functions work under the hood](./attributes.md)
3030

docs/docs/developers/guides/smart_contracts/call_contracts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Contract Composability
3-
sidebar_position: 6
3+
sidebar_position: 5
44
tags: [functions, contracts, composability]
55
description: Learn how to call other contracts from your Aztec smart contracts.
66
---

docs/docs/developers/guides/smart_contracts/define_functions.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,65 @@ tags: [functions, smart-contracts]
55
description: Learn how to define functions in your Aztec smart contracts.
66
---
77

8-
## Overview
8+
There are several types of functions in Aztec contracts that correspond the the different execution environments in which they run. These include:
9+
10+
- private functions
11+
- public functions
12+
- utility functions
13+
- view functions
14+
- contract library methods
15+
- initializer functions
16+
17+
## Examples
18+
19+
### Private Functions
20+
21+
Private functions execute client-side on user devices to maintain private of user inputs and execution. Specify a private function in your contract using the `#[private]` function annotation.
22+
23+
#include_code withdraw noir-projects/noir-contracts/contracts/app/escrow_contract/src/main.nr rust
24+
25+
### Public Functions
26+
27+
<!-- TODO: flesh out with info about all types -->
28+
29+
### Initializer Functions
30+
31+
Initializers are regular functions that set an "initialized" flag (a nullifier) for the contract. A contract can only be initialized once, and contract functions can only be called after the contract has been initialized, much like a constructor. However, if a contract defines no initializers, it can be called at any time. Additionally, you can define as many initializer functions in a contract as you want, both private and public.
32+
33+
#### Annotate with `#[initializer]`
34+
35+
Define your initializer like so:
36+
37+
```rust
38+
#[initializer]
39+
fn constructor(){
40+
// function logic here
41+
}
42+
```
43+
44+
Aztec supports both public and private initializers. Use the appropriate macro, for example for a private initializer:
45+
46+
```rust
47+
#[private]
48+
#[initializer]
49+
fn constructor(){
50+
// function logic here
51+
}
52+
```
53+
54+
Initializers are commonly used to set an admin, such as this example:
55+
56+
#include_code constructor /noir-projects/noir-contracts/contracts/app/token_contract/src/main.nr rust
57+
58+
Here, the initializer is writing to storage. It can also call another function. Learn more about calling functions from functions [here](./call_contracts.md).
59+
60+
## Multiple initializers
61+
62+
You can set multiple functions as an initializer function simply by annotating each of them with `#[initializer]`. You can then decide which one to call when you are deploying the contract.
63+
64+
Calling any one of the functions annotated with `#[initializer]` will mark the contract as initialized.
65+
66+
To see an initializer in action, follow the [Counter codealong tutorial](../../tutorials/codealong/contract_tutorials/counter_contract.md).
967

10-
## Example
1168

1269
## Further Reading

docs/docs/developers/guides/smart_contracts/how_to_prove_history.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Prove Historic State
3-
sidebar_position: 6
3+
sidebar_position: 7
44
tags: [contracts]
55
description: Learn how to prove historical state transitions in your Aztec smart contracts.
66
---

docs/docs/developers/guides/smart_contracts/initializers.md

Lines changed: 0 additions & 49 deletions
This file was deleted.

docs/docs/developers/guides/smart_contracts/note_types.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ A note type can be defined with the macro `#[note]` used on a struct:
2020

2121
#include_code state_vars-CardNote noir-projects/noir-contracts/contracts/docs/docs_example_contract/src/types/card_note.nr rust
2222

23+
<!-- TODO: Explain what #[derive(Eq, Serialize, Deserialize, Packable)] are in the code block -->
24+
2325
In this example, we are implementing a card note that holds a number of `points` as `u8`.
2426

2527
`randomness` is not enforced by the protocol and should be implemented by the application developer. If you do not include `randomness`, and the note preimage can be guessed by an attacker, it makes the note vulnerable to preimage attacks.
@@ -47,21 +49,21 @@ This is the AddressNote:
4749

4850
#include_code address_note_def noir-projects/aztec-nr/address-note/src/address_note.nr rust
4951

50-
### Importing AddressNote
52+
#### Importing AddressNote
5153

52-
#### In Nargo.toml
54+
##### In Nargo.toml
5355

5456
```toml
5557
address_note = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/address-note" }
5658
```
5759

58-
#### In your contract
60+
##### In your contract
5961

6062
#include_code addressnote_import noir-projects/noir-contracts/contracts/app/escrow_contract/src/main.nr rust
6163

62-
### Working with AddressNote
64+
#### Working with AddressNote
6365

64-
#### Creating a new note
66+
##### Creating a new note
6567

6668
Creating a new `AddressNote` takes the following args:
6769

@@ -78,21 +80,21 @@ This is the ValueNote struct:
7880

7981
#include_code value-note-def noir-projects/aztec-nr/value-note/src/value_note.nr rust
8082

81-
### Importing ValueNote
83+
#### Importing ValueNote
8284

83-
#### In Nargo.toml
85+
##### In Nargo.toml
8486

8587
```toml
8688
value_note = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#include_aztec_version", directory="noir-projects/aztec-nr/value-note" }
8789
```
8890

89-
#### In your contract
91+
##### In your contract
9092

9193
#include_code import_valuenote noir-projects/noir-contracts/contracts/test/child_contract/src/main.nr rust
9294

93-
### Working with ValueNote
95+
#### Working with ValueNote
9496

95-
#### Creating a new note
97+
##### Creating a new note
9698

9799
Creating a new `ValueNote` takes the following args:
98100

@@ -101,7 +103,7 @@ Creating a new `ValueNote` takes the following args:
101103

102104
#include_code valuenote_new noir-projects/noir-contracts/contracts/test/child_contract/src/main.nr rust
103105

104-
#### Getting a balance
106+
##### Getting a balance
105107

106108
A user may have multiple notes in a set that all refer to the same content (e.g. a set of notes representing a single token balance). By using the `ValueNote` type to represent token balances, you do not have to manually add each of these notes and can instead use a helper function `get_balance()`.
107109

@@ -111,7 +113,7 @@ It takes one argument - the set of notes.
111113

112114
This can only be used in an unconstrained function.
113115

114-
#### Incrementing and decrementing
116+
##### Incrementing and decrementing
115117

116118
Both `increment` and `decrement` functions take the same args:
117119

docs/docs/developers/tutorials/contract_tutorials/crowdfunding_contract.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ Follow the account contract tutorial on the [next page](./write_accounts_contrac
202202

203203
### Optional: Learn more about concepts mentioned here
204204

205-
- [Initializer functions](../../../guides/smart_contracts/initializers.md)
205+
- [Initializer functions](../../../guides/smart_contracts/define_functions.md#initializer-functions)
206206
- [Versions and Updating](../../../guides/local_env/sandbox.md#updating).
207207
- [Authorizing actions](../../../../aztec/concepts/advanced/authwit.md)
208208
- [Public logs](../../../guides/smart_contracts/how_to_emit_event.md)

noir-projects/noir-contracts/contracts/app/escrow_contract/src/main.nr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub contract Escrow {
2929
storage.owner.initialize(note).emit(encode_and_encrypt_note(&mut context, owner));
3030
}
3131

32+
// docs:start:withdraw
3233
// Withdraws balance. Requires that msg.sender is the owner.
3334
#[private]
3435
fn withdraw(token: AztecAddress, amount: u128, recipient: AztecAddress) {
@@ -40,4 +41,5 @@ pub contract Escrow {
4041
Token::at(token).transfer(recipient, amount).call(&mut context);
4142
// docs:end:call_function
4243
}
44+
// docs:end:withdraw
4345
}

0 commit comments

Comments
 (0)