diff --git a/.gitignore b/.gitignore index 1de5659..6ec4261 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -target \ No newline at end of file +target +.DS_Store \ No newline at end of file diff --git a/starter-token/reference/contract/Nargo.toml b/starter-token/reference/contract/Nargo.toml new file mode 100644 index 0000000..0b0ba79 --- /dev/null +++ b/starter-token/reference/contract/Nargo.toml @@ -0,0 +1,9 @@ +[package] +name = "starter_token_contract" +authors = [""] +compiler_version = ">=1.0.0" +type = "contract" + +[dependencies] +aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/aztec" } +easy_private_state = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/easy-easy_private-state" } diff --git a/starter-token/reference/contract/src/main.nr b/starter-token/reference/contract/src/main.nr new file mode 100644 index 0000000..29b1288 --- /dev/null +++ b/starter-token/reference/contract/src/main.nr @@ -0,0 +1,102 @@ +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 { + balances: Map, Context>, + owner: PublicMutable, + // =============== + private_balances: Map, 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(); + + assert(sender_balance >= amount, "Cannot transfer more than the balance of the user"); + + 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) { + let maybe_contract_owner = context.msg_sender(); + + assert_eq(maybe_owner, storage.owner.read()); + + storage.owner.write(new_owner); + } + + // =============== + + #[private] + fn mint_private(to: AztecAddress, amount: u128) { + GettingStarted::at(context.this_address())._assert_is_owner(context.msg_sender()).enqueue(&mut context); + + storage.private_balances.at(to).add(value, to); + } + + #[private] + fn transfer_private(to: AztecAddress, amount: u128) { + let sender = context.msg_sender(); + + storage.private_balances.at(sender).sub(amount, sender); + + storage.private_balances.at(to).add(amount, to); + } + + #[public] + #[internal] + fn _assert_is_owner(maybe_owner: AztecAddress) { + assert_eq(maybe_owner, storage.owner.read()); + } + + #[utility] + unconstrained fn balance_of(owner: AztecAddress) -> u128 { + let notes = storage.private_balances.at(owner).view_notes(NoteViewerOptions::new()); + + let mut amount = 0 as u128; + for i in 0..notes.len() { + let note = notes.get_unchecked(i); + amount = amount + note.get_value(); + } + + amount + } +} diff --git a/starter-token/reference/external-call-contract/Nargo.toml b/starter-token/reference/external-call-contract/Nargo.toml new file mode 100644 index 0000000..c0a6fec --- /dev/null +++ b/starter-token/reference/external-call-contract/Nargo.toml @@ -0,0 +1,9 @@ +[package] +name = "external_call_contract" +authors = [""] +compiler_version = ">=1.0.0" +type = "contract" + +[dependencies] +aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/aztec" } +starter_token = { path = "../nr" } diff --git a/starter-token/reference/external-call-contract/src/main.nr b/starter-token/reference/external-call-contract/src/main.nr new file mode 100644 index 0000000..b62b2ae --- /dev/null +++ b/starter-token/reference/external-call-contract/src/main.nr @@ -0,0 +1,16 @@ +use aztec::macros::aztec; + +#[aztec] +pub contract ExternalCall { + use aztec::{ + macros::functions::private, + protocol_types::address::AztecAddress, + }; + + use dep::starter_token::StarterToken; + + #[private] + fn call_mint_on_other_contract(contract_address: AztecAddress, to: AztecAddress, amount: u128) { + StarterToken::at(contract_address).mint_private(to, amount).call(&mut context); + } +} diff --git a/starter-token/reference/ts/package.json b/starter-token/reference/ts/package.json new file mode 100644 index 0000000..2d43e63 --- /dev/null +++ b/starter-token/reference/ts/package.json @@ -0,0 +1,19 @@ +{ + "name": "getting-started", + "version": "1.0.0", + "description": "", + "license": "ISC", + "author": "", + "type": "module", + "scripts": { + "build": "npx tsc", + "start": "npm run build && node dist/src/index.js" + }, + "dependencies": { + "@aztec/accounts": "^1.2.1", + "@aztec/aztec.js": "^1.2.1" + }, + "devDependencies": { + "typescript": "^5.8.3" + } +} diff --git a/starter-token/reference/ts/src/index.ts b/starter-token/reference/ts/src/index.ts new file mode 100644 index 0000000..9aa7b18 --- /dev/null +++ b/starter-token/reference/ts/src/index.ts @@ -0,0 +1,20 @@ +import { GettingStartedContract } from '../artifacts/GettingStarted.js'; +import { + Fr, + createPXEClient, + waitForPXE, +} from '@aztec/aztec.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 contractDeploymentSalt = Fr.random(); +const gettingStartedContract = await GettingStartedContract + .deploy(deployerWallet) + .send({ contractAddressSalt: contractDeploymentSalt }).wait(); + +console.log('Contract Address', gettingStartedContract.contract.address); diff --git a/starter-token/reference/ts/tsconfig.json b/starter-token/reference/ts/tsconfig.json new file mode 100644 index 0000000..2580288 --- /dev/null +++ b/starter-token/reference/ts/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "module": "nodenext", /* Specify what module code is generated. */ + "moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */ + "resolveJsonModule": true, /* Enable importing .json files. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + "strict": true, /* Enable all strict type-checking options. */ + "skipLibCheck": true, /* Skip type checking all .d.ts files. */ + "outDir": "./dist" + } +} diff --git a/starter-token/start-here/contract/Nargo.toml b/starter-token/start-here/contract/Nargo.toml new file mode 100644 index 0000000..364c3af --- /dev/null +++ b/starter-token/start-here/contract/Nargo.toml @@ -0,0 +1,8 @@ +[package] +name = "starter_token_contract" +authors = [""] +compiler_version = ">=1.0.0" +type = "contract" + +[dependencies] +aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/aztec" } diff --git a/starter-token/start-here/contract/src/main.nr b/starter-token/start-here/contract/src/main.nr new file mode 100644 index 0000000..08bcca9 --- /dev/null +++ b/starter-token/start-here/contract/src/main.nr @@ -0,0 +1,6 @@ +use aztec::macros::aztec; + +#[aztec] +pub contract StarterToken { + // Start here ! +} diff --git a/starter-token/start-here/external-call-contract/Nargo.toml b/starter-token/start-here/external-call-contract/Nargo.toml new file mode 100644 index 0000000..c0a6fec --- /dev/null +++ b/starter-token/start-here/external-call-contract/Nargo.toml @@ -0,0 +1,9 @@ +[package] +name = "external_call_contract" +authors = [""] +compiler_version = ">=1.0.0" +type = "contract" + +[dependencies] +aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v1.2.1", directory = "noir-projects/aztec-nr/aztec" } +starter_token = { path = "../nr" } diff --git a/starter-token/start-here/external-call-contract/src/main.nr b/starter-token/start-here/external-call-contract/src/main.nr new file mode 100644 index 0000000..b62b2ae --- /dev/null +++ b/starter-token/start-here/external-call-contract/src/main.nr @@ -0,0 +1,16 @@ +use aztec::macros::aztec; + +#[aztec] +pub contract ExternalCall { + use aztec::{ + macros::functions::private, + protocol_types::address::AztecAddress, + }; + + use dep::starter_token::StarterToken; + + #[private] + fn call_mint_on_other_contract(contract_address: AztecAddress, to: AztecAddress, amount: u128) { + StarterToken::at(contract_address).mint_private(to, amount).call(&mut context); + } +} diff --git a/starter-token/start-here/ts/package.json b/starter-token/start-here/ts/package.json new file mode 100644 index 0000000..2d43e63 --- /dev/null +++ b/starter-token/start-here/ts/package.json @@ -0,0 +1,19 @@ +{ + "name": "getting-started", + "version": "1.0.0", + "description": "", + "license": "ISC", + "author": "", + "type": "module", + "scripts": { + "build": "npx tsc", + "start": "npm run build && node dist/src/index.js" + }, + "dependencies": { + "@aztec/accounts": "^1.2.1", + "@aztec/aztec.js": "^1.2.1" + }, + "devDependencies": { + "typescript": "^5.8.3" + } +} diff --git a/starter-token/start-here/ts/src/index.ts b/starter-token/start-here/ts/src/index.ts new file mode 100644 index 0000000..20c7cc5 --- /dev/null +++ b/starter-token/start-here/ts/src/index.ts @@ -0,0 +1,3 @@ +import { StarterTokenContract } from '../artifacts/StarterToken.js'; + +// Start here ! \ No newline at end of file diff --git a/starter-token/start-here/ts/tsconfig.json b/starter-token/start-here/ts/tsconfig.json new file mode 100644 index 0000000..2580288 --- /dev/null +++ b/starter-token/start-here/ts/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "module": "nodenext", /* Specify what module code is generated. */ + "moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */ + "resolveJsonModule": true, /* Enable importing .json files. */ + "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ + "strict": true, /* Enable all strict type-checking options. */ + "skipLibCheck": true, /* Skip type checking all .d.ts files. */ + "outDir": "./dist" + } +}