@@ -2,101 +2,84 @@ use aztec::macros::aztec;
22
33#[aztec]
44pub contract StarterToken {
5- use aztec:: {
6- state_vars ::{private_set::PrivateSet , public_mutable::PublicMutable , map::Map },
7- messages::logs::note:: encode_and_encrypt_note ,
8- note::note_viewer_options::NoteViewerOptions ,
9- macros ::{
10- functions ::{initializer , private , public , utility , internal },
11- storage:: storage ,
12- },
13- protocol_types::address::AztecAddress ,
14- };
15-
16- use easy_private_state::EasyPrivateUint ;
17-
18- #[storage]
19- struct Storage <Context > {
20- balances : Map <AztecAddress , PublicMutable <u128 , Context >, Context >,
21- owner : PublicMutable <AztecAddress , Context >,
22- // ===============
23- private_balances : Map <AztecAddress , EasyPrivateUint <Context >, Context >,
24- }
25-
26- #[initializer]
27- #[public]
28- fn setup () {
29- // The deployer (msg_sender) becomes the owner
30- storage .owner .write (context .msg_sender ());
31- }
32-
33- #[public]
34- fn mint (to : AztecAddress , amount : u128 ) {
35- assert_eq (maybe_owner , storage .owner .read ());
36-
37- let recipient_balance = storage .balances .at (to ).read ();
38-
39- storage .balances .at (context .msg_sender ()).write (recipient_balance + amount );
40- }
41-
42- #[public]
43- fn transfer (to : AztecAddress , amount : u128 ) {
44- let sender = context .msg_sender ();
45-
46- let sender_balance = storage .balances .at (sender ).read ();
5+ use aztec::macros:: {
6+ functions ::{initializer , private , public , utility , internal },
7+ storage:: storage ,
8+ };
9+ use aztec::state_vars:: {PublicMutable , Map };
10+ use aztec::protocol_types::address::AztecAddress ;
11+
12+ use easy_private_state::EasyPrivateUint ;
13+
14+ #[storage]
15+ struct Storage <Context > {
16+ owner : PublicMutable <AztecAddress , Context >,
17+ balances : Map <AztecAddress , PublicMutable <u128 , Context >, Context >,
18+ // ===============
19+ private_balances : Map <AztecAddress , EasyPrivateUint <Context >, Context >
20+ }
4721
48- assert (sender_balance >= amount , "Cannot transfer more than the balance of the user" );
22+ #[initializer]
23+ #[public]
24+ fn setup () {
25+ // The deployer becomes the owner
26+ storage .owner .write (context .msg_sender ());
27+ }
4928
50- storage .balances .at (sender ).write (sender_balance - amount );
29+ #[public]
30+ fn mint (to : AztecAddress , amount : u128 ) {
31+ assert_eq (context .msg_sender (), storage .owner .read ());
5132
52- let recipient_balance = storage .balances .at (to ).read ();
33+ let recipient_balance = storage .balances .at (to ).read ();
34+ storage .balances .at (to ).write (recipient_balance + amount );
35+ }
5336
54- storage .balances .at (to ).write (recipient_balance + amount );
55- }
37+ #[public]
38+ fn transfer (to : AztecAddress , amount : u128 ) {
39+ let sender = context .msg_sender ();
40+ let sender_balance = storage .balances .at (sender ).read ();
5641
57- #[public]
58- fn transfer_ownership (new_owner : AztecAddress ) {
59- let maybe_contract_owner = context .msg_sender ();
42+ assert (sender_balance >= amount , "Insufficient balance" );
6043
61- assert_eq ( maybe_owner , storage .owner . read () );
44+ storage .balances . at ( sender ). write ( sender_balance - amount );
6245
63- storage .owner .write (new_owner );
64- }
65-
66- // ===============
46+ let recipient_balance = storage .balances .at (to ).read ();
47+ storage .balances .at (to ).write (recipient_balance + amount );
48+ }
6749
68- #[private]
69- fn mint_private (to : AztecAddress , amount : u128 ) {
70- GettingStarted ::at (context .this_address ())._assert_is_owner (context .msg_sender ()).enqueue (&mut context );
50+ #[public]
51+ fn transfer_ownership (new_owner : AztecAddress ) {
52+ assert_eq (context .msg_sender (), storage .owner .read ());
53+ storage .owner .write (new_owner );
54+ }
7155
72- storage .private_balances .at (to ).add (value , to );
73- }
56+ // ===============
7457
75- #[private]
76- fn transfer_private (to : AztecAddress , amount : u128 ) {
77- let sender = context .msg_sender ();
58+ #[private]
59+ fn mint_private (to : AztecAddress , amount : u64 ) {
60+ // Enqueue public validation
61+ StarterToken ::at (context .this_address ())._assert_is_owner (context .msg_sender ()).enqueue (&mut context );
7862
79- storage .private_balances .at (sender ).sub (amount , sender );
63+ storage .private_balances .at (to ).add (amount , to );
64+ }
8065
81- storage .private_balances .at (to ).add (amount , to );
82- }
66+ #[private]
67+ fn transfer_private (to : AztecAddress , amount : u64 ) {
68+ let sender = context .msg_sender ();
8369
84- #[public]
85- #[internal]
86- fn _assert_is_owner (maybe_owner : AztecAddress ) {
87- assert_eq (maybe_owner , storage .owner .read ());
88- }
70+ storage .private_balances .at (sender ).sub (amount , sender );
8971
90- #[utility]
91- unconstrained fn balance_of (owner : AztecAddress ) -> u128 {
92- let notes = storage .private_balances .at (owner ).view_notes (NoteViewerOptions ::new ());
72+ storage .private_balances .at (to ).add (amount , to );
73+ }
9374
94- let mut amount = 0 as u128 ;
95- for i in 0 ..notes .len () {
96- let note = notes .get_unchecked (i );
97- amount = amount + note .get_value ();
98- }
75+ #[utility]
76+ unconstrained fn view_private_balance (owner : AztecAddress ) -> Field {
77+ storage .private_balances .at (owner ).get_value ()
78+ }
9979
100- amount
101- }
80+ #[public]
81+ #[internal]
82+ fn _assert_is_owner (maybe_owner : AztecAddress ) {
83+ assert_eq (maybe_owner , storage .owner .read ());
84+ }
10285}
0 commit comments