# Configuring Pre-hooks

Pre-hooks are very powerful feature of the Fusion Vaults. They allow an execution of an additional functions before invoking a primary action. &#x20;

Because the pre-hooks are very powerful functionality their management should **always be done by user with a long timelock.** IPOR web app will be alerting liquidity providers about the use of unverified pre-hooks.&#x20;

## What is a pre-hook

Fusion vault allows for an execution of a code when invoking restricted methods on the core vault.  Pre-hooks are not available to methods on managers.&#x20;

Pre-hooks are smart contracts with an access to the memory of the contract and authority to execute functions. They can be used for various applications such as:&#x20;

* updating balance cache on markets&#x20;
* raising exceptions (effectively pausing functions)
* triggering auto-withdrawals from complex strategies
* etc.

> Due to that power and flexibility it is important that the user allowed to modify the prehooks has timelock configured

To add pre-hook to the vault use `setPreHookImplementaions`

```solidity
PlasmaVaultGovernance(PLASMA_VAULT)
    .setPreHookImplementations(selectors, preHooks, substrates);
```

where `selectors` are the signatures of restricted methods on the vault, pre-hooks are addresses of implementations and substrates are the params required by the pre-hooks.&#x20;

Example implementation can be found in below test case:  <https://github.com/IPOR-Labs/ipor-fusion/blob/main/test/pre_hooks/UpdateBalancesIgnoreDustPreHookTest.t.sol> . It demonstrates the use of the pre-hook that rebalances the cache of the vault before deposit and withdrawal while ignoring balances with dust.&#x20;

List of the deployed prehooks: <https://github.com/IPOR-Labs/ipor-abi?tab=readme-ov-file#prehooks-list>

### Bundled Pre-hooks&#x20;

IPOR Fusion web-app comes with some bunled prehooks that bring some valuable functionalities to the vaults. This list may not be exhaustive as there are new components being developed but below you can find some pre-hooks available at the time of writing this documentation and use cases for them.&#x20;

#### Function Pausing Pre-hook&#x20;

Under the prehooks section of the administrative panel `/edit`  you can find `Pause Functions` . You can use this interface to pause individual functions of the fusion vaults. Underneeth a special pre-hook is used that simply raises an exception. By attaching it before a restricted method you essentially render it unable to execute.&#x20;

<figure><img src="/files/OwfIPhQqzg6bzzeKbzGc" alt=""><figcaption></figcaption></figure>

#### Auto Rebalance Pre-hook

Another prehook provided via the app.ipor.io interface is the auto rebalance prehook. Because rebalancing of the vault before deposit and withdrawals would cost additional gas, vaults for gas optimisation rely on cached balances that are refreshed each time the vault interacts with connected markets. You can force rebalancing before every deposit/withdraw action by using this pre-hook.&#x20;

<figure><img src="/files/5DFev4m0ArBY8TWds2Ie" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/GhEXOtZJkOX4vEUH9LQa" alt=""><figcaption></figcaption></figure>

#### Exchange rate validator&#x20;

Another example of what pre-hooks can be used for is validating of the changes in the exchange rate since the last rebalance. When this is enabled then pre-hook will keep track of the exchange rates and will block selected functions is the jump of the share exchange rate exeeds the defined threshold. This is useful in case you want to restrict access in the situaltions where vault is misconfigured or price oracles are experiencing issues.&#x20;

<figure><img src="/files/ZFWI0wR7xmcxfH1E2s8p" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/VSLvTkBuZCnF6UHa91Gu" alt=""><figcaption></figcaption></figure>


---

# 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.ipor.io/build-on-fusion/developer-guide/configuring-pre-hooks.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.
