> For the complete documentation index, see [llms.txt](https://docs.arbera.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.arbera.io/deep-dive/user-fee-flow.md).

# User/Fee Flow

## Glossary

`TOKEN` — ERC20 token.

`brTOKEN` — Den receipt token for `TOKEN`.

`ARBERA` — Protocol and reward token for the Den LPs.

`HONEY` —  Token paired with `brTOKEN` in the Den LP.

> Many tokens can be used as a Den's paired LP token, below examples are using `HONEY`.

## Fees

### Fees taken on user actions

`buy.fee` — Fee on buying Den tokens (in `brTOKEN`).

`sell.fee` — Fee on selling Den tokens (in `brTOKEN`).

`bond.fee` — Fee on wrapping `TOKEN` for `brTOKEN` (in `brTOKEN`).

`debond.fee` — Fee on unwrapping `brTOKEN` for `TOKEN` (in `brTOKEN`).

<mark style="color:red;">`burn.fee`</mark> — Burn rate of `brTOKEN` ([Growing Dens](/dens/growing-dens.md)) on top of all of the above fees.

#### After the <mark style="color:red;">burn-fee</mark>, but before swapping for ARBERA

1. `partner.fee` — Fee for Den creator (in `brTOKEN`).
2. `yield.protocol.fee` — Protocol fee (in `brTOKEN`).

#### After swapping rewards to ARBERA

> Swapping path: `brTOKEN` → `HONEY` → `ARBERA`.

1. `yield.admin.fee` — Future yield fee (in `ARBERA` or other reward tokens).

#### The rest of fees (in ARBERA) are distributed to Den LPs.

## User actions

Let's go through each user action and see the tokens and fees flow.

### Wrapping into Den

1. User wraps  `X` TOKEN (calls `bond()` on Den contract).
2. Den mints `Y = X/R` brTOKEN (based on `TOKEN:brTOKEN` ratio `R`).
   1. `(Y * bond.fee) * (1 - burn.fee)` brTOKEN stays in the Den contract.
   2. `(Y * bond.fee) * burn.fee` brTOKEN <mark style="color:red;">**is burned**</mark> ([Growing Dens](/dens/growing-dens.md)).
3. User receives `Y * (1 - bond.fee)` brTOKEN.

> 1) First depositor wraps fee free.
> 2) Den creator has 7 days to wrap fee free — for first deposit only.

### Unwrapping from Den

1. User unwraps `Y` brTOKEN (calls `debond()` on Den contract).
2. Den burns `Y * (1 - debond.fee)` brTOKEN.
3. `(Y * debond.fee) * (1 - burn.fee)` brTOKEN stays in the Den contract.
4. `(Y * debond.fee) * burn.fee` brTOKEN <mark style="color:red;">**is burned**</mark> ([Growing Dens](/dens/growing-dens.md)).
5. User receives `R * Y * (1 - debond.fee)` TOKEN (based on `TOKEN:brTOKEN` ratio `R`).
6. <mark style="color:green;">Triggers</mark> [#fee-distribution](#fee-distribution "mention").

> No fee if unwrapping `98%` brTOKEN or more.

### Buying Den token

1. User buys `X` brTOKEN with HONEY (via Den LP).
2. `(X * buy.fee) * (1 - burn.fee)` brTOKEN stays in the Den contract.
3. `(X * buy.fee) * burn.fee` brTOKEN <mark style="color:red;">**is burned**</mark> ([Growing Dens](/dens/growing-dens.md)).
4. User receives `X * (1 - buy.fee)` brTOKEN.

### Selling Den token

1. User sells `X` brTOKEN for HONEY (via Den LP pool).
2. `(X * sell.fee) * (1 - burn.fee)` brTOKEN stays in the Den contract.
3. <mark style="color:green;">Triggers</mark> [#fee-distribution](#fee-distribution "mention").
4. `(X * sell.fee) * burn.fee` brTOKEN <mark style="color:red;">**is burned**</mark> ([Growing Dens](/dens/growing-dens.md)).
5. User receives `X * (1 - sell.fee)` brTOKEN to swap for HONEY.

### Staking/Unstaking/Transferring Staked Den LP Token

1. User interacts with `X` Staked brTOKEN-HONEY LP (calls `stake()`, `unstake()` or `_transfer()`).
2. <mark style="color:green;">Triggers</mark> [#fee-distribution](#fee-distribution "mention").

### Transferring brTOKEN

1. User transfers `X` brTOKEN (calls `_transfer()` on Den contract).
2. <mark style="color:green;">Triggers</mark> [#fee-distribution](#fee-distribution "mention") unless it's a transfer from Den LP.

### Taking Flash Loan

1. User takes `X` TOKEN flashloan paying `10` HONEY (calls `flash()` on Den contract).
2. <mark style="color:green;">Triggers</mark> [#fee-distribution](#fee-distribution "mention") if Den LP rewards are in `HONEY`,&#x20;
3. Otherwise if paired Den LP token is HONEY, fee is sent to `TokenRewards` contract.
4. Otherwise, `HONEY` is sent to the treasury.

### <mark style="color:green;">Fee distribution</mark>

> This is being called on all Den and Staked Den LP transfers (except when [#buying-den-token](#buying-den-token "mention")).
>
> Wrapping TOKEN into brTOKEN also doesn't trigger Fee Distribution.

1. User triggers `_processPreSwapFeesAndSwap()` on Den contract.
2. If accumulated fees `AF` in Den contract (in brTOKEN) are at least `0.025%` of Den's LP balance of brTOKEN `B`, then **Fees To Swap** `FTS = max(AF, 1% * B)` brTOKEN.
3. Swapping for HONEY (Den paired LP token):
   1. `FTS * partner.fee` brTOKEN are sent to the Den creator address.
   2. `FTS * yield.protocol.fee` brTOKEN are sent to the Arbera treasury.
   3. `FTS * (1 - partner.fee - yield.protocol.fee)` brTOKEN are swapped to HONEY.&#x20;
4. `HONEY` (from point 3.3) is swapped for `Y` ARBERA.
5. `Y * yield.admin.fee` ARBERA is sent to treasury for Future LP Yield.
6. `Y * (1 - yield.admin.fee)` ARBERA are deposited as rewards for Staked Den LPs.
