@@ -13,12 +13,14 @@ pub contract StarterToken {
13
13
protocol_types::address::AztecAddress ,
14
14
};
15
15
16
+ use easy_private_state::EasyPrivateUint ;
17
+
16
18
#[storage]
17
19
struct Storage <Context > {
18
20
balances : Map <AztecAddress , PublicMutable <u128 , Context >, Context >,
19
21
owner : PublicMutable <AztecAddress , Context >,
20
22
// ===============
21
- private_balances : Map <AztecAddress , PrivateSet < UintNote , Context >, Context >,
23
+ private_balances : Map <AztecAddress , EasyPrivateUint < Context >, Context >,
22
24
}
23
25
24
26
#[initializer]
@@ -67,38 +69,16 @@ pub contract StarterToken {
67
69
fn mint_private (to : AztecAddress , amount : u128 ) {
68
70
GettingStarted ::at (context .this_address ())._assert_is_owner (context .msg_sender ()).enqueue (&mut context );
69
71
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 );
73
73
}
74
74
75
75
#[private]
76
76
fn transfer_private (to : AztecAddress , amount : u128 ) {
77
77
let sender = context .msg_sender ();
78
78
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 );
97
80
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 );
102
82
}
103
83
104
84
#[public]
0 commit comments