Skip to content

Commit 76e25d1

Browse files
author
Esau
committed
init
ver rename nr to contract modify
1 parent 486aa01 commit 76e25d1

File tree

15 files changed

+264
-1
lines changed

15 files changed

+264
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
target
1+
target
2+
.DS_Store
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "starter_token_contract"
3+
authors = [""]
4+
compiler_version = ">=1.0.0"
5+
type = "contract"
6+
7+
[dependencies]
8+
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/aztec" }
9+
easy_private_state = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/easy-easy_private-state" }
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "external_call_contract"
3+
authors = [""]
4+
compiler_version = ">=1.0.0"
5+
type = "contract"
6+
7+
[dependencies]
8+
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/aztec" }
9+
starter_token = { path = "../nr" }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use aztec::macros::aztec;
2+
3+
#[aztec]
4+
pub contract ExternalCall {
5+
use aztec::{
6+
macros::functions::private,
7+
protocol_types::address::AztecAddress,
8+
};
9+
10+
use dep::starter_token::StarterToken;
11+
12+
#[private]
13+
fn call_mint_on_other_contract(contract_address: AztecAddress, to: AztecAddress, amount: u128) {
14+
StarterToken::at(contract_address).mint_private(to, amount).call(&mut context);
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "getting-started",
3+
"version": "1.0.0",
4+
"description": "",
5+
"license": "ISC",
6+
"author": "",
7+
"type": "module",
8+
"scripts": {
9+
"build": "npx tsc",
10+
"start": "npm run build && node dist/src/index.js"
11+
},
12+
"dependencies": {
13+
"@aztec/accounts": "^1.2.1",
14+
"@aztec/aztec.js": "^1.2.1"
15+
},
16+
"devDependencies": {
17+
"typescript": "^5.8.3"
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { GettingStartedContract } from '../artifacts/GettingStarted.js';
2+
import {
3+
Fr,
4+
createPXEClient,
5+
waitForPXE,
6+
} from '@aztec/aztec.js';
7+
import { getInitialTestAccountsWallets } from '@aztec/accounts/testing';
8+
9+
const pxe = createPXEClient('http://localhost:8080');
10+
await waitForPXE(pxe);
11+
12+
const wallets = await getInitialTestAccountsWallets(pxe);
13+
const deployerWallet = wallets[0];
14+
15+
const contractDeploymentSalt = Fr.random();
16+
const gettingStartedContract = await GettingStartedContract
17+
.deploy(deployerWallet)
18+
.send({ contractAddressSalt: contractDeploymentSalt }).wait();
19+
20+
console.log('Contract Address', gettingStartedContract.contract.address);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
4+
"module": "nodenext", /* Specify what module code is generated. */
5+
"moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */
6+
"resolveJsonModule": true, /* Enable importing .json files. */
7+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
8+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
9+
"strict": true, /* Enable all strict type-checking options. */
10+
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
11+
"outDir": "./dist"
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "starter_token_contract"
3+
authors = [""]
4+
compiler_version = ">=1.0.0"
5+
type = "contract"
6+
7+
[dependencies]
8+
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/aztec" }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use aztec::macros::aztec;
2+
3+
#[aztec]
4+
pub contract StarterToken {
5+
// Start here !
6+
}

0 commit comments

Comments
 (0)