Skip to content

Create the collect_protocol_fee logic #1

@DhruvWebDev

Description

@DhruvWebDev

Title: Implement collect_protocol_fee logic for swaps

Description:

We need to implement the protocol fee collection mechanism inside the swap flow. The logic should:

  1. Deduct a very small fee (0.000001 fraction) from the amount_in for both buy and sell tokens.

  2. Transfer the deducted amount into the fee vault PDA derived from:

    [b"fees_vault_0", token_mint.key(), pool_pda.key()]
    

    for the sell token, and

    [b"fees_vault_1", token_mint.key(), pool_pda.key()]
    

    for the buy token.

  3. The vaults must be token accounts owned by the program and initialized beforehand.

Rationale:

  • This fee is in addition to the existing pool fees set by the pool creator in the pool PDA.
  • Keeps protocol earnings separate from LP or pool fees.
  • Fee amount is tiny to avoid affecting trade UX while still accruing protocol revenue.

Acceptance Criteria:

  • Deduction logic works correctly for both buy and sell token legs of the swap.
  • Fee amount is calculated using integer math (no floating point).
  • Fees are transferred to correct vault PDAs.
  • Unit tests verify the correct fee amount and vault balances after swaps.

Example:

If amount_in = 1_000_000 (in token's smallest units) and fee_rate = 0.000001,
then protocol_fee = 1 unit is transferred to the relevant vault PDA.


Try to keep it in exact integer number and avoid floating point number.
You can do like set two variables:

    pub const PROTOCOL_FEE_NUMERATOR: u64 = 1; // 1 in 1_000_000
    pub const PROTOCOL_FEE_DENOMINATOR: u64 = 1_000_000;
    let fee_amount = amount
        .checked_mul(PROTOCOL_FEE_RATE)
        .unwrap()
        .checked_div(PROTOCOL_FEE_DENOMINATOR)
        .unwrap();

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions