Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions starter-token/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
artifacts
codegenCache.json
4 changes: 2 additions & 2 deletions starter-token/reference/contract/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ compiler_version = ">=1.0.0"
type = "contract"

[dependencies]
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.0", directory = "noir-projects/aztec-nr/aztec" }
easy_private_state = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.0", directory = "noir-projects/aztec-nr/easy-easy_private-state" }
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v2.0.2", directory = "noir-projects/aztec-nr/aztec" }
easy_private_state = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v2.0.2", directory = "noir-projects/aztec-nr/easy-private-state" }
145 changes: 64 additions & 81 deletions starter-token/reference/contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,84 @@ use aztec::macros::aztec;

#[aztec]
pub contract StarterToken {
use aztec::{
state_vars::{private_set::PrivateSet, public_mutable::PublicMutable, map::Map},
messages::logs::note::encode_and_encrypt_note,
note::note_viewer_options::NoteViewerOptions,
macros::{
functions::{initializer, private, public, utility, internal},
storage::storage,
},
protocol_types::address::AztecAddress,
};

use easy_private_state::EasyPrivateUint;

#[storage]
struct Storage<Context> {
balances: Map<AztecAddress, PublicMutable<u128, Context>, Context>,
owner: PublicMutable<AztecAddress, Context>,
// ===============
private_balances: Map<AztecAddress, EasyPrivateUint<Context>, Context>,
}

#[initializer]
#[public]
fn setup() {
// The deployer (msg_sender) becomes the owner
storage.owner.write(context.msg_sender());
}

#[public]
fn mint(to: AztecAddress, amount: u128) {
assert_eq(maybe_owner, storage.owner.read());

let recipient_balance = storage.balances.at(to).read();

storage.balances.at(context.msg_sender()).write(recipient_balance + amount);
}

#[public]
fn transfer(to: AztecAddress, amount: u128) {
let sender = context.msg_sender();

let sender_balance = storage.balances.at(sender).read();
use aztec::macros::{
functions::{initializer, private, public, utility, internal},
storage::storage,
};
use aztec::state_vars::{PublicMutable, Map};
use aztec::protocol_types::address::AztecAddress;

use easy_private_state::EasyPrivateUint;

#[storage]
struct Storage<Context> {
owner: PublicMutable<AztecAddress, Context>,
balances: Map<AztecAddress, PublicMutable<u128, Context>, Context>,
// ===============
private_balances: Map<AztecAddress, EasyPrivateUint<Context>, Context>
}

assert(sender_balance >= amount, "Cannot transfer more than the balance of the user");
#[initializer]
#[public]
fn setup() {
// The deployer becomes the owner
storage.owner.write(context.msg_sender());
}

storage.balances.at(sender).write(sender_balance - amount);
#[public]
fn mint(to: AztecAddress, amount: u128) {
assert_eq(context.msg_sender(), storage.owner.read());

let recipient_balance = storage.balances.at(to).read();
let recipient_balance = storage.balances.at(to).read();
storage.balances.at(to).write(recipient_balance + amount);
}

storage.balances.at(to).write(recipient_balance + amount);
}
#[public]
fn transfer(to: AztecAddress, amount: u128) {
let sender = context.msg_sender();
let sender_balance = storage.balances.at(sender).read();

#[public]
fn transfer_ownership(new_owner: AztecAddress) {
let maybe_contract_owner = context.msg_sender();
assert(sender_balance >= amount, "Insufficient balance");

assert_eq(maybe_owner, storage.owner.read());
storage.balances.at(sender).write(sender_balance - amount);

storage.owner.write(new_owner);
}

// ===============
let recipient_balance = storage.balances.at(to).read();
storage.balances.at(to).write(recipient_balance + amount);
}

#[private]
fn mint_private(to: AztecAddress, amount: u128) {
GettingStarted::at(context.this_address())._assert_is_owner(context.msg_sender()).enqueue(&mut context);
#[public]
fn transfer_ownership(new_owner: AztecAddress) {
assert_eq(context.msg_sender(), storage.owner.read());
storage.owner.write(new_owner);
}

storage.private_balances.at(to).add(value, to);
}
// ===============

#[private]
fn transfer_private(to: AztecAddress, amount: u128) {
let sender = context.msg_sender();
#[private]
fn mint_private(to: AztecAddress, amount: u64) {
// Enqueue public validation
StarterToken::at(context.this_address())._assert_is_owner(context.msg_sender()).enqueue(&mut context);

storage.private_balances.at(sender).sub(amount, sender);
storage.private_balances.at(to).add(amount, to);
}

storage.private_balances.at(to).add(amount, to);
}
#[private]
fn transfer_private(to: AztecAddress, amount: u64) {
let sender = context.msg_sender();

#[public]
#[internal]
fn _assert_is_owner(maybe_owner: AztecAddress) {
assert_eq(maybe_owner, storage.owner.read());
}
storage.private_balances.at(sender).sub(amount, sender);

#[utility]
unconstrained fn balance_of(owner: AztecAddress) -> u128 {
let notes = storage.private_balances.at(owner).view_notes(NoteViewerOptions::new());
storage.private_balances.at(to).add(amount, to);
}

let mut amount = 0 as u128;
for i in 0..notes.len() {
let note = notes.get_unchecked(i);
amount = amount + note.get_value();
}
#[utility]
unconstrained fn view_private_balance(owner: AztecAddress) -> Field {
storage.private_balances.at(owner).get_value()
}

amount
}
#[public]
#[internal]
fn _assert_is_owner(maybe_owner: AztecAddress) {
assert_eq(maybe_owner, storage.owner.read());
}
}
2 changes: 1 addition & 1 deletion starter-token/reference/external-call-contract/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ compiler_version = ">=1.0.0"
type = "contract"

[dependencies]
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.0", directory = "noir-projects/aztec-nr/aztec" }
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v2.0.2", directory = "noir-projects/aztec-nr/aztec" }
starter_token = { path = "../nr" }
3 changes: 2 additions & 1 deletion starter-token/start-here/contract/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ compiler_version = ">=1.0.0"
type = "contract"

[dependencies]
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.0", directory = "noir-projects/aztec-nr/aztec" }
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v2.0.2", directory = "noir-projects/aztec-nr/aztec" }
easy_private_state = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v2.0.2", directory = "noir-projects/aztec-nr/easy-private-state" }
2 changes: 1 addition & 1 deletion starter-token/start-here/external-call-contract/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ compiler_version = ">=1.0.0"
type = "contract"

[dependencies]
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.0", directory = "noir-projects/aztec-nr/aztec" }
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v2.0.2", directory = "noir-projects/aztec-nr/aztec" }
starter_token = { path = "../nr" }
6 changes: 3 additions & 3 deletions starter-token/start-here/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"type": "module",
"scripts": {
"build": "npx tsc",
"start": "npm run build && node dist/src/index.js"
"start": "npm run build && node dist/ts/src/index.js"
},
"dependencies": {
"@aztec/accounts": "^1.2.0",
"@aztec/aztec.js": "^1.2.0"
"@aztec/accounts": "2.0.2",
"@aztec/aztec.js": "2.0.2"
},
"devDependencies": {
"typescript": "^5.8.3"
Expand Down
Loading
Loading