Skip to content

Commit

Permalink
docs: v4 swap hooks quickstart (#823)
Browse files Browse the repository at this point in the history
Co-authored-by: saucepoint <[email protected]>
  • Loading branch information
0xdevant and saucepoint authored Dec 2, 2024
1 parent 290660c commit 2e40356
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions docs/contracts/v4/quickstart/04-hooks/01-swap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Swaps are the most common interaction with the Uniswap protocol. When it comes t

As the names suggest `beforeSwap`/`afterSwap` are functions called before or after a swap is executed on a pool.

This guide will explain the mechanism of encoded flags for hooks, and go through the parameters and examples for `beforeSwap` and `afterSwap`.
This guide will go through the parameters for `beforeSwap` and `afterSwap`, and a simple example of a swap hook.

Note: The swap examples are not production ready code, and are implemented in a simplistic manner for the purpose of learning.
Note: The swap hook is not production ready code, and is implemented in a simplistic manner for the purpose of learning.

## Set Up the Contract

Declare the solidity version used to compile the contract, since transient storage is used the solidity version will be `>=0.8.24`.
Declare the solidity version used to compile the contract, here we will use `>=0.8.24` for the solidity version as transient storage is used.

```solidity
// SPDX-License-Identifier: MIT
Expand All @@ -35,7 +35,7 @@ import {BalanceDelta} from "v4-core/src/types/BalanceDelta.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "v4-core/src/types/BeforeSwapDelta.sol";
```

Create a contract called SwapHook, use `PoolIdLibrary` to attach functions of computing ID of a pool to `PoolKey`. Declare two mappings to act as counters when calling `beforeSwap` and `afterSwap`.
Create a contract called `SwapHook`, use `PoolIdLibrary` to attach functions of computing `poolId` for `PoolKey`. Declare two mappings as counters for `beforeSwap` and `afterSwap`.

```solidity
contract SwapHook is BaseHook {
Expand All @@ -52,8 +52,8 @@ contract SwapHook is BaseHook {
constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}
```

Override `getHookPermissions` from `BaseHook.sol` to return a struct of permissions to signal which hook functions are to be implemented.
It will also be used at deployment to validate the address correctly represents the expected permissions.
Override `getHookPermissions()` from `BaseHook` to return a struct of permissions to signal which hook functions are to be implemented.
It will also be used at deployment to validate the hook address correctly represents the expected permissions.

```solidity
function getHookPermissions() public pure override returns (Hooks.Permissions memory) {
Expand Down Expand Up @@ -93,12 +93,12 @@ function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calld

### `beforeSwap` Parameters

When triggering the `beforeSwap` hook function, there are some parameters we can make use of to customize or extend the behavior of `swap`. These parameters are described in `beforeSwap` from [`IHooks.sol`](https://github.com/Uniswap/v4-core/blob/main/src/interfaces/IHooks.sol#L111).
When triggering the `beforeSwap` hook function, there are some parameters we can make use of to customize or extend the behavior of `swap`. These parameters are described in [`beforeSwap`](/contracts/v4/reference/core/interfaces/IHooks#beforeswap) from `IHooks`.

A brief overview of the parameters:
- `sender` The initial `msg.sender` for the `PoolManager.swap` call. Typically a swap router
- `sender` The initial `msg.sender` for the `PoolManager.swap` call - typically a swap router
- `key` The key for the pool
- `params` The parameters for the swap i.e. `SwapParams` from [`IPoolManager.sol`](https://github.com/Uniswap/v4-core/blob/main/src/interfaces/IPoolManager.sol#L146C12-L146C22)
- `params` The parameters for the swap i.e. [`SwapParams`](/contracts/v4/reference/core/interfaces/IPoolManager#swapparams) from `IPoolManager`
- `hookData` Arbitrary data handed into the `PoolManager` by the swapper to be be passed on to the hook

## afterSwap
Expand All @@ -118,12 +118,12 @@ function afterSwap(address, PoolKey calldata key, IPoolManager.SwapParams callda

### `afterSwap` Parameters

When triggering the `afterSwap` hook function, there are some parameters we can make use of to customize or extend the behavior of `swap`. These parameters are described in `afterSwap` from [`IHooks.sol`](https://github.com/Uniswap/v4-core/blob/main/src/interfaces/IHooks.sol#L126).
When triggering the `afterSwap` hook function, there are some parameters we can make use of to customize or extend the behavior of `swap`. These parameters are described in [`afterSwap`](/contracts/v4/reference/core/interfaces/IPoolManager#swapparams) from `IHooks`.

A brief overview of the parameters:
- `sender` The initial `msg.sender` for the `PoolManager.swap` call. Typically a swap router
- `sender` The initial `msg.sender` for the `PoolManager.swap` call - typically a swap router
- `key` The key for the pool
- `params` The parameters for the swap i.e. `SwapParams` from [`IPoolManager.sol`](https://github.com/Uniswap/v4-core/blob/main/src/interfaces/IPoolManager.sol#L146C12-L146C22)
- `params` The parameters for the swap i.e. [`SwapParams`](/contracts/v4/reference/core/interfaces/IPoolManager#swapparams) from `IPoolManager`
- `delta` The amount owed to the caller (positive) or owed to the pool (negative)
- `hookData` Arbitrary data handed into the `PoolManager` by the swapper to be be passed on to the hook

Expand Down

0 comments on commit 2e40356

Please sign in to comment.