Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Serialize trait impls for U128 following intrinsic Noir serialization #11142

Merged
merged 27 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 66 additions & 115 deletions noir-projects/noir-contracts/contracts/amm_contract/src/main.nr

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract AppSubscription {
target_address: PublicImmutable<AztecAddress, Context>,
subscription_token_address: PublicImmutable<AztecAddress, Context>,
subscription_recipient_address: PublicImmutable<AztecAddress, Context>,
subscription_price: PublicImmutable<Field, Context>,
subscription_price: PublicImmutable<U128, Context>,
subscriptions: Map<AztecAddress, PrivateMutable<SubscriptionNote, Context>, Context>,
fee_juice_limit_per_tx: PublicImmutable<Field, Context>,
}
Expand Down Expand Up @@ -68,7 +68,7 @@ contract AppSubscription {
target_address: AztecAddress,
subscription_recipient_address: AztecAddress,
subscription_token_address: AztecAddress,
subscription_price: Field,
subscription_price: U128,
fee_juice_limit_per_tx: Field,
) {
storage.target_address.initialize(target_address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ contract Claim {
context.push_nullifier(nullifier);

// 4) Finally we mint the reward token to the sender of the transaction
Token::at(storage.reward_token.read()).mint_to_public(recipient, proof_note.value).enqueue(
&mut context,
);
// TODO(benesjan): Instead of ValueNote use UintNote to avoid the conversion to U128 below.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will address this immediately in a follow-up PR.

Token::at(storage.reward_token.read())
.mint_to_public(recipient, U128::from_integer(proof_note.value))
.enqueue(&mut context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract Crowdfunding {
#[event]
struct WithdrawalProcessed {
who: AztecAddress,
amount: u64,
amount: U128,
}

// docs:start:storage
Expand Down Expand Up @@ -65,7 +65,7 @@ contract Crowdfunding {

// docs:start:donate
#[private]
fn donate(amount: u64) {
fn donate(amount: U128) {
// 1) Check that the deadline has not passed --> we do that via the router contract to conceal which contract
// is performing the check.
// docs:start:call-check-deadline
Expand All @@ -76,13 +76,14 @@ contract Crowdfunding {
// 2) Transfer the donation tokens from donor to this contract
let donor = context.msg_sender();
Token::at(storage.donation_token.read())
.transfer_in_private(donor, context.this_address(), amount as Field, 0)
.transfer_in_private(donor, context.this_address(), amount, 0)
.call(&mut context);
// 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
let mut note = ValueNote::new(amount as Field, donor);
// TODO(benesjan): Instead of ValueNote use UintNote to avoid the conversion to a Field below.
let mut note = ValueNote::new(amount.to_field(), donor);

// docs:end:valuenote_new
storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note(
Expand All @@ -96,13 +97,13 @@ contract Crowdfunding {
// docs:start:operator-withdrawals
// Withdraws balance to the operator. Requires that msg_sender() is the operator.
#[private]
fn withdraw(amount: u64) {
fn withdraw(amount: U128) {
// 1) Check that msg_sender() is the operator
let operator_address = storage.operator.read();
assert(context.msg_sender() == operator_address, "Not an operator");

// 2) Transfer the donation tokens from this contract to the operator
Token::at(storage.donation_token.read()).transfer(operator_address, amount as Field).call(
Token::at(storage.donation_token.read()).transfer(operator_address, amount).call(
&mut context,
);
// 3) Emit a public event so that anyone can audit how much the operator has withdrawn
Expand All @@ -114,7 +115,7 @@ contract Crowdfunding {

#[public]
#[internal]
fn _publish_donation_receipts(amount: u64, to: AztecAddress) {
fn _publish_donation_receipts(amount: U128, to: AztecAddress) {
WithdrawalProcessed { amount, who: to }.emit(encode_event(&mut context));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract Escrow {

// Withdraws balance. Requires that msg.sender is the owner.
#[private]
fn withdraw(token: AztecAddress, amount: Field, recipient: AztecAddress) {
fn withdraw(token: AztecAddress, amount: U128, recipient: AztecAddress) {
let sender = context.msg_sender();

let note = storage.owner.get_note();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub fn calculate_fee<TPublicContext>(context: PublicContext) -> Field {
context.transaction_fee()
}

pub fn get_bridge_gas_msg_hash(owner: AztecAddress, amount: Field) -> Field {
pub fn get_bridge_gas_msg_hash(owner: AztecAddress, amount: U128) -> Field {
let mut hash_bytes = [0; 68];
let recipient_bytes: [u8; 32] = owner.to_field().to_be_bytes();
let amount_bytes: [u8; 32] = amount.to_be_bytes();
let amount_bytes: [u8; 32] = amount.to_field().to_be_bytes();

// The purpose of including the following selector is to make the message unique to that specific call. Note that
// it has nothing to do with calling the function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract FeeJuice {
// Not flagged as initializer to reduce cost of checking init nullifier in all functions.
// This function should be called as entrypoint to initialize the contract by minting itself funds.
#[private]
fn initialize(portal_address: EthAddress, initial_mint: Field) {
fn initialize(portal_address: EthAddress, initial_mint: U128) {
// Validate contract class parameters are correct
let self = context.this_address();

Expand All @@ -46,7 +46,7 @@ contract FeeJuice {
}

#[private]
fn claim(to: AztecAddress, amount: Field, secret: Field, message_leaf_index: Field) {
fn claim(to: AztecAddress, amount: U128, secret: Field, message_leaf_index: Field) {
let content_hash = get_bridge_gas_msg_hash(to, amount);
let portal_address = storage.portal_address.read();
assert(!portal_address.is_zero());
Expand All @@ -63,22 +63,21 @@ contract FeeJuice {

#[public]
#[internal]
fn _increase_public_balance(to: AztecAddress, amount: Field) {
let new_balance = storage.balances.at(to).read().add(U128::from_integer(amount));
fn _increase_public_balance(to: AztecAddress, amount: U128) {
let new_balance = storage.balances.at(to).read().add(amount);
storage.balances.at(to).write(new_balance);
}

#[public]
#[view]
fn check_balance(fee_limit: Field) {
let fee_limit = U128::from_integer(fee_limit);
fn check_balance(fee_limit: U128) {
assert(storage.balances.at(context.msg_sender()).read() >= fee_limit, "Balance too low");
}

// utility function for testing
#[public]
#[view]
fn balance_of_public(owner: AztecAddress) -> pub Field {
storage.balances.at(owner).read().to_field()
fn balance_of_public(owner: AztecAddress) -> pub U128 {
storage.balances.at(owner).read()
}
}
22 changes: 15 additions & 7 deletions noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ contract FPC {
/// - which FPC has been used to make the payment;
/// - the asset which was used to make the payment.
#[private]
fn fee_entrypoint_private(max_fee: Field, nonce: Field) {
fn fee_entrypoint_private(max_fee: U128, nonce: Field) {
// TODO(PR #8022): Once PublicImmutable performs only 1 merkle proof here, we'll save ~4k gates
let config = storage.config.read();

Expand Down Expand Up @@ -110,7 +110,7 @@ contract FPC {
/// Protocol-enshrined fee-payment phase:
/// 4. The protocol deducts the actual fee denominated in fee juice from the FPC's balance.
#[private]
fn fee_entrypoint_public(max_fee: Field, nonce: Field) {
fn fee_entrypoint_public(max_fee: U128, nonce: Field) {
// TODO(PR #8022): Once PublicImmutable performs only 1 merkle proof here, we'll save ~4k gates
let config = storage.config.read();

Expand All @@ -124,10 +124,18 @@ contract FPC {
context.set_as_fee_payer();
// TODO(#6277) for improving interface:
// FPC::at(context.this_address()).pay_refund(...).set_public_teardown_function(&mut context);
let max_fee_serialized = max_fee.serialize();
context.set_public_teardown_function(
context.this_address(),
comptime { FunctionSelector::from_signature("pay_refund((Field),Field,(Field))") },
[context.msg_sender().to_field(), max_fee, config.accepted_asset.to_field()],
comptime {
FunctionSelector::from_signature("pay_refund((Field),(Field,Field),(Field))")
},
[
context.msg_sender().to_field(),
max_fee_serialized[0],
max_fee_serialized[1],
config.accepted_asset.to_field(),
],
);
}

Expand All @@ -136,9 +144,9 @@ contract FPC {
/// to avoid the need for another read from public storage.
#[public]
#[internal]
fn pay_refund(refund_recipient: AztecAddress, max_fee: Field, accepted_asset: AztecAddress) {
let actual_fee = context.transaction_fee();
assert(!max_fee.lt(actual_fee), "Max fee paid to the paymaster does not cover actual fee");
fn pay_refund(refund_recipient: AztecAddress, max_fee: U128, accepted_asset: AztecAddress) {
let actual_fee = U128::from_integer(context.transaction_fee());
assert(actual_fee <= max_fee, "Max fee paid to the paymaster does not cover actual fee");
// TODO(#10805): Introduce a real exchange rate
let refund = max_fee - actual_fee;

Expand Down
Loading
Loading