# Fund Flow Controller

The `FundFlowController` manages deposits and withdrawals for Chainlink staking vaults in the `OperatorVCS` and `CommunityVCS`. Specifcally, it calculates vault deposit/withdrawal order, calculates the current state of accounting across vault groups, and periodically unbonds vaults in the Chainlink staking contract to ensure there are always funds available to withdraw.

## View Functions

### operatorVCS

Returns the address of the Operator VCS

```solidity
function operatorVCS() public view returns (address)
```

#### Return Values

| Name        | Type    | Description             |
| ----------- | ------- | ----------------------- |
| operatorVCS | address | Address of Operator VCS |

### communityVCS

Returns the address of the Community VCS

```solidity
function communityVCS() public view returns (address)
```

#### Return Values

| Name         | Type    | Description              |
| ------------ | ------- | ------------------------ |
| communityVCS | address | Address of Community VCS |

### unbondingPeriod

Returns the duration of the unbonding period in the Chainlink staking contract

```solidity
function unbondingPeriod() public view returns (uint64)
```

#### Return Values

| Name            | Type   | Description      |
| --------------- | ------ | ---------------- |
| unbondingPeriod | uint64 | Unbonding period |

### claimPeriod

Returns the duration of the claim period in the Chainlink staking contract

```solidity
function claimPeriod() public view returns (uint64)
```

#### Return Values

| Name        | Type   | Description  |
| ----------- | ------ | ------------ |
| claimPeriod | uint64 | Claim period |

### numVaultGroups

Returns the total number of vault groups

```solidity
function numVaultGroups() public view returns (uint64)
```

#### Return Values

| Name           | Type   | Description            |
| -------------- | ------ | ---------------------- |
| numVaultGroups | uint64 | Number of vault groups |

### curUnbondedVaultGroup

Returns the index of current unbonded vault group

```solidity
function curUnbondedVaultGroup() public view returns (uint64)
```

#### Return Values

| Name                  | Type   | Description                  |
| --------------------- | ------ | ---------------------------- |
| curUnbondedVaultGroup | uint64 | Current unbonded vault group |

### timeOfLastUpdateByGroup

Returns the time that a vault group was last unbonded

```solidity
function timeOfLastUpdateByGroup() public view returns (uint64)
```

#### Parameters

| Name       | Type   | Description          |
| ---------- | ------ | -------------------- |
| vaultGroup | uint64 | Index of vault group |

#### Return Values

| Name             | Type   | Description                            |
| ---------------- | ------ | -------------------------------------- |
| timeOfLastUpdate | uint64 | Time of last update for selected group |

### getDepositData

Returns encoded vault deposit order for each strategy

*Return data should be passed to the priority pool when depositing into the staking pool*

```solidity
function getDepositData(uint256 _toDeposit) external view returns (bytes[])
```

#### Parameters

| Name        | Type    | Description       |
| ----------- | ------- | ----------------- |
| \_toDeposit | uint256 | amount to deposit |

#### Return Values

| Name        | Type     | Description                        |
| ----------- | -------- | ---------------------------------- |
| depositData | bytes\[] | list of encoded vault deposit data |

### getWithdrawalData

Returns encoded vault withdrawal order for each strategy

*Return data should be passed to the priority pool when withdrawing from the staking pool*

```solidity
function getWithdrawalData(uint256 _toWithdraw) external view returns (bytes[])
```

#### Parameters

| Name         | Type    | Description        |
| ------------ | ------- | ------------------ |
| \_toWithdraw | uint256 | amount to withdraw |

#### Return Values

| Name           | Type     | Description                           |
| -------------- | -------- | ------------------------------------- |
| withdrawalData | bytes\[] | list of encoded vault withdrawal data |

### claimPeriodActive

Returns whether claim period is active

*Funds can only be withdrawn while the claim period is active*

```solidity
function claimPeriodActive() external view returns (bool)
```

#### Return Values

| Name     | Type | Description                                     |
| -------- | ---- | ----------------------------------------------- |
| isActive | bool | true if claim period is active, false otherwise |

### shouldUpdateVaultGroups

Returns whether vault groups should be updated.

```solidity
function shouldUpdateVaultGroups() external view returns (bool)
```

#### Return Values

| Name         | Type | Description                                             |
| ------------ | ---- | ------------------------------------------------------- |
| shouldUpdate | bool | true if vault groups should be updated, false otherwise |

## Write Functions

### updateVaultGroups

Executes a vault group update

*Re-unbonds all vaults in the current vault group and increments the current vault group*\
\&#xNAN;*to the next one which will have just entered the claim period*\
\&#xNAN;*an update is needed once per claim period right after the claim period expires for the*\
\&#xNAN;*current vault group*

```solidity
function updateVaultGroups() external
```

### updateOperatorVaultGroupAccounting

Calculates and updates totalDepositRoom and totalUnbonded for a list of operator vault groups

*Used to correct minor accounting errors that result from the removal or slashing*\
\&#xNAN;*of operators in the Chainlink staking contract*

```solidity
function updateOperatorVaultGroupAccounting(uint256[] _vaultGroups) external
```

#### Parameters

| Name          | Type       | Description          |
| ------------- | ---------- | -------------------- |
| \_vaultGroups | uint256\[] | list of vault groups |

### delegateVaults

Delegates to an address for a group of vaults.

```solidity
function delegateVaults(address[] _vaults, address _to, bytes32 _rights, bool _enable) external
```

#### Parameters

| Name     | Type       | Description                            |
| -------- | ---------- | -------------------------------------- |
| \_vaults | address\[] | List of vault addresses                |
| \_to     | address    | Address to delegate to                 |
| \_rights | bytes32    | Rights to grant                        |
| \_enable | bool       | Whether to enable or revoke delegation |

### withdrawTokenRewards

Withdraws non-LINK token rewards from a group of vaults.

```solidity
function withdrawTokenRewards(address[] _vaults, address[] _tokens) external
```

#### Parameters

| Name     | Type       | Description                |
| -------- | ---------- | -------------------------- |
| \_vaults | address\[] | List of vault addresses    |
| \_tokens | address\[] | List of tokens to withdraw |

### setNonLINKRewardReceiver

Sets the address of reward receiver for non-LINK vault rewards.

```solidity
function setNonLINKRewardReceiver(address _nonLINKRewardReceiver) external
```

#### Parameters

| Name                    | Type    | Description                |
| ----------------------- | ------- | -------------------------- |
| \_nonLINKRewardReceiver | address | Address of reward receiver |


---

# 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.stake.link/link-staking-contracts/fundflowcontroller.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.
