# Fee Manager

### Intro

The `FeeManager` contract is used to calculate and collect fees from various operations within the Blueberry protocol, such as depositing, withdrawing, and generating rewards. The contract is responsible for deducting fees from the relevant token amounts and transferring them to the designated treasury address.

### Contract Overview

The FeeManager contract is implemented in Solidity programming language with version 0.8.16. It is imported with the `OwnableUpgradeable` and `SafeERC20Upgradeable` contracts from the OpenZeppelin library, which are used for access control and safe ERC20 token transfers, respectively.

The contract implements four public functions for cutting fees from token transactions: `doCutDepositFee`, `doCutWithdrawFee`, `doCutRewardsFee`, and `doCutVaultWithdrawFee`. Each function takes the relevant token address and amount as input parameters and calls the internal function `_doCutFee` to calculate and collect the fee.

### &#x20;Functions

#### initialize

{% code overflow="wrap" %}

```solidity
function initialize(IProtocolConfig config_) external initializer
```

{% endcode %}

This function is an initializer function that sets the `config` variable, which is an interface to the main Blueberry protocol configuration contract. The function also uses the `__Ownable_init()` function from the `OwnableUpgradeable` contract to initialize the owner of the contract.

#### &#x20;doCutDepositFee

{% code overflow="wrap" %}

```solidity
function doCutDepositFee(address token, uint256 amount) external returns (uint256)
```

{% endcode %}

This function cuts the deposit fee from the specified token `amount` and transfers it to the treasury address. The fee rate is obtained from the `config` variable using the `depositFee` function. The function then returns the amount of the token after the fee has been deducted.

#### doCutWithdrawFee

{% code overflow="wrap" %}

```solidity
function doCutWithdrawFee(address token, uint256 amount) external returns (uint256)
```

{% endcode %}

This function cuts the withdraw fee from the specified token `amount` and transfers it to the treasury address. The fee rate is obtained from the `config` variable using the `withdrawFee` function. The function then returns the amount of the token after the fee has been deducted.

#### doCutRewardsFee

{% code overflow="wrap" %}

```solidity
function doCutRewardsFee(address token, uint256 amount) external returns (uint256)
```

{% endcode %}

This function cuts the performance fee from the specified token `amount` and transfers it to the treasury address. The fee rate is obtained from the `config` variable using the `rewardFee` function. The function then returns the amount of the token after the fee has been deducted.

#### doCutVaultWithdrawFee

{% code overflow="wrap" %}

```solidity
function doCutVaultWithdrawFee(address token, uint256 amount) external returns (uint256)
```

{% endcode %}

This function cuts the withdraw fee from the specified token `amount` only if the withdraw transaction is executed within a given window of time (2 months). The fee rate is obtained from the `config` variable using the `withdrawVaultFee` function. The function then returns the amount of the token after the fee has been deducted, or the full `amount` if the withdraw transaction is executed outside the withdraw fee window.

#### \_doCutFee

{% code overflow="wrap" %}

```solidity
function _doCutFee(address token, uint256 amount, uint256 feeRate) internal returns (uint256)
```

{% endcode %}

This function is an internal function that cuts a fee from a specified token `amount` based on the given `feeRate` and transfers the fee amount to the treasury address. The function returns the amount of the token after the fee has been deducted.


---

# 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://v1.docs.blueberry.garden/developer-guides/contracts/blueberry-bank/fee-manager.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.
