# Fee Structure

Understand how trading fees work in DLMM's bin-based system, including fee accumulation, distribution to liquidity providers, and protocol revenue sharing.

### How Fees Work in DLMM

Trading fees in DLMM:

* Charged on each swap
* Accumulate in the bins used
* Distributed proportionally to LPs
* Include base + variable components

```
Swap → Fee charged → Stored in bins → LPs collect share
```

### Fee Components

#### Base Fee

```typescript
interface PairParameters {
  base_factor: string;     // Base fee rate (e.g., 3000 = 0.3%)
  protocol_share: string;  // Protocol's share (e.g., 1000 = 10%)
}

// Example: 0.3% base fee
const baseFeePercent = Number(pair.parameters.base_factor) / 10000;
```

#### Variable Fee

Adjusts based on volatility:

```typescript
// Increases during high volatility
// Decreases during stable periods
const variableFee = calculateVariableFee(
  pair.parameters.volatility_accumulator,
  pair.parameters.max_volatility_accumulator
);

const totalFee = baseFee + variableFee;
```

### Fee Accumulation in Bins

```typescript
// Fees stored separately from reserves
interface BinReserves {
  reserve_x: bigint;    // Trading liquidity
  reserve_y: bigint;    // Trading liquidity
  fee_x: bigint;        // Accumulated fees
  fee_y: bigint;        // Accumulated fees
}

// Total bin value = reserves + fees
const totalX = bin.reserve_x + bin.fee_x;
const totalY = bin.reserve_y + bin.fee_y;
```

### LP Fee Share Calculation

```typescript
// Your share of fees in a bin
function calculateLPFeeShare(
  yourLiquidity: bigint,
  totalSupply: bigint,
  binFees: { fee_x: bigint, fee_y: bigint }
): { feeX: bigint, feeY: bigint } {
  const feeX = (yourLiquidity * binFees.fee_x) / totalSupply;
  const feeY = (yourLiquidity * binFees.fee_y) / totalSupply;
  
  return { feeX, feeY };
}
```

### Protocol Revenue

```typescript
// Protocol takes a percentage of fees
function calculateProtocolFee(
  swapFee: bigint,
  protocolShare: number // basis points
): bigint {
  return (swapFee * BigInt(protocolShare)) / 10000n;
}

// Example: 10% protocol share of 0.3% swap fee
const swapAmount = parseEther("100");
const swapFee = (swapAmount * 30n) / 10000n; // 0.3%
const protocolFee = (swapFee * 1000n) / 10000n; // 10% of fee
const lpFee = swapFee - protocolFee; // 90% to LPs
```

### Fee Tiers by Pair Type

Common configurations:

| Pair Type  | Base Fee | Typical Range |
| ---------- | -------- | ------------- |
| Stable     | 0.01%    | 0.01-0.05%    |
| Correlated | 0.05%    | 0.05-0.10%    |
| Blue-chip  | 0.20%    | 0.20-0.30%    |
| Volatile   | 0.30%    | 0.30-5.00%    |

### Viewing Pair Fees

```typescript
// Check current fee parameters
const pair = await sdk.Pair.getPair(pairAddress);

const feeInfo = {
  baseFee: Number(pair.parameters.base_factor) / 100, // basis points to percent
  protocolShare: Number(pair.parameters.protocol_share) / 100,
  currentVolatility: pair.parameters.volatility_accumulator
};

console.log(`Base fee: ${feeInfo.baseFee}%`);
console.log(`Protocol takes: ${feeInfo.protocolShare}% of fees`);
```

### Fee Collection Process

Fees are collected automatically when:

1. **Removing liquidity** - Proportional share included
2. **Closing position** - All fees withdrawn
3. **No separate claim** - Unlike some protocols

```typescript
// Fees included in removal
const removal = await sdk.Position.getPositionBinsAmount(pair, positionId);

removal.forEach(bin => {
  // amountX and amountY already include fees
  console.log(`Bin ${bin.id}: Includes accumulated fees`);
});
```

### Estimating Fee Income

```typescript
// Estimate daily fees for a position
async function estimateDailyFees(
  pair: LBPair,
  positionId: string,
  dailyVolume: bigint
): Promise<bigint> {
  const bins = await sdk.Position.getPositionBins(pair, positionId);
  const activeId = pair.parameters.active_id;
  
  // Assume volume concentrates around active bin
  const activeBin = bins.find(b => b.id === activeId);
  if (!activeBin) return 0n;
  
  const baseFee = BigInt(pair.parameters.base_factor);
  const yourShare = activeBin.liquidity; // Simplified
  
  // Rough estimate
  return (dailyVolume * baseFee * yourShare) / (10000n * totalLiquidity);
}
```

### Key Points

* Fees accumulate in bins, not positions
* Automatically collected on withdrawal
* Base + variable fee structure
* Protocol takes percentage of fees
* Higher volatility = higher fees

### Related Topics

* [Calculate Position Fees](/integration/dlmm/typescript-sdk/fees-and-analytics/calculate-position-fees.md) - Track your earnings
* [Collect Fees](/integration/dlmm/typescript-sdk/fees-and-analytics/collect-fees.md) - Claim accumulated fees
* [Calculate APR](/integration/dlmm/typescript-sdk/fees-and-analytics/calculate-apr.md) - Fee-based returns
* [Get Pair Reserves](/integration/dlmm/typescript-sdk/trading-pairs/get-pair-reserves.md) - View fee accumulation


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ferra.ag/integration/dlmm/typescript-sdk/fees-and-analytics/fee-structure.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
