You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/developers/guides/smart_contracts/define_functions.md
+43-8Lines changed: 43 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,22 +11,49 @@ There are several types of functions in Aztec contracts that correspond the the
11
11
- public functions
12
12
- utility functions
13
13
- view functions
14
-
-contract library methods
14
+
-internal functions
15
15
- initializer functions
16
+
- contract library methods
16
17
17
-
## Examples
18
-
19
-
### Private Functions
18
+
## Private Functions
20
19
21
20
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.
A public function is executed by the sequencer and has access to a state model that is very similar to that of the EVM and Ethereum. Even though they work in an EVM-like model for public transactions, they are able to write data into private storage that can be consumed later by a private function.
27
+
28
+
Read more about the concept of public functions [here](../../../aztec/smart_contracts/functions/attributes.md#public-functions).
29
+
30
+
Declare a public function in your contract using the `#[public]` function annotation.
31
+
32
+
#include_code mint noir-projects/noir-contracts/contracts/app/nft_contract/src/main.nr rust
33
+
34
+
## Utility Functions
35
+
36
+
Contract functions marked with `#[utility]` are used to perform state queries from an off-chain client (from both private and public state!) or to modify local contract-related PXE state (e.g. when processing logs in Aztec.nr), and are never included in any transaction. No guarantees are made on the correctness of the result since the entire execution is unconstrained and heavily reliant on [oracle calls](https://noir-lang.org/docs/explainers/explainer-oracle). Read more about the concept of utility functions [here](../../../aztec/smart_contracts/functions/attributes.md#utility-functions).
<!-- TODO: flesh out with info about all types -->
42
+
The #[view] attribute can be applied to a #[private] or a #[public] function and it guarantees that the function cannot modify any contract state (just like view functions in Solidity). This allows you to read private or public state by calling the function from another contract.
28
43
29
-
### Initializer Functions
44
+
For examples, to get the admin address from the NFT contract, you can use the `get_admin` function:
Internal functions are functions that are only callable within the same contract. They are not visible to other contracts and cannot be called from outside the contract.
51
+
52
+
Mark an internal function with the `#[internal]` attribute.
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
59
@@ -57,10 +84,18 @@ Initializers are commonly used to set an admin, such as this example:
57
84
58
85
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
86
60
-
## Multiple initializers
87
+
###Multiple initializers
61
88
62
89
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
90
64
91
Calling any one of the functions annotated with `#[initializer]` will mark the contract as initialized.
65
92
66
93
To see an initializer in action, follow the [Counter codealong tutorial](../../tutorials/contract_tutorials/counter_contract.md).
94
+
95
+
## Contract Library Methods
96
+
97
+
Contract library methods are functions that are used to implement the logic of a contract and reduce code duplication. When called by another function, they are inlined into the calling function. They are not visible to the outside world and are only callable within the same contract.
98
+
99
+
For example, the `subtract_balance` function in the simple token contract:
0 commit comments