Skip to content

Commit

Permalink
chore(contracts): migrate all contracts to use the AVM
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed May 8, 2024
1 parent 4d1ceb5 commit a803893
Show file tree
Hide file tree
Showing 33 changed files with 138 additions and 136 deletions.
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/authwit/src/account.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::aztec::context::{PrivateContext, PublicContext, Context};
use dep::aztec::context::{AvmContext, PrivateContext, PublicContext, Context};
use dep::aztec::state_vars::{Map, PublicMutable};
use dep::aztec::protocol_types::{address::AztecAddress, abis::function_selector::FunctionSelector, hash::pedersen_hash};

Expand Down Expand Up @@ -43,12 +43,12 @@ impl AccountActions {
}

pub fn public(
context: &mut PublicContext,
context: &mut AvmContext,
approved_action_storage_slot: Field,
is_valid_impl: fn(&mut PrivateContext, Field) -> bool
) -> Self {
AccountActions::init(
Context::public(context),
Context::avm(context),
approved_action_storage_slot,
is_valid_impl
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract AppSubscription {
payload.execute_calls(&mut context, storage.target_address.read_private());
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(initializer)]
fn constructor(
target_address: AztecAddress,
Expand All @@ -76,13 +76,13 @@ contract AppSubscription {
storage.gas_token_limit_per_tx.initialize(gas_token_limit_per_tx);
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn assert_not_expired(expiry_block_number: Field) {
assert((context.block_number()) as u64 < expiry_block_number as u64);
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn assert_block_number(expiry_block_number: Field) {
assert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract Auth {
// docs:end:shared_mutable_storage
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(initializer)]
fn constructor(admin: AztecAddress) {
assert(!admin.is_zero(), "invalid admin");
Expand All @@ -26,21 +26,21 @@ contract Auth {
}

// docs:start:shared_mutable_schedule
#[aztec(public)]
#[aztec(public-vm)]
fn set_authorized(authorized: AztecAddress) {
assert_eq(storage.admin.read(), context.msg_sender(), "caller is not admin");
storage.authorized.schedule_value_change(authorized);
// docs:end:shared_mutable_schedule
}

#[aztec(public)]
#[aztec(public-vm)]
fn get_authorized() -> AztecAddress {
// docs:start:shared_mutable_get_current_public
storage.authorized.get_current_value_in_public()
// docs:end:shared_mutable_get_current_public
}

#[aztec(public)]
#[aztec(public-vm)]
fn get_scheduled_authorized() -> AztecAddress {
// docs:start:shared_mutable_get_scheduled_public
let (scheduled_value, _block_of_change): (AztecAddress, u32) = storage.authorized.get_scheduled_value_in_public();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ contract Benchmarking {
}

// Reads and writes to public storage and enqueues a call to another public function.
#[aztec(public)]
#[aztec(public-vm)]
fn increment_balance(owner: AztecAddress, value: Field) {
let current = storage.balances.at(owner).read();
storage.balances.at(owner).write(current + value);
Benchmarking::at(context.this_address()).broadcast(owner).call(&mut context);
}

// Emits a public log.
#[aztec(public)]
#[aztec(public-vm)]
fn broadcast(owner: AztecAddress) {
context.emit_unencrypted_log(storage.balances.at(owner).read());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract CardGame {
CardGame::at(context.this_address()).on_game_joined(game, player, strength as u32).enqueue(&mut context);
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn on_game_joined(game: u32, player: AztecAddress, deck_strength: u32) {
let game_storage = storage.games.at(game as Field);
Expand All @@ -54,7 +54,7 @@ contract CardGame {
game_storage.write(game_data);
}

#[aztec(public)]
#[aztec(public-vm)]
fn start_game(game: u32) {
let game_storage = storage.games.at(game as Field);

Expand All @@ -75,7 +75,7 @@ contract CardGame {
// docs:end:call_public_function
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn on_card_played(game: u32, player: AztecAddress, card_as_field: Field) {
let game_storage = storage.games.at(game as Field);
Expand All @@ -100,7 +100,7 @@ contract CardGame {
CardGame::at(context.this_address()).on_cards_claimed(game, player, pedersen_hash(cards_fields, 0)).enqueue(&mut context);
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn on_cards_claimed(game: u32, player: AztecAddress, cards_hash: Field) {
let game_storage = storage.games.at(game as Field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ contract Child {
}

// Returns base_value + chain_id + version + block_number + timestamp
#[aztec(public)]
#[aztec(public-vm)]
fn pub_get_value(base_value: Field) -> Field {
let return_value = base_value
+ context.chain_id()
Expand All @@ -41,7 +41,7 @@ contract Child {
}

// Sets `current_value` to `new_value`
#[aztec(public)]
#[aztec(public-vm)]
fn pub_set_value(new_value: Field) -> Field {
storage.current_value.write(new_value);
context.emit_unencrypted_log(new_value);
Expand Down Expand Up @@ -69,7 +69,7 @@ contract Child {
}

// Increments `current_value` by `new_value`
#[aztec(public)]
#[aztec(public-vm)]
fn pub_inc_value(new_value: Field) -> Field {
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
Expand All @@ -79,7 +79,7 @@ contract Child {
}

// Increments `current_value` by `new_value`. Can only be called from this contract.
#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn pub_inc_value_internal(new_value: Field) -> Field {
let old_value = storage.current_value.read();
Expand All @@ -89,21 +89,21 @@ contract Child {
new_value
}

#[aztec(public)]
#[aztec(public-vm)]
fn set_value_twice_with_nested_first() {
let _result = Child::at(context.this_address()).pub_set_value(10).call(&mut context);
storage.current_value.write(20);
context.emit_unencrypted_log(20);
}

#[aztec(public)]
#[aztec(public-vm)]
fn set_value_twice_with_nested_last() {
storage.current_value.write(20);
context.emit_unencrypted_log(20);
let _result = Child::at(context.this_address()).pub_set_value(10).call(&mut context);
}

#[aztec(public)]
#[aztec(public-vm)]
// TODO(6052): The logs emitted are currently in the wrong order as we don't update
// counters for nested public calls
fn set_value_with_two_nested_calls() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract Claim {
reward_token: SharedImmutable<AztecAddress>,
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(initializer)]
fn constructor(target_contract: AztecAddress, reward_token: AztecAddress) {
storage.target_contract.initialize(target_contract);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract Crowdfunding {
// docs:start:init
// docs:start:init-header
// docs:start:init-header-error
#[aztec(public)]
#[aztec(public-vm)]
#[aztec(initializer)]
// this-will-error:init-header-error
fn init(donation_token: AztecAddress, operator: AztecAddress, deadline: u64) {
Expand All @@ -54,7 +54,7 @@ contract Crowdfunding {

// docs:start:deadline
// docs:start:deadline-header
#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn _check_deadline() {
// docs:end:deadline-header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract DelegatedOn {
new_value
}

#[aztec(public)]
#[aztec(public-vm)]
fn public_set_value(new_value: Field) -> Field {
storage.current_value.write(new_value);
new_value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract Delegator {
DelegatedOn::at(target_contract).public_set_value(value).delegate_enqueue(&mut context)
}

#[aztec(public)]
#[aztec(public-vm)]
fn public_delegate_set_value(target_contract: AztecAddress, value: Field) -> Field {
DelegatedOn::at(target_contract).public_set_value(value).delegate_call(&mut context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ contract DocsExample {
}
}

#[aztec(public)]
#[aztec(public-vm)]
fn initialize_shared_immutable(points: u8) {
let mut new_leader = Leader { account: context.msg_sender(), points };
storage.shared_immutable.initialize(new_leader);
Expand All @@ -118,7 +118,7 @@ contract DocsExample {
leader
}

#[aztec(public)]
#[aztec(public-vm)]
fn get_shared_immutable_constrained_public_indirect() -> pub Leader {
// This is a public function that calls another public function
// and returns the response.
Expand All @@ -129,12 +129,12 @@ contract DocsExample {
leader
}

#[aztec(public)]
#[aztec(public-vm)]
fn get_shared_immutable_constrained_public() -> pub Leader {
storage.shared_immutable.read_public()
}

#[aztec(public)]
#[aztec(public-vm)]
fn get_shared_immutable_constrained_public_multiple() -> pub [Leader; 5] {
let a = storage.shared_immutable.read_public();
[a, a, a, a, a]
Expand All @@ -149,7 +149,7 @@ contract DocsExample {
storage.shared_immutable.read_public()
}

#[aztec(public)]
#[aztec(public-vm)]
fn initialize_public_immutable(points: u8) {
// docs:start:initialize_public_immutable
let mut new_leader = Leader { account: context.msg_sender(), points };
Expand Down Expand Up @@ -239,12 +239,12 @@ contract DocsExample {
1
}

#[aztec(public)]
#[aztec(public-vm)]
fn spend_public_authwit(inner_hash: Field) -> Field {
1
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn update_leader(account: AztecAddress, points: u8) {
let new_leader = Leader { account, points };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract EasyPrivateVoting {
// docs:end:storage_struct

// docs:start:constructor
#[aztec(public)]
#[aztec(public-vm)]
#[aztec(initializer)] // annotation to mark function as a constructor
fn constructor(admin: AztecAddress) {
storage.admin.write(admin);
Expand All @@ -35,7 +35,7 @@ contract EasyPrivateVoting {
// docs:end:cast_vote

// docs:start:add_to_tally_public
#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn add_to_tally_public(candidate: Field) {
assert(storage.vote_ended.read() == false, "Vote has ended"); // assert that vote has not ended
Expand All @@ -45,7 +45,7 @@ contract EasyPrivateVoting {
// docs:end:add_to_tally_public

// docs:start:end_vote
#[aztec(public)]
#[aztec(public-vm)]
fn end_vote() {
assert(storage.admin.read().eq(context.msg_sender()), "Only admin can end votes"); // assert that caller is admin
storage.vote_ended.write(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract EcdsaAccount {
actions.spend_private_authwit(inner_hash)
}

#[aztec(public)]
#[aztec(public-vm)]
fn spend_public_authwit(inner_hash: Field) -> Field {
let actions = AccountActions::public(&mut context, ACCOUNT_ACTIONS_STORAGE_SLOT, is_valid_impl);
actions.spend_public_authwit(inner_hash)
Expand All @@ -58,7 +58,7 @@ contract EcdsaAccount {
context.push_new_nullifier(outer_hash, 0);
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn approve_public_authwit(outer_hash: Field) {
let actions = AccountActions::public(&mut context, ACCOUNT_ACTIONS_STORAGE_SLOT, is_valid_impl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract FPC {
gas_token_address: SharedImmutable<AztecAddress>,
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(initializer)]
fn constructor(other_asset: AztecAddress, gas_token_address: AztecAddress) {
storage.other_asset.initialize(other_asset);
Expand All @@ -31,21 +31,21 @@ contract FPC {
FPC::at(context.this_address()).pay_fee(context.msg_sender(), amount, asset).enqueue(&mut context);
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn prepare_fee(from: AztecAddress, amount: Field, asset: AztecAddress, nonce: Field) {
Token::at(asset).transfer_public(from, context.this_address(), amount, nonce).call(&mut context);
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn pay_fee(refund_address: AztecAddress, amount: Field, asset: AztecAddress) {
let refund = GasToken::at(storage.gas_token_address.read_public()).pay_fee(amount).call(&mut context);
// Just do public refunds for the present
Token::at(asset).transfer_public(context.this_address(), refund_address, refund, 0).call(&mut context);
}

#[aztec(public)]
#[aztec(public-vm)]
#[aztec(internal)]
fn pay_fee_with_shielded_rebate(amount: Field, asset: AztecAddress, secret_hash: Field) {
let refund = GasToken::at(storage.gas_token_address.read_public()).pay_fee(amount).call(&mut context);
Expand Down
Loading

0 comments on commit a803893

Please sign in to comment.