|
| 1 | +use aztec::macros::aztec; |
| 2 | + |
| 3 | +#[aztec] |
| 4 | +pub contract GettingStarted { |
| 5 | + use aztec::{ |
| 6 | + state_vars::{private_mutable::PrivateMutable, private_set::PrivateSet, public_mutable::PublicMutable, map::Map}, |
| 7 | + messages::logs::note::encode_and_encrypt_note_unconstrained, |
| 8 | + note::{constants::MAX_NOTES_PER_PAGE, note_viewer_options::NoteViewerOptions}, |
| 9 | + |
| 10 | + macros::{ |
| 11 | + functions::{initializer, private, public, utility, internal}, |
| 12 | + storage::storage, |
| 13 | + }, |
| 14 | + protocol_types::{ |
| 15 | + address::AztecAddress, |
| 16 | + }, |
| 17 | + }; |
| 18 | + |
| 19 | + use uint_note::uint_note::UintNote; |
| 20 | + |
| 21 | + #[storage] |
| 22 | + struct Storage<Context> { |
| 23 | + contract_private_state: PrivateMutable<UintNote, Context>, |
| 24 | + user_private_state: Map<AztecAddress, PrivateSet<UintNote, Context>, Context>, |
| 25 | + contract_public_state: PublicMutable<u128, Context>, |
| 26 | + user_public_state: Map<AztecAddress, PublicMutable<u128, Context>, Context>, |
| 27 | + owner: PublicMutable<AztecAddress, Context>, |
| 28 | + } |
| 29 | + |
| 30 | + #[initializer] |
| 31 | + #[public] |
| 32 | + fn setup() { |
| 33 | + storage.owner.write(context.msg_sender()); |
| 34 | + } |
| 35 | + |
| 36 | + #[private] |
| 37 | + fn create_note_for_user(value: u128) { |
| 38 | + let note_owner = context.msg_sender(); |
| 39 | + storage.user_private_state.at(note_owner) |
| 40 | + // randomness should actually be random, but is a workaround because we can't recover it now |
| 41 | + .insert(UintNote { value, owner: note_owner, randomness: 6969 }) |
| 42 | + .emit(encode_and_encrypt_note_unconstrained(&mut context, note_owner, note_owner)); |
| 43 | + } |
| 44 | + |
| 45 | + #[public] |
| 46 | + fn modify_public_state_for_user(value: u128) { |
| 47 | + storage.user_public_state.at(context.msg_sender()).write(value); |
| 48 | + } |
| 49 | + |
| 50 | + #[private] |
| 51 | + fn modify_private_state_for_contract(value: u128) { |
| 52 | + let maybe_contract_owner = context.msg_sender(); |
| 53 | + |
| 54 | + storage.contract_private_state.initialize_or_replace(UintNote::new(value, maybe_contract_owner)) |
| 55 | + .emit(encode_and_encrypt_note_unconstrained(&mut context, maybe_contract_owner, maybe_contract_owner)); |
| 56 | + |
| 57 | + GettingStarted::at(context.this_address())._assert_is_owner(maybe_contract_owner).enqueue(&mut context); |
| 58 | + } |
| 59 | + |
| 60 | + #[public] |
| 61 | + #[internal] |
| 62 | + fn _assert_is_owner(maybe_owner: AztecAddress) { |
| 63 | + assert_eq(maybe_owner, storage.owner.read()); |
| 64 | + } |
| 65 | + |
| 66 | + #[public] |
| 67 | + fn modify_public_state_for_contract(value: u128) { |
| 68 | + let maybe_contract_owner = context.msg_sender(); |
| 69 | + |
| 70 | + GettingStarted::at(context.this_address())._assert_is_owner(maybe_contract_owner).call(&mut context); |
| 71 | + |
| 72 | + storage.user_public_state.at(context.msg_sender()).write(value); |
| 73 | + } |
| 74 | + |
| 75 | + #[utility] |
| 76 | + unconstrained fn view_created_notes(owner: AztecAddress) -> BoundedVec<UintNote, MAX_NOTES_PER_PAGE> { |
| 77 | + storage.user_private_state.at(owner).view_notes(NoteViewerOptions::new()) |
| 78 | + } |
| 79 | +} |
0 commit comments