# Hook Logic

### Overview

When deploying a new pool, you (or the pool creator) pass a `Permissions` struct that tells the core AMM contract which lifecycle callbacks your hook should receive. This is a security and gas-optimization feature: the core only calls hooks for which the corresponding flag is `true`.

Only hooks that set the appropriate flags can be registered for a pool.

```solidity
struct Permissions {
    bool beforeInitialize;
    bool afterInitialize;
    bool beforeAddLiquidity;
    bool afterAddLiquidity;
    bool beforeRemoveLiquidity;
    bool afterRemoveLiquidity;
    bool beforeSwap;
    bool afterSwap;
    bool beforeDonate;
    bool afterDonate;
    bool beforeSwapReturnDelta;
    bool afterSwapReturnDelta;
    bool afterAddLiquidityReturnDelta;
    bool afterRemoveLiquidityReturnDelta;
}
```

### Permission Flags

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

#### Pool Lifecycle

* `beforeInitialize` — Called before a pool is created. Useful for validating parameters, setting initial state, or enforcing custom deployment rules.
* `afterInitialize` — Called immediately after pool creation. Good for one-time setup inside the hook (e.g., initializing oracle data, whitelists, etc.).

#### Liquidity Operations

* `beforeAddLiquidity` — Runs before liquidity is added. Common uses: position validation, fees, dynamic range checks, or KYC-style restrictions.
* `afterAddLiquidity` — Runs after liquidity is successfully added. Useful for post-mint accounting, reward distribution, or updating external contracts.
* `beforeRemoveLiquidity` — Runs before liquidity is removed. Can be used for withdrawal fees, cooldowns, or position lock checks.
* `afterRemoveLiquidity` — Runs after liquidity removal. Useful for cleanup or emitting events to external systems.

#### Swaps (Trading)

* `beforeSwap` — Executed before every swap. Most powerful hook for:
  * Dynamic fees
  * Price limits / circuit breakers
  * MEV protection (e.g., order flow auctions)
  * Custom routing or intent-based logic
* `afterSwap` — Executed after a swap completes. Ideal for:
  * Post-trade rewards
  * Oracle updates
  * TWAP calculations
  * Protocol fee logic

#### Donate

* `beforeDonate` — Called before a donate operation (adding tokens to the pool without minting LP tokens).
* `afterDonate` — Called after a successful donate.

#### Return Delta Flags (Advanced)

These flags allow your hook to return a delta (positive or negative token amount adjustment) that modifies the core operation. They enable very powerful custom accounting.

* `beforeSwapReturnDelta` — Hook can return a delta on input/output amounts before the swap executes.
* `afterSwapReturnDelta` — Hook can return a delta after the swap (e.g., to take a custom fee in output tokens).
* `afterAddLiquidityReturnDelta` — Hook can adjust the amount of liquidity minted or tokens required.
* `afterRemoveLiquidityReturnDelta` — Hook can adjust the tokens returned to the user on withdrawal.

Note: Return-delta hooks usually require the corresponding `before`/`after` flag to also be enabled.

#### Example Usage

```rust
// When creating a pool
let permissions = Permissions {
    beforeSwap: true,
    afterSwap: true,
    afterAddLiquidityReturnDelta: true,
    // ... other flags
    ..Default::default()
};

create_pool(token0, token1, fee, tickSpacing, hook_address, permissions);
```


---

# 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://soliquid.gitbook.io/soliquid-docs/documentation/hook-launchpad/hook-logic.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.
