# BaseFeeCalculator

**Inherits:** IBaseFeeCalculator, IFeeCalculator, Auth2Step, VaultAuth

Module used with FeeVault to allow an off-chain accountant to submit necessary inputs that help compute TVL and performance fees owed to the vault. Serves as a central registry for all vaults and their associated fees

## State Variables

### protocolFees

The protocol's fee configuration

```solidity
Fee public protocolFees;
```

### protocolFeeRecipient

The address that receives the protocol's fees

```solidity
address public protocolFeeRecipient;
```

### \_vaultAccruals

A mapping of vault addresses to their associated state

```solidity
mapping(address vault => VaultAccruals vaultAccruals) internal _vaultAccruals;
```

### vaultAccountant

A mapping of vault addresses to their assigned accountant

```solidity
mapping(address vault => address accountant) public vaultAccountant;
```

## Functions

### onlyVaultAccountant

Modifier that checks the caller is the accountant assigned to the specified vault

```solidity
modifier onlyVaultAccountant(address vault);
```

**Parameters**

| Name    | Type      | Description              |
| ------- | --------- | ------------------------ |
| `vault` | `address` | The address of the vault |

### constructor

```solidity
constructor(address initialOwner, Authority initialAuthority) Auth2Step(initialOwner, initialAuthority);
```

### setProtocolFeeRecipient

Set the protocol fee recipient

```solidity
function setProtocolFeeRecipient(address feeRecipient) external requiresAuth;
```

**Parameters**

| Name           | Type      | Description                               |
| -------------- | --------- | ----------------------------------------- |
| `feeRecipient` | `address` | The address of the protocol fee recipient |

### setProtocolFees

Set the protocol fee rates

```solidity
function setProtocolFees(uint16 tvl, uint16 performance) external requiresAuth;
```

**Parameters**

| Name          | Type     | Description                              |
| ------------- | -------- | ---------------------------------------- |
| `tvl`         | `uint16` | The TVL fee rate in basis points         |
| `performance` | `uint16` | The performance fee rate in basis points |

### setVaultAccountant

Set the accountant for a vault

```solidity
function setVaultAccountant(address vault, address accountant) external requiresVaultAuth(vault);
```

**Parameters**

| Name         | Type      | Description                       |
| ------------ | --------- | --------------------------------- |
| `vault`      | `address` | The address of the vault          |
| `accountant` | `address` | The address of the new accountant |

### registerVault

Register a new vault with the fee calculator

```solidity
function registerVault() external virtual;
```

### setVaultFees

Set the vault-specific fee rates

```solidity
function setVaultFees(address vault, uint16 tvl, uint16 performance) external requiresVaultAuth(vault);
```

**Parameters**

| Name          | Type      | Description                              |
| ------------- | --------- | ---------------------------------------- |
| `vault`       | `address` | The address of the vault                 |
| `tvl`         | `uint16`  | The TVL fee rate in basis points         |
| `performance` | `uint16`  | The performance fee rate in basis points |

### claimFees

Process a fee claim for a specific vault

*Expected to be called by the vault only when claiming fees Only accrues fees and updates stored values; does not transfer tokens Caller must perform the actual transfers to avoid permanent fee loss*

```solidity
function claimFees(uint256 feeTokenBalance) external virtual returns (uint256, uint256, address);
```

**Parameters**

| Name              | Type      | Description                               |
| ----------------- | --------- | ----------------------------------------- |
| `feeTokenBalance` | `uint256` | Available fee token balance to distribute |

**Returns**

| Name     | Type      | Description                                                                  |
| -------- | --------- | ---------------------------------------------------------------------------- |
| `<none>` | `uint256` | earnedFees The amount of fees to be claimed by the fee recipient             |
| `<none>` | `uint256` | protocolEarnedFees The amount of protocol fees to be claimed by the protocol |
| `<none>` | `address` | protocolFeeRecipient The address of the protocol fee recipient               |

### claimProtocolFees

Process a protocol fee claim for a vault

*Expected to be called by the vault only when claiming protocol fees Only accrues protocol fees and updates stored values; does not transfer tokens Caller must perform the actual transfers to avoid permanent protocol fee loss*

```solidity
function claimProtocolFees(uint256 feeTokenBalance) external virtual returns (uint256, address);
```

**Parameters**

| Name              | Type      | Description                               |
| ----------------- | --------- | ----------------------------------------- |
| `feeTokenBalance` | `uint256` | Available fee token balance to distribute |

**Returns**

| Name     | Type      | Description                                                    |
| -------- | --------- | -------------------------------------------------------------- |
| `<none>` | `uint256` | accruedFees The amount of protocol fees claimed                |
| `<none>` | `address` | protocolFeeRecipient The address of the protocol fee recipient |

### previewFees

Returns the current claimable fees for the given vault, as if a claim was made now

```solidity
function previewFees(address vault, uint256 feeTokenBalance) external view virtual returns (uint256, uint256);
```

**Parameters**

| Name              | Type      | Description                                                                                                                                                                               |
| ----------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `vault`           | `address` | The address of the vault to preview fees for                                                                                                                                              |
| `feeTokenBalance` | `uint256` | Available fee token balance to distribute If set to `type(uint256).max`, the function returns all accrued fees If set to an actual balance, the result is capped to that claimable amount |

**Returns**

| Name     | Type      | Description                                          |
| -------- | --------- | ---------------------------------------------------- |
| `<none>` | `uint256` | vaultFees The amount of claimable fees for the vault |
| `<none>` | `uint256` | protocolFees The amount of claimable protocol fees   |

### \_beforeClaimFees

Hook called before claiming fees

*Can be overridden by child contracts to add custom logic*

```solidity
function _beforeClaimFees() internal virtual;
```

### \_beforeClaimProtocolFees

Hook called before claiming protocol fees

*Can be overridden by child contracts to add custom logic*

```solidity
function _beforeClaimProtocolFees() internal virtual;
```

### \_calculateTvlFee

Calculates the TVL fee for a given period

*Fee is annualized and prorated for the time period*

```solidity
function _calculateTvlFee(uint256 averageValue, uint256 tvlFee, uint256 timeDelta) internal pure returns (uint256);
```

**Parameters**

| Name           | Type      | Description                               |
| -------------- | --------- | ----------------------------------------- |
| `averageValue` | `uint256` | The average value during the period       |
| `tvlFee`       | `uint256` | The TVL fee rate in basis points          |
| `timeDelta`    | `uint256` | The duration of the fee period in seconds |

**Returns**

| Name     | Type      | Description        |
| -------- | --------- | ------------------ |
| `<none>` | `uint256` | The earned TVL fee |

### \_calculatePerformanceFee

Calculates the performance fee for a given period

```solidity
function _calculatePerformanceFee(uint256 profit, uint256 feeRate) internal pure returns (uint256);
```

**Parameters**

| Name      | Type      | Description                              |
| --------- | --------- | ---------------------------------------- |
| `profit`  | `uint256` | The profit during the period             |
| `feeRate` | `uint256` | The performance fee rate in basis points |

**Returns**

| Name     | Type      | Description                |
| -------- | --------- | -------------------------- |
| `<none>` | `uint256` | The earned performance fee |


---

# 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.aera.finance/the-protocol/core/basefeecalculator.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.
