-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Labels
enhancementNew feature or requestNew feature or request
Description
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:
-
Deduct a very small fee (
0.000001fraction) from theamount_infor both buy and sell tokens. -
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.
-
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
Labels
enhancementNew feature or requestNew feature or request