-
Notifications
You must be signed in to change notification settings - Fork 151
/
main.nr
53 lines (44 loc) · 1.7 KB
/
main.nr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// docs:start:easy_private_token_contract
contract EasyPrivateToken {
use dep::aztec::prelude::{AztecAddress, NoteHeader, Map};
use dep::value_note::balance_utils;
use dep::easy_private_state::EasyPrivateUint;
#[aztec(storage)]
struct Storage {
balances: Map<AztecAddress, EasyPrivateUint>,
}
/**
* initialize the contract's initial state variables.
*/
#[aztec(private)]
#[aztec(initializer)]
fn constructor(initial_supply: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let balances = storage.balances;
balances.at(owner).add(initial_supply, owner, outgoing_viewer);
}
// Mints `amount` of tokens to `owner`.
#[aztec(private)]
fn mint(amount: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let balances = storage.balances;
balances.at(owner).add(amount, owner, outgoing_viewer);
}
// Transfers `amount` of tokens from `sender` to a `recipient`.
#[aztec(private)]
fn transfer(
amount: u64,
sender: AztecAddress,
recipient: AztecAddress,
outgoing_viewer: AztecAddress
) {
let balances = storage.balances;
balances.at(sender).sub(amount, sender, outgoing_viewer);
balances.at(recipient).add(amount, recipient, outgoing_viewer);
}
// Helper function to get the balance of a user ("unconstrained" is a Noir alternative of Solidity's "view" function).
unconstrained fn get_balance(owner: AztecAddress) -> pub Field {
let balances = storage.balances;
// Return the sum of all notes in the set.
balance_utils::get_balance(balances.at(owner).set)
}
}
// docs:end:easy_private_token_contract