From 1085d0914be8f3dca0301d563f1f089897d96e84 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 14 Aug 2024 11:46:05 +0000 Subject: [PATCH 1/2] feat: private refunds optimizations --- .../contracts/token_with_refunds_contract/src/main.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/main.nr index a246e4d8874..438851f0a87 100644 --- a/noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/main.nr @@ -545,7 +545,7 @@ contract TokenWithRefunds { // 3. Deduct the funded amount from the user's balance - this is a maximum fee a user is willing to pay // (called fee limit in aztec spec). The difference between fee limit and the actual tx fee will be refunded // to the user in the `complete_refund(...)` function. - storage.balances.sub(user, U128::from_integer(funded_amount)).emit(encode_and_encrypt_note_with_keys(&mut context, user_ovpk, user_ivpk, user)); + storage.balances.sub(user, U128::from_integer(funded_amount)).emit(encode_and_encrypt_note_with_keys_unconstrained(&mut context, user_ovpk, user_ivpk, user)); // 4. We create the partial notes for the fee payer and the user. // --> Called "partial" because they don't have the amount set yet (that will be done in `complete_refund(...)`). From 7ecc3ae24fa9b838a33dfdf7a162b108ac237b8a Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 14 Aug 2024 12:44:22 +0000 Subject: [PATCH 2/2] optimizing balance subtraction --- .../contracts/token_with_refunds_contract/src/main.nr | 9 ++++++++- .../end-to-end/src/e2e_fees/private_refunds.test.ts | 8 ++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/main.nr index 438851f0a87..0e89f3a101e 100644 --- a/noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_with_refunds_contract/src/main.nr @@ -545,7 +545,14 @@ contract TokenWithRefunds { // 3. Deduct the funded amount from the user's balance - this is a maximum fee a user is willing to pay // (called fee limit in aztec spec). The difference between fee limit and the actual tx fee will be refunded // to the user in the `complete_refund(...)` function. - storage.balances.sub(user, U128::from_integer(funded_amount)).emit(encode_and_encrypt_note_with_keys_unconstrained(&mut context, user_ovpk, user_ivpk, user)); + let change = subtract_balance( + &mut context, + storage, + user, + U128::from_integer(funded_amount), + INITIAL_TRANSFER_CALL_MAX_NOTES + ); + storage.balances.add(user, change).emit(encode_and_encrypt_note_with_keys_unconstrained(&mut context, user_ovpk, user_ivpk, user)); // 4. We create the partial notes for the fee payer and the user. // --> Called "partial" because they don't have the amount set yet (that will be done in `complete_refund(...)`). diff --git a/yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts b/yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts index a382cd116be..409adbc6d5f 100644 --- a/yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts +++ b/yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts @@ -153,7 +153,7 @@ describe('e2e_fees/private_refunds', () => { aliceRandomness, bobRandomness, t.bobWallet.getAddress(), // Bob is the recipient of the fee notes. - true, // We set max fee/funded amount to zero to trigger the error. + true, // We set max fee/funded amount to 1 to trigger the error. ), }, }), @@ -195,10 +195,10 @@ class PrivateRefundPaymentMethod implements FeePaymentMethod { private feeRecipient: AztecAddress, /** - * If true, the max fee will be set to 0. + * If true, the max fee will be set to 1. * TODO(#7694): Remove this param once the lacking feature in TXE is implemented. */ - private setMaxFeeToZero = false, + private setMaxFeeToOne = false, ) {} /** @@ -221,7 +221,7 @@ class PrivateRefundPaymentMethod implements FeePaymentMethod { async getFunctionCalls(gasSettings: GasSettings): Promise { // We assume 1:1 exchange rate between fee juice and token. But in reality you would need to convert feeLimit // (maxFee) to be in token denomination. - const maxFee = this.setMaxFeeToZero ? Fr.ZERO : gasSettings.getFeeLimit(); + const maxFee = this.setMaxFeeToOne ? Fr.ONE : gasSettings.getFeeLimit(); await this.wallet.createAuthWit({ caller: this.paymentContract,