@@ -13,12 +13,14 @@ pub contract StarterToken {
1313 protocol_types::address::AztecAddress ,
1414 };
1515
16+ use easy_private_state::EasyPrivateUint ;
17+
1618 #[storage]
1719 struct Storage <Context > {
1820 balances : Map <AztecAddress , PublicMutable <u128 , Context >, Context >,
1921 owner : PublicMutable <AztecAddress , Context >,
2022 // ===============
21- private_balances : Map <AztecAddress , PrivateSet < UintNote , Context >, Context >,
23+ private_balances : Map <AztecAddress , EasyPrivateUint < Context >, Context >,
2224 }
2325
2426 #[initializer]
@@ -67,38 +69,16 @@ pub contract StarterToken {
6769 fn mint_private (to : AztecAddress , amount : u128 ) {
6870 GettingStarted ::at (context .this_address ())._assert_is_owner (context .msg_sender ()).enqueue (&mut context );
6971
70- storage .private_balances .at (to )
71- .insert (UintNote ::new (value , to ))
72- .emit (encode_and_encrypt_note (&mut context , to ));
72+ storage .private_balances .at (to ).add (value , to );
7373 }
7474
7575 #[private]
7676 fn transfer_private (to : AztecAddress , amount : u128 ) {
7777 let sender = context .msg_sender ();
7878
79- // This can be optimized with a preprocessor
80- // This will fail in a case where the accumulated note value < amount, but we have more notes than what can be read in one iteration.
81- let notes = storage .private_balances .at (sender ).pop_notes (NoteGetterOptions ::new ());
82-
83- // This is a very naive approach that just consolidates all the user's notes into one change note.
84- let mut subtracted = 0 as u128 ;
85- for i in 0 ..notes .len () {
86- let note = notes .get_unchecked (i );
87- subtracted = subtracted + note .get_value ();
88- }
89-
90- assert (subtracted >= amount );
91-
92- storage .private_balances .at (to )
93- .insert (UintNote ::new (amount , to ))
94- .emit (encode_and_encrypt_note (&mut context , to ));
95-
96- let change = subtracted - amount ;
79+ storage .private_balances .at (sender ).sub (amount , sender );
9780
98- // This possibly creates a change note of 0, but that is okay in our case because we will be consolidating via this method
99- storage .private_balances .at (sender )
100- .insert (UintNote ::new (change , sender ))
101- .emit (encode_and_encrypt_note (&mut context , sender , sender ));
81+ storage .private_balances .at (to ).add (amount , to );
10282 }
10383
10484 #[public]
0 commit comments