|
| 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 | + 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(); |
| 47 | + |
| 48 | + assert(sender_balance >= amount, "Cannot transfer more than the balance of the user"); |
| 49 | + |
| 50 | + storage.balances.at(sender).write(sender_balance - amount); |
| 51 | + |
| 52 | + let recipient_balance = storage.balances.at(to).read(); |
| 53 | + |
| 54 | + storage.balances.at(to).write(recipient_balance + amount); |
| 55 | + } |
| 56 | + |
| 57 | + #[public] |
| 58 | + fn transfer_ownership(new_owner: AztecAddress) { |
| 59 | + let maybe_contract_owner = context.msg_sender(); |
| 60 | + |
| 61 | + assert_eq(maybe_owner, storage.owner.read()); |
| 62 | + |
| 63 | + storage.owner.write(new_owner); |
| 64 | + } |
| 65 | + |
| 66 | + // =============== |
| 67 | + |
| 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); |
| 71 | + |
| 72 | + storage.private_balances.at(to).add(value, to); |
| 73 | + } |
| 74 | + |
| 75 | + #[private] |
| 76 | + fn transfer_private(to: AztecAddress, amount: u128) { |
| 77 | + let sender = context.msg_sender(); |
| 78 | + |
| 79 | + storage.private_balances.at(sender).sub(amount, sender); |
| 80 | + |
| 81 | + storage.private_balances.at(to).add(amount, to); |
| 82 | + } |
| 83 | + |
| 84 | + #[public] |
| 85 | + #[internal] |
| 86 | + fn _assert_is_owner(maybe_owner: AztecAddress) { |
| 87 | + assert_eq(maybe_owner, storage.owner.read()); |
| 88 | + } |
| 89 | + |
| 90 | + #[utility] |
| 91 | + unconstrained fn balance_of(owner: AztecAddress) -> u128 { |
| 92 | + let notes = storage.private_balances.at(owner).view_notes(NoteViewerOptions::new()); |
| 93 | + |
| 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 | + } |
| 99 | + |
| 100 | + amount |
| 101 | + } |
| 102 | +} |
0 commit comments