Skip to content

Commit

Permalink
last touches to FPC
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Dec 11, 2024
1 parent 3e84db5 commit bc6c54b
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ contract FPC {
/// the AT) and sets a public teardown function (in which the partial notes will be finalized),
/// 2. then the private and public functions of a tx get executed,
/// 3. at this point we know the tx fee so we can compute in the teardown function how much of AT the user needs
/// to pay to the FPC `fee_recipient` and how much of it will be refunded back. Note that this is computed based on
/// an exchange rate between the accepted asset and the fee token (called fee juice in some places),
/// to pay to the `fee_recipient` and how much of it will be refunded back. Note that this is computed based on
/// an exchange rate between AT and fee juice.
/// 4. the protocol deducts the actual fee denominated in fee juice from the FPC's balance.
///
/// With this scheme a user has privately paid for the tx fee with an arbitrary AT (e.g. could be a stablecoin).
Expand All @@ -54,23 +54,35 @@ contract FPC {
context.set_as_fee_payer();
}

/// Pays for the tx fee with msg_sender's public balance of accepted token (AT). The maximum fee a user is willing
/// to pay is defined by `max_fee` and is denominated in AT.
///
/// ## Overview
/// The refund flow works as follows:
/// 1. We pull the `max_fee` from the user's balance of the AT to this contract (requires setting an authwit),
/// 2. then the private and public functions of a tx get executed,
/// 3. at this point we know the tx fee so we can compute how much of AT the user needs to pay to
/// the `fee_recipient` and how much of it will be refunded back. Note that this is computed based on an exchange
/// rate between AT and fee juice.
/// 4. the protocol deducts the actual fee denominated in fee juice from the FPC's balance.
///
/// ***Note:***
/// AT funds sent by the users to this contract stay in this contract and are not transferred
/// to the `fee_recipient`. In the private flow we needed a separate `fee_recipient` as this contract does not
/// have keys associated with it. In production we would want to have a method allowing for pulling of these funds
/// from this contract.
#[private]
fn fee_entrypoint_public(max_fee: Field, nonce: Field) {
// TODO(PR #8022): Once PublicImmutable performs only 1 merkle proof here, we'll save ~4k gates
let config = storage.config.read();

// We pull the max fee from the user's balance of the accepted asset to this contract. We cannot enqueue a call
// to the token directly as we need msg_sender to be revealed.
// TODO(benesjan): is this ^ true?
FPC::at(context.this_address())
.pull_max_fee(
context.msg_sender(),
// We pass the following value as arg to avoid the need to read the config again in public.
config.accepted_asset,
max_fee,
nonce,
)
// We pull the max fee from the user's balance of the accepted asset to this contract.
// docs:start:public_call
Token::at(config.accepted_asset)
.transfer_in_public(context.msg_sender(), context.this_address(), max_fee, nonce)
.enqueue(&mut context);
// docs:end:public_call

context.set_as_fee_payer();
// TODO(#6277) for improving interface:
// FPC::at(context.this_address()).pay_refund(...).set_public_teardown_function(&mut context);
Expand All @@ -81,21 +93,8 @@ contract FPC {
);
}

#[public]
#[internal]
fn pull_max_fee(
from: AztecAddress,
accepted_asset: AztecAddress,
max_fee: Field,
nonce: Field,
) {
// docs:start:public_call
Token::at(accepted_asset)
.transfer_in_public(from, context.this_address(), max_fee, nonce)
.call(&mut context);
// docs:end:public_call
}

/// Pays the refund to the `refund_recipient`. The refund is the difference between the `max_fee` and
/// the actual fee.
#[public]
#[internal]
fn pay_refund(refund_recipient: AztecAddress, max_fee: Field, accepted_asset: AztecAddress) {
Expand Down

0 comments on commit bc6c54b

Please sign in to comment.