From 323c517d5556c216efe14c7cc147bb9456704206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Sat, 13 Sep 2025 04:46:46 +0000 Subject: [PATCH] wip --- .../aztec-nr/aztec/src/context/mod.nr | 2 + .../aztec/src/context/self_contract.nr | 104 ++++++++++++++++ .../aztec/src/macros/functions/utils.nr | 27 +++-- .../contracts/app/amm_contract/src/main.nr | 111 ++++++++---------- .../contracts/app/auth_contract/src/main.nr | 4 +- .../app/crowdfunding_contract/src/main.nr | 20 ++-- .../app/simple_token_contract/src/main.nr | 8 +- .../contracts/app/token_contract/src/main.nr | 42 +++---- .../auth_registry_contract/src/main.nr | 14 ++- .../pending_note_hashes_contract/src/main.nr | 10 +- 10 files changed, 222 insertions(+), 120 deletions(-) create mode 100644 noir-projects/aztec-nr/aztec/src/context/self_contract.nr diff --git a/noir-projects/aztec-nr/aztec/src/context/mod.nr b/noir-projects/aztec-nr/aztec/src/context/mod.nr index cf1f9f5e853d..a7cab3e77670 100644 --- a/noir-projects/aztec-nr/aztec/src/context/mod.nr +++ b/noir-projects/aztec-nr/aztec/src/context/mod.nr @@ -17,3 +17,5 @@ pub use private_context::PrivateContext; pub use public_context::PublicContext; pub use returns_hash::ReturnsHash; pub use utility_context::UtilityContext; + +pub mod self_contract; diff --git a/noir-projects/aztec-nr/aztec/src/context/self_contract.nr b/noir-projects/aztec-nr/aztec/src/context/self_contract.nr new file mode 100644 index 000000000000..587afe21e8a1 --- /dev/null +++ b/noir-projects/aztec-nr/aztec/src/context/self_contract.nr @@ -0,0 +1,104 @@ +use crate::{ + context::{ + call_interfaces::{PrivateCallInterface, PublicCallInterface, PublicStaticCallInterface}, + private_context::PrivateContext, + public_context::PublicContext, + utility_context::UtilityContext, + }, + event::{ + event_emission::{emit_event_in_private, emit_event_in_public}, + event_interface::EventInterface, + }, +}; +use protocol_types::{address::AztecAddress, traits::{Deserialize, Serialize}}; + +pub struct SelfContract { + context: Context, +} + +impl SelfContract { + pub fn new(context: Context) -> Self { + SelfContract { context } + } +} + +impl SelfContract<&mut PrivateContext> { + pub fn msg_sender(self) -> AztecAddress { + self.context.msg_sender() + } + + pub fn address(self) -> AztecAddress { + self.context.this_address() + } + + pub fn call(self, call_interface: PrivateCallInterface) -> T + where + T: Deserialize, + { + call_interface.call(self.context) + } + + pub fn enqueue(self, call_interface: PublicCallInterface) + where + T: Deserialize, + { + call_interface.enqueue(self.context) + } + + pub fn emit_event(self, event: Event, recipient: AztecAddress, delivery_mode: u8) + where + Event: EventInterface + Serialize, + { + emit_event_in_private(event, self.context, recipient, delivery_mode); + } + // aztec-nr does not know about the router contract (because the router contract depends on aztec-nr) + // pub fn check_timestamp(self, operation: u8, value: u64) { + // privately_check_timestamp(operation, value, self.context); + // } + // pub fn check_block_number(self, operation: u8, value: u32) { + // privately_check_block_number(operation, value, self.context); + // } +} + +impl SelfContract { + pub fn msg_sender(self) -> AztecAddress { + self.context.msg_sender() + } + + pub fn address(self) -> AztecAddress { + self.context.this_address() + } + + pub unconstrained fn call(self, call_interface: PublicCallInterface) -> T + where + T: Deserialize, + { + let mut ctx = self.context; + call_interface.call(&mut ctx) + } + + pub unconstrained fn view( + self, + call_interface: PublicStaticCallInterface, + ) -> T + where + T: Deserialize, + { + let mut ctx = self.context; + call_interface.view(&mut ctx) + } + + pub fn emit_event(self, event: Event) + where + Event: EventInterface + Serialize, + { + let mut ctx = self.context; + emit_event_in_public(event, &mut ctx); + } +} + +impl SelfContract { + pub fn address(self) -> AztecAddress { + self.context.this_address() + } +} diff --git a/noir-projects/aztec-nr/aztec/src/macros/functions/utils.nr b/noir-projects/aztec-nr/aztec/src/macros/functions/utils.nr index bd8772f87a2e..3b7ed4680240 100644 --- a/noir-projects/aztec-nr/aztec/src/macros/functions/utils.nr +++ b/noir-projects/aztec-nr/aztec/src/macros/functions/utils.nr @@ -50,8 +50,12 @@ pub(crate) comptime fn transform_private(f: FunctionDefinition) { }, ); - let context_creation = quote { + let context_and_self_creation = quote { let mut context = dep::aztec::context::private_context::PrivateContext::new(inputs, dep::aztec::protocol_types::traits::Hash::hash($args_hasher_name)); + // Some functions don't use `self`, but it'd be quite difficult to only inject this variable if it is + // referenced. We instead ignore 'unused variable' warnings for it. + #[allow(unused_variables)] + let self = dep::aztec::context::self_contract::SelfContract::new(&mut context); }; // Modifications introduced by the different marker attributes. @@ -155,7 +159,7 @@ pub(crate) comptime fn transform_private(f: FunctionDefinition) { let to_prepend = quote { dep::aztec::oracle::version::assert_compatible_oracle_version(); $args_hasher - $context_creation + $context_and_self_creation $assert_initializer $init_check $internal_check @@ -204,12 +208,16 @@ pub(crate) comptime fn transform_public(f: FunctionDefinition) { }; // Unlike in the private case, in public the `context` does not need to receive the hash of the original params. - let context_creation = quote { + let context_and_self_creation = quote { let mut context = dep::aztec::context::public_context::PublicContext::new(|| { // We start from 1 because we skip the selector for the dispatch function. let serialized_args : [Field; $args_len_quote] = dep::aztec::context::public_context::calldata_copy(1, $args_len_quote); dep::aztec::hash::hash_args_array(serialized_args) }); + // Some functions don't use `self`, but it'd be quite difficult to only inject this variable if it is + // referenced. We instead ignore 'unused variable' warnings for it. + #[allow(unused_variables)] + let self = dep::aztec::context::self_contract::SelfContract::new(context); }; // Modifications introduced by the different marker attributes. @@ -257,7 +265,7 @@ pub(crate) comptime fn transform_public(f: FunctionDefinition) { }; let to_prepend = quote { - $context_creation + $context_and_self_creation $assert_initializer $init_check $internal_check @@ -286,8 +294,13 @@ pub(crate) comptime fn transform_utility(f: FunctionDefinition) { stub_registry::register(f.module(), fn_stub); // Create utility context - let context_creation = - quote { let mut context = dep::aztec::context::utility_context::UtilityContext::new(); }; + let context_and_self_creation = quote { + let mut context = dep::aztec::context::utility_context::UtilityContext::new(); + // Some functions don't use `self`, but it'd be quite difficult to only inject this variable if it is + // referenced. We instead ignore 'unused variable' warnings for it. + #[allow(unused_variables)] + let self = dep::aztec::context::self_contract::SelfContract::new(context); + }; // Initialize Storage if module has storage let storage_init = if module_has_storage(f.module()) { @@ -309,7 +322,7 @@ pub(crate) comptime fn transform_utility(f: FunctionDefinition) { // A quote to be injected at the beginning of the function body. let to_prepend = quote { dep::aztec::oracle::version::assert_compatible_oracle_version(); - $context_creation + $context_and_self_creation $storage_init $message_discovery_call }; diff --git a/noir-projects/noir-contracts/contracts/app/amm_contract/src/main.nr b/noir-projects/noir-contracts/contracts/app/amm_contract/src/main.nr index 5cf2c47879f4..08404cc9ba00 100644 --- a/noir-projects/noir-contracts/contracts/app/amm_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/app/amm_contract/src/main.nr @@ -99,50 +99,46 @@ pub contract AMM { let token1 = Token::at(config.token1); let liquidity_token = Token::at(config.liquidity_token); - let sender = context.msg_sender(); + let sender = self.msg_sender(); // We don't yet know how many tokens the sender will actually supply - that can only be computed during public // execution since the amounts supplied must have the same ratio as the live balances. We therefore transfer the // maximum amounts here, and prepare partial notes that return the change to the sender (if any). - let refund_token0_partial_note = token0 + let refund_token0_partial_note = self.call(token0 .transfer_to_public_and_prepare_private_balance_increase( sender, - context.this_address(), + self.address(), amount0_max, authwit_nonce, - ) - .call(&mut context); + )); - let refund_token1_partial_note = token1 + let refund_token1_partial_note = self.call(token1 .transfer_to_public_and_prepare_private_balance_increase( sender, - context.this_address(), + self.address(), amount1_max, authwit_nonce, - ) - .call(&mut context); + )); // The number of liquidity tokens to mint for the caller depends on both the live balances and the amount // supplied, both of which can only be known during public execution. We therefore prepare a partial note that // will get completed via minting. let liquidity_partial_note = - liquidity_token.prepare_private_balance_increase(sender).call(&mut context); + self.call(liquidity_token.prepare_private_balance_increase(sender)); // We then complete the flow in public. Note that the type of operation and amounts will all be publicly known, // but the identity of the caller is not revealed despite us being able to send tokens to them by completing the // partial notes. - AMM::at(context.this_address()) - ._add_liquidity( - config, - refund_token0_partial_note, - refund_token1_partial_note, - liquidity_partial_note, - amount0_max, - amount1_max, - amount0_min, - amount1_min, - ) - .enqueue(&mut context); + self.enqueue(AMM::at(self.address())._add_liquidity( + config, + refund_token0_partial_note, + refund_token1_partial_note, + liquidity_partial_note, + amount0_max, + amount1_max, + amount0_min, + amount1_min, + )); } #[public] @@ -164,12 +160,10 @@ pub contract AMM { // We read the current AMM balance of both tokens. Note that by the time this function is called the token // transfers have already been completed (since those calls were enqueued before this call), and so we need to // subtract the transfer amount to get the pre-deposit balance. - let balance0_plus_amount0_max = - token0.balance_of_public(context.this_address()).view(&mut context); + let balance0_plus_amount0_max = self.view(token0.balance_of_public(self.address())); let balance0 = balance0_plus_amount0_max - amount0_max; - let balance1_plus_amount1_max = - token1.balance_of_public(context.this_address()).view(&mut context); + let balance1_plus_amount1_max = self.view(token1.balance_of_public(self.address())); let balance1 = balance1_plus_amount1_max - amount1_max; // With the current balances known, we can calculate the token amounts to the pool, respecting the user's @@ -190,14 +184,16 @@ pub contract AMM { // We can simply skip the refund if the amount to return is 0 in order to save gas: the partial note will // simply stay in public storage and not be completed, but this is not an issue. if (refund_amount_token0 > 0 as u128) { - token0 - .finalize_transfer_to_private(refund_amount_token0, refund_token0_partial_note) - .call(&mut context); + self.call(token0.finalize_transfer_to_private( + refund_amount_token0, + refund_token0_partial_note, + )); } if (refund_amount_token1 > 0 as u128) { - token1 - .finalize_transfer_to_private(refund_amount_token1, refund_token1_partial_note) - .call(&mut context); + self.call(token1.finalize_transfer_to_private( + refund_amount_token1, + refund_token1_partial_note, + )); } // With the deposit amounts known, we can compute the number of liquidity tokens to mint and finalize the @@ -219,9 +215,7 @@ pub contract AMM { // As part of initialization, we mint some tokens to the zero address to 'lock' them (i.e. make them // impossible to redeem), guaranteeing total supply will never be zero again. - liquidity_token.mint_to_public(AztecAddress::zero(), MINIMUM_LIQUIDITY).call( - &mut context, - ); + self.call(liquidity_token.mint_to_public(AztecAddress::zero(), MINIMUM_LIQUIDITY)); INITIAL_LIQUIDITY }; @@ -252,27 +246,28 @@ pub contract AMM { let token0 = Token::at(config.token0); let token1 = Token::at(config.token1); - let sender = context.msg_sender(); + let sender = self.msg_sender(); // Liquidity tokens are burned when liquidity is removed in order to reduce the total supply. However, we lack // a function to privately burn, so we instead transfer the tokens into the AMM's public balance, and then have // the AMM publicly burn its own tokens. // TODO(#10287): consider adding a private burn - liquidity_token - .transfer_to_public(sender, context.this_address(), liquidity, authwit_nonce) - .call(&mut context); + self.call(liquidity_token.transfer_to_public( + sender, + self.address(), + liquidity, + authwit_nonce, + )); // We don't yet know how many tokens the sender will get - that can only be computed during public execution // since the it depends on the live balances. We therefore simply prepare partial notes to the sender. - let token0_partial_note = - token0.prepare_private_balance_increase(sender).call(&mut context); - let token1_partial_note = - token1.prepare_private_balance_increase(sender).call(&mut context); + let token0_partial_note = self.call(token0.prepare_private_balance_increase(sender)); + let token1_partial_note = self.call(token1.prepare_private_balance_increase(sender)); // We then complete the flow in public. Note that the type of operation and amounts will all be publicly known, // but the identity of the caller is not revealed despite us being able to send tokens to them by completing the // partial notes. - AMM::at(context.this_address()) + AMM::at(self.address()) ._remove_liquidity( config, liquidity, @@ -300,8 +295,8 @@ pub contract AMM { // We need the current balance of both tokens as well as the liquidity token total supply in order to compute // the amounts to send the user. - let balance0 = token0.balance_of_public(context.this_address()).view(&mut context); - let balance1 = token1.balance_of_public(context.this_address()).view(&mut context); + let balance0 = token0.balance_of_public(self.address()).view(&mut context); + let balance1 = token1.balance_of_public(self.address()).view(&mut context); let total_supply = liquidity_token.total_supply().view(&mut context); // We calculate the amounts of token0 and token1 the user is entitled to based on the amount of liquidity they @@ -312,7 +307,7 @@ pub contract AMM { // We can now burn the liquidity tokens that had been privately transferred into the AMM, as well as complete // both partial notes. - liquidity_token.burn_public(context.this_address(), liquidity, 0).call(&mut context); + liquidity_token.burn_public(self.address(), liquidity, 0).call(&mut context); token0.finalize_transfer_to_private(amount0, token0_partial_note).call(&mut context); token1.finalize_transfer_to_private(amount1, token1_partial_note).call(&mut context); } @@ -337,17 +332,17 @@ pub contract AMM { assert((token_out == config.token0) | (token_out == config.token1), "TOKEN_OUT_IS_INVALID"); assert(token_in != token_out, "SAME_TOKEN_SWAP"); - let sender = context.msg_sender(); + let sender = self.msg_sender(); // We transfer the full amount in, since it is an exact amount, and prepare a partial note for the amount out, // which will only be known during public execution as it depends on the live balances. Token::at(token_in) - .transfer_to_public(sender, context.this_address(), amount_in, authwit_nonce) + .transfer_to_public(sender, self.address(), amount_in, authwit_nonce) .call(&mut context); let token_out_partial_note = Token::at(token_out).prepare_private_balance_increase(sender).call(&mut context); - AMM::at(context.this_address()) + AMM::at(self.address()) ._swap_exact_tokens_for_tokens( token_in, token_out, @@ -371,11 +366,10 @@ pub contract AMM { // transfer has already been completed as that function call was enqueued before this one. We therefore need to // subtract the amount in to get the pre-swap balances. let balance_in_plus_amount_in = - Token::at(token_in).balance_of_public(context.this_address()).view(&mut context); + Token::at(token_in).balance_of_public(self.address()).view(&mut context); let balance_in = balance_in_plus_amount_in - amount_in; - let balance_out = - Token::at(token_out).balance_of_public(context.this_address()).view(&mut context); + let balance_out = Token::at(token_out).balance_of_public(self.address()).view(&mut context); // We can now compute the number of tokens to transfer and complete the partial note. let amount_out = get_amount_out(amount_in, balance_in, balance_out); @@ -406,7 +400,7 @@ pub contract AMM { assert((token_out == config.token0) | (token_out == config.token1), "TOKEN_OUT_IS_INVALID"); assert(token_in != token_out, "SAME_TOKEN_SWAP"); - let sender = context.msg_sender(); + let sender = self.msg_sender(); // We don't know how many tokens we'll receive from the user, since the swap amount will only be known during // public execution as it depends on the live balances. We therefore transfer the full maximum amount and @@ -417,7 +411,7 @@ pub contract AMM { let change_token_in_partial_note = Token::at(token_in) .transfer_to_public_and_prepare_private_balance_increase( sender, - context.this_address(), + self.address(), amount_in_max, authwit_nonce, ) @@ -426,7 +420,7 @@ pub contract AMM { let token_out_partial_note = Token::at(token_out).prepare_private_balance_increase(sender).call(&mut context); - AMM::at(context.this_address()) + AMM::at(self.address()) ._swap_tokens_for_exact_tokens( token_in, token_out, @@ -452,11 +446,10 @@ pub contract AMM { // transfer has already been completed as that function call was enqueued before this one. We therefore need to // subtract the amount in to get the pre-swap balances. let balance_in_plus_amount_in_max = - Token::at(token_in).balance_of_public(context.this_address()).view(&mut context); + Token::at(token_in).balance_of_public(self.address()).view(&mut context); let balance_in = balance_in_plus_amount_in_max - amount_in_max; - let balance_out = - Token::at(token_out).balance_of_public(context.this_address()).view(&mut context); + let balance_out = Token::at(token_out).balance_of_public(self.address()).view(&mut context); // We can now compute the number of tokens we need to receive and complete the partial note with the change. let amount_in = get_amount_in(amount_out, balance_in, balance_out); diff --git a/noir-projects/noir-contracts/contracts/app/auth_contract/src/main.nr b/noir-projects/noir-contracts/contracts/app/auth_contract/src/main.nr index b09ddadec9d3..5f94924aa5ab 100644 --- a/noir-projects/noir-contracts/contracts/app/auth_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/app/auth_contract/src/main.nr @@ -34,7 +34,7 @@ pub contract Auth { // docs:start:delayed_public_mutable_schedule #[public] fn set_authorized(authorized: AztecAddress) { - assert_eq(storage.admin.read(), context.msg_sender(), "caller is not admin"); + assert_eq(storage.admin.read(), self.msg_sender(), "caller is not admin"); storage.authorized.schedule_value_change(authorized); // docs:end:delayed_public_mutable_schedule } @@ -77,7 +77,7 @@ pub contract Auth { // docs:start:delayed_public_mutable_get_current_private let authorized = storage.authorized.get_current_value(); // docs:end:delayed_public_mutable_get_current_private - assert_eq(authorized, context.msg_sender(), "caller is not authorized"); + assert_eq(authorized, self.msg_sender(), "caller is not authorized"); } #[private] diff --git a/noir-projects/noir-contracts/contracts/app/crowdfunding_contract/src/main.nr b/noir-projects/noir-contracts/contracts/app/crowdfunding_contract/src/main.nr index d796106f7bbb..e0e252ab4816 100644 --- a/noir-projects/noir-contracts/contracts/app/crowdfunding_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/app/crowdfunding_contract/src/main.nr @@ -9,7 +9,6 @@ pub contract Crowdfunding { // docs:start:all-deps use crate::config::Config; use aztec::{ - event::event_emission::emit_event_in_public, macros::{ events::event, functions::{initializer, internal, private, public}, @@ -68,10 +67,13 @@ pub contract Crowdfunding { // docs:start:do-transfer // 2) Transfer the donation tokens from donor to this contract - let donor = context.msg_sender(); - Token::at(config.donation_token) - .transfer_in_private(donor, context.this_address(), amount, 0) - .call(&mut context); + let donor = self.msg_sender(); + self.call(Token::at(config.donation_token).transfer_in_private( + donor, + self.address(), + amount, + 0, + )); // docs:end:do-transfer // 3) Create a value note for the donor so that he can later on claim a rewards token in the Claim @@ -94,12 +96,12 @@ pub contract Crowdfunding { let operator_address = config.operator; // 1) Check that msg_sender() is the operator - assert(context.msg_sender() == operator_address, "Not an operator"); + assert(self.msg_sender() == operator_address, "Not an operator"); // 2) Transfer the donation tokens from this contract to the operator - Token::at(config.donation_token).transfer(operator_address, amount).call(&mut context); + self.call(Token::at(config.donation_token).transfer(operator_address, amount)); // 3) Emit a public event so that anyone can audit how much the operator has withdrawn - Crowdfunding::at(context.this_address()) + Crowdfunding::at(self.address()) ._publish_donation_receipts(amount, operator_address) .enqueue(&mut context); } @@ -107,7 +109,7 @@ pub contract Crowdfunding { #[public] #[internal] fn _publish_donation_receipts(amount: u128, to: AztecAddress) { - emit_event_in_public(WithdrawalProcessed { amount, who: to }, &mut context); + self.emit_event(WithdrawalProcessed { amount, who: to }); } // docs:end:operator-withdrawals } diff --git a/noir-projects/noir-contracts/contracts/app/simple_token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/app/simple_token_contract/src/main.nr index c5b5d7e5a3e3..246cd0ffa870 100644 --- a/noir-projects/noir-contracts/contracts/app/simple_token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/app/simple_token_contract/src/main.nr @@ -15,7 +15,6 @@ pub contract SimpleToken { use aztec::{ authwit::auth::compute_authwit_nullifier, context::{PrivateCallInterface, PrivateContext, PublicContext}, - event::event_emission::emit_event_in_private, macros::{ events::event, functions::{authorize_once, initializer, internal, private, public, utility, view}, @@ -159,12 +158,7 @@ pub contract SimpleToken { MessageDelivery.UNCONSTRAINED_ONCHAIN, ); - emit_event_in_private( - Transfer { from, to, amount }, - &mut context, - to, - MessageDelivery.UNCONSTRAINED_ONCHAIN, - ); + self.emit_event(Transfer { from, to, amount }, to, MessageDelivery.UNCONSTRAINED_ONCHAIN); } #[authorize_once("from", "authwit_nonce")] diff --git a/noir-projects/noir-contracts/contracts/app/token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/app/token_contract/src/main.nr index 51ae870b4ba5..7e1dd38dbec5 100644 --- a/noir-projects/noir-contracts/contracts/app/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/app/token_contract/src/main.nr @@ -20,7 +20,6 @@ pub contract Token { use dep::aztec::{ context::{PrivateCallInterface, PrivateContext, PublicContext}, - event::event_emission::emit_event_in_private, macros::{ events::event, functions::{authorize_once, initializer, internal, private, public, utility, view}, @@ -97,7 +96,7 @@ pub contract Token { // docs:start:set_admin #[public] fn set_admin(new_admin: AztecAddress) { - assert(storage.admin.read().eq(context.msg_sender()), "caller is not admin"); + assert(storage.admin.read().eq(self.msg_sender()), "caller is not admin"); // docs:start:write_admin storage.admin.write(new_admin); // docs:end:write_admin @@ -176,7 +175,7 @@ pub contract Token { #[public] fn set_minter(minter: AztecAddress, approve: bool) { // docs:start:read_admin - assert(storage.admin.read().eq(context.msg_sender()), "caller is not admin"); + assert(storage.admin.read().eq(self.msg_sender()), "caller is not admin"); // docs:end:read_admin // docs:start:write_minter storage.minters.at(minter).write(approve); @@ -188,7 +187,7 @@ pub contract Token { #[public] fn mint_to_public(to: AztecAddress, amount: u128) { // docs:start:read_minter - assert(storage.minters.at(context.msg_sender()).read(), "caller is not minter"); + assert(storage.minters.at(self.msg_sender()).read(), "caller is not minter"); // docs:end:read_minter let new_balance = storage.public_balances.at(to).read().add(amount); let supply = storage.total_supply.read().add(amount); @@ -240,7 +239,7 @@ pub contract Token { from, MessageDelivery.CONSTRAINED_ONCHAIN, ); - Token::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context); + self.enqueue(Token::at(self.address())._increase_public_balance(to, amount)); } // docs:end:transfer_to_public @@ -271,7 +270,7 @@ pub contract Token { from, MessageDelivery.CONSTRAINED_ONCHAIN, ); - Token::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context); + self.enqueue(Token::at(self.address())._increase_public_balance(to, amount)); // We prepare the private balance increase (the partial note for the change). _prepare_private_balance_increase(from, &mut context, storage) @@ -280,7 +279,7 @@ pub contract Token { // docs:start:transfer #[private] fn transfer(to: AztecAddress, amount: u128) { - let from = context.msg_sender(); + let from = self.msg_sender(); // We reduce `from`'s balance by amount by recursively removing notes over potentially multiple calls. This // method keeps the gate count for each individual call low - reading too many notes at once could result in @@ -311,12 +310,7 @@ pub contract Token { // another person where the payment is considered to be successful when the other party successfully decrypts a // note). // docs:start:encrypted_unconstrained - emit_event_in_private( - Transfer { from, to, amount }, - &mut context, - to, - MessageDelivery.UNCONSTRAINED_ONCHAIN, - ); + self.emit_event(Transfer { from, to, amount }, to, MessageDelivery.UNCONSTRAINED_ONCHAIN); // docs:end:encrypted_unconstrained } // docs:end:transfer @@ -377,7 +371,7 @@ pub contract Token { // docs:start:cancel_authwit #[private] fn cancel_authwit(inner_hash: Field) { - let on_behalf_of = context.msg_sender(); + let on_behalf_of = self.msg_sender(); let nullifier = compute_authwit_nullifier(on_behalf_of, inner_hash); context.push_nullifier(nullifier); } @@ -418,7 +412,7 @@ pub contract Token { from, MessageDelivery.CONSTRAINED_ONCHAIN, ); - Token::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context); + self.enqueue(Token::at(self.address())._reduce_total_supply(amount)); } // docs:end:burn_private @@ -427,15 +421,15 @@ pub contract Token { #[private] fn transfer_to_private(to: AztecAddress, amount: u128) { // `from` is the owner of the public balance from which we'll subtract the `amount`. - let from = context.msg_sender(); - let token = Token::at(context.this_address()); + let from = self.msg_sender(); + let token = Token::at(self.address()); // We prepare the private balance increase (the partial note). let partial_note = _prepare_private_balance_increase(to, &mut context, storage); // At last we finalize the transfer. Usage of the `unsafe` method here is safe because we set the `from` // function argument to a message sender, guaranteeing that he can transfer only his own tokens. - token._finalize_transfer_to_private_unsafe(from, amount, partial_note).enqueue(&mut context); + self.enqueue(token._finalize_transfer_to_private_unsafe(from, amount, partial_note)); } // docs:end:transfer_to_private @@ -486,7 +480,7 @@ pub contract Token { fn finalize_transfer_to_private(amount: u128, partial_note: PartialUintNote) { // Completer is the entity that can complete the partial note. In this case, it's the same as the account // `from` from whose balance we're subtracting the `amount`. - let from_and_completer = context.msg_sender(); + let from_and_completer = self.msg_sender(); _finalize_transfer_to_private( from_and_completer, amount, @@ -520,7 +514,7 @@ pub contract Token { MessageDelivery.CONSTRAINED_ONCHAIN, ); - partial_note.complete_from_private(&mut context, context.msg_sender(), amount); + partial_note.complete_from_private(&mut context, self.msg_sender(), amount); } // docs:start:finalize_transfer_to_private_unsafe @@ -568,7 +562,7 @@ pub contract Token { /// in the enqueued call). #[private] fn mint_to_private(to: AztecAddress, amount: u128) { - let token = Token::at(context.this_address()); + let token = Token::at(self.address()); // We prepare the partial note to which we'll "send" the minted amount. let partial_note = _prepare_private_balance_increase(to, &mut context, storage); @@ -576,9 +570,7 @@ pub contract Token { // At last we finalize the mint. Usage of the `unsafe` method here is safe because we set // the `minter_and_completer` function argument to a message sender, guaranteeing that only a message sender // with minter permissions can successfully execute the function. - token._finalize_mint_to_private_unsafe(context.msg_sender(), amount, partial_note).enqueue( - &mut context, - ); + self.enqueue(token._finalize_mint_to_private_unsafe(self.msg_sender(), amount, partial_note)); } // docs:end:mint_to_private @@ -594,7 +586,7 @@ pub contract Token { fn finalize_mint_to_private(amount: u128, partial_note: PartialUintNote) { // Completer is the entity that can complete the partial note. In this case, it's the same as the minter // account. - let minter_and_completer = context.msg_sender(); + let minter_and_completer = self.msg_sender(); assert(storage.minters.at(minter_and_completer).read(), "caller is not minter"); _finalize_mint_to_private( diff --git a/noir-projects/noir-contracts/contracts/protocol/auth_registry_contract/src/main.nr b/noir-projects/noir-contracts/contracts/protocol/auth_registry_contract/src/main.nr index 39475ff3b864..4f2ba3783aea 100644 --- a/noir-projects/noir-contracts/contracts/protocol/auth_registry_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/protocol/auth_registry_contract/src/main.nr @@ -34,7 +34,7 @@ pub contract AuthRegistry { */ #[public] fn set_authorized(message_hash: Field, authorize: bool) { - storage.approved_actions.at(context.msg_sender()).at(message_hash).write(authorize); + storage.approved_actions.at(self.msg_sender()).at(message_hash).write(authorize); } /** @@ -46,7 +46,7 @@ pub contract AuthRegistry { */ #[public] fn set_reject_all(reject: bool) { - storage.reject_all.at(context.msg_sender()).write(reject); + storage.reject_all.at(self.msg_sender()).write(reject); } /** @@ -64,7 +64,7 @@ pub contract AuthRegistry { assert_eq(false, storage.reject_all.at(on_behalf_of).read(), "rejecting all"); let message_hash = compute_authwit_message_hash( - context.msg_sender(), + self.msg_sender(), context.chain_id(), context.version(), inner_hash, @@ -92,9 +92,11 @@ pub contract AuthRegistry { #[private] fn set_authorized_private(approver: AztecAddress, message_hash: Field, authorize: bool) { assert_current_call_valid_authwit::<3>(&mut context, approver); - AuthRegistry::at(context.this_address()) - ._set_authorized(approver, message_hash, authorize) - .enqueue(&mut context); + self.enqueue(AuthRegistry::at(self.address())._set_authorized( + approver, + message_hash, + authorize, + )); } /** diff --git a/noir-projects/noir-contracts/contracts/test/pending_note_hashes_contract/src/main.nr b/noir-projects/noir-contracts/contracts/test/pending_note_hashes_contract/src/main.nr index 1ba80f6c25a7..e4b266f12a6c 100644 --- a/noir-projects/noir-contracts/contracts/test/pending_note_hashes_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test/pending_note_hashes_contract/src/main.nr @@ -44,7 +44,7 @@ pub contract PendingNoteHashes { let note = ValueNote::new(amount, owner); // Insert note - owner_balance.insert(note).emit(&mut context, owner, MessageDelivery.CONSTRAINED_ONCHAIN); + owner_balance.insert(note).emit(owner, MessageDelivery.CONSTRAINED_ONCHAIN); let options = NoteGetterOptions::with_filter(filter_notes_min_sum, amount); // get note inserted above @@ -87,7 +87,7 @@ pub contract PendingNoteHashes { let note = ValueNote::new(amount, owner); // Insert note - owner_balance.insert(note).emit(&mut context, owner, MessageDelivery.CONSTRAINED_ONCHAIN); + owner_balance.insert(note).emit(owner, MessageDelivery.CONSTRAINED_ONCHAIN); } // Nested/inner function to create and insert a note @@ -103,7 +103,7 @@ pub contract PendingNoteHashes { let note = ValueNote::unpack([amount.to_field(), owner.to_field(), randomness.to_field()]); // Insert note - owner_balance.insert(note).emit(&mut context, owner, MessageDelivery.CONSTRAINED_ONCHAIN); + owner_balance.insert(note).emit(owner, MessageDelivery.CONSTRAINED_ONCHAIN); } // Nested/inner function to create and insert a note @@ -117,10 +117,10 @@ pub contract PendingNoteHashes { // Insert note let emission = owner_balance.insert(note); - emission.emit(&mut context, owner, MessageDelivery.CONSTRAINED_ONCHAIN); + emission.emit(owner, MessageDelivery.CONSTRAINED_ONCHAIN); // Emit note again - emission.emit(&mut context, owner, MessageDelivery.CONSTRAINED_ONCHAIN); + emission.emit(owner, MessageDelivery.CONSTRAINED_ONCHAIN); } // Nested/inner function to get a note and confirm it matches the expected value