From 1dce274d59baf4324b60949b07c02a4b1f7cdeed Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Tue, 27 Feb 2024 22:42:58 +0000 Subject: [PATCH 1/3] avm_token_contract --- noir-projects/noir-contracts/Nargo.toml | 1 + .../contracts/avm_token_contract/Nargo.toml | 8 +++ .../contracts/avm_token_contract/src/main.nr | 51 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 noir-projects/noir-contracts/contracts/avm_token_contract/Nargo.toml create mode 100644 noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr diff --git a/noir-projects/noir-contracts/Nargo.toml b/noir-projects/noir-contracts/Nargo.toml index 61eeb44fa70..bb616b86c14 100644 --- a/noir-projects/noir-contracts/Nargo.toml +++ b/noir-projects/noir-contracts/Nargo.toml @@ -2,6 +2,7 @@ members = [ "contracts/app_subscription_contract", "contracts/avm_test_contract", + "contracts/avm_token_contract", "contracts/fpc_contract", "contracts/benchmarking_contract", "contracts/card_game_contract", diff --git a/noir-projects/noir-contracts/contracts/avm_token_contract/Nargo.toml b/noir-projects/noir-contracts/contracts/avm_token_contract/Nargo.toml new file mode 100644 index 00000000000..d8dee0090a4 --- /dev/null +++ b/noir-projects/noir-contracts/contracts/avm_token_contract/Nargo.toml @@ -0,0 +1,8 @@ +[package] +name = "avm_token_contract" +authors = [""] +compiler_version = ">=0.18.0" +type = "contract" + +[dependencies] +aztec = { path = "../../../aztec-nr/aztec" } diff --git a/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr new file mode 100644 index 00000000000..dbf04d0d554 --- /dev/null +++ b/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr @@ -0,0 +1,51 @@ +contract AvmToken { + // Libs + use dep::aztec::protocol_types::address::{AztecAddress, EthAddress}; + use dep::std::option::Option; + // use dep::compressed_string::FieldCompressedString; + + use dep::aztec::{ + note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader, utils as note_utils}, + hash::{compute_secret_hash}, + state_vars::{map::Map, public_state::PublicState, stable_public_state::StablePublicState, set::Set}, + protocol_types::{abis::function_selector::FunctionSelector} + }; + + struct Storage { + total_supply: PublicState, + owner: PublicState, + balances: Map> + } + + #[aztec(private)] + fn constructor(owner: AztecAddress) { + let selector = FunctionSelector::from_signature("_initialize((Field))"); + context.call_public_function(context.this_address(), selector, [owner.to_field()]); + } + + #[aztec(public-vm)] + internal fn _initialize(new_owner: AztecAddress) { + assert(!new_owner.is_zero(), "invalid admin"); + storage.owner.write(new_owner); + } + + #[aztec(public-vm)] + fn setOwner() { + assert(context.sender() == storage.owner.read(), "only owner can set admin"); + storage.owner.write(context.sender()); + } + + #[aztec(public-vm)] + fn transfer(to: AztecAddress, amount: Field) { + // Co pilot version - lets see how this goes! + let sender = context.sender(); + let amount = U128::from_integer(amount); + let sender_balance = storage.balances.at(sender).read(); + let receiver_balance = storage.balances.at(to).read(); + + // require(sender_balance >= amount, "Insufficient balance"); + + storage.balances.at(sender).write(sender_balance - amount); + storage.balances.at(to).write(receiver_balance + amount); + } +} From 0c40c8e56baa3c77855afeaf3eb6c3f013002d4a Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Tue, 27 Feb 2024 22:46:23 +0000 Subject: [PATCH 2/3] fix: update for new type names --- .../contracts/avm_token_contract/src/main.nr | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr index dbf04d0d554..3483c8d4b64 100644 --- a/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr @@ -6,15 +6,14 @@ contract AvmToken { use dep::aztec::{ note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader, utils as note_utils}, - hash::{compute_secret_hash}, - state_vars::{map::Map, public_state::PublicState, stable_public_state::StablePublicState, set::Set}, + hash::{compute_secret_hash}, state_vars::{map::Map, PublicMutable}, protocol_types::{abis::function_selector::FunctionSelector} }; struct Storage { - total_supply: PublicState, - owner: PublicState, - balances: Map> + total_supply: PublicMutable, + owner: PublicMutable, + balances: Map> } #[aztec(private)] From 66edcaff80fb6f45ef80ef38227fab0d13e8596f Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Tue, 27 Feb 2024 22:58:18 +0000 Subject: [PATCH 3/3] temp --- .../contracts/avm_token_contract/src/main.nr | 26 ++++++++-------- .../simulator/src/avm/avm_simulator.test.ts | 31 ++++++++++++++++++- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr index 3483c8d4b64..d1ee888f577 100644 --- a/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/avm_token_contract/src/main.nr @@ -18,21 +18,21 @@ contract AvmToken { #[aztec(private)] fn constructor(owner: AztecAddress) { - let selector = FunctionSelector::from_signature("_initialize((Field))"); - context.call_public_function(context.this_address(), selector, [owner.to_field()]); + // let selector = FunctionSelector::from_signature("_initialize((Field))"); + // context.call_public_function(context.this_address(), selector, [owner.to_field()]); } - #[aztec(public-vm)] - internal fn _initialize(new_owner: AztecAddress) { - assert(!new_owner.is_zero(), "invalid admin"); - storage.owner.write(new_owner); - } - - #[aztec(public-vm)] - fn setOwner() { - assert(context.sender() == storage.owner.read(), "only owner can set admin"); - storage.owner.write(context.sender()); - } + // #[aztec(public-vm)] + // internal fn _initialize(new_owner: AztecAddress) { + // assert(!new_owner.is_zero(), "invalid admin"); + // storage.owner.write(new_owner); + // } + + // #[aztec(public-vm)] + // fn setOwner() { + // assert(context.sender() == storage.owner.read(), "only owner can set admin"); + // storage.owner.write(context.sender()); + // } #[aztec(public-vm)] fn transfer(to: AztecAddress, amount: Field) { diff --git a/yarn-project/simulator/src/avm/avm_simulator.test.ts b/yarn-project/simulator/src/avm/avm_simulator.test.ts index a17cbf15a11..cd5df3dd0c3 100644 --- a/yarn-project/simulator/src/avm/avm_simulator.test.ts +++ b/yarn-project/simulator/src/avm/avm_simulator.test.ts @@ -2,7 +2,7 @@ import { AztecAddress } from '@aztec/foundation/aztec-address'; import { keccak, pedersenHash, poseidonHash, sha256 } from '@aztec/foundation/crypto'; import { EthAddress } from '@aztec/foundation/eth-address'; import { Fr } from '@aztec/foundation/fields'; -import { AvmTestContractArtifact } from '@aztec/noir-contracts.js'; +import { AvmTestContractArtifact, AvmTokenContractArtifact } from '@aztec/noir-contracts.js'; import { jest } from '@jest/globals'; @@ -446,4 +446,33 @@ describe('AVM simulator', () => { }); }); }); + + describe("avm_token", () => { + it('execute transfer', async () => { + const to = AztecAddress.fromField(new Fr(1)); + const amount = new Fr(1); + + const calldata = [to, amount]; + + // Get contract function artifact + const artifact = AvmTokenContractArtifact.functions.find(f => f.name === 'avm_transfer')!; + + // Decode bytecode into instructions + const bytecode = Buffer.from(artifact.bytecode, 'base64'); + + const context = initContext({ env: initExecutionEnvironment({ calldata }) }); + jest + .spyOn(context.persistableState.hostStorage.contractsDb, 'getBytecode') + .mockReturnValue(Promise.resolve(bytecode)); + jest + .spyOn(context.persistableState.hostStorage.publicStateDb, 'storageRead') + .mockReturnValueOnce(Promise.resolve(new Fr(0))) + .mockReturnValueOnce(Promise.resolve(new Fr(2))) + + const results = await new AvmSimulator(context).execute(); + + expect(results.reverted).toBe(false); + + }); + }) });