Skip to content

Commit

Permalink
refactor: using UintNote in crowdfunding and claim
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jan 16, 2025
1 parent f0ce52f commit da0a8c5
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ value_note = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#inc

### In your contract

#include_code import_valuenote noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust
#include_code import_valuenote noir-projects/noir-contracts/contracts/child_contract/src/main.nr rust

## Working with ValueNote

Expand All @@ -30,11 +30,9 @@ value_note = { git="https://github.com/AztecProtocol/aztec-packages/", tag="#inc
Creating a new `ValueNote` takes the following args:

- `value` (`Field`): the value of the ValueNote
- `npk_m_hash` (`Field`): the master nullifier public key hash of the user
- `owner` (`AztecAddress`): owner is the party whose nullifying key can be used to spend the note

#include_code valuenote_new noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr rust

In this example, `amount` is the `value` and the `npk_m_hash` of the donor was computed earlier.
#include_code valuenote_new noir-projects/noir-contracts/contracts/child_contract/src/main.nr rust

### Getting a balance

Expand Down
3 changes: 2 additions & 1 deletion noir-projects/aztec-nr/uint-note/src/uint_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use dep::aztec::{
prelude::{NoteHeader, NullifiableNote, PrivateContext},
protocol_types::{
address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
hash::poseidon2_hash_with_separator,
hash::poseidon2_hash_with_separator, traits::Serialize,
},
};

// docs:start:UintNote
#[partial_note(quote {value})]
#[derive(Serialize)]
pub struct UintNote {
// The amount of tokens in the note
value: U128,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ contract Child {
note::note_getter_options::NoteGetterOptions,
utils::comparison::Comparator,
};
// docs:start:import_valuenote
use dep::value_note::value_note::ValueNote;
// docs:end:import_valuenote

#[storage]
struct Storage<Context> {
Expand Down Expand Up @@ -55,7 +57,10 @@ contract Child {

#[private]
fn private_set_value(new_value: Field, owner: AztecAddress) -> Field {
// docs:start:valuenote_new
let mut note = ValueNote::new(new_value, owner);
// docs:end:valuenote_new

storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(
&mut context,
owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ type = "contract"

[dependencies]
aztec = { path = "../../../aztec-nr/aztec" }
value_note = { path = "../../../aztec-nr/value-note" }
uint_note = { path = "../../../aztec-nr/uint-note" }
token = { path = "../token_contract" }
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ contract Claim {
protocol_types::address::AztecAddress,
state_vars::PublicImmutable,
};
use dep::value_note::value_note::ValueNote;
use dep::uint_note::uint_note::UintNote;
use token::Token;

#[storage]
Expand All @@ -27,7 +27,7 @@ contract Claim {
}

#[private]
fn claim(proof_note: ValueNote, recipient: AztecAddress) {
fn claim(proof_note: UintNote, recipient: AztecAddress) {
// 1) Check that the note corresponds to the target contract and belongs to the sender
let target_address = storage.target_contract.read();
assert(
Expand All @@ -51,9 +51,8 @@ contract Claim {
context.push_nullifier(nullifier);

// 4) Finally we mint the reward token to the sender of the transaction
// TODO(benesjan): Instead of ValueNote use UintNote to avoid the conversion to U128 below.
Token::at(storage.reward_token.read())
.mint_to_public(recipient, U128::from_integer(proof_note.value))
.enqueue(&mut context);
Token::at(storage.reward_token.read()).mint_to_public(recipient, proof_note.value).enqueue(
&mut context,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ type = "contract"

[dependencies]
aztec = { path = "../../../aztec-nr/aztec" }
value_note = { path = "../../../aztec-nr/value-note" }
uint_note = { path = "../../../aztec-nr/uint-note" }
token = { path = "../token_contract" }
router = { path = "../router_contract" }
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ contract Crowdfunding {
unencrypted_logs::unencrypted_event_emission::encode_event,
utils::comparison::Comparator,
};
use std::meta::derive;
// docs:start:import_valuenote
use dep::value_note::value_note::ValueNote;
// docs:end:import_valuenote
use dep::uint_note::uint_note::UintNote;
use router::utils::privately_check_timestamp;
use std::meta::derive;
use token::Token;
// docs:end:all-deps

Expand All @@ -43,7 +41,7 @@ contract Crowdfunding {
// End of the crowdfunding campaign after which no more donations are accepted
deadline: PublicImmutable<u64, Context>,
// Notes emitted to donors when they donate (can be used as proof to obtain rewards, eg in Claim contracts)
donation_receipts: PrivateSet<ValueNote, Context>,
donation_receipts: PrivateSet<UintNote, Context>,
}
// docs:end:storage

Expand Down Expand Up @@ -81,11 +79,8 @@ contract Crowdfunding {
// 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
// contract by proving that the hash of this note exists in the note hash tree.
// docs:start:valuenote_new
// TODO(benesjan): Instead of ValueNote use UintNote to avoid the conversion to a Field below.
let mut note = ValueNote::new(amount.to_field(), donor);
let mut note = UintNote::new(amount, donor);

// docs:end:valuenote_new
storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note(
&mut context,
donor,
Expand Down

0 comments on commit da0a8c5

Please sign in to comment.