From 2c2d72b7799b24273e498805ecf4c36d69f08d7d Mon Sep 17 00:00:00 2001 From: Leila Wang Date: Thu, 7 Sep 2023 15:36:13 +0200 Subject: [PATCH] refactor: move storage into main.nr. (#2068) Closes #1877 # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist). --- docs/docs/dev_docs/contracts/layout.md | 7 +- docs/docs/dev_docs/contracts/main.md | 3 - .../dev_docs/contracts/state_variables.md | 18 ++- docs/docs/dev_docs/contracts/storage.md | 10 +- .../src/contracts/child_contract/src/main.nr | 31 ++++- .../contracts/child_contract/src/storage.nr | 25 ---- .../docs_example_contract/src/main.nr | 82 ++++++++++-- .../docs_example_contract/src/storage.nr | 75 ----------- .../easy_private_token_contract/src/main.nr | 42 ++++-- .../src/storage.nr | 28 ---- .../ecdsa_account_contract/src/main.nr | 45 ++++--- .../ecdsa_account_contract/src/storage.nr | 19 --- .../src/contracts/escrow_contract/src/main.nr | 40 ++++-- .../contracts/escrow_contract/src/storage.nr | 20 --- .../Nargo.toml | 8 -- .../src/main.nr | 46 ------- .../src/storage.nr | 30 ----- .../contracts/lending_contract/src/asset.nr | 46 +++++++ .../lending_contract/src/interfaces.nr | 4 +- .../contracts/lending_contract/src/main.nr | 88 ++++++++++++- .../contracts/lending_contract/src/storage.nr | 122 ------------------ .../multi_transfer_contract/Nargo.toml | 1 - .../multi_transfer_contract/src/main.nr | 14 -- .../native_token_contract/src/main.nr | 105 ++++++++++++--- .../native_token_contract/src/storage.nr | 80 ------------ .../non_native_token_contract/src/main.nr | 50 ++++++- .../non_native_token_contract/src/storage.nr | 53 -------- .../pending_commitments_contract/src/main.nr | 43 ++++-- .../src/storage.nr | 26 ---- .../pokeable_token_contract/src/main.nr | 45 +++++-- .../pokeable_token_contract/src/storage.nr | 31 ----- .../price_feed_contract/src/asset.nr | 28 ++++ .../contracts/price_feed_contract/src/main.nr | 39 +++++- .../price_feed_contract/src/storage.nr | 63 --------- .../src/main.nr | 46 +++++-- .../src/storage.nr | 29 ----- .../private_token_contract/src/main.nr | 39 ++++-- .../private_token_contract/src/storage.nr | 27 ---- .../public_token_contract/src/main.nr | 42 +++++- .../public_token_contract/src/storage.nr | 39 ------ .../schnorr_account_contract/src/main.nr | 46 ++++--- .../schnorr_account_contract/src/storage.nr | 17 --- .../src/contracts/test_contract/src/main.nr | 15 +-- .../contracts/uniswap_contract/src/main.nr | 8 +- 44 files changed, 731 insertions(+), 944 deletions(-) delete mode 100644 yarn-project/noir-contracts/src/contracts/child_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/escrow_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/Nargo.toml delete mode 100644 yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/src/main.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/src/storage.nr create mode 100644 yarn-project/noir-contracts/src/contracts/lending_contract/src/asset.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/lending_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/native_token_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/pending_commitments_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/pokeable_token_contract/src/storage.nr create mode 100644 yarn-project/noir-contracts/src/contracts/price_feed_contract/src/asset.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/price_feed_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/private_token_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/public_token_contract/src/storage.nr delete mode 100644 yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/storage.nr diff --git a/docs/docs/dev_docs/contracts/layout.md b/docs/docs/dev_docs/contracts/layout.md index 2b08b584216..2c657d37fc7 100644 --- a/docs/docs/dev_docs/contracts/layout.md +++ b/docs/docs/dev_docs/contracts/layout.md @@ -1,15 +1,14 @@ # Layout + ## Directory structure Here's a common layout for a basic Noir Contract project: -``` title="layout of an aztec contract project" +```title="layout of an aztec contract project" ─── my_aztec_contract_project ├── src │ ├── main.nr <-- your contract - │ └── storage.nr <-- state variable declarations (by convention) └── Nargo.toml <-- package and dependency management ``` - -> See the vanilla Noir docs for [more info on packages](https://noir-lang.org/modules_packages_crates/crates_and_packages). \ No newline at end of file +> See the vanilla Noir docs for [more info on packages](https://noir-lang.org/modules_packages_crates/crates_and_packages). diff --git a/docs/docs/dev_docs/contracts/main.md b/docs/docs/dev_docs/contracts/main.md index 04238411395..571a4fe3156 100644 --- a/docs/docs/dev_docs/contracts/main.md +++ b/docs/docs/dev_docs/contracts/main.md @@ -30,13 +30,10 @@ There are a number of tools to make writing Noir Contracts more pleasant. See [h Starter kit ::: - ## Example Noir Contract In keeping with the origins of blockchain, here's an example of a simple private token contract. Everyone's balances are private. -#include_code easy_private_token_storage /yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/storage.nr rust - #include_code easy_private_token_contract /yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/main.nr rust :::info Disclaimer diff --git a/docs/docs/dev_docs/contracts/state_variables.md b/docs/docs/dev_docs/contracts/state_variables.md index e1176840d8e..e368c30ab42 100644 --- a/docs/docs/dev_docs/contracts/state_variables.md +++ b/docs/docs/dev_docs/contracts/state_variables.md @@ -9,6 +9,7 @@ Public state is persistent state that is _publicly visible_ to anyone in the wor For developers coming from other blockchain ecosystems (such as Ethereum), this will be a familiar concept, because there, _all_ state is _publicly visible_. Aztec public state follows an account-based model. That is, each state occupies a leaf in an account-based merkle tree: the _public state tree_ (INSERT LINK HERE). See _here_ (INSERT LINK HERE) for more of the technical details. + The `PublicState` struct serves as a wrapper around conventional Noir types `T`, allowing these types to be written to and read from the public state tree. @@ -19,11 +20,11 @@ To declare a type `T` as a persistent, public state variable, use the `PublicSta In the following example, we define a public state with a boolean type: -#include_code state_vars-PublicState /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr rust +#include_code state_vars-PublicState /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust The BoolSerialisationMethods is part of the Aztec stdlib: -#include_code state_vars-PublicStateBoolImport /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr rust +#include_code state_vars-PublicStateBoolImport /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust It contains methods that instruct its PublicState wrapper how to serialise and deserialise a boolean to and from a Field, which is the data type being saved in the public state tree. @@ -47,7 +48,7 @@ First, define how to serialise and deserialise the custom type: And then initialise the PublicState with it: -#include_code state_vars-PublicStateCustomStruct /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr rust +#include_code state_vars-PublicStateCustomStruct /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust ### `.read` @@ -92,9 +93,11 @@ For example, the following function calls the account contract before it updates In contrast to public state, private state is persistent state that is _not_ visible to the whole world. Depending on the logic of the smart contract, a _private_ state variable's current value will only be known to one entity, or a closed group of entities. The value of a private state variable can either be shared via an _encrypted log_ (INSERT_LINK_HERE), or offchain via web2, or completely offline: it's up to the app developer. + Aztec private state follows a utxo-based model. That is, a private state's current value is represented as one or many [notes](#notes). Each note is stored as an individual leaf in a utxo-based merkle tree: the _private state tree_ (INSERT_LINK_HERE). + To greatly simplify the experience of writing private state, Aztec.nr provides three different types of private state variable: @@ -140,7 +143,7 @@ Singleton is a private state variable that is unique in a way. When a Singleton Here we define a Singleton for storing a `CardNote`: -#include_code state_vars-Singleton /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr rust +#include_code state_vars-Singleton /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust ### `.initialise` @@ -178,7 +181,7 @@ ImmutableSingleton represents a unique private state variable that, as the name In the following example, we define an ImmutableSingleton that utilises the `RulesMethods` struct as its underlying note type: -#include_code state_vars-ImmutableSingleton /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr rust +#include_code state_vars-ImmutableSingleton /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust ### `.initialise` @@ -208,7 +211,7 @@ The `new` method creates a Set that employs a specific note type. When a new Set In the following example, we define a set whose underlying note type is `CardNote`: -#include_code state_vars-Set /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr rust +#include_code state_vars-Set /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust ### `.insert` @@ -231,6 +234,7 @@ This function returns the notes the account has access to: #include_code state_vars-SetGet /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/actions.nr rust There's a limit on the maxinum number of notes this function can return at a time. Check _here_ (INSERT_LINK_HERE) and look for `MAX_READ_REQUESTS_PER_CALL` for the up-to-date number. + Because of this limit, we should always consider using the second argument `NoteGetterOptions` to target the notes we need, and to reduce the time required to recursively call this function. @@ -291,7 +295,7 @@ The process of applying the options to get the final notes is not constrained. I The following declares a mapping from a `Field` to a `Singleton`: -#include_code state_vars-MapSingleton /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr rust +#include_code state_vars-MapSingleton /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust The second argument `|slot| Singleton::new(slot, ProfileMethods)` is a Noir closure function. It teaches this instance of `Map` how to create a new instance of a `Singleton` whenever the `.at` method is called to access a state variable at a particular mapping key. The `slot` argument will be derived when `.at` is called, based on the lookup key provided. diff --git a/docs/docs/dev_docs/contracts/storage.md b/docs/docs/dev_docs/contracts/storage.md index 73bf120c164..366839646df 100644 --- a/docs/docs/dev_docs/contracts/storage.md +++ b/docs/docs/dev_docs/contracts/storage.md @@ -1,19 +1,13 @@ # Storage -> A common convention in Noir Contracts is to declare the state variables for your Noir Contract in a `storage.nr` file inside your project (see [directory structure](./layout.md#directory-structure)). This is just a convention, though: your contract would still work if you declare storage in the `main.nr` file. - State variables must be declared inside a struct. (This enables us to declare types composed of nested generics in Noir - see [types](./types.md)). We could define any kinds of state variables in the Storage struct: -#include_code storage-declaration /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr rust +#include_code storage-declaration /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust See [State Variables](./state_variables.md) for how to initialise them. -Using Storage in a contract is like using any other struct in Noir. First, import the struct into the contract's `main.nr` file: - -#include_code storage-import /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust - -For each function that needs access to the storage, initialise the storage inside the function, and call the state variables in it: +Using Storage in a contract is like using any other struct in Noir. For each function that needs access to the storage, initialise the storage inside the function, and then access its state variable members: #include_code storage-init /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust diff --git a/yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr index 879ccd537f8..d661347201d 100644 --- a/yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr @@ -1,16 +1,37 @@ -mod storage; - // A contract used along with `Parent` contract to test nested calls. contract Child { - use crate::storage::Storage; + use dep::std::option::Option; + use dep::aztec::{ abi::CallContext, + context::{PrivateContext, PublicContext}, oracle::{ logs::emit_unencrypted_log, compute_selector::compute_selector, - } + }, + state_vars::public_state::PublicState, + types::type_serialisation::field_serialisation::{FieldSerialisationMethods, FIELD_SERIALISED_LEN}, }; - use dep::std::option::Option; + + struct Storage { + current_value: PublicState, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + current_value: PublicState::new( + private_context, + public_context, + 1, + FieldSerialisationMethods, + ), + } + } + } #[aztec(private)] fn constructor() {} diff --git a/yarn-project/noir-contracts/src/contracts/child_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/child_contract/src/storage.nr deleted file mode 100644 index 0bec493efd8..00000000000 --- a/yarn-project/noir-contracts/src/contracts/child_contract/src/storage.nr +++ /dev/null @@ -1,25 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::public_state::PublicState; -use dep::aztec::types::type_serialisation::field_serialisation::FieldSerialisationMethods; -use dep::aztec::types::type_serialisation::field_serialisation::FIELD_SERIALISED_LEN; -use dep::std::option::Option; - -struct Storage { - current_value: PublicState, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - current_value: PublicState::new( - private_context, - public_context, - 1, - FieldSerialisationMethods, - ), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr index 6bc752cd975..a47de701dcd 100644 --- a/yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr @@ -1,22 +1,88 @@ mod account_contract_interface; mod actions; mod options; -mod storage; mod types; contract DocsExample { + use dep::std::option::Option; + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + state_vars::{ + immutable_singleton::ImmutableSingleton, map::Map, public_state::PublicState, set::Set, + singleton::Singleton, + }, + }; + // docs:start:state_vars-PublicStateBoolImport + use dep::aztec::types::type_serialisation::bool_serialisation::{ + BoolSerialisationMethods, BOOL_SERIALISED_LEN, + }; + // docs:end:state_vars-PublicStateBoolImport use crate::account_contract_interface::AccountContractInterface; use crate::actions; use crate::options::{create_account_card_getter_options, create_largest_account_card_getter_options}; - // docs:start:storage-import - use crate::storage::Storage; - // docs:end:storage-import use crate::types::{ - card_note::CardNote, - queen::Queen, - rules_note::RulesNote, + card_note::{CardNote, CardNoteMethods, CARD_NOTE_LEN}, + profile_note::{ProfileNote, ProfileNoteMethods, PROFILE_NOTE_LEN}, + queen::{Queen, QueenSerialisationMethods, QUEEN_SERIALISED_LEN}, + rules_note::{RulesNote, RulesNoteMethods, RULES_NOTE_LEN}, }; - use dep::std::option::Option; + + // docs:start:storage-declaration + struct Storage { + locked: PublicState, + queen: PublicState, + game_rules: ImmutableSingleton, + legendary_card: Singleton, + cards: Set, + profiles: Map>, + } + + // docs:start:state_vars-PublicState + // docs:start:state_vars-PublicStateCustomStruct + // docs:start:state_vars-Singleton + // docs:start:state_vars-ImmutableSingleton + // docs:start:state_vars-Set + // docs:start:state_vars-MapSingleton + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + // highlight-next-line:state_vars-PublicState + locked: PublicState::new(private_context, public_context, 1, BoolSerialisationMethods), + // highlight-next-line:state_vars-PublicStateCustomStruct + queen: PublicState::new( + private_context, + public_context, + 2, + QueenSerialisationMethods, + ), + // highlight-next-line:state_vars-ImmutableSingleton + game_rules: ImmutableSingleton::new(private_context, 3, RulesNoteMethods), + // highlight-next-line:state_vars-Singleton + legendary_card: Singleton::new(private_context, public_context, 4, CardNoteMethods), + // highlight-next-line:state_vars-Set + cards: Set::new(private_context, public_context, 5, CardNoteMethods), + // highlight-next-line:state_vars-MapSingleton + profiles: Map::new( + private_context, + public_context, + 6, + |private_context, public_context, slot| { + Singleton::new(private_context, public_context, slot, ProfileNoteMethods) + }, + ), + } + } + } + // docs:end:state_vars-PublicState + // docs:end:state_vars-PublicStateCustomStruct + // docs:end:state_vars-Singleton + // docs:end:state_vars-ImmutableSingleton + // docs:end:state_vars-Set + // docs:end:state_vars-MapSingleton + // docs:end:storage-declaration global REPLACE_QUEEN_FUNCTION_SELECTOR = 11111111; global GET_POINTS_OF_COMMON_CARD_FUNCTION_SELECTOR = 11111111; diff --git a/yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr deleted file mode 100644 index b496b123225..00000000000 --- a/yarn-project/noir-contracts/src/contracts/docs_example_contract/src/storage.nr +++ /dev/null @@ -1,75 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::{ - immutable_singleton::ImmutableSingleton, map::Map, public_state::PublicState, set::Set, - singleton::Singleton, -}; -use dep::std::option::Option; -// docs:start:state_vars-PublicStateBoolImport -use dep::aztec::types::type_serialisation::bool_serialisation::{ - BoolSerialisationMethods, BOOL_SERIALISED_LEN, -}; -// docs:end:state_vars-PublicStateBoolImport - -use crate::types::{ - card_note::{CardNote, CardNoteMethods, CARD_NOTE_LEN}, - profile_note::{ProfileNote, ProfileNoteMethods, PROFILE_NOTE_LEN}, - queen::{Queen, QueenSerialisationMethods, QUEEN_SERIALISED_LEN}, - rules_note::{RulesNote, RulesNoteMethods, RULES_NOTE_LEN}, -}; - -// docs:start:storage-declaration -struct Storage { - locked: PublicState, - queen: PublicState, - game_rules: ImmutableSingleton, - legendary_card: Singleton, - cards: Set, - profiles: Map>, -} - -// docs:start:state_vars-PublicState -// docs:start:state_vars-PublicStateCustomStruct -// docs:start:state_vars-Singleton -// docs:start:state_vars-ImmutableSingleton -// docs:start:state_vars-Set -// docs:start:state_vars-MapSingleton -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - // highlight-next-line:state_vars-PublicState - locked: PublicState::new(private_context, public_context, 1, BoolSerialisationMethods), - // highlight-next-line:state_vars-PublicStateCustomStruct - queen: PublicState::new( - private_context, - public_context, - 2, - QueenSerialisationMethods, - ), - // highlight-next-line:state_vars-ImmutableSingleton - game_rules: ImmutableSingleton::new(private_context, 3, RulesNoteMethods), - // highlight-next-line:state_vars-Singleton - legendary_card: Singleton::new(private_context, public_context, 4, CardNoteMethods), - // highlight-next-line:state_vars-Set - cards: Set::new(private_context, public_context, 5, CardNoteMethods), - // highlight-next-line:state_vars-MapSingleton - profiles: Map::new( - private_context, - public_context, - 6, - |private_context, public_context, slot| { - Singleton::new(private_context, public_context, slot, ProfileNoteMethods) - }, - ), - } - } -} -// docs:end:state_vars-PublicState -// docs:end:state_vars-PublicStateCustomStruct -// docs:end:state_vars-Singleton -// docs:end:state_vars-ImmutableSingleton -// docs:end:state_vars-Set -// docs:end:state_vars-MapSingleton -// docs:end:storage-declaration diff --git a/yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/main.nr index 29135c0398b..37901c0a260 100644 --- a/yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/main.nr @@ -1,9 +1,14 @@ // docs:start:easy_private_token_contract -mod storage; - contract EasyPrivateToken { use dep::std::option::Option; - use dep::aztec::abi::Hasher; + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + note::{ + note_header::NoteHeader, + utils as note_utils, + }, + state_vars::map::Map, + }; use dep::value_note::{ balance_utils, value_note::{ @@ -11,18 +16,29 @@ contract EasyPrivateToken { VALUE_NOTE_LEN, }, }; + use dep::easy_private_state::easy_private_state::EasyPrivateUint; - use dep::aztec::{ - abi, - abi::PrivateContextInputs, - context::PrivateContext, - note::{ - note_header::NoteHeader, - utils as note_utils, - }, - }; + struct Storage { + balances: Map, + } - use crate::storage::Storage; + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + balances: Map::new( + private_context, + public_context, + 1, + |private_context, public_context, slot| { + EasyPrivateUint::new(private_context, public_context, slot) + }, + ), + } + } + } /** * Initialise the contract's initial state variables. diff --git a/yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/storage.nr deleted file mode 100644 index caac9c3dc51..00000000000 --- a/yarn-project/noir-contracts/src/contracts/easy_private_token_contract/src/storage.nr +++ /dev/null @@ -1,28 +0,0 @@ -// docs:start:easy_private_token_storage -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::map::Map; -use dep::easy_private_state::easy_private_state::EasyPrivateUint; -use dep::std::option::Option; - -struct Storage { - balances: Map, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - balances: Map::new( - private_context, - public_context, - 1, - |private_context, public_context, slot| { - EasyPrivateUint::new(private_context, public_context, slot) - }, - ), - } - } -} -// docs:end:easy_private_token_storage diff --git a/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/main.nr index c87755cf215..b7739942369 100644 --- a/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/main.nr @@ -1,4 +1,3 @@ -mod storage; mod ecdsa_public_key_note; // Account contract that uses ECDSA signatures for authentication on the same curve as Ethereum. @@ -6,25 +5,35 @@ mod ecdsa_public_key_note; contract EcdsaAccount { use dep::std; use dep::std::option::Option; - use dep::aztec::entrypoint; - use dep::aztec::entrypoint::EntrypointPayload; - use dep::aztec::abi::CallContext; - use dep::aztec::log::emit_encrypted_log; - use dep::aztec::oracle::get_public_key::get_public_key; - use dep::aztec::types::vec::BoundedVec; - use dep::aztec::types::point::Point; - use dep::aztec::constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD; + use dep::aztec::{ + abi::CallContext, + constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD, + context::{PrivateContext, PublicContext}, + entrypoint::{EntrypointPayload, ENTRYPOINT_PAYLOAD_SIZE}, + log::emit_encrypted_log, + note::{ + note_header::NoteHeader, + utils as note_utils, + }, + oracle::get_public_key::get_public_key, + state_vars::immutable_singleton::ImmutableSingleton, + }; - use dep::aztec::constants_gen::MAX_NOTE_FIELDS_LENGTH; - use dep::aztec::note::{ - note_header::{NoteHeader}, - utils as note_utils, + use crate::ecdsa_public_key_note::{ + EcdsaPublicKeyNote, EcdsaPublicKeyNoteInterface, ECDSA_PUBLIC_KEY_NOTE_LEN, }; - use crate::storage::Storage; - use crate::ecdsa_public_key_note::EcdsaPublicKeyNote; - use crate::ecdsa_public_key_note::EcdsaPublicKeyNoteInterface; - use crate::ecdsa_public_key_note::ECDSA_PUBLIC_KEY_NOTE_LEN; + struct Storage { + public_key: ImmutableSingleton, + } + + impl Storage { + fn init(private_context: Option<&mut PrivateContext>, _: Option<&mut PublicContext>) -> pub Self { + Storage { + public_key: ImmutableSingleton::new(private_context, 1, EcdsaPublicKeyNoteInterface), + } + } + } // All calls made by this account will be routed through this entrypoint #[aztec(private)] @@ -38,7 +47,7 @@ contract EcdsaAccount { // Verify payload signature using Ethereum's signing scheme // Note that noir expects the hash of the message/challenge as input to the ECDSA verification. - let payload_fields: [Field; entrypoint::ENTRYPOINT_PAYLOAD_SIZE] = payload.serialize(); + let payload_fields: [Field; ENTRYPOINT_PAYLOAD_SIZE] = payload.serialize(); let message_field: Field = std::hash::pedersen_with_separator(payload_fields, GENERATOR_INDEX__SIGNATURE_PAYLOAD)[0]; // TODO workaround for https://github.com/noir-lang/noir/issues/2421 let message_bytes_slice = message_field.to_be_bytes(32); diff --git a/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/storage.nr deleted file mode 100644 index 9a93c595e73..00000000000 --- a/yarn-project/noir-contracts/src/contracts/ecdsa_account_contract/src/storage.nr +++ /dev/null @@ -1,19 +0,0 @@ -use dep::aztec::state_vars::immutable_singleton::ImmutableSingleton; - -use crate::ecdsa_public_key_note::{ - EcdsaPublicKeyNote, EcdsaPublicKeyNoteInterface, ECDSA_PUBLIC_KEY_NOTE_LEN, -}; -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::std::option::Option; - -struct Storage { - public_key: ImmutableSingleton, -} - -impl Storage { - fn init(private_context: Option<&mut PrivateContext>, _: Option<&mut PublicContext>) -> Self { - Storage { - public_key: ImmutableSingleton::new(private_context, 1, EcdsaPublicKeyNoteInterface), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr index 80559dd3635..af93d9e74c2 100644 --- a/yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr @@ -1,33 +1,45 @@ mod address_note; -mod storage; mod private_token_contract_interface; // Sample escrow contract that stores a balance of a private token on behalf of an owner. contract Escrow { - use dep::std; - use dep::aztec::abi::CallContext; - use dep::aztec::private_call_stack_item::PrivateCallStackItem; - use dep::aztec::log::emit_encrypted_log; - use dep::aztec::oracle::get_public_key::get_public_key; + use dep::std::option::Option; - use dep::aztec::note::{ - note_getter_options::NoteGetterOptions, - note_header::{NoteHeader}, - utils as note_utils, + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + log::emit_encrypted_log, + note::{ + note_getter_options::NoteGetterOptions, + note_header::NoteHeader, + utils as note_utils, + }, + oracle::get_public_key::get_public_key, + state_vars::set::Set, }; - use dep::std::option::Option; - use crate::address_note::{ AddressNote, AddressNoteMethods, ADDRESS_NOTE_LEN, }; - use crate::storage::Storage; - use crate::private_token_contract_interface::PrivateTokenPrivateContextInterface; + struct Storage { + owners: Set, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + owners: Set::new(private_context, public_context, 1, AddressNoteMethods), + } + } + } + // Creates a new instance #[aztec(private)] fn constructor( diff --git a/yarn-project/noir-contracts/src/contracts/escrow_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/escrow_contract/src/storage.nr deleted file mode 100644 index 205086c2b44..00000000000 --- a/yarn-project/noir-contracts/src/contracts/escrow_contract/src/storage.nr +++ /dev/null @@ -1,20 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::set::Set; -use dep::std::option::Option; - -use crate::address_note::{AddressNote, AddressNoteMethods, ADDRESS_NOTE_LEN}; - -struct Storage { - owners: Set, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - owners: Set::new(private_context, public_context, 1, AddressNoteMethods), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/Nargo.toml b/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/Nargo.toml deleted file mode 100644 index ecf14c9d70b..00000000000 --- a/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/Nargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "example_public_state_increment_contract" -authors = [""] -compiler_version = "0.1" -type = "contract" - -[dependencies] -aztec = { path = "../../../../noir-libs/noir-aztec" } \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/src/main.nr b/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/src/main.nr deleted file mode 100644 index b09183741be..00000000000 --- a/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/src/main.nr +++ /dev/null @@ -1,46 +0,0 @@ -mod storage; - -contract ExamplePublicStateIncrement { - use dep::aztec::abi; - use dep::aztec::abi::Hasher; - use dep::aztec::abi::PrivateContextInputs; - use dep::aztec::abi::PublicContextInputs; - use dep::aztec::context::{ - PrivateContext, - PublicContext, - }; - use dep::aztec::types::point::Point; - use crate::storage::Storage; - use dep::aztec::state_vars::{ - type_serialisation::u32_serialisation::{ - U32SerialisationMethods, - U32_SERIALISED_LEN, - }, - }; - - // call initialise_a(); - #[aztec(private)] - fn constructor( - ) { - let initialise_a_function_selector: Field = 1234; - let _return_values = context.call_public_function_no_args(context.this_address(), initialise_a_function_selector); - } - - // a = 100; - #[aztec(public)] - internal fn initialise_a() { - let storage = Storage::init(Option::none(), Option::some(&mut context)); - storage.a.write(100); - } - - // a += b; - #[aztec(public)] - fn increment_a( - b: Field, - ) { - let storage = Storage::init(Option::none(), Option::some(&mut context)); - let mut a = storage.a.read(); - a += b; - storage.a.write(a); - } -} diff --git a/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/src/storage.nr b/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/src/storage.nr deleted file mode 100644 index 56a808be3aa..00000000000 --- a/yarn-project/noir-contracts/src/contracts/example_public_state_increment_BROKE/src/storage.nr +++ /dev/null @@ -1,30 +0,0 @@ -// docs:start:PublicState -use dep::aztec::state_vars::{ - // highlight-start:PublicState - public_state::PublicState, - type_serialisation::field_serialisation::{ - FieldSerialisationMethods, - FIELD_SERIALISED_LEN, - }, - // highlight-end:PublicState -}; -use dep::std::option::Option; -use dep::aztec::context::{PrivateContext, PublicContext}; - -struct Storage { - // highlight-next-line:PublicState - a: PublicState, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - // highlight-next-line:PublicState - a: PublicState::new(1, FieldSerialisationMethods), - } - } -} -// docs:end:PublicState \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/asset.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/asset.nr new file mode 100644 index 00000000000..5fbc903abca --- /dev/null +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/src/asset.nr @@ -0,0 +1,46 @@ +use dep::aztec::types::type_serialisation::TypeSerialisationInterface; + +// Struct to be used to represent "totals". Generally, there should be one per asset. +// It stores the global values that are shared among all users, such as an accumulator +// and last time it was updated. +// In practice, it should also point to an oracle and have more fields related to +// loan to value ratios and other things, but we did not have enough reads/writes for this. +struct Asset { + interest_accumulator: u120, + last_updated_ts: u120, + loan_to_value: u120, + oracle_address: Field, +} + +global ASSET_SERIALISED_LEN: Field = 4; + +// Right now we are wasting so many writes. If changing last_updated_ts +// we will end up rewriting all of them, wasting writes. +fn deserialiseAsset(fields: [Field; ASSET_SERIALISED_LEN]) -> Asset { + Asset { + interest_accumulator: fields[0] as u120, + last_updated_ts: fields[1] as u120, + loan_to_value: fields[2] as u120, + oracle_address: fields[3], + } +} + +fn serialiseAsset(asset: Asset) -> [Field; ASSET_SERIALISED_LEN] { + [ + asset.interest_accumulator as Field, + asset.last_updated_ts as Field, + asset.loan_to_value as Field, + asset.oracle_address, + ] +} + +impl Asset { + fn serialize(self: Self) -> [Field; ASSET_SERIALISED_LEN] { + serialiseAsset(self) + } +} + +global AssetSerialisationMethods = TypeSerialisationInterface { + deserialise: deserialiseAsset, + serialise: serialiseAsset, +}; diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/interfaces.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/interfaces.nr index d6b457652a3..c79378d7ecf 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/interfaces.nr +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/src/interfaces.nr @@ -1,11 +1,9 @@ -mod storage; - use dep::aztec::context::{ PrivateContext, PublicContext }; -use crate::storage::Asset; +use crate::asset::Asset; use dep::aztec::constants_gen::RETURN_VALUES_LENGTH; use dep::aztec::oracle::compute_selector::compute_selector; diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr index 2f33b92a719..dc887d2ea04 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr @@ -1,4 +1,4 @@ -mod storage; +mod asset; mod interest_math; mod helpers; mod interfaces; @@ -11,14 +11,94 @@ mod interfaces; // - A way to repay all debt at once // - Liquidations contract Lending { - use dep::aztec::public_call_stack_item::PublicCallStackItem; use dep::safe_math::SafeU120; use dep::std::option::Option; - use crate::storage::{Storage, Asset}; + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + oracle::compute_selector::compute_selector, + state_vars::{ + map::Map, + public_state::PublicState, + }, + types::type_serialisation::{ + field_serialisation::{FieldSerialisationMethods, FIELD_SERIALISED_LEN}, + TypeSerialisationInterface, + }, + }; + use crate::asset::{ASSET_SERIALISED_LEN, Asset, AssetSerialisationMethods}; use crate::interest_math::compute_multiplier; use crate::helpers::{covered_by_collateral, DebtReturn, debt_updates, debt_value, compute_identifier}; use crate::interfaces::{Token, Lending, PriceFeed}; - use dep::aztec::oracle::compute_selector::compute_selector; + + // Storage structure, containing all storage, and specifying what slots they use. + struct Storage { + collateral_asset: PublicState, + stable_coin: PublicState, + assets: Map>, + collateral: Map>, + static_debt: Map>, // abusing keys very heavily + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + collateral_asset: PublicState::new( + private_context, + public_context, + 1, + FieldSerialisationMethods, + ), + stable_coin: PublicState::new( + private_context, + public_context, + 2, + FieldSerialisationMethods, + ), + assets: Map::new( + private_context, + public_context, + 3, + |private_context, public_context, slot| { + PublicState::new( + private_context, + public_context, + slot, + AssetSerialisationMethods, + ) + }, + ), + collateral: Map::new( + private_context, + public_context, + 4, + |private_context, public_context, slot| { + PublicState::new( + private_context, + public_context, + slot, + FieldSerialisationMethods, + ) + }, + ), + static_debt: Map::new( + private_context, + public_context, + 5, + |private_context, public_context, slot| { + PublicState::new( + private_context, + public_context, + slot, + FieldSerialisationMethods, + ) + }, + ), + } + } + } struct Position { collateral: Field, diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/storage.nr deleted file mode 100644 index 43dc315851d..00000000000 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/storage.nr +++ /dev/null @@ -1,122 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::map::Map; -use dep::aztec::state_vars::public_state::PublicState; -use dep::aztec::types::type_serialisation::field_serialisation::FieldSerialisationMethods; -use dep::aztec::types::type_serialisation::field_serialisation::FIELD_SERIALISED_LEN; -use dep::aztec::types::type_serialisation::TypeSerialisationInterface; -use dep::std::option::Option; - -// Struct to be used to represent "totals". Generally, there should be one per asset. -// It stores the global values that are shared among all users, such as an accumulator -// and last time it was updated. -// In practice, it should also point to an oracle and have more fields related to -// loan to value ratios and other things, but we did not have enough reads/writes for this. -struct Asset { - interest_accumulator: u120, - last_updated_ts: u120, - loan_to_value: u120, - oracle_address: Field, -} - -global ASSET_SERIALISED_LEN: Field = 4; - -// Right now we are wasting so many writes. If changing last_updated_ts -// we will end up rewriting all of them, wasting writes. -fn deserialiseAsset(fields: [Field; ASSET_SERIALISED_LEN]) -> Asset { - Asset { - interest_accumulator: fields[0] as u120, - last_updated_ts: fields[1] as u120, - loan_to_value: fields[2] as u120, - oracle_address: fields[3], - } -} - -fn serialiseAsset(asset: Asset) -> [Field; ASSET_SERIALISED_LEN] { - [ - asset.interest_accumulator as Field, - asset.last_updated_ts as Field, - asset.loan_to_value as Field, - asset.oracle_address, - ] -} - -impl Asset { - fn serialize(self: Self) -> [Field; ASSET_SERIALISED_LEN] { - serialiseAsset(self) - } -} - -global AssetSerialisationMethods = TypeSerialisationInterface { - deserialise: deserialiseAsset, - serialise: serialiseAsset, -}; - -// Storage structure, containing all storage, and specifying what slots they use. -struct Storage { - collateral_asset: PublicState, - stable_coin: PublicState, - assets: Map>, - collateral: Map>, - static_debt: Map>, // abusing keys very heavily -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - collateral_asset: PublicState::new( - private_context, - public_context, - 1, - FieldSerialisationMethods, - ), - stable_coin: PublicState::new( - private_context, - public_context, - 2, - FieldSerialisationMethods, - ), - assets: Map::new( - private_context, - public_context, - 3, - |private_context, public_context, slot| { - PublicState::new( - private_context, - public_context, - slot, - AssetSerialisationMethods, - ) - }, - ), - collateral: Map::new( - private_context, - public_context, - 4, - |private_context, public_context, slot| { - PublicState::new( - private_context, - public_context, - slot, - FieldSerialisationMethods, - ) - }, - ), - static_debt: Map::new( - private_context, - public_context, - 5, - |private_context, public_context, slot| { - PublicState::new( - private_context, - public_context, - slot, - FieldSerialisationMethods, - ) - }, - ), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/Nargo.toml b/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/Nargo.toml index d6220bfb1eb..e65ae99c146 100644 --- a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/Nargo.toml +++ b/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/Nargo.toml @@ -6,4 +6,3 @@ type = "contract" [dependencies] aztec = { path = "../../../../noir-libs/noir-aztec" } -value_note = { path = "../../../../noir-libs/value-note"} diff --git a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/main.nr index 102bd6383ce..05e173019c5 100644 --- a/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/multi_transfer_contract/src/main.nr @@ -2,20 +2,6 @@ mod private_token_airdrop_interface; // Demonstrates how to perform 4 x 4 = 16 transfers in one transaction. Uses the private airdrop contract in the backend. contract MultiTransfer { - use dep::aztec::oracle::public_call; - use dep::aztec::private_call_stack_item::PrivateCallStackItem; - use dep::aztec::public_call_stack_item::PublicCallStackItem; - use dep::aztec::types::point::Point; - - // Libs - use dep::value_note::{ - value_note::{VALUE_NOTE_LEN, ValueNoteMethods}, - }; - use dep::aztec::note::{ - note_header::NoteHeader, - utils as note_utils, - }; - // Interfaces use crate::private_token_airdrop_interface::PrivateTokenAirdropPrivateContextInterface; diff --git a/yarn-project/noir-contracts/src/contracts/native_token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/native_token_contract/src/main.nr index 02500944991..b7a14525a4f 100644 --- a/yarn-project/noir-contracts/src/contracts/native_token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/native_token_contract/src/main.nr @@ -1,5 +1,3 @@ -mod storage; - // Testing token that can be bridged in and out. // TODOS: // - Add role based access control to mint functions @@ -11,34 +9,105 @@ contract NativeToken { use dep::value_note::{ balance_utils, utils::{increment, decrement}, - value_note::{VALUE_NOTE_LEN, ValueNoteMethods}, + value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods}, }; use dep::std; - use dep::aztec::constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD; - - use dep::non_native::{ - hash::{get_mint_content_hash, get_withdraw_content_hash}, - transparent_note::{ - TransparentNote, - TransparentNoteMethods - }, - }; - - use crate::storage::Storage; - use dep::aztec::{ + constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD, + context::{PrivateContext, PublicContext}, note::{ note_header::NoteHeader, utils as note_utils, }, oracle::{ + compute_selector::compute_selector, logs::emit_unencrypted_log, - compute_selector::compute_selector }, - public_call_stack_item::PublicCallStackItem, - types::point::Point, + state_vars::{ + map::Map, + public_state::PublicState, + set::Set, + }, + types::type_serialisation::field_serialisation::{ + FieldSerialisationMethods, + FIELD_SERIALISED_LEN, + }, auth::assert_valid_message_for, }; + use dep::non_native::{ + hash::{get_mint_content_hash, get_withdraw_content_hash}, + transparent_note::{ + TransparentNote, + TransparentNoteMethods, + TRANSPARENT_NOTE_LEN, + }, + }; + + struct Storage { + balances: Map>, + total_supply: PublicState, + pending_shields: Set, + public_balances: Map>, + public_allowances: Map>>, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + balances: Map::new( + private_context, + public_context, + 1, // Storage slot + |private_context, public_context, slot| { + Set::new(private_context, public_context, slot, ValueNoteMethods) + }, + ), + total_supply: PublicState::new( + private_context, + public_context, + 2, + FieldSerialisationMethods, + ), + pending_shields: Set::new(private_context, public_context, 3, TransparentNoteMethods), + public_balances: Map::new( + private_context, + public_context, + 4, + |private_context, public_context, slot| { + PublicState::new( + private_context, + public_context, + slot, + FieldSerialisationMethods, + ) + }, + ), + public_allowances: Map::new( + private_context, + public_context, + 5, + |private_context, public_context, s1| { + Map::new( + private_context, + public_context, + s1, + |private_context, public_context, s2| { + PublicState::new( + private_context, + public_context, + s2, + FieldSerialisationMethods, + ) + }, + ) + }, + ), + } + } + } #[aztec(private)] fn constructor( diff --git a/yarn-project/noir-contracts/src/contracts/native_token_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/native_token_contract/src/storage.nr deleted file mode 100644 index 25cce9b8463..00000000000 --- a/yarn-project/noir-contracts/src/contracts/native_token_contract/src/storage.nr +++ /dev/null @@ -1,80 +0,0 @@ -use dep::value_note::value_note::{ValueNote, ValueNoteMethods, VALUE_NOTE_LEN}; - -use dep::non_native::transparent_note::{ - TransparentNote, TransparentNoteMethods, TRANSPARENT_NOTE_LEN, -}; - -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::{ - state_vars::{map::Map, public_state::PublicState, set::Set}, - types::type_serialisation::field_serialisation::{ - FieldSerialisationMethods, FIELD_SERIALISED_LEN, - }, -}; -use dep::std::option::Option; - -struct Storage { - balances: Map>, - total_supply: PublicState, - pending_shields: Set, - public_balances: Map>, - public_allowances: Map>>, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - balances: Map::new( - private_context, - public_context, - 1, // Storage slot - |private_context, public_context, slot| { - Set::new(private_context, public_context, slot, ValueNoteMethods) - }, - ), - total_supply: PublicState::new( - private_context, - public_context, - 2, - FieldSerialisationMethods, - ), - pending_shields: Set::new(private_context, public_context, 3, TransparentNoteMethods), - public_balances: Map::new( - private_context, - public_context, - 4, - |private_context, public_context, slot| { - PublicState::new( - private_context, - public_context, - slot, - FieldSerialisationMethods, - ) - }, - ), - public_allowances: Map::new( - private_context, - public_context, - 5, - |private_context, public_context, s1| { - Map::new( - private_context, - public_context, - s1, - |private_context, public_context, s2| { - PublicState::new( - private_context, - public_context, - s2, - FieldSerialisationMethods, - ) - }, - ) - }, - ), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/main.nr index 53d7e84e8e4..8c585b078d3 100644 --- a/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/main.nr @@ -1,5 +1,4 @@ mod hash; -mod storage; mod transparent_note; // Represents an ERC20 token bridged from L1 to L2 via `l1-contracts/test/portals/TokenPortal.sol`. The bridged user @@ -22,7 +21,7 @@ contract NonNativeToken { use dep::value_note::{ balance_utils, utils::{increment, decrement}, - value_note::{VALUE_NOTE_LEN, ValueNoteMethods}, + value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods}, }; use crate::transparent_note::{ @@ -31,19 +30,58 @@ contract NonNativeToken { TRANSPARENT_NOTE_LEN, }; - use crate::storage::Storage; use crate::hash::{get_mint_content_hash, get_withdraw_content_hash}; - use dep::aztec::types::point::Point; use dep::aztec::{ + context::{PrivateContext, PublicContext}, note::{ note_header::NoteHeader, utils as note_utils, }, + oracle::compute_selector::compute_selector, + state_vars::{map::Map, public_state::PublicState, set::Set}, + types::type_serialisation::field_serialisation::{ + FieldSerialisationMethods, FIELD_SERIALISED_LEN, + }, }; - use dep::aztec::public_call_stack_item::PublicCallStackItem; - use dep::aztec::oracle::compute_selector::compute_selector; + struct Storage { + balances: Map>, + pending_shields: Set, + public_balances: Map>, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + balances: Map::new( + private_context, + public_context, + 1, // Storage slot + |private_context, public_context, slot| { + Set::new(private_context, public_context, slot, ValueNoteMethods) + }, + ), + pending_shields: Set::new(private_context, public_context, 2, TransparentNoteMethods), + public_balances: Map::new( + private_context, + public_context, + 3, + |private_context, public_context, slot| { + PublicState::new( + private_context, + public_context, + slot, + FieldSerialisationMethods, + ) + }, + ), + } + } + } #[aztec(private)] fn constructor( diff --git a/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/storage.nr deleted file mode 100644 index 688f39cf6e1..00000000000 --- a/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/storage.nr +++ /dev/null @@ -1,53 +0,0 @@ -mod transparent_note; - -use crate::transparent_note::{TransparentNote, TransparentNoteMethods, TRANSPARENT_NOTE_LEN}; - -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::{ - state_vars::{map::Map, public_state::PublicState, set::Set}, - types::type_serialisation::field_serialisation::{ - FieldSerialisationMethods, FIELD_SERIALISED_LEN, - }, -}; -use dep::std::option::Option; -use dep::value_note::value_note::{ValueNote, ValueNoteMethods, VALUE_NOTE_LEN}; - -struct Storage { - balances: Map>, - - pending_shields: Set, - - public_balances: Map>, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - balances: Map::new( - private_context, - public_context, - 1, // Storage slot - |private_context, public_context, slot| { - Set::new(private_context, public_context, slot, ValueNoteMethods) - }, - ), - pending_shields: Set::new(private_context, public_context, 2, TransparentNoteMethods), - public_balances: Map::new( - private_context, - public_context, - 3, - |private_context, public_context, slot| { - PublicState::new( - private_context, - public_context, - slot, - FieldSerialisationMethods, - ) - }, - ), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/pending_commitments_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/pending_commitments_contract/src/main.nr index e463d48f44f..cd9110b7629 100644 --- a/yarn-project/noir-contracts/src/contracts/pending_commitments_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/pending_commitments_contract/src/main.nr @@ -1,5 +1,3 @@ -mod storage; - // Test contract to confirm that notes can be inserted and then later // read (eventually even nullified) in the same TX. This contract // also contains some "bad" test cases to ensure that notes cannot @@ -12,17 +10,40 @@ contract PendingCommitments { filter::filter_notes_min_sum, value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods}, }; + use dep::aztec::{ + constants_gen::ARGS_LENGTH, + context::{PrivateContext, PublicContext}, + log::emit_encrypted_log, + note::{ + note_getter::NoteGetterOptions, + note_header::NoteHeader, + utils as note_utils, + }, + oracle::get_public_key::get_public_key, + state_vars::{map::Map, set::Set}, + }; - use crate::storage::Storage; + struct Storage { + balances: Map>, + } - use dep::aztec::constants_gen::ARGS_LENGTH; - use dep::aztec::note::{ - note_getter::NoteGetterOptions, - note_header::NoteHeader, - utils as note_utils, - }; - use dep::aztec::log::emit_encrypted_log; - use dep::aztec::oracle::get_public_key::get_public_key; + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + balances: Map::new( + private_context, + public_context, + 1, // Storage slot + |private_context, public_context, slot| { + Set::new(private_context, public_context, slot, ValueNoteMethods) + }, + ), + } + } + } // TODO(dbanks12): consolidate code into internal helper functions // (once Noir's support for this is more robust) diff --git a/yarn-project/noir-contracts/src/contracts/pending_commitments_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/pending_commitments_contract/src/storage.nr deleted file mode 100644 index c8c5741bd49..00000000000 --- a/yarn-project/noir-contracts/src/contracts/pending_commitments_contract/src/storage.nr +++ /dev/null @@ -1,26 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::{map::Map, set::Set}; -use dep::std::option::Option; -use dep::value_note::value_note::{ValueNote, ValueNoteMethods, VALUE_NOTE_LEN}; - -struct Storage { - balances: Map>, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - balances: Map::new( - private_context, - public_context, - 1, // Storage slot - |private_context, public_context, slot| { - Set::new(private_context, public_context, slot, ValueNoteMethods) - }, - ), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/pokeable_token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/pokeable_token_contract/src/main.nr index d230cb5324e..860350460b7 100644 --- a/yarn-project/noir-contracts/src/contracts/pokeable_token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/pokeable_token_contract/src/main.nr @@ -1,4 +1,3 @@ -mod storage; mod address_note; contract PokeableToken { @@ -9,16 +8,44 @@ contract PokeableToken { utils::increment, value_note::{VALUE_NOTE_LEN, ValueNoteMethods, ValueNote}, }; - use dep::aztec::note::{ - note_getter::NoteGetterOptions, - note_header::{NoteHeader}, - utils as note_utils, + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + log::emit_encrypted_log, + note::{ + note_getter::NoteGetterOptions, + note_header::{NoteHeader}, + utils as note_utils, + }, + state_vars::{immutable_singleton::ImmutableSingleton, map::Map, set::Set}, + types::point::Point, }; - use dep::aztec::types::point::Point; - use dep::aztec::log::emit_encrypted_log; + use crate::address_note::{AddressNote, AddressNoteMethods, ADDRESS_NOTE_LEN}; - use crate::storage::Storage; - use crate::address_note::{AddressNote, AddressNoteMethods}; + struct Storage { + sender: ImmutableSingleton, + recipient: ImmutableSingleton, + balances: Map>, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + sender: ImmutableSingleton::new(private_context, 1, AddressNoteMethods), + recipient: ImmutableSingleton::new(private_context, 2, AddressNoteMethods), + balances: Map::new( + private_context, + public_context, + 3, + |private_context, public_context, slot| { + Set::new(private_context, public_context, slot, ValueNoteMethods) + }, + ), + } + } + } // Constructs the contract and sets `initial_supply` which is fully owned by `sender`. #[aztec(private)] diff --git a/yarn-project/noir-contracts/src/contracts/pokeable_token_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/pokeable_token_contract/src/storage.nr deleted file mode 100644 index 0a7193f66c7..00000000000 --- a/yarn-project/noir-contracts/src/contracts/pokeable_token_contract/src/storage.nr +++ /dev/null @@ -1,31 +0,0 @@ -use crate::address_note::{AddressNote, AddressNoteMethods, ADDRESS_NOTE_LEN}; -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::{immutable_singleton::ImmutableSingleton, map::Map, set::Set}; -use dep::std::option::Option; -use dep::value_note::value_note::{ValueNote, ValueNoteMethods, VALUE_NOTE_LEN}; - -struct Storage { - sender: ImmutableSingleton, - recipient: ImmutableSingleton, - balances: Map>, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - sender: ImmutableSingleton::new(private_context, 1, AddressNoteMethods), - recipient: ImmutableSingleton::new(private_context, 2, AddressNoteMethods), - balances: Map::new( - private_context, - public_context, - 3, - |private_context, public_context, slot| { - Set::new(private_context, public_context, slot, ValueNoteMethods) - }, - ), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/asset.nr b/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/asset.nr new file mode 100644 index 00000000000..dc942b40d8e --- /dev/null +++ b/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/asset.nr @@ -0,0 +1,28 @@ +use dep::aztec::types::type_serialisation::TypeSerialisationInterface; + +struct Asset { + price: u120, +} + +global ASSET_SERIALISED_LEN: Field = 1; + +fn deserialiseAsset(fields: [Field; ASSET_SERIALISED_LEN]) -> Asset { + Asset { + price: fields[0] as u120, + } +} + +fn serialiseAsset(asset: Asset) -> [Field; ASSET_SERIALISED_LEN] { + [asset.price as Field] +} + +impl Asset { + fn serialize(self: Self) -> [Field; ASSET_SERIALISED_LEN] { + serialiseAsset(self) + } +} + +global AssetSerialisationMethods = TypeSerialisationInterface { + deserialise: deserialiseAsset, + serialise: serialiseAsset, +}; diff --git a/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/main.nr index bfbe23e501c..78441d762f2 100644 --- a/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/main.nr @@ -1,8 +1,43 @@ -mod storage; +mod asset; contract PriceFeed { - use crate::storage::{Storage, Asset}; use dep::std::option::Option; + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + state_vars::{ + map::Map, + public_state::PublicState, + }, + }; + use crate::asset::{ASSET_SERIALISED_LEN, Asset, AssetSerialisationMethods}; + + // Storage structure, containing all storage, and specifying what slots they use. + struct Storage { + assets: Map>, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + assets: Map::new( + private_context, + public_context, + 1, + |private_context, public_context, slot| { + PublicState::new( + private_context, + public_context, + slot, + AssetSerialisationMethods, + ) + }, + ), + } + } + } #[aztec(private)] fn constructor(){} diff --git a/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/storage.nr deleted file mode 100644 index af6d550a3bc..00000000000 --- a/yarn-project/noir-contracts/src/contracts/price_feed_contract/src/storage.nr +++ /dev/null @@ -1,63 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::map::Map; -use dep::aztec::state_vars::public_state::PublicState; -use dep::aztec::types::type_serialisation::TypeSerialisationInterface; -use dep::aztec::types::type_serialisation::field_serialisation::FieldSerialisationMethods; -use dep::aztec::types::type_serialisation::field_serialisation::FIELD_SERIALISED_LEN; -use dep::std::hash::pedersen; -use dep::std::option::Option; - -struct Asset { - price: u120, -} - -global ASSET_SERIALISED_LEN: Field = 1; - -fn deserialiseAsset(fields: [Field; ASSET_SERIALISED_LEN]) -> Asset { - Asset { - price: fields[0] as u120, - } -} - -fn serialiseAsset(asset: Asset) -> [Field; ASSET_SERIALISED_LEN] { - [asset.price as Field] -} - -impl Asset { - fn serialize(self: Self) -> [Field; ASSET_SERIALISED_LEN] { - serialiseAsset(self) - } -} - -global AssetSerialisationMethods = TypeSerialisationInterface { - deserialise: deserialiseAsset, - serialise: serialiseAsset, -}; - -// Storage structure, containing all storage, and specifying what slots they use. -struct Storage { - assets: Map>, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - assets: Map::new( - private_context, - public_context, - 1, - |private_context, public_context, slot| { - PublicState::new( - private_context, - public_context, - slot, - AssetSerialisationMethods, - ) - }, - ), - } - } -} \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/main.nr index 42a91044375..359a46bdca9 100644 --- a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/main.nr @@ -1,6 +1,5 @@ mod claim_note; mod interface; -mod storage; contract PrivateTokenAirdrop { // Libs @@ -8,20 +7,47 @@ contract PrivateTokenAirdrop { use dep::value_note::{ balance_utils, utils::{increment, decrement, decrement_by_at_most}, - value_note::{VALUE_NOTE_LEN, ValueNoteMethods}, + value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods}, }; - - use dep::aztec::note::{ - note_getter_options::NoteGetterOptions, - note_header::NoteHeader, - utils as note_utils, + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + state_vars::{map::Map, set::Set}, + note::{ + note_getter_options::NoteGetterOptions, + note_header::NoteHeader, + utils as note_utils, + }, + log::emit_unencrypted_log, }; - use dep::aztec::log::emit_unencrypted_log; - use crate::storage::Storage; - use crate::claim_note::{ClaimNote, ClaimNoteMethods}; + use crate::claim_note::{ClaimNote, ClaimNoteMethods, CLAIM_NOTE_LEN}; use crate::interface::PrivateTokenAirdropPrivateContextInterface; + + struct Storage { + balances: Map>, + claims: Set, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + balances: Map::new( + private_context, + public_context, + 1, // Storage slot + |private_context, public_context, slot| { + Set::new(private_context, public_context, slot, ValueNoteMethods) + }, + ), + claims: Set::new(private_context, public_context, 2, ClaimNoteMethods), + } + } + } + // Constructs the contract and sets `initial_supply` which is fully owned by `owner`. #[aztec(private)] fn constructor( diff --git a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/storage.nr deleted file mode 100644 index c1d86591252..00000000000 --- a/yarn-project/noir-contracts/src/contracts/private_token_airdrop_contract/src/storage.nr +++ /dev/null @@ -1,29 +0,0 @@ -use crate::claim_note::{ClaimNote, ClaimNoteMethods, CLAIM_NOTE_LEN}; -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::{map::Map, set::Set}; -use dep::std::option::Option; -use dep::value_note::value_note::{ValueNote, ValueNoteMethods, VALUE_NOTE_LEN}; - -struct Storage { - balances: Map>, - claims: Set, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - balances: Map::new( - private_context, - public_context, - 1, // Storage slot - |private_context, public_context, slot| { - Set::new(private_context, public_context, slot, ValueNoteMethods) - }, - ), - claims: Set::new(private_context, public_context, 2, ClaimNoteMethods), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/private_token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/private_token_contract/src/main.nr index 37b953c2e7d..f3aae3c8dcc 100644 --- a/yarn-project/noir-contracts/src/contracts/private_token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/private_token_contract/src/main.nr @@ -1,20 +1,41 @@ -mod storage; - contract PrivateToken { - // Libs use dep::std::option::Option; use dep::value_note::{ balance_utils, utils::{increment, decrement}, - value_note::{VALUE_NOTE_LEN, ValueNoteMethods}, + value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods}, }; - - use dep::aztec::note::{ - note_header::NoteHeader, - utils as note_utils, + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + note::{ + note_header::NoteHeader, + utils as note_utils, + }, + state_vars::{map::Map, set::Set}, }; - use crate::storage::Storage; + struct Storage { + // maps an aztec address to its balance + balances: Map>, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + balances: Map::new( + private_context, + public_context, + 1, // Storage slot + |private_context, public_context, slot| { + Set::new(private_context, public_context, slot, ValueNoteMethods) + }, + ), + } + } + } // docs:start:constructor // Constructs the contract and sets `initial_supply` which is fully owned by `owner`. diff --git a/yarn-project/noir-contracts/src/contracts/private_token_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/private_token_contract/src/storage.nr deleted file mode 100644 index 84cdd0ddcfa..00000000000 --- a/yarn-project/noir-contracts/src/contracts/private_token_contract/src/storage.nr +++ /dev/null @@ -1,27 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::{map::Map, set::Set}; -use dep::std::option::Option; -use dep::value_note::value_note::{ValueNote, ValueNoteMethods, VALUE_NOTE_LEN}; - -struct Storage { - // maps an aztec address to its balance - balances: Map>, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - balances: Map::new( - private_context, - public_context, - 1, // Storage slot - |private_context, public_context, slot| { - Set::new(private_context, public_context, slot, ValueNoteMethods) - }, - ), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr index 14f58ae5006..e94ccf1b7e0 100644 --- a/yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/public_token_contract/src/main.nr @@ -1,13 +1,47 @@ -mod storage; - contract PublicToken { + use dep::std::option::Option; // docs:start:unencrypted_import use dep::aztec::oracle::logs::emit_unencrypted_log; // docs:end:unencrypted_import - use dep::std::option::Option; - use crate::storage::Storage; + use dep::aztec::{ + context::{PrivateContext, PublicContext}, + state_vars::{ + map::Map, + public_state::PublicState, + }, + types::type_serialisation::field_serialisation::{ + FieldSerialisationMethods, FIELD_SERIALISED_LEN, + }, + }; + + struct Storage { + balances: Map>, + } + + impl Storage { + fn init( + private_context: Option<&mut PrivateContext>, + public_context: Option<&mut PublicContext>, + ) -> pub Self { + Storage { + balances: Map::new( + private_context, + public_context, + 1, + |private_context, public_context, slot| { + PublicState::new( + private_context, + public_context, + slot, + FieldSerialisationMethods, + ) + }, + ), + } + } + } // Constructs the contract. #[aztec(private)] diff --git a/yarn-project/noir-contracts/src/contracts/public_token_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/public_token_contract/src/storage.nr deleted file mode 100644 index 72881b478aa..00000000000 --- a/yarn-project/noir-contracts/src/contracts/public_token_contract/src/storage.nr +++ /dev/null @@ -1,39 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::{ - map::Map, - // highlight-start:PublicState - public_state::PublicState, - // highlight-end:PublicState -}; -use dep::std::option::Option; - -use dep::aztec::types::type_serialisation::field_serialisation::{ - FieldSerialisationMethods, FIELD_SERIALISED_LEN, -}; - -struct Storage { - balances: Map>, -} - -impl Storage { - fn init( - private_context: Option<&mut PrivateContext>, - public_context: Option<&mut PublicContext>, - ) -> Self { - Storage { - balances: Map::new( - private_context, - public_context, - 1, - |private_context, public_context, slot| { - PublicState::new( - private_context, - public_context, - slot, - FieldSerialisationMethods, - ) - }, - ), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/main.nr index 9bbe81dcd10..65aebff95c2 100644 --- a/yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/main.nr @@ -1,4 +1,3 @@ -mod storage; mod public_key_note; // Account contract that uses Schnorr signatures for authentication. @@ -6,25 +5,32 @@ mod public_key_note; contract SchnorrAccount { use dep::std; use dep::std::option::Option; - use dep::aztec::entrypoint; - use dep::aztec::entrypoint::EntrypointPayload; - use dep::aztec::abi::CallContext; - use dep::aztec::private_call_stack_item::PrivateCallStackItem; - use dep::aztec::public_call_stack_item::PublicCallStackItem; - use dep::aztec::log::emit_encrypted_log; - use dep::aztec::oracle::get_public_key::get_public_key; - use dep::aztec::types::vec::BoundedVec; - use dep::aztec::types::point::Point; - use dep::aztec::note::utils as note_utils; - use dep::aztec::note::note_header::NoteHeader; - use dep::aztec::constants_gen::MAX_NOTE_FIELDS_LENGTH; - use dep::aztec::constants_gen::GENERATOR_INDEX__CONTRACT_ADDRESS; - use dep::aztec::constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD; + use dep::aztec::{ + constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD, + context::{PrivateContext, PublicContext}, + entrypoint::{ENTRYPOINT_PAYLOAD_SIZE, EntrypointPayload}, + log::emit_encrypted_log, + note::{ + note_header::NoteHeader, + utils as note_utils, + }, + oracle::get_public_key::get_public_key, + state_vars::immutable_singleton::ImmutableSingleton, + }; - use crate::storage::Storage; - use crate::public_key_note::PublicKeyNote; - use crate::public_key_note::PublicKeyNoteMethods; - use crate::public_key_note::PUBLIC_KEY_NOTE_LEN; + use crate::public_key_note::{PublicKeyNote, PublicKeyNoteMethods, PUBLIC_KEY_NOTE_LEN}; + + struct Storage { + signing_public_key: ImmutableSingleton, + } + + impl Storage { + fn init(private_context: Option<&mut PrivateContext>, _: Option<&mut PublicContext>) -> pub Self { + Storage { + signing_public_key: ImmutableSingleton::new(private_context, 1, PublicKeyNoteMethods), + } + } + } // docs:start:entrypoint #[aztec(private)] @@ -37,7 +43,7 @@ contract SchnorrAccount { let public_key = storage.signing_public_key.get_note(); // Verify payload signature - let payload_fields: [Field; entrypoint::ENTRYPOINT_PAYLOAD_SIZE] = payload.serialize(); + let payload_fields: [Field; ENTRYPOINT_PAYLOAD_SIZE] = payload.serialize(); let message_field: Field = std::hash::pedersen_with_separator(payload_fields, GENERATOR_INDEX__SIGNATURE_PAYLOAD)[0]; // TODO workaround for https://github.com/noir-lang/noir/issues/2421 let message_bytes_slice = message_field.to_be_bytes(32); diff --git a/yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/storage.nr b/yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/storage.nr deleted file mode 100644 index e6344a376e8..00000000000 --- a/yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/storage.nr +++ /dev/null @@ -1,17 +0,0 @@ -use dep::aztec::context::{PrivateContext, PublicContext}; -use dep::aztec::state_vars::immutable_singleton::ImmutableSingleton; -use dep::std::option::Option; - -use crate::public_key_note::{PublicKeyNote, PublicKeyNoteMethods, PUBLIC_KEY_NOTE_LEN}; - -struct Storage { - signing_public_key: ImmutableSingleton, -} - -impl Storage { - fn init(private_context: Option<&mut PrivateContext>, _: Option<&mut PublicContext>) -> Self { - Storage { - signing_public_key: ImmutableSingleton::new(private_context, 1, PublicKeyNoteMethods), - } - } -} diff --git a/yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr index 41f398ead1c..fe9fa044e5e 100644 --- a/yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/test_contract/src/main.nr @@ -2,15 +2,14 @@ contract Test { use dep::aztec::{ abi, - types::vec::BoundedVec, abi::PrivateContextInputs, - }; - use dep::aztec::context::PrivateContext; - - use dep::aztec::oracle::{ - get_public_key::get_public_key, - context::get_portal_address, - rand::rand, + context::PrivateContext, + oracle::{ + get_public_key::get_public_key, + context::get_portal_address, + rand::rand, + }, + types::vec::BoundedVec, }; #[aztec(private)] diff --git a/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr index b41f6c4ea3b..475181bcb63 100644 --- a/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr @@ -2,13 +2,7 @@ mod non_native_token_interface; // Demonstrates how to send a message to a portal contract on L1. We use Uniswap here as it's the most typical example. contract Uniswap { - use dep::aztec::oracle::{ - public_call, - context::get_portal_address - }; - use dep::aztec::private_call_stack_item::PrivateCallStackItem; - use dep::aztec::public_call_stack_item::PublicCallStackItem; - use dep::aztec::types::point::Point; + use dep::aztec::oracle::context::get_portal_address; use crate::non_native_token_interface::NonNativeTokenPrivateContextInterface;