Skip to content

Commit bc997ec

Browse files
docs about contract structure (#7)
* docs about contract structure * Update contract-structure.md * fix the build * remove the index.md for framework-description --------- Co-authored-by: Ciara Nightingale <ciara.nightingale@sky.com> Co-authored-by: ciaranightingale <52419674+ciaranightingale@users.noreply.github.com>
1 parent 8b73f29 commit bc997ec

File tree

3 files changed

+130
-27
lines changed

3 files changed

+130
-27
lines changed

docs/pages/aztec-nr/framework-description/contract-scope.md

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,134 @@
11
# Contract Structure
22

3-
## Mike's comments
3+
high level structure of how contracts are written and what they're made of
44

5-
1. High-level structure
6-
- Imports
7-
- Contract Scope
8-
- State Variables
9-
- Functions
10-
- Events
11-
- See the dedicated section below
5+
(i am not sure if there should be links throughout this page to the different relevant sections, eg state vars, events, functions, etc., or if we should just do the entire thing in one go with much more detail. i am writing this as if you had never seen a contract in your life and needed to get a sense of how the thing sort of looks to wrap your head around it. there'd be some minor content duplication between this page and the dedicated page in that case - i think that's fine)
126

7+
## contract block
8+
9+
all contracts begin the same:
10+
11+
```noir
12+
// import the `aztec` macro from aztecnr
13+
use aztec::macros::aztec;
14+
15+
// use the 'contract' keyword to declare a contract, applying the `aztec` macro
16+
#[aztec]
17+
contract MyContract {
18+
// contract code here
19+
}
20+
```
21+
22+
the `#[aztec]` macro performs a lot of the low-level operations required to take a circuit language like Noir and build smart contracts out of it - including things like automatically creating external interfaces, inserting standard contract functions, etc. all aztec smart contracts must have this macro applied to them.
23+
24+
>note: each noir crate (package) can only have _a single_ contract. if you are writing a multi-contract system, then each of them needs to be in their own separate crate. (link to docs on noir crates and workspaces)
25+
26+
## Imports
27+
28+
except for the `#[aztec]` macro import, all other imports need to go _inside_ the `contract` block - this is because `contract` acts like `mod`, creating a new module (link to noir modules).
29+
30+
```noir
31+
use aztec::macros::aztec;
32+
33+
#[aztec]
34+
contract MyContract {
35+
// other imports go here
36+
use aztec::state_vars::{PrivateMutable, PrivateSet};
37+
}
38+
```
39+
40+
>note: noir's vscode extension is able to take care of most imports and put them in the correct place automatically
41+
42+
## State Variables
43+
44+
with the boilerplate out of the way, it is now the time to begin defining the contract logic. it is recommended to start development by understanding the shape the _state_ of the contract will have: which values will be private, which will be public, and what properties are required (is mutability or immutability needed? is there a single global value, like a token total supply, or does each user get one, like a balance?).
45+
46+
in solidity, this is done by simply declaring variables inside of the contract, like so:
47+
48+
```solidity
49+
contract MyContract {
50+
uint128 public my_public_state_variable;
51+
}
52+
```
53+
54+
in aztec this process is a bit more involved, as not only are there both private and public variables, there are multiple _kinds_ of state variables. we do this by defining a `struct` (link to noir structs) that will hold the entire contract state. we call this struct _the storage struct_, and each variable inside this struct is called _a state variable_ (link).
55+
56+
```noir
57+
use aztec::macros::aztec;
58+
59+
#[aztec]
60+
contract MyContract {
61+
use aztec::{
62+
macros::storage,
63+
state_vars::{PrivateMutable, PublicMutable}
64+
};
65+
66+
// the storage struct can have any name, but it is typically just called `Storage`. it must have the `#[storage]` macro applied to it.
67+
// this struct must also have a generic type called C or Context. for now try to pretend it is not there
68+
#[storage]
69+
struct Storage<C> {
70+
// a private numeric value which can change over time. this value will be hidden, and only people who are shown the secret will be able to know its current value
71+
my_private_state_variable: PrivateMutable<u128, C>,
72+
// a public numeric value which can change over time. this value will be known to everyone. this is equivalent to the solidity example above
73+
my_public_state_variable: PublicMutable<u128, C>,
74+
}
75+
}
76+
```
77+
78+
## Events
79+
80+
same as in solidity, aztec contracts can define events which notify people that something has happened. in aztec however events can also be emitted privately, in which case only some users will learn of the event.
81+
82+
events are simply a struct marked with the `#[event]` macro:
83+
```noir
84+
#[event]
85+
struct Transfer {
86+
from: AztecAddress,
87+
to: AztecAddress,
88+
amount: 128,
89+
}
90+
```
91+
92+
## Functions
93+
94+
contracts are interacted with by invoking their `external` functions. there are three kinds of `external` functions:
95+
96+
- external private functions, which reveal nothing about their execution and are run on the user's device, producing a zero knowledge proof that is sent to the network as part of a transaction.
97+
- external public functions, which are invoked publicly by nodes in the network (like any `external` Solidity contract function) as they process transactions
98+
- external utility functions, which are executed on the user's device by applications in order to display useful information, e.g. retrieve contract state. these are never part of a transaction
99+
100+
```noir
101+
use aztec::macros::aztec;
102+
103+
#[aztec]
104+
contract MyContract {
105+
use aztec::macros::functions::external;
106+
107+
#[external("private")]
108+
fn my_private_function(parameter_a: u128, parameter_b: AztecAddress) {
109+
// ...
110+
}
111+
112+
#[external("public")]
113+
fn my_public_function(parameter_a: u128, parameter_b: AztecAddress) {
114+
// ...
115+
}
116+
117+
#[external("utility")]
118+
fn my_utility_function(parameter_a: u128, parameter_b: AztecAddress) {
119+
// ...
120+
}
121+
}
122+
```
123+
124+
additionally, contracts can also define `internal` functions, which cannot be called by other contracts (like any `internal` Solidity function). these exist to help organize the user's code, reuse functionality, etc.
125+
126+
// show an internal fn being called from one or two external ones. this feature is not yet complete in aztecnr
127+
128+
### Current Limitations
129+
130+
all #[external] contract functions must be defined _directly inside the `contract` block_, that is, in the same file. it is possible to define `#[internal]` and helper functions in `mod`s in other files, but not `#[external]` functions.
131+
132+
additionally, noir does not feature inheritance nor is there currently any other mechanism to extend and reuse contract logic. e.g. it is not possible to take a token contract and extend it to add minting functionality, or to reuse it in a liquidity pool. like in vyper, the entire logic must live in a single file.
133+
134+
we expect to lift some of these restrictions sometime after the release of noir 1.0.

docs/pages/aztec-nr/framework-description/index.md

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

0 commit comments

Comments
 (0)