Skip to content

Commit

Permalink
Load actual fee from tx receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed May 2, 2024
1 parent 96c3eb1 commit 6f9062a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
11 changes: 6 additions & 5 deletions yarn-project/end-to-end/src/e2e_fees/failures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { type TokenContract as BananaCoin, type FPCContract } from '@aztec/noir-
import { expectMapping } from '../fixtures/utils.js';
import { FeesTest } from './fees_test.js';

describe('e2e_fees failure scenarios', () => {
describe('e2e_fees failures', () => {
let aliceWallet: AccountWallet;
let aliceAddress: AztecAddress;
let sequencerAddress: AztecAddress;
let bananaCoin: BananaCoin;
let bananaFPC: FPCContract;
let gasSettings: GasSettings;

const t = new FeesTest('private_payment');
const t = new FeesTest('failures');

beforeAll(async () => {
({ aliceWallet, aliceAddress, sequencerAddress, bananaCoin, bananaFPC, gasSettings } = await t.setup());
Expand All @@ -35,7 +35,6 @@ describe('e2e_fees failure scenarios', () => {
it('reverts transactions but still pays fees using PublicFeePaymentMethod', async () => {
const OutrageousPublicAmountAliceDoesNotHave = BigInt(1e15);
const PublicMintedAlicePublicBananas = BigInt(1e12);
const FeeAmount = 1n;

const [initialAlicePrivateBananas, initialFPCPrivateBananas] = await t.bananaPrivateBalances(
aliceAddress,
Expand Down Expand Up @@ -93,7 +92,9 @@ describe('e2e_fees failure scenarios', () => {
},
})
.wait({ dontThrowOnRevert: true });

expect(txReceipt.status).toBe(TxStatus.REVERTED);
const feeAmount = txReceipt.transactionFee!;

// and thus we paid the fee
await expectMapping(
Expand All @@ -104,12 +105,12 @@ describe('e2e_fees failure scenarios', () => {
await expectMapping(
t.bananaPublicBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAlicePublicBananas + PublicMintedAlicePublicBananas - FeeAmount, initialFPCPublicBananas + FeeAmount, 0n],
[initialAlicePublicBananas + PublicMintedAlicePublicBananas - feeAmount, initialFPCPublicBananas + feeAmount, 0n],
);
await expectMapping(
t.gasBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAliceGas, initialFPCGas - FeeAmount, initialSequencerGas + FeeAmount],
[initialAliceGas, initialFPCGas - feeAmount, initialSequencerGas + feeAmount],
);

// TODO(#4712) - demonstrate reverts with the PrivateFeePaymentMethod.
Expand Down
66 changes: 36 additions & 30 deletions yarn-project/end-to-end/src/e2e_fees/private_payments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BatchCall,
Fr,
PrivateFeePaymentMethod,
type TxReceipt,
type Wallet,
computeSecretHash,
} from '@aztec/aztec.js';
Expand Down Expand Up @@ -47,18 +48,14 @@ describe('e2e_fees private_payment', () => {

let InitialSequencerGas: bigint;

let MaxFee: bigint;
let FeeAmount: bigint;
let RefundAmount: bigint;
let RefundSecret: Fr;
let maxFee: bigint;
let refundSecret: Fr;

beforeEach(async () => {
FeeAmount = 1n;
MaxFee = BigInt(20e9);
RefundAmount = MaxFee - FeeAmount;
RefundSecret = Fr.random();
maxFee = BigInt(20e9);
refundSecret = Fr.random();

expect(gasSettings.getFeeLimit().toBigInt()).toEqual(MaxFee);
expect(gasSettings.getFeeLimit().toBigInt()).toEqual(maxFee);

[
[InitialAlicePrivateBananas, InitialBobPrivateBananas, InitialFPCPrivateBananas],
Expand All @@ -71,6 +68,8 @@ describe('e2e_fees private_payment', () => {
]);
});

const getFeeAndRefund = (tx: Pick<TxReceipt, 'transactionFee'>) => [tx.transactionFee!, maxFee - tx.transactionFee!];

it('pays fees for tx that dont run public app logic', async () => {
/**
* PRIVATE SETUP (1 nullifier for tx)
Expand Down Expand Up @@ -107,7 +106,7 @@ describe('e2e_fees private_payment', () => {
.send({
fee: {
gasSettings,
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, RefundSecret),
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, refundSecret),
},
})
.wait();
Expand All @@ -131,26 +130,27 @@ describe('e2e_fees private_payment', () => {
* but we shouldn't.
*/
expect(tx.transactionFee).toEqual(200018496n);
const [feeAmount, refundAmount] = getFeeAndRefund(tx);

await expectMapping(
t.bananaPrivateBalances,
[aliceAddress, bobAddress, bananaFPC.address, sequencerAddress],
[InitialAlicePrivateBananas - MaxFee - transferAmount, transferAmount, InitialFPCPrivateBananas, 0n],
[InitialAlicePrivateBananas - maxFee - transferAmount, transferAmount, InitialFPCPrivateBananas, 0n],
);
await expectMapping(
t.bananaPublicBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAlicePublicBananas, InitialFPCPublicBananas + MaxFee - RefundAmount, 0n],
[InitialAlicePublicBananas, InitialFPCPublicBananas + maxFee - refundAmount, 0n],
);
await expectMapping(
t.gasBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAliceGas, InitialFPCGas - FeeAmount, InitialSequencerGas + FeeAmount],
[InitialAliceGas, InitialFPCGas - feeAmount, InitialSequencerGas + feeAmount],
);

await expect(
// this rejects if note can't be added
t.addPendingShieldNoteToPXE(t.aliceWallet, RefundAmount, computeSecretHash(RefundSecret), tx.txHash),
t.addPendingShieldNoteToPXE(t.aliceWallet, refundAmount, computeSecretHash(refundSecret), tx.txHash),
).resolves.toBeUndefined();
});

Expand Down Expand Up @@ -185,30 +185,32 @@ describe('e2e_fees private_payment', () => {
.send({
fee: {
gasSettings,
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, RefundSecret),
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, refundSecret),
},
})
.wait();

const [feeAmount, refundAmount] = getFeeAndRefund(tx);

await expectMapping(
t.bananaPrivateBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAlicePrivateBananas - MaxFee + newlyMintedBananas, InitialFPCPrivateBananas, 0n],
[InitialAlicePrivateBananas - maxFee + newlyMintedBananas, InitialFPCPrivateBananas, 0n],
);
await expectMapping(
t.bananaPublicBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAlicePublicBananas, InitialFPCPublicBananas + MaxFee - RefundAmount, 0n],
[InitialAlicePublicBananas, InitialFPCPublicBananas + maxFee - refundAmount, 0n],
);
await expectMapping(
t.gasBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAliceGas, InitialFPCGas - FeeAmount, InitialSequencerGas + FeeAmount],
[InitialAliceGas, InitialFPCGas - feeAmount, InitialSequencerGas + feeAmount],
);

await expect(
// this rejects if note can't be added
t.addPendingShieldNoteToPXE(t.aliceWallet, RefundAmount, computeSecretHash(RefundSecret), tx.txHash),
t.addPendingShieldNoteToPXE(t.aliceWallet, refundAmount, computeSecretHash(refundSecret), tx.txHash),
).resolves.toBeUndefined();
});

Expand Down Expand Up @@ -246,33 +248,35 @@ describe('e2e_fees private_payment', () => {
.send({
fee: {
gasSettings,
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, RefundSecret),
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, refundSecret),
},
})
.wait();

const [feeAmount, refundAmount] = getFeeAndRefund(tx);

await expectMapping(
t.bananaPrivateBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAlicePrivateBananas - MaxFee, InitialFPCPrivateBananas, 0n],
[InitialAlicePrivateBananas - maxFee, InitialFPCPrivateBananas, 0n],
);
await expectMapping(
t.bananaPublicBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAlicePublicBananas - shieldedBananas, InitialFPCPublicBananas + MaxFee - RefundAmount, 0n],
[InitialAlicePublicBananas - shieldedBananas, InitialFPCPublicBananas + maxFee - refundAmount, 0n],
);
await expectMapping(
t.gasBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAliceGas, InitialFPCGas - FeeAmount, InitialSequencerGas + FeeAmount],
[InitialAliceGas, InitialFPCGas - feeAmount, InitialSequencerGas + feeAmount],
);

await expect(
t.addPendingShieldNoteToPXE(t.aliceWallet, shieldedBananas, shieldSecretHash, tx.txHash),
).resolves.toBeUndefined();

await expect(
t.addPendingShieldNoteToPXE(t.aliceWallet, RefundAmount, computeSecretHash(RefundSecret), tx.txHash),
t.addPendingShieldNoteToPXE(t.aliceWallet, refundAmount, computeSecretHash(refundSecret), tx.txHash),
).resolves.toBeUndefined();
});

Expand Down Expand Up @@ -315,16 +319,18 @@ describe('e2e_fees private_payment', () => {
.send({
fee: {
gasSettings,
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, RefundSecret),
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, refundSecret),
},
})
.wait();

const [feeAmount, refundAmount] = getFeeAndRefund(tx);

await expectMapping(
t.bananaPrivateBalances,
[aliceAddress, bobAddress, bananaFPC.address, sequencerAddress],
[
InitialAlicePrivateBananas - MaxFee - privateTransfer,
InitialAlicePrivateBananas - maxFee - privateTransfer,
InitialBobPrivateBananas + privateTransfer,
InitialFPCPrivateBananas,
0n,
Expand All @@ -333,20 +339,20 @@ describe('e2e_fees private_payment', () => {
await expectMapping(
t.bananaPublicBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAlicePublicBananas - shieldedBananas, InitialFPCPublicBananas + MaxFee - RefundAmount, 0n],
[InitialAlicePublicBananas - shieldedBananas, InitialFPCPublicBananas + maxFee - refundAmount, 0n],
);
await expectMapping(
t.gasBalances,
[aliceAddress, bananaFPC.address, sequencerAddress],
[InitialAliceGas, InitialFPCGas - FeeAmount, InitialSequencerGas + FeeAmount],
[InitialAliceGas, InitialFPCGas - feeAmount, InitialSequencerGas + feeAmount],
);

await expect(
t.addPendingShieldNoteToPXE(t.aliceWallet, shieldedBananas, shieldSecretHash, tx.txHash),
).resolves.toBeUndefined();

await expect(
t.addPendingShieldNoteToPXE(t.aliceWallet, RefundAmount, computeSecretHash(RefundSecret), tx.txHash),
t.addPendingShieldNoteToPXE(t.aliceWallet, refundAmount, computeSecretHash(refundSecret), tx.txHash),
).resolves.toBeUndefined();
});

Expand All @@ -370,7 +376,7 @@ describe('e2e_fees private_payment', () => {
bananaCoin.address,
bankruptFPC.address,
aliceWallet,
RefundSecret,
refundSecret,
),
},
})
Expand Down

0 comments on commit 6f9062a

Please sign in to comment.