Skip to content

Commit

Permalink
fetching accepted asset from FPC
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Dec 11, 2024
1 parent fd877ef commit ae343c7
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dep::aztec::macros::aztec;
contract FPC {
use crate::config::Config;
use dep::aztec::{
macros::{functions::{initializer, internal, private, public}, storage::storage},
macros::{functions::{initializer, internal, private, public, view}, storage::storage},
protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress},
state_vars::PublicImmutable,
};
Expand Down Expand Up @@ -111,4 +111,10 @@ contract FPC {
.transfer_in_public(context.this_address(), refund_recipient, refund, 0)
.call(&mut context);
}

#[public]
#[view]
fn get_accepted_asset() -> AztecAddress {
storage.config.read().accepted_asset
}
}
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/fee/fee_juice_payment_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class FeeJuicePaymentMethod implements FeePaymentMethod {
constructor(protected sender: AztecAddress) {}

getAsset() {
return ProtocolContractAddress.FeeJuice;
return Promise.resolve(ProtocolContractAddress.FeeJuice);
}

getFunctionCalls(): Promise<FunctionCall[]> {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/fee/fee_payment_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { type AztecAddress } from '@aztec/foundation/aztec-address';
*/
export interface FeePaymentMethod {
/** The asset used to pay the fee. */
getAsset(): AztecAddress;
getAsset(): Promise<AztecAddress>;
/**
* Creates a function call to pay the fee in the given asset.
* @param gasSettings - The gas limits and max fees.
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/fee/no_fee_payment_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class NoFeePaymentMethod implements FeePaymentMethod {
constructor() {}

getAsset() {
return AztecAddress.ZERO;
return Promise.resolve(AztecAddress.ZERO);
}

getFunctionCalls(): Promise<FunctionCall[]> {
Expand Down
46 changes: 38 additions & 8 deletions yarn-project/aztec.js/src/fee/private_fee_payment_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';

import { type Wallet } from '../account/wallet.js';
import { ContractFunctionInteraction } from '../contract/contract_function_interaction.js';
import { type FeePaymentMethod } from './fee_payment_method.js';

/**
* Holds information about how the fee for a transaction is to be paid.
*/
export class PrivateFeePaymentMethod implements FeePaymentMethod {
private assetPromise: Promise<AztecAddress> | null = null;

constructor(
/**
* The asset used to pay the fee.
*/
private asset: AztecAddress,
/**
* Address which will hold the fee payment.
*/
Expand All @@ -42,9 +41,40 @@ export class PrivateFeePaymentMethod implements FeePaymentMethod {
* The asset used to pay the fee.
* @returns The asset used to pay the fee.
*/
// TODO(benesjan): obtain directly from contract?
getAsset() {
return this.asset;
getAsset(): Promise<AztecAddress> {
if (!this.assetPromise) {
const interaction = new ContractFunctionInteraction(
this.wallet,
this.paymentContract,
{
name: 'get_accepted_asset',
functionType: FunctionType.PUBLIC,
isInternal: false,
isStatic: true,
parameters: [],
returnTypes: [
{
kind: 'struct',
path: 'authwit::aztec::protocol_types::address::aztec_address::AztecAddress',
fields: [
{
name: 'inner',
type: {
kind: 'field',
},
},
],
},
],
errorTypes: {},
isInitializer: false,
},
[],
);

this.assetPromise = interaction.simulate();
}
return this.assetPromise!;
}

getFeePayer(): Promise<AztecAddress> {
Expand All @@ -70,7 +100,7 @@ export class PrivateFeePaymentMethod implements FeePaymentMethod {
selector: FunctionSelector.fromSignature('setup_refund((Field),(Field),Field,Field)'),
type: FunctionType.PRIVATE,
isStatic: false,
to: this.asset,
to: await this.getAsset(),
returnTypes: [],
},
});
Expand Down
47 changes: 39 additions & 8 deletions yarn-project/aztec.js/src/fee/public_fee_payment_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import { FunctionSelector, FunctionType } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';

import { ContractFunctionInteraction } from '../contract/contract_function_interaction.js';
import { type AccountWallet } from '../wallet/account_wallet.js';
import { type FeePaymentMethod } from './fee_payment_method.js';

/**
* Holds information about how the fee for a transaction is to be paid.
*/
export class PublicFeePaymentMethod implements FeePaymentMethod {
private assetPromise: Promise<AztecAddress> | null = null;

constructor(
/**
* The asset used to pay the fee.
*/
protected asset: AztecAddress,
/**
* Address which will hold the fee payment.
*/
Expand All @@ -30,8 +29,40 @@ export class PublicFeePaymentMethod implements FeePaymentMethod {
* The asset used to pay the fee.
* @returns The asset used to pay the fee.
*/
getAsset() {
return this.asset;
getAsset(): Promise<AztecAddress> {
if (!this.assetPromise) {
const interaction = new ContractFunctionInteraction(
this.wallet,
this.paymentContract,
{
name: 'get_accepted_asset',
functionType: FunctionType.PUBLIC,
isInternal: false,
isStatic: true,
parameters: [],
returnTypes: [
{
kind: 'struct',
path: 'authwit::aztec::protocol_types::address::aztec_address::AztecAddress',
fields: [
{
name: 'inner',
type: {
kind: 'field',
},
},
],
},
],
errorTypes: {},
isInitializer: false,
},
[],
);

this.assetPromise = interaction.simulate();
}
return this.assetPromise!;
}

getFeePayer(): Promise<AztecAddress> {
Expand All @@ -43,7 +74,7 @@ export class PublicFeePaymentMethod implements FeePaymentMethod {
* @param gasSettings - The gas settings.
* @returns The function call to pay the fee.
*/
getFunctionCalls(gasSettings: GasSettings): Promise<FunctionCall[]> {
async getFunctionCalls(gasSettings: GasSettings): Promise<FunctionCall[]> {
const nonce = Fr.random();
const maxFee = gasSettings.getFeeLimit();

Expand All @@ -58,7 +89,7 @@ export class PublicFeePaymentMethod implements FeePaymentMethod {
selector: FunctionSelector.fromSignature('transfer_in_public((Field),(Field),Field,Field)'),
type: FunctionType.PUBLIC,
isStatic: false,
to: this.asset,
to: await this.getAsset(),
returnTypes: [],
},
},
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/cli-wallet/src/utils/options/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ export function parsePaymentMethod(
case 'fpc-public': {
const [asset, fpc] = getFpcOpts(parsed, db);
log(`Using public fee payment with asset ${asset} via paymaster ${fpc}`);
return new PublicFeePaymentMethod(asset, fpc, sender);
return new PublicFeePaymentMethod(fpc, sender);
}
case 'fpc-private': {
const [asset, fpc, feeRecipient] = getFpcOpts(parsed, db);
log(
`Using private fee payment with asset ${asset} via paymaster ${fpc} with rebate secret ${feeRecipient.toString()}`,
);
return new PrivateFeePaymentMethod(asset, fpc, sender, feeRecipient);
return new PrivateFeePaymentMethod(fpc, sender, feeRecipient);
}
case undefined:
throw new Error('Missing "method" in payment option');
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/end-to-end/src/benchmarks/bench_prover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,12 @@ describe('benchmarks/proving', () => {

const feeFnCall0 = {
gasSettings,
paymentMethod: new PublicFeePaymentMethod(initialTokenContract.address, initialFpContract.address, wallet),
paymentMethod: new PublicFeePaymentMethod(initialFpContract.address, wallet),
};

// const feeFnCall1 = {
// gasSettings,
// paymentMethod: new PrivateFeePaymentMethod(
// initialTokenContract.address,
// initialFpContract.address,
// await getWalletOnPxe(1),
// ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('benchmarks/tx_size_fees', () => {
],
[
'public fee',
() => new PublicFeePaymentMethod(token.address, fpc.address, aliceWallet),
() => new PublicFeePaymentMethod(fpc.address, aliceWallet),
// DA:
// non-rev: 1 nullifiers, overhead; rev: 2 note hashes, 1 nullifier, 1168 B enc note logs, 0 B enc logs,0 B unenc logs, teardown
// L2:
Expand All @@ -98,7 +98,7 @@ describe('benchmarks/tx_size_fees', () => {
],
[
'private fee',
() => new PrivateFeePaymentMethod(token.address, fpc.address, aliceWallet, sequencerAddress),
() => new PrivateFeePaymentMethod(fpc.address, aliceWallet, sequencerAddress),
// DA:
// non-rev: 3 nullifiers, overhead; rev: 2 note hashes, 1168 B enc note logs, 0 B enc logs, 0 B unenc logs, teardown
// L2:
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/end-to-end/src/e2e_fees/account_init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ describe('e2e_fees account_init', () => {

// Bob deploys his account through the private FPC
const paymentMethod = new PrivateFeePaymentMethod(
bananaCoin.address,
bananaFPC.address,
await bobsAccountManager.getWallet(),
t.sequencerAddress, // Sequencer is the recipient of the refund fee notes because it's the FPC admin.
Expand All @@ -142,7 +141,7 @@ describe('e2e_fees account_init', () => {
const mintedBananas = FEE_FUNDING_FOR_TESTER_ACCOUNT;
await bananaCoin.methods.mint_to_public(bobsAddress, mintedBananas).send().wait();

const paymentMethod = new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, bobsWallet);
const paymentMethod = new PublicFeePaymentMethod(bananaFPC.address, bobsWallet);
const tx = await bobsAccountManager
.deploy({
skipPublicDeployment: false,
Expand Down
16 changes: 5 additions & 11 deletions yarn-project/end-to-end/src/e2e_fees/dapp_subscription.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('e2e_fees dapp_subscription', () => {
*/

const { transactionFee } = await subscribe(
new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, feeRecipient),
new PrivateFeePaymentMethod(bananaFPC.address, aliceWallet, feeRecipient),
);

// We let Alice see Bob's notes because the expect uses Alice's wallet to interact with the contracts to "get" state.
Expand Down Expand Up @@ -146,9 +146,7 @@ describe('e2e_fees dapp_subscription', () => {
PUBLIC TEARDOWN
the FPC finalizes the partial notes for the fee and the refund
*/
const { transactionFee } = await subscribe(
new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet),
);
const { transactionFee } = await subscribe(new PublicFeePaymentMethod(bananaFPC.address, aliceWallet));

await expectMapping(
t.getGasBalanceFn,
Expand All @@ -167,7 +165,7 @@ describe('e2e_fees dapp_subscription', () => {
it('should call dapp subscription entrypoint', async () => {
// Subscribe again, so this test does not depend on the previous ones being run.

await subscribe(new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, feeRecipient));
await subscribe(new PrivateFeePaymentMethod(bananaFPC.address, aliceWallet, feeRecipient));

expect(await subscriptionContract.methods.is_initialized(aliceAddress).simulate()).toBe(true);

Expand All @@ -189,18 +187,14 @@ describe('e2e_fees dapp_subscription', () => {

it('should reject after the sub runs out', async () => {
// Subscribe again. This will overwrite the previous subscription.
await subscribe(new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, feeRecipient), 0);
await subscribe(new PrivateFeePaymentMethod(bananaFPC.address, aliceWallet, feeRecipient), 0);
// TODO(#6651): Change back to /(context.block_number()) as u64 < expiry_block_number as u64/ when fixed
await expect(dappIncrement()).rejects.toThrow(/Note encrypted logs hash mismatch/);
});

it('should reject after the txs run out', async () => {
// Subscribe again. This will overwrite the previous subscription.
await subscribe(
new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, feeRecipient),
5,
1,
);
await subscribe(new PrivateFeePaymentMethod(bananaFPC.address, aliceWallet, feeRecipient), 5, 1);
await expect(dappIncrement()).resolves.toBeDefined();
await expect(dappIncrement()).rejects.toThrow(/note.remaining_txs as u64 > 0/);
});
Expand Down
Loading

0 comments on commit ae343c7

Please sign in to comment.