|
| 1 | +use aztec::macros::aztec; |
| 2 | + |
| 3 | +#[aztec] |
| 4 | +pub 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 | + #[storage] |
| 17 | + struct Storage<Context> { |
| 18 | + balances: Map<AztecAddress, PublicMutable<u128, Context>, Context>, |
| 19 | + owner: PublicMutable<AztecAddress, Context>, |
| 20 | + // =============== |
| 21 | + private_balances: Map<AztecAddress, PrivateSet<UintNote, Context>, Context>, |
| 22 | + } |
| 23 | + |
| 24 | + #[initializer] |
| 25 | + #[public] |
| 26 | + fn setup() { |
| 27 | + // The deployer (msg_sender) becomes the owner |
| 28 | + storage.owner.write(context.msg_sender()); |
| 29 | + } |
| 30 | + |
| 31 | + #[public] |
| 32 | + fn mint(to: AztecAddress, amount: u128) { |
| 33 | + assert_eq(maybe_owner, storage.owner.read()); |
| 34 | + |
| 35 | + let recipient_balance = storage.balances.at(to).read(); |
| 36 | + |
| 37 | + storage.balances.at(context.msg_sender()).write(recipient_balance + amount); |
| 38 | + } |
| 39 | + |
| 40 | + #[public] |
| 41 | + fn transfer(to: AztecAddress, amount: u128) { |
| 42 | + let sender = context.msg_sender(); |
| 43 | + |
| 44 | + let sender_balance = storage.balances.at(sender).read(); |
| 45 | + |
| 46 | + assert(sender_balance >= amount, "Cannot transfer more than the balance of the user"); |
| 47 | + |
| 48 | + storage.balances.at(sender).write(sender_balance - amount); |
| 49 | + |
| 50 | + let recipient_balance = storage.balances.at(to).read(); |
| 51 | + |
| 52 | + storage.balances.at(to).write(recipient_balance + amount); |
| 53 | + } |
| 54 | + |
| 55 | + #[public] |
| 56 | + fn transfer_ownership(new_owner: AztecAddress) { |
| 57 | + let maybe_contract_owner = context.msg_sender(); |
| 58 | + |
| 59 | + assert_eq(maybe_owner, storage.owner.read()); |
| 60 | + |
| 61 | + storage.owner.write(new_owner); |
| 62 | + } |
| 63 | + |
| 64 | + // =============== |
| 65 | + |
| 66 | + #[private] |
| 67 | + fn mint_private(to: AztecAddress, amount: u128) { |
| 68 | + GettingStarted::at(context.this_address())._assert_is_owner(context.msg_sender()).enqueue(&mut context); |
| 69 | + |
| 70 | + storage.private_balances.at(to) |
| 71 | + .insert(UintNote::new(value, to)) |
| 72 | + .emit(encode_and_encrypt_note(&mut context, to)); |
| 73 | + } |
| 74 | + |
| 75 | + #[private] |
| 76 | + fn transfer_private(to: AztecAddress, amount: u128) { |
| 77 | + let sender = context.msg_sender(); |
| 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; |
| 97 | + |
| 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)); |
| 102 | + } |
| 103 | + |
| 104 | + #[public] |
| 105 | + #[internal] |
| 106 | + fn _assert_is_owner(maybe_owner: AztecAddress) { |
| 107 | + assert_eq(maybe_owner, storage.owner.read()); |
| 108 | + } |
| 109 | + |
| 110 | + #[utility] |
| 111 | + unconstrained fn balance_of(owner: AztecAddress) -> u128 { |
| 112 | + let notes = storage.private_balances.at(owner).view_notes(NoteViewerOptions::new()); |
| 113 | + |
| 114 | + let mut amount = 0 as u128; |
| 115 | + for i in 0..notes.len() { |
| 116 | + let note = notes.get_unchecked(i); |
| 117 | + amount = amount + note.get_value(); |
| 118 | + } |
| 119 | + |
| 120 | + amount |
| 121 | + } |
| 122 | +} |
0 commit comments