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
144 changes: 72 additions & 72 deletions starter-token/reference/contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,84 @@ use aztec::macros::aztec;

#[aztec]
pub contract StarterToken {
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>
}

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

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

let recipient_balance = storage.balances.at(to).read();
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();
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>
}

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

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

let recipient_balance = storage.balances.at(to).read();
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();

assert(sender_balance >= amount, "Insufficient balance");

storage.balances.at(sender).write(sender_balance - amount);

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

#[public]
fn transfer_ownership(new_owner: AztecAddress) {
assert_eq(context.msg_sender(), storage.owner.read());
storage.owner.write(new_owner);
}

assert(sender_balance >= amount, "Insufficient balance");

storage.balances.at(sender).write(sender_balance - amount);

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

#[public]
fn transfer_ownership(new_owner: AztecAddress) {
assert_eq(context.msg_sender(), storage.owner.read());
storage.owner.write(new_owner);
}

// ===============
// ===============

#[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);
#[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(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();
#[private]
fn transfer_private(to: AztecAddress, amount: u64) {
let sender = context.msg_sender();

storage.private_balances.at(sender).sub(amount, sender);
storage.private_balances.at(sender).sub(amount, sender);

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

#[utility]
unconstrained fn view_private_balance(owner: AztecAddress) -> Field {
storage.private_balances.at(owner).get_value()
}
#[utility]
unconstrained fn view_private_balance(owner: AztecAddress) -> Field {
storage.private_balances.at(owner).get_value()
}

#[public]
#[internal]
fn _assert_is_owner(maybe_owner: AztecAddress) {
assert_eq(maybe_owner, storage.owner.read());
}
#[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/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub contract ExternalCall {
use dep::starter_token::StarterToken;

#[private]
fn call_mint_on_other_contract(contract_address: AztecAddress, to: AztecAddress, amount: u128) {
fn call_mint_on_other_contract(contract_address: AztecAddress, to: AztecAddress, amount: u64) {
StarterToken::at(contract_address).mint_private(to, amount).call(&mut context);
}
}
4 changes: 2 additions & 2 deletions starter-token/reference/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"start": "npm run build && node dist/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
18 changes: 8 additions & 10 deletions starter-token/reference/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { GettingStartedContract } from '../artifacts/GettingStarted.js';
import {
Fr,
createPXEClient,
waitForPXE,
} from '@aztec/aztec.js';
import { createPXEClient, waitForPXE } from '@aztec/aztec.js';
import { StarterTokenContract } from '../artifacts/StarterToken.js';
import { getInitialTestAccountsWallets } from '@aztec/accounts/testing';

const pxe = createPXEClient('http://localhost:8080');
await waitForPXE(pxe);

const wallets = await getInitialTestAccountsWallets(pxe);
const deployerWallet = wallets[0];
const deployerAddress = deployerWallet.getAddress();

const contractDeploymentSalt = Fr.random();
const gettingStartedContract = await GettingStartedContract
const starterTokenContract = await StarterTokenContract
.deploy(deployerWallet)
.send({ contractAddressSalt: contractDeploymentSalt }).wait();
.send({
from: deployerAddress
}).wait();

console.log('Contract Address', gettingStartedContract.contract.address);
console.log(starterTokenContract.contract.address);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub contract ExternalCall {
use dep::starter_token::StarterToken;

#[private]
fn call_mint_on_other_contract(contract_address: AztecAddress, to: AztecAddress, amount: u128) {
fn call_mint_on_other_contract(contract_address: AztecAddress, to: AztecAddress, amount: u64) {
StarterToken::at(contract_address).mint_private(to, amount).call(&mut context);
}
}
Loading