# DelayedFeeCalculator

**Inherits:** IDelayedFeeCalculator, BaseFeeCalculator

To protect vault owners from inaccurate submissions, the DelayedFeeCalculator uses a dispute period and pending snapshot system that only accepts submitted values after the dispute period has passed. Each vault accrues fees independently but a shared protocol fee recipient accrues protocol level fees from all vaults

*All fees are calculated in the numeraire token's decimals*

## State Variables

### DISPUTE\_PERIOD

Dispute period for vault snapshot

```solidity
uint256 public immutable DISPUTE_PERIOD;
```

### \_vaultSnapshots

A mapping of vault addresses to their associated state

```solidity
mapping(address vault => VaultSnapshot vaultSnapshot) internal _vaultSnapshots;
```

## Functions

### constructor

```solidity
constructor(address owner_, Authority authority_, uint256 disputePeriod) BaseFeeCalculator(owner_, authority_);
```

### registerVault

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

### submitSnapshot

Submit a new snapshot for fee calculation

```solidity
function submitSnapshot(address vault, uint160 averageValue, uint128 highestProfit, uint32 timestamp)
    external
    onlyVaultAccountant(vault);
```

**Parameters**

| Name            | Type      | Description                                                                        |
| --------------- | --------- | ---------------------------------------------------------------------------------- |
| `vault`         | `address` | The address of the vault                                                           |
| `averageValue`  | `uint160` | The average value during the period since last snapshot to this snapshot timestamp |
| `highestProfit` | `uint128` | The highest profit achieved up to the snapshot timestamp                           |
| `timestamp`     | `uint32`  | The timestamp of the snapshot                                                      |

### accrueFees

Process fee accrual for a vault

```solidity
function accrueFees(address vault) external returns (uint256 protocolFeesEarned, uint256 vaultFeesEarned);
```

**Parameters**

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

**Returns**

| Name                 | Type      | Description                                                                      |
| -------------------- | --------- | -------------------------------------------------------------------------------- |
| `protocolFeesEarned` | `uint256` | tvlFeesEarned The earned TVL fees for the vault and the protocol                 |
| `vaultFeesEarned`    | `uint256` | performanceFeesEarned The earned performance fees for the vault and the protocol |

### previewFees

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

### vaultFeeState

Get the fee state for a vault

```solidity
function vaultFeeState(address vault) external view returns (VaultSnapshot memory, VaultAccruals memory);
```

**Parameters**

| Name    | Type      | Description                            |
| ------- | --------- | -------------------------------------- |
| `vault` | `address` | The vault address to get fee state for |

**Returns**

| Name     | Type            | Description                                                    |
| -------- | --------------- | -------------------------------------------------------------- |
| `<none>` | `VaultSnapshot` | The vault snapshot containing average value and highest profit |
| `<none>` | `VaultAccruals` | The vault accruals containing fees and accrued amounts         |

### \_beforeClaimFees

Called before claiming fees

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

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

### \_beforeClaimProtocolFees

Called before claiming protocol fees

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

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

### \_accrueFees

Accrues fees for a vault based on its pending snapshot

*Updates the vault's state including lastFeeAccrual, lastHighestProfit, and accruedFees*

*Deletes pending snapshot if dispute period has passed*

```solidity
function _accrueFees(
    VaultSnapshot storage vaultSnapshot,
    VaultAccruals storage vaultAccruals,
    uint256 lastFeeAccrualCached
) internal returns (uint256 protocolFeesEarned, uint256 vaultFeesEarned);
```

**Parameters**

| Name                   | Type            | Description                                                            |
| ---------------------- | --------------- | ---------------------------------------------------------------------- |
| `vaultSnapshot`        | `VaultSnapshot` | The storage pointer to the vault's state                               |
| `vaultAccruals`        | `VaultAccruals` | The storage pointer to the vault's accruals                            |
| `lastFeeAccrualCached` | `uint256`       | The last fee accrual timestamp cached to avoid re-reading from storage |

**Returns**

| Name                 | Type      | Description              |
| -------------------- | --------- | ------------------------ |
| `protocolFeesEarned` | `uint256` | The earned protocol fees |
| `vaultFeesEarned`    | `uint256` | The earned vault fees    |

### \_calculatePerformanceFees

Calculates performance fees for both vault and protocol

*Returns zero fees if no new profit has been made*

```solidity
function _calculatePerformanceFees(uint256 vaultPerformanceFeeRate, uint256 newHighestProfit, uint256 oldHighestProfit)
    internal
    view
    returns (uint256, uint256);
```

**Parameters**

| Name                      | Type      | Description                                 |
| ------------------------- | --------- | ------------------------------------------- |
| `vaultPerformanceFeeRate` | `uint256` | The performance fee rate for the vault      |
| `newHighestProfit`        | `uint256` | The highest profit in the pending snapshot  |
| `oldHighestProfit`        | `uint256` | The highest profit in the previous snapshot |

**Returns**

| Name     | Type      | Description                                                 |
| -------- | --------- | ----------------------------------------------------------- |
| `<none>` | `uint256` | vaultPerformanceFee The performance fee for the vault       |
| `<none>` | `uint256` | protocolPerformanceFee The performance fee for the protocol |

### \_calculateTvlFees

Calculates TVL fees for both vault and protocol

```solidity
function _calculateTvlFees(
    uint256 vaultTvlFeeRate,
    uint256 averageValue,
    uint256 snapshotTimestamp,
    uint256 lastFeeAccrual
) internal view returns (uint256, uint256);
```

**Parameters**

| Name                | Type      | Description                           |
| ------------------- | --------- | ------------------------------------- |
| `vaultTvlFeeRate`   | `uint256` | The TVL fee rate for the vault        |
| `averageValue`      | `uint256` | The average value of the vault        |
| `snapshotTimestamp` | `uint256` | The timestamp of the snapshot         |
| `lastFeeAccrual`    | `uint256` | The timestamp of the last fee accrual |

**Returns**

| Name     | Type      | Description                                        |
| -------- | --------- | -------------------------------------------------- |
| `<none>` | `uint256` | vaultTvlFee The earned TVL fee for the vault       |
| `<none>` | `uint256` | protocolTvlFee The earned TVL fee for the protocol |
