Skip to content

Commit d11d4d6

Browse files
committed
add function type details
1 parent f741d51 commit d11d4d6

File tree

2 files changed

+45
-8
lines changed
  • docs/docs/developers/guides/smart_contracts
  • noir-projects/noir-contracts/contracts/app/simple_token_contract/src

2 files changed

+45
-8
lines changed

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,49 @@ There are several types of functions in Aztec contracts that correspond the the
1111
- public functions
1212
- utility functions
1313
- view functions
14-
- contract library methods
14+
- internal functions
1515
- initializer functions
16+
- contract library methods
1617

17-
## Examples
18-
19-
### Private Functions
18+
## Private Functions
2019

2120
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.
2221

2322
#include_code withdraw noir-projects/noir-contracts/contracts/app/escrow_contract/src/main.nr rust
2423

25-
### Public Functions
24+
## Public Functions
25+
26+
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).
37+
38+
#include_code get_private_nfts noir-projects/noir-contracts/contracts/app/nft_contract/src/main.nr rust
39+
40+
## View Functions
2641

27-
<!-- 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.
2843

29-
### Initializer Functions
44+
For examples, to get the admin address from the NFT contract, you can use the `get_admin` function:
45+
46+
#include_code admin noir-projects/noir-contracts/contracts/app/nft_contract/src/main.nr rust
47+
48+
## Internal Functions
49+
50+
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.
53+
54+
#include_code add_to_tally_public noir-projects/noir-contracts/contracts/app/easy_private_voting_contract/src/main.nr rust
55+
56+
## Initializer Functions
3057

3158
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.
3259

@@ -57,10 +84,18 @@ Initializers are commonly used to set an admin, such as this example:
5784

5885
Here, the initializer is writing to storage. It can also call another function. Learn more about calling functions from functions [here](./call_contracts.md).
5986

60-
## Multiple initializers
87+
### Multiple initializers
6188

6289
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.
6390

6491
Calling any one of the functions annotated with `#[initializer]` will mark the contract as initialized.
6592

6693
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:
100+
101+
#include_code subtract_balance noir-projects/noir-contracts/contracts/app/simple_token_contract/src/main.nr rust

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ pub contract SimpleToken {
321321
context.push_nullifier(nullifier);
322322
}
323323

324+
// docs:start:subtract_balance
324325
#[contract_library_method]
325326
fn subtract_balance(
326327
context: &mut PrivateContext,
@@ -338,6 +339,7 @@ pub contract SimpleToken {
338339
compute_recurse_subtract_balance_call(*context, account, remaining).call(context)
339340
}
340341
}
342+
// docs:end:subtract_balance
341343

342344
#[no_predicates]
343345
#[contract_library_method]

0 commit comments

Comments
 (0)