# Chainlink driven mechanism

Chainlink regularly calls the RebalanceStrategy contract to check if it's time to rebalance. If it is, it initiates a rebalance transaction. RebalanceStrategy then calls VaultManager to rebalance the Vault.

Every Vault has a set of Providers connected to lending protocols. Each provider represents a single lending protocol. The Vault can allocate the funds only among those Providers. Rebalancing of the Vault reallocates the funds among its Providers according to the strategy defined in the RebalanceStrategy contract to maximize the yield.

### RebalanceStrategy

RebalanceStrategy checks every Provider's APR to decide when to rebalance the Vault and how to reallocate the funds among them.

It has two main functions that make it compatible with Chainlink Automation:

1. `checkUpkeep` - view function that checks if it's time to rebalance the Vault
2. `performUpkeep` - function that performs the rebalancing of the Vault

Chainlink makes sure to regularly call `checkUpkeep` and whenever it signals that it's time to rebalance, it calls `performUpkeep`.

#### Privileges

RebalanceStrategy only allows Chainlink's Forwarder to call `performUpkeep`. Chainlink creates a new Forwarder contract for every new Chainlink Upkeep, so after the creation of each Upkeep we set the Forwarder address in the RebalanceStrategy contract by calling `setForwarder(address)`. The function `setForwarder()` can only be called once.

The owner of the RebalanceStrategy contract can update the rebalance settings by calling `updateSettings()` to change the rebalance interval and the minimum rate delta. This affects how often the rebalance will be performed.

RebalanceStrategy has the Executor role in VaultManager which allows it to rebalance the Vault.

### Security

The worst-case scenario is that Chainlink Automation stops working, which means that automatic rebalancing of fund distribution will be temporarily paused. The funds themselves remain safe at all times and in all cases and can be withdrawn by the user at any time regardless of the rebalancing process.


---

# 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://rebalance.gitbook.io/rebalance/how-it-works/chill-r-strategy/chainlink-driven-mechanism.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.
